ctkDoubleSpinBoxValueProxyTest.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <QDebug>
  17. #include <QString>
  18. // CTK includes
  19. #include "ctkDoubleSpinBox.h"
  20. #include "ctkLinearValueProxy.h"
  21. #include "ctkTest.h"
  22. #include "ctkValueProxy.h"
  23. // STD includes
  24. #include <limits>
  25. namespace
  26. {
  27. //-----------------------------------------------------------------------------
  28. void getSpyReport(QSignalSpy& spy, double expectedValue)
  29. {
  30. QCOMPARE(spy.count(), 1);
  31. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  32. QCOMPARE(arguments.at(0).toDouble(), expectedValue);
  33. }
  34. //-----------------------------------------------------------------------------
  35. void getSpyReport(QSignalSpy& spy, QString expectedValue)
  36. {
  37. QCOMPARE(spy.count(), 1);
  38. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  39. QCOMPARE(arguments.at(0).toString(), expectedValue);
  40. }
  41. } // end namespace
  42. // ----------------------------------------------------------------------------
  43. class ctkDoubleSpinBoxValueProxyTester: public QObject
  44. {
  45. Q_OBJECT
  46. private slots:
  47. void testSetValue();
  48. void testSetValue_data();
  49. void testSetDisplayedValue();
  50. void testSetDisplayedValue_data();
  51. void testSetCoefficient();
  52. void testSetCoefficient_data();
  53. };
  54. //-----------------------------------------------------------------------------
  55. void ctkDoubleSpinBoxValueProxyTester::testSetValue()
  56. {
  57. // Setup
  58. ctkDoubleSpinBox spinBox;
  59. spinBox.setRange(-200., 200.);
  60. spinBox.setValue(-32.6);
  61. QFETCH(double, coefficient);
  62. QFETCH(double, offset);
  63. ctkLinearValueProxy proxy;
  64. proxy.setCoefficient(coefficient);
  65. proxy.setOffset(offset);
  66. spinBox.setValueProxy(&proxy);
  67. // Spies
  68. QSignalSpy valueSpy(&spinBox, SIGNAL(valueChanged(double)));
  69. QSignalSpy valueStringSpy(&spinBox, SIGNAL(valueChanged(QString)));
  70. // Test
  71. QFETCH(double, value);
  72. spinBox.setValue(value);
  73. QFETCH(double, expectedValue);
  74. QFETCH(QString, expectedStringValue);
  75. getSpyReport(valueSpy, expectedValue);
  76. getSpyReport(valueStringSpy, expectedStringValue);
  77. ctkTest::COMPARE(spinBox.value(), expectedValue);
  78. }
  79. //-----------------------------------------------------------------------------
  80. void ctkDoubleSpinBoxValueProxyTester::testSetValue_data()
  81. {
  82. QTest::addColumn<double>("coefficient");
  83. QTest::addColumn<double>("offset");
  84. QTest::addColumn<double>("value");
  85. QTest::addColumn<double>("expectedValue");
  86. QTest::addColumn<QString>("expectedStringValue");
  87. //---------------------------------------------------------------------------
  88. // Offset
  89. QTest::newRow("Offset only") << 1. << 42.19 << 0.1 << 0.1 << "0.10";
  90. // \tbd could be improved ?
  91. QTest::newRow("Offset only: keep precision 3")
  92. << 1. << 42.197 << 0.1 << 0.1 << "0.10";
  93. QTest::newRow("Offset only: keep precision 4")
  94. << 1. << 42.1971 << 0.1 << 0.1 << "0.10";
  95. QTest::newRow("Offset only: less than min")
  96. << 1. << -42.19 << -220.0 << -200. << "-200.00";
  97. QTest::newRow("Offset only: less than min with offset")
  98. << 1. << -42.19 << -190.0 << -190. << "-190.00";
  99. QTest::newRow("Offset only: more than min")
  100. << 1. << 42.19 << -190.0 << -190. << "-190.00";
  101. QTest::newRow("Offset only: more than min with offset")
  102. << 1. << 42.19 << -220.0 << -200. << "-200.00";
  103. QTest::newRow("Offset only: more than max")
  104. << 1. << 42.19 << 220.0 << 200. << "200.00";
  105. QTest::newRow("Offset only: more than max with offset")
  106. << 1. << 42.19 << 190.0 << 190. << "190.00";
  107. QTest::newRow("Offset only: less than max")
  108. << 1. << -42.19 << 190.0 << 190. << "190.00";
  109. QTest::newRow("Offset only: less than max with offset")
  110. << 1. << -42.19 << 220.0 << 200. << "200.00";
  111. //---------------------------------------------------------------------------
  112. // Coefficient
  113. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1 << "0.10";
  114. QTest::newRow("Coeff only: less than min")
  115. << 5. << 0. << -220. << -200. << "-200.00";
  116. QTest::newRow("Coeff only: less than min with offset")
  117. << 5. << 0. << -190. << -190. << "-190.00";
  118. QTest::newRow("Coeff only: more than min")
  119. << 0.5 << 0. << -190. << -190. << "-190.00";
  120. QTest::newRow("Coeff only: more than min with offset")
  121. << 0.5 << 0. << -220. << -200. << "-200.00";
  122. QTest::newRow("Coeff only: more than max")
  123. << 5. << 0. << 220. << 200. << "200.00";
  124. QTest::newRow("Coeff only: more than max with offset")
  125. << 5. << 0. << 190. << 190. << "190.00";
  126. QTest::newRow("Coeff only: less than max")
  127. << 0.5 << 0. << 190. << 190. << "190.00";
  128. QTest::newRow("Coeff only: less than max with offset")
  129. << 0.5 << 0. << 220. << 200. << "200.00";
  130. }
  131. //-----------------------------------------------------------------------------
  132. void ctkDoubleSpinBoxValueProxyTester::testSetDisplayedValue()
  133. {
  134. // Setup
  135. ctkDoubleSpinBox spinBox;
  136. spinBox.setMinimum(-200);
  137. spinBox.setMaximum(200);
  138. spinBox.setValue(-32.6);
  139. QFETCH(double, coefficient);
  140. QFETCH(double, offset);
  141. ctkLinearValueProxy proxy;
  142. proxy.setCoefficient(coefficient);
  143. proxy.setOffset(offset);
  144. spinBox.setValueProxy(&proxy);
  145. // Spies
  146. QSignalSpy valueSpy(&spinBox, SIGNAL(valueChanged(double)));
  147. QSignalSpy valueStringSpy(&spinBox, SIGNAL(valueChanged(QString)));
  148. // Test
  149. QFETCH(double, displayValue);
  150. spinBox.setDisplayedValue(displayValue);
  151. QFETCH(double, expectedValue);
  152. QFETCH(QString, expectedStringValue);
  153. QFETCH(double, expectedDisplayValue);
  154. getSpyReport(valueSpy, expectedValue);
  155. getSpyReport(valueStringSpy, expectedStringValue);
  156. QCOMPARE(spinBox.value(), expectedValue);
  157. QCOMPARE(spinBox.displayedValue(), expectedDisplayValue);
  158. }
  159. //-----------------------------------------------------------------------------
  160. void ctkDoubleSpinBoxValueProxyTester::testSetDisplayedValue_data()
  161. {
  162. QTest::addColumn<double>("coefficient");
  163. QTest::addColumn<double>("offset");
  164. QTest::addColumn<double>("displayValue");
  165. QTest::addColumn<double>("expectedValue");
  166. QTest::addColumn<QString>("expectedStringValue");
  167. QTest::addColumn<double>("expectedDisplayValue");
  168. //---------------------------------------------------------------------------
  169. // Offset
  170. QTest::newRow("Offset only")
  171. << 1.0 << 42.19 << 0.1 << -42.09 << "-42.09" << 0.1;
  172. QTest::newRow("Offset only: less than min")
  173. << 1.0 << 42.19 << -510.0 << -200. << "-200.00" << -157.81;
  174. QTest::newRow("Offset only: more than max")
  175. << 1.0 << -42.19 << 65010.0 << 200. << "200.00" << 157.81;
  176. //---------------------------------------------------------------------------
  177. // Coefficient
  178. QTest::newRow("Coeff only")
  179. << 5.0 << 0.0 << 5.0 << 1.0 << "1.00" << 5.0;
  180. QTest::newRow("Coeff only: less than min")
  181. << 5.0 << 0.0 << -1010.0 << -200. << "-200.00" << -1000.;
  182. QTest::newRow("Coeff only: more than max")
  183. << 5.0 << 0.0 << 65010.0 << 200.0 << "200.00" << 1000.0;
  184. //---------------------------------------------------------------------------
  185. // Linear
  186. QTest::newRow("Linear") << 5.0 << 12.0 << 42.0 << 6.0 << "6.00" << 42.0;
  187. QTest::newRow("Linear: less than min")
  188. << 5.0 << 12.0 << -5010.0 << -200. << "-200.00" << -988.;
  189. QTest::newRow("Linear: more than max")
  190. << 5.0 << 12.0 << 65010.0 << 200.00 << "200.00" << 1012.;
  191. }
  192. //-----------------------------------------------------------------------------
  193. void ctkDoubleSpinBoxValueProxyTester::testSetCoefficient()
  194. {
  195. ctkDoubleSpinBox spinBox;
  196. spinBox.setRange(-10000., 10000.);
  197. spinBox.setValue(10.12);
  198. ctkLinearValueProxy proxy;
  199. proxy.setCoefficient(10.);
  200. spinBox.setValueProxy(&proxy);
  201. QCOMPARE(spinBox.value(), 10.12);
  202. QCOMPARE(spinBox.displayedValue(), 101.2);
  203. QFETCH(double, newCoefficient);
  204. proxy.setCoefficient(newCoefficient);
  205. QFETCH(double, expectedValue);
  206. QFETCH(double, expectedDisplayedValue);
  207. QCOMPARE(spinBox.value(), expectedValue);
  208. QCOMPARE(spinBox.displayedValue(), expectedDisplayedValue);
  209. }
  210. //-----------------------------------------------------------------------------
  211. void ctkDoubleSpinBoxValueProxyTester::testSetCoefficient_data()
  212. {
  213. QTest::addColumn<double>("newCoefficient");
  214. QTest::addColumn<double>("expectedValue");
  215. QTest::addColumn<double>("expectedDisplayedValue");
  216. QTest::newRow("100") << 100.0 << 10.12 << 1012.;
  217. QTest::newRow("10") << 10.0 << 10.12 << 101.2;
  218. QTest::newRow("1") << 1.0 << 10.12 << 10.12;
  219. QTest::newRow("0.10") << 0.1 << 10.12 << 1.01;
  220. QTest::newRow("-10") << -10.0 << 10.12 << -101.2;
  221. }
  222. // ----------------------------------------------------------------------------
  223. CTK_TEST_MAIN(ctkDoubleSpinBoxValueProxyTest)
  224. #include "moc_ctkDoubleSpinBoxValueProxyTest.cpp"