ctkDoubleSpinBoxTest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QDoubleSpinBox>
  17. #include <QLineEdit>
  18. #include <QString>
  19. #include <QStyle>
  20. #include <QStyleOptionSlider>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkDoubleSpinBox.h"
  24. #include "ctkTest.h"
  25. // ----------------------------------------------------------------------------
  26. class ctkDoubleSpinBoxTester: public QObject
  27. {
  28. Q_OBJECT
  29. private slots:
  30. void testUI();
  31. void testToLocals();
  32. void testDecimalsByKey();
  33. void testDecimalsByKey_data();
  34. void testPrefix();
  35. void testPrefix_data();
  36. void testDecimalsByValue();
  37. void testDecimalsByValue_data();
  38. void testDecimalPointAlwaysVisible();
  39. void testDecimalPointAlwaysVisible_data();
  40. };
  41. // ----------------------------------------------------------------------------
  42. void ctkDoubleSpinBoxTester::testUI()
  43. {
  44. ctkDoubleSpinBox spinBox;
  45. spinBox.setMinimum(-100.);
  46. spinBox.setMaximum(100.);
  47. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByValue |ctkDoubleSpinBox::DecimalsByShortcuts );
  48. spinBox.setValue(26.2110001);
  49. spinBox.setPrefix("A: ");
  50. spinBox.setSetMode(ctkDoubleSpinBox::SetAlways);
  51. spinBox.show();
  52. QTest::qWaitForWindowShown(&spinBox);
  53. QObject::connect(&spinBox, SIGNAL(valueChanged(double)),
  54. &spinBox, SLOT(setValue(double)), Qt::QueuedConnection);
  55. QDoubleSpinBox doubleSpinBox;
  56. doubleSpinBox.setMinimum(-100.);
  57. doubleSpinBox.setMaximum(100.);
  58. doubleSpinBox.setValue(2.);
  59. //doubleSpinBox.show();
  60. //QTest::qWaitForWindowShown(&doubleSpinBox);
  61. //qApp->exec();
  62. }
  63. // ----------------------------------------------------------------------------
  64. void ctkDoubleSpinBoxTester::testToLocals()
  65. {
  66. bool ok;
  67. QLocale().toDouble("+.0", &ok);
  68. qDebug() << "+.0" << ok;
  69. QLocale().toDouble("0.0 1", &ok);
  70. qDebug() << "0.0 1" << ok;
  71. }
  72. // ----------------------------------------------------------------------------
  73. void ctkDoubleSpinBoxTester::testDecimalsByKey()
  74. {
  75. ctkDoubleSpinBox spinBox;
  76. spinBox.setMinimum(-200.);
  77. spinBox.setMaximum(200.);
  78. spinBox.setValue(1.23);
  79. QFETCH(int, decimalsOptions);
  80. spinBox.setDecimalsOption( static_cast<ctkDoubleSpinBox::DecimalsOptions>(decimalsOptions) );
  81. const int oldDecimals = spinBox.decimals();
  82. QFETCH(int, cursorPosition);
  83. QFETCH(int, key);
  84. spinBox.lineEdit()->setCursorPosition(cursorPosition);
  85. //spinBox.show();
  86. //QTest::qWaitForWindowShown(&spinBox);
  87. //qApp->exec();
  88. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  89. QTest::keyClick(spinBox.lineEdit(), static_cast<Qt::Key>(key));
  90. QFETCH(QString, expectedText);
  91. QFETCH(int, expectedDecimals);
  92. QCOMPARE(spinBox.text(), expectedText);
  93. QCOMPARE(spinBox.value(), expectedText.toDouble());
  94. QCOMPARE(spinBox.decimals(), expectedDecimals);
  95. QCOMPARE(spy.count(), spinBox.decimals() != oldDecimals ? 1 : 0);
  96. }
  97. // ----------------------------------------------------------------------------
  98. void ctkDoubleSpinBoxTester::testDecimalsByKey_data()
  99. {
  100. QTest::addColumn<int>("decimalsOptions");
  101. QTest::addColumn<int>("cursorPosition");
  102. QTest::addColumn<int>("key");
  103. QTest::addColumn<QString>("expectedText");
  104. QTest::addColumn<int>("expectedDecimals");
  105. QList<int> options;
  106. // ctkDoubleSpinBox::DecimalsByKey
  107. options << ctkDoubleSpinBox::DecimalsByKey;
  108. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'1' -> 11.23") << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 2;
  109. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'del' -> .23") << options.last() << 0 << int(Qt::Key_Delete) << ".23" << 2;
  110. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'backspace' -> 1.23") << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 2;
  111. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'1' -> 11.23") << options.last() << 1 << int(Qt::Key_1) << "11.23" << 2;
  112. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'del' -> 123") << options.last() << 1 << int(Qt::Key_Delete) << "123" << 0;
  113. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'backspace' -> .23") << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << 2;
  114. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'1' -> 1.12") << options.last() << 2 << int(Qt::Key_1) << "1.12" << 2;
  115. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'del' -> 1.3") << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1;
  116. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'backspace' -> 123") << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 0;
  117. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'1' -> 1.21") << options.last() << 3 << int(Qt::Key_1) << "1.21" << 2;
  118. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'del' -> 1.2") << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1;
  119. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'backspace' -> 1.3") << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1;
  120. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'1' -> 1.231") << options.last() << 4 << int(Qt::Key_1) << "1.231" << 3;
  121. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'del' -> 1.23") << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 2;
  122. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'backspace' -> 1.2") << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1;
  123. // ctkDoubleSpinBox::ReplaceDecimals
  124. options << ctkDoubleSpinBox::ReplaceDecimals;
  125. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.23") << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 2;
  126. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .23") << options.last() << 0 << int(Qt::Key_Delete) << ".23" << 2;
  127. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.23") << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 2;
  128. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.23") << options.last() << 1 << int(Qt::Key_1) << "11.23" << 2;
  129. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 123") << options.last() << 1 << int(Qt::Key_Delete) << "123" << 2;
  130. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .23") << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << 2;
  131. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.13") << options.last() << 2 << int(Qt::Key_1) << "1.13" << 2;
  132. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.3") << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 2;
  133. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 123") << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 2;
  134. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.21") << options.last() << 3 << int(Qt::Key_1) << "1.21" << 2;
  135. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.2") << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 2;
  136. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.3") << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 2;
  137. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.23") << options.last() << 4 << int(Qt::Key_1) << "1.23" << 2;
  138. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.23") << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 2;
  139. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.2") << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 2;
  140. // ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals
  141. options << (ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals);
  142. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.23")
  143. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 2;
  144. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .23")
  145. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << 2;
  146. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.23")
  147. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 2;
  148. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.23")
  149. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 2;
  150. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 123")
  151. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 0;
  152. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .23")
  153. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << 2;
  154. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.13")
  155. << options.last() << 2 << int(Qt::Key_1) << "1.13" << 2;
  156. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.3")
  157. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1;
  158. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 123")
  159. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 0;
  160. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.21")
  161. << options.last() << 3 << int(Qt::Key_1) << "1.21" << 2;
  162. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.2")
  163. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1;
  164. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.3")
  165. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1;
  166. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.231")
  167. << options.last() << 4 << int(Qt::Key_1) << "1.231" << 3;
  168. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.23")
  169. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 2;
  170. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.23")
  171. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1;
  172. // ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals
  173. options << (ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals);
  174. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'1' -> 11.23")
  175. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 2;
  176. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'del' -> .23")
  177. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << 2;
  178. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'backspace' -> 1.23")
  179. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 2;
  180. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'1' -> 11.23")
  181. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 2;
  182. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'del' -> 123")
  183. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 0;
  184. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'backspace' -> .23")
  185. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << 2;
  186. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'1' -> 1.123")
  187. << options.last() << 2 << int(Qt::Key_1) << "1.123" << 3;
  188. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'del' -> 1.3")
  189. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1;
  190. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'backspace' -> 123")
  191. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 0;
  192. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'1' -> 1.213")
  193. << options.last() << 3 << int(Qt::Key_1) << "1.213" << 3;
  194. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'del' -> 1.2")
  195. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1;
  196. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'backspace' -> 1.3")
  197. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1;
  198. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'1' -> 1.231")
  199. << options.last() << 4 << int(Qt::Key_1) << "1.231" << 3;
  200. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'del' -> 1.23")
  201. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 2;
  202. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'backspace' -> 1.23")
  203. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1;
  204. foreach(int option, options)
  205. {
  206. // bad keys are always rejected
  207. for (int i = 0; i < 5; ++i)
  208. {
  209. QTest::newRow(QString("%1 %2:'a' -> 1.23").arg(option).arg(i).toLatin1())
  210. << option << i << int(Qt::Key_A) << "1.23" << 2;
  211. }
  212. // sign keys are only for the first digit
  213. QTest::newRow(QString("%1 0:'+' -> 1.23").arg(option).toLatin1())
  214. << option << 0 << int(Qt::Key_Plus) << "+1.23" << 2;
  215. QTest::newRow(QString("%1 0:'-' -> -1.23").arg(option).toLatin1())
  216. << option << 0 << int(Qt::Key_Minus) << "-1.23" << 2;
  217. for (int i = 1; i < 5; ++i)
  218. {
  219. QTest::newRow(QString("%1 %2:'+' -> 1.23").arg(option).arg(i).toLatin1())
  220. << option << i << int(Qt::Key_Plus) << "1.23" << 2;
  221. QTest::newRow(QString("%1 %2:'-' -> 1.23").arg(option).arg(i).toLatin1())
  222. << option << i << int(Qt::Key_Minus) << "1.23" << 2;
  223. }
  224. }
  225. }
  226. // ----------------------------------------------------------------------------
  227. void ctkDoubleSpinBoxTester::testPrefix()
  228. {
  229. ctkDoubleSpinBox spinBox;
  230. spinBox.setPrefix("A: ");
  231. spinBox.setDecimalsOption( ctkDoubleSpinBox::FixedDecimals );
  232. QFETCH(int, cursorPosition);
  233. QFETCH(int, key);
  234. spinBox.lineEdit()->setCursorPosition(cursorPosition);
  235. QTest::keyClick(spinBox.lineEdit(), static_cast<Qt::Key>(key));
  236. //spinBox.show();
  237. //QTest::qWaitForWindowShown(&spinBox);
  238. //qApp->exec();
  239. QFETCH(double, expectedValue);
  240. QFETCH(QString, expectedText);
  241. QFETCH(int, expectedCursorPosition);
  242. QCOMPARE(spinBox.text(), expectedText);
  243. QCOMPARE(spinBox.value(), expectedValue);
  244. QCOMPARE(spinBox.decimals(), 2);
  245. QCOMPARE(spinBox.lineEdit()->cursorPosition(), expectedCursorPosition);
  246. }
  247. // ----------------------------------------------------------------------------
  248. void ctkDoubleSpinBoxTester::testPrefix_data()
  249. {
  250. QTest::addColumn<int>("cursorPosition");
  251. QTest::addColumn<int>("key");
  252. QTest::addColumn<QString>("expectedText");
  253. QTest::addColumn<double>("expectedValue");
  254. QTest::addColumn<int>("expectedCursorPosition");
  255. QTest::newRow("0:'1' -> 0.00") << 0 << int(Qt::Key_1) << "A: 0.00"<< 0.00 << 0;
  256. QTest::newRow("1:'1' -> 10.00") << 1 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  257. QTest::newRow("2:'1' -> 10.00") << 2 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  258. QTest::newRow("3:'1' -> 10.00") << 3 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  259. QTest::newRow("4:'1' -> 01.00") << 4 << int(Qt::Key_1) << "A: 01.00"<< 1.00 << 5;
  260. }
  261. // ----------------------------------------------------------------------------
  262. void ctkDoubleSpinBoxTester::testDecimalsByValue()
  263. {
  264. ctkDoubleSpinBox spinBox;
  265. spinBox.setMinimum(-100.);
  266. spinBox.setMaximum(100.);
  267. spinBox.setValue(1.23);
  268. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByValue );
  269. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  270. const int oldDecimals = spinBox.decimals();
  271. QFETCH(double, value);
  272. spinBox.setValue(value);
  273. QFETCH(QString, expectedText);
  274. QFETCH(int, expectedDecimals);
  275. QCOMPARE(spinBox.text(), expectedText);
  276. QCOMPARE(spinBox.value(), value);
  277. QCOMPARE(spinBox.decimals(), expectedDecimals);
  278. QCOMPARE(spy.count(), spinBox.decimals() != oldDecimals ? 1 : 0);
  279. }
  280. // ----------------------------------------------------------------------------
  281. void ctkDoubleSpinBoxTester::testDecimalsByValue_data()
  282. {
  283. QTest::addColumn<double>("value");
  284. QTest::addColumn<QString>("expectedText");
  285. QTest::addColumn<int>("expectedDecimals");
  286. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0") << 0. << "0"<< 0;
  287. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.1") << 0.1 << "0.1" << 1;
  288. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.02") << 0.02 << "0.02" << 2;
  289. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 10.003") << 10.003 << "10.003" << 3;
  290. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue -0.0004") << -0.0004 << "-0.0004" << 4;
  291. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.000056") << 0.000056 << "0.000056" << 6;
  292. // internally represented as 123456.001109999997425
  293. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 5.00111") << 5.00111 << "5.00111" << 5;
  294. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.1234567891013151") << 0.1234567891013151 << "0.1234567891013151" << 16;
  295. }
  296. // ----------------------------------------------------------------------------
  297. void ctkDoubleSpinBoxTester::testDecimalPointAlwaysVisible()
  298. {
  299. ctkDoubleSpinBox spinBox;
  300. spinBox.setDecimals(0);
  301. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalPointAlwaysVisible );
  302. QFETCH(double, value);
  303. spinBox.setValue(value);
  304. QFETCH(QString, expectedText);
  305. QCOMPARE(spinBox.text(), expectedText);
  306. }
  307. // ----------------------------------------------------------------------------
  308. void ctkDoubleSpinBoxTester::testDecimalPointAlwaysVisible_data()
  309. {
  310. QTest::addColumn<double>("value");
  311. QTest::addColumn<QString>("expectedText");
  312. QTest::newRow("ctkDoubleSpinBox::DecimalPointAlwaysVisible 0") << 0. << "0.";
  313. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 2") << 2. << "2.";
  314. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 1.01") << 1.01 << "1.";
  315. }
  316. // ----------------------------------------------------------------------------
  317. CTK_TEST_MAIN(ctkDoubleSpinBoxTest)
  318. #include "moc_ctkDoubleSpinBoxTest.cpp"