ctkDoubleSpinBoxTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // ----------------------------------------------------------------------------
  26. class ctkDoubleSpinBoxTester: public QObject
  27. {
  28. Q_OBJECT
  29. private slots:
  30. void testUI();
  31. void testToLocals();
  32. void testDecimalsByKey();
  33. void testDecimalsByKey_data();
  34. void testDecimalsByValue();
  35. void testDecimalsByValue_data();
  36. };
  37. // ----------------------------------------------------------------------------
  38. void ctkDoubleSpinBoxTester::testUI()
  39. {
  40. ctkDoubleSpinBox spinBox;
  41. spinBox.setMinimum(-100.);
  42. spinBox.setMaximum(100.);
  43. spinBox.setValue(1.);
  44. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByKey );
  45. spinBox.show();
  46. QDoubleSpinBox doubleSpinBox;
  47. doubleSpinBox.setMinimum(-100.);
  48. doubleSpinBox.setMaximum(100.);
  49. doubleSpinBox.setValue(2.);
  50. doubleSpinBox.show();
  51. QTest::qWaitForWindowShown(&spinBox);
  52. QTest::qWaitForWindowShown(&doubleSpinBox);
  53. //qApp->exec();
  54. }
  55. // ----------------------------------------------------------------------------
  56. void ctkDoubleSpinBoxTester::testToLocals()
  57. {
  58. bool ok;
  59. QLocale().toDouble("+.0", &ok);
  60. qDebug() << "+.0" << ok;
  61. QLocale().toDouble("0.0 1", &ok);
  62. qDebug() << "0.0 1" << ok;
  63. }
  64. // ----------------------------------------------------------------------------
  65. void ctkDoubleSpinBoxTester::testDecimalsByKey()
  66. {
  67. ctkDoubleSpinBox spinBox;
  68. spinBox.setMinimum(-100.);
  69. spinBox.setMaximum(100.);
  70. spinBox.setValue(1.);
  71. QFETCH(int, decimalsOptions);
  72. spinBox.setDecimalsOption( static_cast<ctkDoubleSpinBox::DecimalsOptions>(decimalsOptions) );
  73. QFETCH(int, cursorPosition);
  74. QFETCH(int, key);
  75. spinBox.lineEdit()->setCursorPosition(cursorPosition);
  76. //spinBox.show();
  77. //QTest::qWaitForWindowShown(&spinBox);
  78. //qApp->exec();
  79. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  80. QTest::keyClick(spinBox.lineEdit(), static_cast<Qt::Key>(key));
  81. QFETCH(QString, expectedText);
  82. QFETCH(int, expectedDecimals);
  83. QCOMPARE(spinBox.text(), expectedText);
  84. QCOMPARE(spinBox.value(), expectedText.toDouble());
  85. QCOMPARE(spinBox.decimals(), expectedDecimals);
  86. QCOMPARE(spy.count(), spinBox.decimals() != 2 ? 1 : 0);
  87. }
  88. // ----------------------------------------------------------------------------
  89. void ctkDoubleSpinBoxTester::testDecimalsByKey_data()
  90. {
  91. QTest::addColumn<int>("decimalsOptions");
  92. QTest::addColumn<int>("cursorPosition");
  93. QTest::addColumn<int>("key");
  94. QTest::addColumn<QString>("expectedText");
  95. QTest::addColumn<int>("expectedDecimals");
  96. QList<int> options;
  97. // ctkDoubleSpinBox::DecimalsByKey
  98. options << ctkDoubleSpinBox::DecimalsByKey;
  99. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'1' -> 11.00") << options.last() << 0 << int(Qt::Key_1) << "11.00"<< 2;
  100. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'-' -> -1.00") << options.last() << 0 << int(Qt::Key_Minus) << "-1.00" << 2;
  101. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'del' -> .00") << options.last() << 0 << int(Qt::Key_Delete) << ".00" << 2;
  102. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 0:'backspace' -> 1.00") << options.last() << 0 << int(Qt::Key_Backspace) << "1.00" << 2;
  103. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'1' -> 11.00") << options.last() << 1 << int(Qt::Key_1) << "11.00" << 2;
  104. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'-' -> 1.00") << options.last() << 1 << int(Qt::Key_Minus) << "1.00" << 2;
  105. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'del' -> 100.") << options.last() << 1 << int(Qt::Key_Delete) << "100." << 0;
  106. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 1:'backspace' -> .00") << options.last() << 1 << int(Qt::Key_Backspace) << ".00" << 2;
  107. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'1' -> 1.100") << options.last() << 2 << int(Qt::Key_1) << "1.100" << 3;
  108. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'-' -> 1.00") << options.last() << 2 << int(Qt::Key_Minus) << "1.00" << 2;
  109. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'del' -> 1.0") << options.last() << 2 << int(Qt::Key_Delete) << "1.0" << 1;
  110. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 2:'backspace' -> 100.") << options.last() << 2 << int(Qt::Key_Backspace) << "100." << 0;
  111. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'1' -> 1.010") << options.last() << 3 << int(Qt::Key_1) << "1.010" << 3;
  112. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'-' -> 1.00") << options.last() << 3 << int(Qt::Key_Minus) << "1.00" << 2;
  113. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'del' -> 1.0") << options.last() << 3 << int(Qt::Key_Delete) << "1.0" << 1;
  114. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 3:'backspace' -> 1.0") << options.last() << 3 << int(Qt::Key_Backspace) << "1.0" << 1;
  115. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'1' -> 1.001") << options.last() << 4 << int(Qt::Key_1) << "1.001" << 3;
  116. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'-' -> 1.00") << options.last() << 4 << int(Qt::Key_Minus) << "1.00" << 2;
  117. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'del' -> 1.00") << options.last() << 4 << int(Qt::Key_Delete) << "1.00" << 2;
  118. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey 4:'backspace' -> 1.0") << options.last() << 4 << int(Qt::Key_Backspace) << "1.0" << 1;
  119. // ctkDoubleSpinBox::ReplaceDecimals
  120. options << ctkDoubleSpinBox::ReplaceDecimals;
  121. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.00") << options.last() << 0 << int(Qt::Key_1) << "11.00"<< 2;
  122. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'-' -> -1.00") << options.last() << 0 << int(Qt::Key_Minus) << "-1.00" << 2;
  123. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .00") << options.last() << 0 << int(Qt::Key_Delete) << ".00" << 2;
  124. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.00") << options.last() << 0 << int(Qt::Key_Backspace) << "1.00" << 2;
  125. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.00") << options.last() << 1 << int(Qt::Key_1) << "11.00" << 2;
  126. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'-' -> 1.00") << options.last() << 1 << int(Qt::Key_Minus) << "1.00" << 2;
  127. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 100") << options.last() << 1 << int(Qt::Key_Delete) << "100" << 2;
  128. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .00") << options.last() << 1 << int(Qt::Key_Backspace) << ".00" << 2;
  129. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.10") << options.last() << 2 << int(Qt::Key_1) << "1.10" << 2;
  130. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'-' -> 1.00") << options.last() << 2 << int(Qt::Key_Minus) << "1.00" << 2;
  131. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.00") << options.last() << 2 << int(Qt::Key_Delete) << "1.0" << 2;
  132. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 100") << options.last() << 2 << int(Qt::Key_Backspace) << "100" << 2;
  133. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.01") << options.last() << 3 << int(Qt::Key_1) << "1.01" << 2;
  134. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'-' -> 1.00") << options.last() << 3 << int(Qt::Key_Minus) << "1.00" << 2;
  135. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.0") << options.last() << 3 << int(Qt::Key_Delete) << "1.0" << 2;
  136. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.00") << options.last() << 3 << int(Qt::Key_Backspace) << "1.0" << 2;
  137. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.00") << options.last() << 4 << int(Qt::Key_1) << "1.00" << 2;
  138. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'-' -> 1.00") << options.last() << 4 << int(Qt::Key_Minus) << "1.00" << 2;
  139. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.00") << options.last() << 4 << int(Qt::Key_Delete) << "1.00" << 2;
  140. QTest::newRow("ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.00") << options.last() << 4 << int(Qt::Key_Backspace) << "1.0" << 2;
  141. // ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals
  142. options << (ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals);
  143. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'1' -> 11.00")
  144. << options.last() << 0 << int(Qt::Key_1) << "11.00"<< 2;
  145. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'-' -> -1.00")
  146. << options.last() << 0 << int(Qt::Key_Minus) << "-1.00" << 2;
  147. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'del' -> .00")
  148. << options.last() << 0 << int(Qt::Key_Delete) << ".00" << 2;
  149. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 0:'backspace' -> 1.00")
  150. << options.last() << 0 << int(Qt::Key_Backspace) << "1.00" << 2;
  151. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'1' -> 11.00")
  152. << options.last() << 1 << int(Qt::Key_1) << "11.00" << 2;
  153. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'-' -> 1.00")
  154. << options.last() << 1 << int(Qt::Key_Minus) << "1.00" << 2;
  155. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'del' -> 100.")
  156. << options.last() << 1 << int(Qt::Key_Delete) << "100." << 0;
  157. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 1:'backspace' -> .00")
  158. << options.last() << 1 << int(Qt::Key_Backspace) << ".00" << 2;
  159. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'1' -> 1.10")
  160. << options.last() << 2 << int(Qt::Key_1) << "1.10" << 2;
  161. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'-' -> 1.00")
  162. << options.last() << 2 << int(Qt::Key_Minus) << "1.00" << 2;
  163. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'del' -> 1.0")
  164. << options.last() << 2 << int(Qt::Key_Delete) << "1.0" << 1;
  165. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 2:'backspace' -> 100.")
  166. << options.last() << 2 << int(Qt::Key_Backspace) << "100." << 0;
  167. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'1' -> 1.01")
  168. << options.last() << 3 << int(Qt::Key_1) << "1.01" << 2;
  169. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'-' -> 1.00")
  170. << options.last() << 3 << int(Qt::Key_Minus) << "1.00" << 2;
  171. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'del' -> 1.0")
  172. << options.last() << 3 << int(Qt::Key_Delete) << "1.0" << 1;
  173. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 3:'backspace' -> 1.0")
  174. << options.last() << 3 << int(Qt::Key_Backspace) << "1.0" << 1;
  175. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'1' -> 1.001")
  176. << options.last() << 4 << int(Qt::Key_1) << "1.001" << 3;
  177. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'-' -> 1.00")
  178. << options.last() << 4 << int(Qt::Key_Minus) << "1.00" << 2;
  179. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'del' -> 1.00")
  180. << options.last() << 4 << int(Qt::Key_Delete) << "1.00" << 2;
  181. QTest::newRow("ctkDoubleSpinBox::DecimalsByKey|ctkDoubleSpinBox::ReplaceDecimals 4:'backspace' -> 1.00")
  182. << options.last() << 4 << int(Qt::Key_Backspace) << "1.0" << 1;
  183. foreach(int option, options)
  184. {
  185. // bad keys are always rejected
  186. for (int i = 0; i < 5; ++i)
  187. {
  188. QTest::newRow(QString("%1 %2:'a' -> 1.00").arg(option).arg(i).toLatin1())
  189. << option << i << int(Qt::Key_A) << "1.00" << 2;
  190. }
  191. // sign keys are only for the first digit
  192. QTest::newRow(QString("%1 0:'+' -> 1.00").arg(option).toLatin1())
  193. << option << 0 << int(Qt::Key_Plus) << "+1.00" << 2;
  194. QTest::newRow(QString("%1 0:'-' -> -1.00").arg(option).toLatin1())
  195. << option << 0 << int(Qt::Key_Minus) << "-1.00" << 2;
  196. for (int i = 1; i < 5; ++i)
  197. {
  198. QTest::newRow(QString("%1 %2:'+' -> 1.00").arg(option).arg(i).toLatin1())
  199. << option << i << int(Qt::Key_Plus) << "1.00" << 2;
  200. QTest::newRow(QString("%1 %2:'-' -> 1.00").arg(option).arg(i).toLatin1())
  201. << option << i << int(Qt::Key_Minus) << "1.00" << 2;
  202. }
  203. }
  204. }
  205. // ----------------------------------------------------------------------------
  206. void ctkDoubleSpinBoxTester::testDecimalsByValue()
  207. {
  208. ctkDoubleSpinBox spinBox;
  209. spinBox.setMinimum(-100.);
  210. spinBox.setMaximum(100.);
  211. spinBox.setValue(1.);
  212. spinBox.setDecimalsOption( ctkDoubleSpinBox::DecimalsByValue );
  213. QSignalSpy spy(&spinBox, SIGNAL(decimalsChanged(int)));
  214. QFETCH(double, value);
  215. spinBox.setValue(value);
  216. QFETCH(QString, expectedText);
  217. QFETCH(int, expectedDecimals);
  218. QCOMPARE(spinBox.text(), expectedText);
  219. QCOMPARE(spinBox.value(), value);
  220. QCOMPARE(spinBox.decimals(), expectedDecimals);
  221. QCOMPARE(spy.count(), spinBox.decimals() != 2 ? 1 : 0);
  222. }
  223. // ----------------------------------------------------------------------------
  224. void ctkDoubleSpinBoxTester::testDecimalsByValue_data()
  225. {
  226. QTest::addColumn<double>("value");
  227. QTest::addColumn<QString>("expectedText");
  228. QTest::addColumn<int>("expectedDecimals");
  229. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.00") << 0.00 << "0."<< 0;
  230. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.1") << 0.1 << "0.1" << 1;
  231. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.02") << 0.02 << "0.02" << 2;
  232. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 10.003") << 10.003 << "10.003" << 3;
  233. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue -0.0004") << -0.0004 << "-0.0004" << 4;
  234. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.000056") << 0.000056 << "0.000056" << 6;
  235. // internally represented as 123456.001109999997425
  236. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 5.00111") << 5.00111 << "5.00111" << 5;
  237. QTest::newRow("ctkDoubleSpinBox::DecimalsByValue 0.1234567891013151") << 0.1234567891013151 << "0.1234567891013151" << 16;
  238. }
  239. // ----------------------------------------------------------------------------
  240. CTK_TEST_MAIN(ctkDoubleSpinBoxTest)
  241. #include "moc_ctkDoubleSpinBoxTest.cpp"