ctkDoubleSpinBoxTest.cpp 23 KB

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