ctkSliderWidgetValueProxyTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "ctkDoubleSlider.h"
  24. #include "ctkDoubleSpinBox.h"
  25. #include "ctkLinearValueProxy.h"
  26. #include "ctkSliderWidget.h"
  27. #include "ctkTest.h"
  28. #include "ctkValueProxy.h"
  29. namespace
  30. {
  31. //-----------------------------------------------------------------------------
  32. void getSpyReport(QSignalSpy& spy, double expectedValue)
  33. {
  34. QCOMPARE(spy.count(), 1);
  35. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  36. QCOMPARE(arguments.at(0).toDouble(), expectedValue);
  37. }
  38. } // end namespace
  39. // ----------------------------------------------------------------------------
  40. class ctkSliderWidgetValueProxyTester: public QObject
  41. {
  42. Q_OBJECT
  43. private slots:
  44. void testSetValue();
  45. void testSetValue_data();
  46. void testSetCoefficient();
  47. void testSetCoefficient_data();
  48. void testDecimalsOption();
  49. void testDecimalsOption_data();
  50. };
  51. //-----------------------------------------------------------------------------
  52. void ctkSliderWidgetValueProxyTester::testSetValue()
  53. {
  54. // Setup
  55. ctkSliderWidget slider;
  56. slider.setMinimum(-200);
  57. slider.setMaximum(200);
  58. slider.setValue(-32.6);
  59. QFETCH(double, coefficient);
  60. QFETCH(double, offset);
  61. ctkLinearValueProxy proxy;
  62. proxy.setCoefficient(coefficient);
  63. proxy.setOffset(offset);
  64. slider.setValueProxy(&proxy);
  65. // Spy
  66. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  67. // Test
  68. QFETCH(double, value);
  69. slider.setValue(value);
  70. QFETCH(double, expectedValue);
  71. getSpyReport(valueSpy, expectedValue);
  72. QCOMPARE(slider.value(), expectedValue);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkSliderWidgetValueProxyTester::testSetValue_data()
  76. {
  77. QTest::addColumn<double>("coefficient");
  78. QTest::addColumn<double>("offset");
  79. QTest::addColumn<double>("value");
  80. QTest::addColumn<double>("expectedValue");
  81. //---------------------------------------------------------------------------
  82. // Offset
  83. QTest::newRow("Offset only") << 1.0 << 42.19 << 0.1 << 0.1;
  84. QTest::newRow("Offset only: keep precision") << 1.0 << 42.19176 << 0.1 << 0.1;
  85. QTest::newRow("Offset only: less than min")
  86. << 1.0 << 42.19 << -510.0 << -200.;
  87. QTest::newRow("Offset only: less than min but ok with offset")
  88. << 1.0 << 42.19 << -230.0 << -200.;
  89. QTest::newRow("Offset only: less than min with offset")
  90. << 1.0 << -42.19 << -190.0 << -190.0;
  91. QTest::newRow("Offset only: more than max with offset")
  92. << 1.0 << 42.19 << 160.0 << 160.;
  93. QTest::newRow("Offset only: more than max")
  94. << 1.0 << -42.19 << 65010.0 << 200.;
  95. QTest::newRow("Offset only: less than max but ok with offset")
  96. << 1.0 << -42.19 << 229.1 << 200.;
  97. // coeff // offset // value // expectedValue
  98. //---------------------------------------------------------------------------
  99. // Coefficient
  100. QTest::newRow("Coeff only") << 5. << 0. << 0.1 << 0.1;
  101. QTest::newRow("Coeff only: less than min")
  102. << 5. << 0. << -510. << -200.;
  103. QTest::newRow("Coeff only: less than min but ok with coeff")
  104. << 0.5 << 0. << -230. << -200.;
  105. QTest::newRow("Coeff only: less than min with coeff")
  106. << 5. << 0. << -190. << -190.;
  107. QTest::newRow("Coeff only: more than max with coeff")
  108. << 5. << 0. << 160. << 160.;
  109. QTest::newRow("Coeff only: more than max")
  110. << 5. << 0. << 65010.0 << 200.;
  111. QTest::newRow("Offset only: less than max but ok with coeff")
  112. << 0.5 << 0. << 229.2 << 200.;
  113. // coeff // offset // value // expectedValue
  114. //---------------------------------------------------------------------------
  115. // Linear
  116. QTest::newRow("Linear") << 5. << 0. << 0.1 << 0.1;
  117. QTest::newRow("Linear: less than min")
  118. << 5. << 12. << -510.0 << -200.;
  119. QTest::newRow("Linear: less than min but ok with function")
  120. << 0.5 << 12. << -230. << -200.;
  121. QTest::newRow("Linear: less than min with function")
  122. << 5. << 12. << -61.5 << -61.5;
  123. QTest::newRow("Linear: more than max with function")
  124. << 5. << 12. << 160. << 160.;
  125. QTest::newRow("Linear: more than max")
  126. << 5. << 12. << 65010. << 200.;
  127. QTest::newRow("Linear: less than max but ok with function")
  128. << 0.5 << 12. << 229.2 << 200.;
  129. }
  130. //-----------------------------------------------------------------------------
  131. void ctkSliderWidgetValueProxyTester::testSetCoefficient()
  132. {
  133. ctkSliderWidget sliderWidget;
  134. sliderWidget.setRange(-10000., 10000.);
  135. sliderWidget.setValue(10.);
  136. ctkLinearValueProxy proxy;
  137. proxy.setCoefficient(10.);
  138. sliderWidget.setValueProxy(&proxy);
  139. QCOMPARE(sliderWidget.value(), 10.);
  140. QCOMPARE(sliderWidget.spinBox()->displayedValue(), 100.);
  141. QFETCH(double, newCoefficient);
  142. proxy.setCoefficient(newCoefficient);
  143. QFETCH(double, expectedDisplayedValue);
  144. QCOMPARE(sliderWidget.value(), 10.);
  145. QCOMPARE(sliderWidget.spinBox()->displayedValue(),
  146. expectedDisplayedValue);
  147. }
  148. //-----------------------------------------------------------------------------
  149. void ctkSliderWidgetValueProxyTester::testSetCoefficient_data()
  150. {
  151. QTest::addColumn<double>("newCoefficient");
  152. QTest::addColumn<double>("expectedDisplayedValue");
  153. QTest::newRow("100") << 100.0 << 1000.;
  154. QTest::newRow("10") << 10.0 << 100.;
  155. QTest::newRow("1") << 1.0 << 10.;
  156. QTest::newRow("0.10") << 0.1 << 1.;
  157. QTest::newRow("-10") << -10.0 << -100.;
  158. }
  159. //-----------------------------------------------------------------------------
  160. void ctkSliderWidgetValueProxyTester::testDecimalsOption()
  161. {
  162. ctkSliderWidget sliderWidget;
  163. sliderWidget.setRange(-10000., 10000.);
  164. sliderWidget.setDecimals(3);
  165. sliderWidget.setValue(6.924);
  166. QFETCH(int, decimalsOption);
  167. sliderWidget.spinBox()->setDecimalsOption(
  168. static_cast<ctkDoubleSpinBox::DecimalsOptions>(decimalsOption));
  169. ctkLinearValueProxy proxy;
  170. sliderWidget.setValueProxy(&proxy);
  171. QFETCH(double, coefficient);
  172. proxy.setCoefficient(coefficient);
  173. // test if it does not trigger asserts
  174. sliderWidget.setRange(-10000., 10000.);
  175. if (decimalsOption & ctkDoubleSpinBox::DecimalsByValue)
  176. {
  177. QCOMPARE(sliderWidget.spinBox()->value(), 6.924);
  178. }
  179. proxy.setCoefficient(1.);
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkSliderWidgetValueProxyTester::testDecimalsOption_data()
  183. {
  184. QTest::addColumn<int>("decimalsOption");
  185. QTest::addColumn<double>("coefficient");
  186. for (double coef = 1.; coef < 10000000.; coef *= 10.)
  187. {
  188. QTest::newRow("coef by value") << static_cast<int>(ctkDoubleSpinBox::DecimalsByValue) << coef;
  189. QTest::newRow("coef by value") << static_cast<int>(ctkDoubleSpinBox::FixedDecimals) << coef;
  190. QTest::newRow("1./coef by value") << static_cast<int>(ctkDoubleSpinBox::DecimalsByValue) << 1. / coef;
  191. QTest::newRow("1./coef") << static_cast<int>(ctkDoubleSpinBox::FixedDecimals) << 1./ coef;
  192. }
  193. }
  194. // ----------------------------------------------------------------------------
  195. CTK_TEST_MAIN(ctkSliderWidgetValueProxyTest)
  196. #include "moc_ctkSliderWidgetValueProxyTest.cpp"