ctkDoubleSpinBoxTest.cpp 27 KB

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