ctkDoubleSpinBoxTest.cpp 27 KB

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