ctkDoubleSpinBoxTest.cpp 27 KB

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