ctkDoubleSpinBoxTest.cpp 25 KB

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