ctkDoubleSpinBoxTest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. // STD includes
  26. #include <limits>
  27. // ----------------------------------------------------------------------------
  28. class ctkDoubleSpinBoxTester: public QObject
  29. {
  30. Q_OBJECT
  31. private slots:
  32. void testUI();
  33. void testToLocals();
  34. void testSetValue();
  35. void testSetValue_data();
  36. void testSetValueOutsideRange();
  37. void testSetMinimum();
  38. void testSetMinimum_data();
  39. void testSetMaximum();
  40. void testSetMaximum_data();
  41. void testSetRange();
  42. void testSetRange_data();
  43. void testDecimalsByKey();
  44. void testDecimalsByKey_data();
  45. void testPrefix();
  46. void testPrefix_data();
  47. void testDecimalsByValue();
  48. void testDecimalsByValue_data();
  49. void testDecimalPointAlwaysVisible();
  50. void testDecimalPointAlwaysVisible_data();
  51. };
  52. // ----------------------------------------------------------------------------
  53. void ctkDoubleSpinBoxTester::testUI()
  54. {
  55. ctkDoubleSpinBox spinBox;
  56. spinBox.setMinimum(-100.);
  57. spinBox.setMaximum(100.);
  58. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByValue |ctkDoubleSpinBox::DecimalsByShortcuts );
  59. spinBox.setValue(26.2110001);
  60. spinBox.setPrefix("A: ");
  61. spinBox.setSetMode(ctkDoubleSpinBox::SetAlways);
  62. spinBox.show();
  63. #if (QT_VERSION >= 0x50000)
  64. QTest::qWaitForWindowActive(&spinBox);
  65. #else
  66. QTest::qWaitForWindowShown(&spinBox);
  67. #endif
  68. QObject::connect(&spinBox, SIGNAL(valueChanged(double)),
  69. &spinBox, SLOT(setValue(double)), Qt::QueuedConnection);
  70. QDoubleSpinBox doubleSpinBox;
  71. doubleSpinBox.setMinimum(-100.);
  72. doubleSpinBox.setMaximum(100.);
  73. doubleSpinBox.setValue(2.);
  74. //doubleSpinBox.show();
  75. //QTest::qWaitForWindowShown(&doubleSpinBox);
  76. //qApp->exec();
  77. }
  78. // ----------------------------------------------------------------------------
  79. void ctkDoubleSpinBoxTester::testToLocals()
  80. {
  81. bool ok;
  82. QLocale().toDouble("+.0", &ok);
  83. QVERIFY(ok);
  84. QLocale().toDouble("0.0 1", &ok);
  85. QVERIFY(!ok);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkDoubleSpinBoxTester::testSetValue()
  89. {
  90. ctkDoubleSpinBox spinBox;
  91. spinBox.setValue(25.);
  92. QFETCH(double, value);
  93. QSignalSpy valueChangedSpy(&spinBox, SIGNAL(valueChanged(double)));
  94. spinBox.setValue(value);
  95. QFETCH(double, expectedValue);
  96. QCOMPARE(spinBox.value(), expectedValue);
  97. const bool valueChanged = (expectedValue != 25.);
  98. QCOMPARE(valueChangedSpy.count(), valueChanged ? 1 : 0);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void ctkDoubleSpinBoxTester::testSetValue_data()
  102. {
  103. QTest::addColumn<double>("value");
  104. QTest::addColumn<double>("expectedValue");
  105. QTest::newRow("1. -> 1.]") << 1. << 1.;
  106. QTest::newRow("25. -> 25.]") << 25. << 25.;
  107. QTest::newRow("25.00001 -> 25.00001]") << 25.00001 << 25.00001;
  108. QTest::newRow("100. -> 99.99]") << 100. << 99.99;
  109. QTest::newRow("-1. -> 0.]") << -1. << 0.;
  110. QTest::newRow("min -> 0.") << std::numeric_limits<double>::min() << std::numeric_limits<double>::min();
  111. QTest::newRow("max -> 99.99") << std::numeric_limits<double>::max() << 99.99;
  112. QTest::newRow("-inf -> 0.") << -std::numeric_limits<double>::infinity() << 0.;
  113. QTest::newRow("inf -> 99.99") << std::numeric_limits<double>::infinity() << 99.99;
  114. QTest::newRow("NaN -> 99.99") << std::numeric_limits<double>::quiet_NaN() << 0.;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void ctkDoubleSpinBoxTester::testSetValueOutsideRange()
  118. {
  119. // This test is a bit different from testSetValue(), we start at 0. and must
  120. // stay in 0.
  121. ctkDoubleSpinBox spinBox;
  122. QSignalSpy valueChangedSpy(&spinBox, SIGNAL(valueChanged(double)));
  123. spinBox.setValue(-10.);
  124. QCOMPARE(spinBox.value(), 0.);
  125. QCOMPARE(valueChangedSpy.count(), 0);
  126. }
  127. // ----------------------------------------------------------------------------
  128. void ctkDoubleSpinBoxTester::testSetMinimum()
  129. {
  130. ctkDoubleSpinBox spinBox;
  131. QFETCH(double, minimum);
  132. spinBox.setMinimum(minimum);
  133. QFETCH(double, expectedMinimum);
  134. QFETCH(double, expectedValue);
  135. QCOMPARE(spinBox.minimum(), expectedMinimum);
  136. QCOMPARE(spinBox.value(), expectedValue);
  137. }
  138. // ----------------------------------------------------------------------------
  139. void ctkDoubleSpinBoxTester::testSetMinimum_data()
  140. {
  141. QTest::addColumn<double>("minimum");
  142. QTest::addColumn<double>("expectedMinimum");
  143. QTest::addColumn<double>("expectedValue");
  144. QTest::newRow("0. -> 0.") << 0. << 0. << 0.;
  145. QTest::newRow("99.99 -> 99.99") << 99.99 << 99.99 << 99.99;
  146. QTest::newRow("10.0123 -> 10.0123") << 10.0123 << 10.0123 << 10.0123;
  147. QTest::newRow("-10.0123 -> 0.") << -10.0123 << -10.0123 << 0.;
  148. QTest::newRow("200.0123 -> 200.0123") << 200.0123 << 200.0123 << 200.0123;
  149. }
  150. // ----------------------------------------------------------------------------
  151. void ctkDoubleSpinBoxTester::testSetMaximum()
  152. {
  153. ctkDoubleSpinBox spinBox;
  154. QFETCH(double, maximum);
  155. spinBox.setMaximum(maximum);
  156. QFETCH(double, expectedMaximum);
  157. QFETCH(double, expectedValue);
  158. QCOMPARE(spinBox.maximum(), expectedMaximum);
  159. QCOMPARE(spinBox.value(), expectedValue);
  160. }
  161. // ----------------------------------------------------------------------------
  162. void ctkDoubleSpinBoxTester::testSetMaximum_data()
  163. {
  164. QTest::addColumn<double>("maximum");
  165. QTest::addColumn<double>("expectedMaximum");
  166. QTest::addColumn<double>("expectedValue");
  167. QTest::newRow("0. -> 0.") << 0. << 0. << 0.;
  168. QTest::newRow("99.99 -> 0.") << 99.99 << 99.99 << 0.;
  169. QTest::newRow("10.0123 -> 0.") << 10.0123 << 10.0123 << 0.;
  170. QTest::newRow("-10.0123 -> -10.0123") << -10.0123 << -10.0123 << -10.0123;
  171. QTest::newRow("200.0123 -> 0.") << 200.0123 << 200.0123 << 0.;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void ctkDoubleSpinBoxTester::testSetRange()
  175. {
  176. ctkDoubleSpinBox spinBox;
  177. spinBox.setValue(25.);
  178. QSignalSpy valueChangedSpy(&spinBox,
  179. SIGNAL(valueChanged(double)));
  180. QFETCH(double, minimum);
  181. QFETCH(double, maximum);
  182. spinBox.setRange(minimum, maximum);
  183. QFETCH(double, expectedMinimum);
  184. QFETCH(double, expectedMaximum);
  185. ctkTest::COMPARE(spinBox.minimum(), expectedMinimum);
  186. ctkTest::COMPARE(spinBox.maximum(), expectedMaximum);
  187. QFETCH(double, expectedValue);
  188. ctkTest::COMPARE(spinBox.value(), expectedValue);
  189. const bool valueChanged = expectedValue != 25.;
  190. ctkTest::COMPARE(valueChangedSpy.count(), valueChanged ? 1 : 0);
  191. }
  192. //-----------------------------------------------------------------------------
  193. void ctkDoubleSpinBoxTester::testSetRange_data()
  194. {
  195. QTest::addColumn<double>("minimum");
  196. QTest::addColumn<double>("maximum");
  197. QTest::addColumn<double>("expectedMinimum");
  198. QTest::addColumn<double>("expectedMaximum");
  199. QTest::addColumn<double>("expectedValue");
  200. QTest::newRow("[1.,98.]") << 1. << 98. << 1. << 98. << 25.;
  201. QTest::newRow("[-1.,101.]") << -1. << 101. << -1. << 101. << 25.;
  202. QTest::newRow("[1.,10.]") << 1. << 10. << 1. << 10. << 10.;
  203. QTest::newRow("[90.,99.]") << 90. << 99. << 90. << 99. << 90.;
  204. QTest::newRow("[min,max]")
  205. << std::numeric_limits<double>::min()
  206. << std::numeric_limits<double>::max()
  207. << std::numeric_limits<double>::min()
  208. << QVariant(std::numeric_limits<double>::max()).toDouble()
  209. << 25.;
  210. QTest::newRow("[-max,max]")
  211. << -std::numeric_limits<double>::max()
  212. << std::numeric_limits<double>::max()
  213. << -std::numeric_limits<double>::max()
  214. << std::numeric_limits<double>::max()
  215. << 25.;
  216. QTest::newRow("[-inf,inf]")
  217. << -std::numeric_limits<double>::infinity()
  218. << std::numeric_limits<double>::infinity()
  219. << -std::numeric_limits<double>::infinity()
  220. << std::numeric_limits<double>::infinity()
  221. << 25.;
  222. QTest::newRow("[NaN,NaN]")
  223. << std::numeric_limits<double>::quiet_NaN()
  224. << std::numeric_limits<double>::quiet_NaN()
  225. << std::numeric_limits<double>::quiet_NaN()
  226. << std::numeric_limits<double>::quiet_NaN()
  227. << std::numeric_limits<double>::quiet_NaN();
  228. }
  229. // ----------------------------------------------------------------------------
  230. void ctkDoubleSpinBoxTester::testDecimalsByKey()
  231. {
  232. ctkDoubleSpinBox spinBox;
  233. spinBox.setMinimum(-200.);
  234. spinBox.setMaximum(200.);
  235. spinBox.setValue(1.23);
  236. QFETCH(int, decimalsOptions);
  237. spinBox.setDecimalsOption( static_cast<ctkDoubleSpinBox::DecimalsOptions>(decimalsOptions) );
  238. const int oldDecimals = spinBox.decimals();
  239. QFETCH(int, cursorPosition);
  240. QFETCH(int, key);
  241. spinBox.lineEdit()->setCursorPosition(cursorPosition);
  242. //spinBox.show();
  243. //QTest::qWaitForWindowShown(&spinBox);
  244. //qApp->exec();
  245. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  246. QTest::keyClick(spinBox.lineEdit(), static_cast<Qt::Key>(key));
  247. QFETCH(QString, expectedText);
  248. QFETCH(double, expectedValue);
  249. QFETCH(int, expectedDecimals);
  250. QCOMPARE(spinBox.text(), expectedText);
  251. QCOMPARE(spinBox.value(), expectedValue);
  252. QCOMPARE(spinBox.decimals(), expectedDecimals);
  253. QCOMPARE(spy.count(), spinBox.decimals() != oldDecimals ? 1 : 0);
  254. }
  255. // ----------------------------------------------------------------------------
  256. void ctkDoubleSpinBoxTester::testDecimalsByKey_data()
  257. {
  258. QTest::addColumn<int>("decimalsOptions");
  259. QTest::addColumn<int>("cursorPosition");
  260. QTest::addColumn<int>("key");
  261. QTest::addColumn<QString>("expectedText");
  262. QTest::addColumn<double>("expectedValue");
  263. QTest::addColumn<int>("expectedDecimals");
  264. QList<int> options;
  265. // ctkDoubleSpinBox::DecimalsByKey
  266. options << ctkDoubleSpinBox::DecimalsByKey;
  267. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'1' -> 11.23")
  268. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 11.23 << 2;
  269. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'del' -> .23")
  270. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << .23 << 2;
  271. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'backspace' -> 1.23")
  272. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 1.23 << 2;
  273. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'1' -> 11.23")
  274. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 11.23 << 2;
  275. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'del' -> 123")
  276. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 123. << 0;
  277. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'backspace' -> .23")
  278. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << .23 << 2;
  279. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'1' -> 1.12")
  280. << options.last() << 2 << int(Qt::Key_1) << "1.12" << 1.12 << 2;
  281. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'del' -> 1.3")
  282. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1.3 << 1;
  283. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'backspace' -> 123")
  284. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 123. << 0;
  285. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'1' -> 1.21")
  286. << options.last() << 3 << int(Qt::Key_1) << "1.21" << 1.21 << 2;
  287. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'del' -> 1.2")
  288. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1.2 << 1;
  289. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'backspace' -> 1.3")
  290. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1.3 << 1;
  291. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'1' -> 1.231")
  292. << options.last() << 4 << int(Qt::Key_1) << "1.231" << 1.231 << 3;
  293. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'del' -> 1.23")
  294. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 1.23 << 2;
  295. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'backspace' -> 1.2")
  296. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1.2 << 1;
  297. // ctkDoubleSpinBox::ReplaceDecimals
  298. options << ctkDoubleSpinBox::ReplaceDecimals;
  299. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.23")
  300. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 11.23 << 2;
  301. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .23")
  302. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << .23 << 2;
  303. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.23")
  304. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 1.23 << 2;
  305. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.23")
  306. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 11.23 << 2;
  307. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 123")
  308. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 123. << 2;
  309. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .23")
  310. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << .23 << 2;
  311. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.13")
  312. << options.last() << 2 << int(Qt::Key_1) << "1.13" << 1.13 << 2;
  313. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.3")
  314. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1.3 << 2;
  315. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 123")
  316. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 123. << 2;
  317. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.21")
  318. << options.last() << 3 << int(Qt::Key_1) << "1.21" << 1.21 << 2;
  319. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.2")
  320. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1.2 << 2;
  321. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.3")
  322. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1.3 << 2;
  323. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.23")
  324. << options.last() << 4 << int(Qt::Key_1) << "1.23" << 1.23 << 2;
  325. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.23")
  326. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 1.23 << 2;
  327. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.2")
  328. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1.2 << 2;
  329. // ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals
  330. options << (ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals);
  331. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.23")
  332. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 11.23 << 2;
  333. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .23")
  334. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << .23 << 2;
  335. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.23")
  336. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 1.23 << 2;
  337. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.23")
  338. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 11.23 << 2;
  339. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 123")
  340. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 123. << 0;
  341. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .23")
  342. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << .23 << 2;
  343. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.13")
  344. << options.last() << 2 << int(Qt::Key_1) << "1.13" << 1.13 << 2;
  345. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.3")
  346. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1.3 << 1;
  347. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 123")
  348. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 123. << 0;
  349. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.21")
  350. << options.last() << 3 << int(Qt::Key_1) << "1.21" << 1.21 << 2;
  351. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.2")
  352. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1.2 << 1;
  353. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.3")
  354. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1.3 << 1;
  355. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.231")
  356. << options.last() << 4 << int(Qt::Key_1) << "1.231" << 1.231 << 3;
  357. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.23")
  358. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 1.23 << 2;
  359. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.2")
  360. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1.2 << 1;
  361. // ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals
  362. options << (ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals);
  363. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'1' -> 11.23")
  364. << options.last() << 0 << int(Qt::Key_1) << "11.23"<< 11.23 << 2;
  365. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'del' -> .23")
  366. << options.last() << 0 << int(Qt::Key_Delete) << ".23" << .23 << 2;
  367. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 0:'backspace' -> 1.23")
  368. << options.last() << 0 << int(Qt::Key_Backspace) << "1.23" << 1.23 << 2;
  369. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'1' -> 11.23")
  370. << options.last() << 1 << int(Qt::Key_1) << "11.23" << 11.23 << 2;
  371. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'del' -> 123")
  372. << options.last() << 1 << int(Qt::Key_Delete) << "123" << 123. << 0;
  373. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 1:'backspace' -> .23")
  374. << options.last() << 1 << int(Qt::Key_Backspace) << ".23" << .23 << 2;
  375. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'1' -> 1.123")
  376. << options.last() << 2 << int(Qt::Key_1) << "1.123" << 1.123 << 3;
  377. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'del' -> 1.3")
  378. << options.last() << 2 << int(Qt::Key_Delete) << "1.3" << 1.3 << 1;
  379. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 2:'backspace' -> 123")
  380. << options.last() << 2 << int(Qt::Key_Backspace) << "123" << 123. << 0;
  381. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'1' -> 1.213")
  382. << options.last() << 3 << int(Qt::Key_1) << "1.213" << 1.213 << 3;
  383. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'del' -> 1.2")
  384. << options.last() << 3 << int(Qt::Key_Delete) << "1.2" << 1.2 << 1;
  385. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 3:'backspace' -> 1.3")
  386. << options.last() << 3 << int(Qt::Key_Backspace) << "1.3" << 1.3 << 1;
  387. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'1' -> 1.231")
  388. << options.last() << 4 << int(Qt::Key_1) << "1.231" << 1.231 << 3;
  389. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'del' -> 1.23")
  390. << options.last() << 4 << int(Qt::Key_Delete) << "1.23" << 1.23 << 2;
  391. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::InsertDecimals 4:'backspace' -> 1.2")
  392. << options.last() << 4 << int(Qt::Key_Backspace) << "1.2" << 1.2 << 1;
  393. foreach(int option, options)
  394. {
  395. // bad keys are always rejected
  396. for (int i = 0; i < 5; ++i)
  397. {
  398. QTest::newRow(QString("%1 %2:'a' -> 1.23").arg(option).arg(i).toLatin1())
  399. << option << i << int(Qt::Key_A) << "1.23" << 1.23 << 2;
  400. }
  401. // sign keys are only for the first digit
  402. QTest::newRow(QString("%1 0:'+' -> 1.23").arg(option).toLatin1())
  403. << option << 0 << int(Qt::Key_Plus) << "+1.23" << 1.23 << 2;
  404. QTest::newRow(QString("%1 0:'-' -> -1.23").arg(option).toLatin1())
  405. << option << 0 << int(Qt::Key_Minus) << "-1.23" << -1.23 << 2;
  406. for (int i = 1; i < 5; ++i)
  407. {
  408. QTest::newRow(QString("%1 %2:'+' -> 1.23").arg(option).arg(i).toLatin1())
  409. << option << i << int(Qt::Key_Plus) << "1.23" << 1.23 << 2;
  410. QTest::newRow(QString("%1 %2:'-' -> 1.23").arg(option).arg(i).toLatin1())
  411. << option << i << int(Qt::Key_Minus) << "1.23" << 1.23 << 2;
  412. }
  413. }
  414. }
  415. // ----------------------------------------------------------------------------
  416. void ctkDoubleSpinBoxTester::testPrefix()
  417. {
  418. ctkDoubleSpinBox spinBox;
  419. spinBox.setPrefix("A: ");
  420. spinBox.setDecimalsOption( ctkDoubleSpinBox::FixedDecimals );
  421. QFETCH(int, cursorPosition);
  422. QFETCH(int, key);
  423. spinBox.lineEdit()->setCursorPosition(cursorPosition);
  424. QTest::keyClick(spinBox.lineEdit(), static_cast<Qt::Key>(key));
  425. //spinBox.show();
  426. //QTest::qWaitForWindowShown(&spinBox);
  427. //qApp->exec();
  428. QFETCH(double, expectedValue);
  429. QFETCH(QString, expectedText);
  430. QFETCH(int, expectedCursorPosition);
  431. QCOMPARE(spinBox.text(), expectedText);
  432. QCOMPARE(spinBox.value(), expectedValue);
  433. QCOMPARE(spinBox.decimals(), 2);
  434. QCOMPARE(spinBox.lineEdit()->cursorPosition(), expectedCursorPosition);
  435. }
  436. // ----------------------------------------------------------------------------
  437. void ctkDoubleSpinBoxTester::testPrefix_data()
  438. {
  439. QTest::addColumn<int>("cursorPosition");
  440. QTest::addColumn<int>("key");
  441. QTest::addColumn<QString>("expectedText");
  442. QTest::addColumn<double>("expectedValue");
  443. QTest::addColumn<int>("expectedCursorPosition");
  444. QTest::newRow("0:'1' -> 0.00") << 0 << int(Qt::Key_1) << "A: 0.00"<< 0.00 << 0;
  445. QTest::newRow("1:'1' -> 10.00") << 1 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  446. QTest::newRow("2:'1' -> 10.00") << 2 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  447. QTest::newRow("3:'1' -> 10.00") << 3 << int(Qt::Key_1) << "A: 10.00"<< 10.00 << 4;
  448. QTest::newRow("4:'1' -> 01.00") << 4 << int(Qt::Key_1) << "A: 01.00"<< 1.00 << 5;
  449. }
  450. // ----------------------------------------------------------------------------
  451. void ctkDoubleSpinBoxTester::testDecimalsByValue()
  452. {
  453. ctkDoubleSpinBox spinBox;
  454. spinBox.setMinimum(-100.);
  455. spinBox.setMaximum(100.);
  456. spinBox.setValue(1.23);
  457. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByValue );
  458. spinBox.setDecimals(4);
  459. const int oldDecimals = spinBox.decimals();
  460. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  461. QFETCH(double, value);
  462. spinBox.setValue(value);
  463. QFETCH(QString, expectedText);
  464. QFETCH(double, expectedValue);
  465. QFETCH(int, expectedDecimals);
  466. QCOMPARE(spinBox.text(), expectedText);
  467. QCOMPARE(spinBox.value(), expectedValue);
  468. QCOMPARE(spinBox.decimals(), expectedDecimals);
  469. QCOMPARE(spy.count(), spinBox.decimals() != oldDecimals ? 1 : 0);
  470. }
  471. // ----------------------------------------------------------------------------
  472. void ctkDoubleSpinBoxTester::testDecimalsByValue_data()
  473. {
  474. QTest::addColumn<double>("value");
  475. QTest::addColumn<QString>("expectedText");
  476. QTest::addColumn<double>("expectedValue");
  477. QTest::addColumn<int>("expectedDecimals");
  478. QTest::newRow("0") << 0. << "0"<< 0. << 0;
  479. QTest::newRow("0.1") << 0.1 << "0.1" << 0.1 << 1;
  480. QTest::newRow("0.02") << 0.02 << "0.02" << 0.02 << 2;
  481. QTest::newRow("10.003") << 10.003 << "10.003" << 10.003 << 3;
  482. QTest::newRow("-0.0004") << -0.0004 << "-0.0004" << -0.0004 << 4;
  483. QTest::newRow("0.000056") << 0.000056 << "0.000056" << 0.000056 << 6;
  484. // internally represented as 123456.001109999997425
  485. QTest::newRow("5.00111") << 5.00111 << "5.00111" << 5.00111 << 5;
  486. QTest::newRow("same value with more decimals") << 1.234567 << "1.234567" << 1.234567 << 6;
  487. QTest::newRow("same value") << 1.23 << "1.23" << 1.23 << 2;
  488. QTest::newRow("same value with less decimals") << 1.234 << "1.234" << 1.234 << 3;
  489. QTest::newRow("16 decimals") << 0.1234567891013151 << "0.1235" << 0.1234567891013151 << 4;
  490. }
  491. // ----------------------------------------------------------------------------
  492. void ctkDoubleSpinBoxTester::testDecimalPointAlwaysVisible()
  493. {
  494. ctkDoubleSpinBox spinBox;
  495. spinBox.setDecimals(0);
  496. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalPointAlwaysVisible );
  497. QFETCH(double, value);
  498. spinBox.setValue(value);
  499. QFETCH(QString, expectedText);
  500. QCOMPARE(spinBox.text(), expectedText);
  501. }
  502. // ----------------------------------------------------------------------------
  503. void ctkDoubleSpinBoxTester::testDecimalPointAlwaysVisible_data()
  504. {
  505. QTest::addColumn<double>("value");
  506. QTest::addColumn<QString>("expectedText");
  507. QTest::newRow("ctkDoubleSpinBox::DecimalPointAlwaysVisible 0") << 0. << "0.";
  508. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 2") << 2. << "2.";
  509. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 1.01") << 1.01 << "1.";
  510. }
  511. // ----------------------------------------------------------------------------
  512. CTK_TEST_MAIN(ctkDoubleSpinBoxTest)
  513. #include "moc_ctkDoubleSpinBoxTest.cpp"