ctkSliderWidgetValueProxyTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. ctkTest::COMPARE(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. };
  49. //-----------------------------------------------------------------------------
  50. void ctkSliderWidgetValueProxyTester::testSetValue()
  51. {
  52. // Setup
  53. ctkSliderWidget slider;
  54. slider.setMinimum(-200);
  55. slider.setMaximum(200);
  56. slider.setValue(-32.6);
  57. QFETCH(double, coefficient);
  58. QFETCH(double, offset);
  59. ctkLinearValueProxy proxy;
  60. proxy.setCoefficient(coefficient);
  61. proxy.setOffset(offset);
  62. slider.setValueProxy(&proxy);
  63. // Spy
  64. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  65. // Test
  66. QFETCH(double, value);
  67. slider.setValue(value);
  68. QFETCH(double, expectedValue);
  69. getSpyReport(valueSpy, expectedValue);
  70. ctkTest::COMPARE(slider.value(), expectedValue);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void ctkSliderWidgetValueProxyTester::testSetValue_data()
  74. {
  75. QTest::addColumn<double>("coefficient");
  76. QTest::addColumn<double>("offset");
  77. QTest::addColumn<double>("value");
  78. QTest::addColumn<double>("expectedValue");
  79. //---------------------------------------------------------------------------
  80. // Offset
  81. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1;
  82. QTest::newRow("Offset only: less than min")
  83. << 1.0 << 42.19 << -510.0 << -242.19;
  84. QTest::newRow("Offset only: less than min but ok with offset")
  85. << 1.0 << 42.19 << -230.0 << -230.0;
  86. QTest::newRow("Offset only: less than min with offset")
  87. << 1.0 << -42.19 << -190.0 << -157.81;
  88. QTest::newRow("Offset only: more than max with offset")
  89. << 1.0 << 42.19 << 160.0 << 157.81;
  90. QTest::newRow("Offset only: more than max")
  91. << 1.0 << -42.19 << 65010.0 << 242.19;
  92. QTest::newRow("Offset only: less than max but ok with offset")
  93. << 1.0 << -42.19 << 229.1 << 229.1;
  94. QTest::newRow("Offset only: max")
  95. << 1.0 << 42.19 << std::numeric_limits<double>::max() << 157.81;
  96. QTest::newRow("Offset only: min")
  97. << 1.0 << 42.19 << -std::numeric_limits<double>::max() << -242.19;
  98. QTest::newRow("Offset only: infinity")
  99. << 1.0 << 42.19 << std::numeric_limits<double>::infinity() << 157.81;
  100. QTest::newRow("Offset only: - infinity")
  101. << 1.0 << 42.19 << -std::numeric_limits<double>::infinity() << -242.19;
  102. QTest::newRow("Offset only: Nan")
  103. << 1.0 << 42.19 << std::numeric_limits<double>::quiet_NaN() << 157.81;
  104. // coeff // offset // value // expectedValue
  105. //---------------------------------------------------------------------------
  106. // Coefficient
  107. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1;
  108. QTest::newRow("Coeff only: less than min")
  109. << 5.0 << 0.0 << -510.0 << -40.0;
  110. QTest::newRow("Coeff only: less than min but ok with coeff")
  111. << 0.5 << 0.0 << -230.0 << -230.0;
  112. QTest::newRow("Coeff only: less than min with coeff")
  113. << 5.0 << 0.0 << -190.0 << -40.0;
  114. QTest::newRow("Coeff only: more than max with coeff")
  115. << 5.0 << 0.0 << 160.0 << 40.0;
  116. QTest::newRow("Coeff only: more than max")
  117. << 5.0 << 0.0 << 65010.0 << 40.0;
  118. QTest::newRow("Offset only: less than max but ok with coeff")
  119. << 0.5 << 0.0 << 229.2 << 229.2;
  120. QTest::newRow("Coeff only: max")
  121. << 5.0 << 0.0 << std::numeric_limits<double>::max() << 40.0;
  122. QTest::newRow("Coeff only: min")
  123. << 5.0 << 0.0 << -std::numeric_limits<double>::max() << -40.0;
  124. QTest::newRow("Coeff only: infinity")
  125. << 5.0 << 0.0 << std::numeric_limits<double>::infinity() << 40.0;
  126. QTest::newRow("Coeff only: - infinity")
  127. << 5.0 << 0.0 << -std::numeric_limits<double>::infinity() << -40.0;
  128. QTest::newRow("Coeff only: Nan")
  129. << 5.0 << 0.0 << std::numeric_limits<double>::quiet_NaN() << 40.0;
  130. // coeff // offset // value // expectedValue
  131. //---------------------------------------------------------------------------
  132. // Linear
  133. QTest::newRow("Linear") << 5.0 << 0.0 << 0.1 << 0.1;
  134. QTest::newRow("Linear: less than min")
  135. << 5.0 << 12.0 << -510.0 << -42.4;
  136. QTest::newRow("Linear: less than min but ok with function")
  137. << 0.5 << 12.0 << -230.0 << -230.0;
  138. QTest::newRow("Linear: less than min with function")
  139. << 5.0 << 12.0 << -61.5 << -42.4;
  140. QTest::newRow("Linear: more than max with function")
  141. << 5.0 << 12.0 << 160.0 << 37.6;
  142. QTest::newRow("Linear: more than max")
  143. << 5.0 << 12.0 << 65010.0 << 37.6;
  144. QTest::newRow("Offset only: less than max but ok with function")
  145. << 0.5 << 12.0 << 229.2 << 229.2;
  146. QTest::newRow("Linear: max")
  147. << 5.0 << 12.0 << std::numeric_limits<double>::max() << 37.6;
  148. QTest::newRow("Linear: min")
  149. << 5.0 << 12.0 << -std::numeric_limits<double>::max() << -42.4;
  150. QTest::newRow("Linear: infinity")
  151. << 5.0 << 12.0 << std::numeric_limits<double>::infinity() << 37.6;
  152. QTest::newRow("Linear: - infinity")
  153. << 5.0 << 12.0 << -std::numeric_limits<double>::infinity() << -42.4;
  154. QTest::newRow("Linear: Nan")
  155. << 5.0 << 12.0 << std::numeric_limits<double>::quiet_NaN() << 37.6;
  156. }
  157. //-----------------------------------------------------------------------------
  158. void ctkSliderWidgetValueProxyTester::testSetCoefficient()
  159. {
  160. ctkSliderWidget sliderWidget;
  161. sliderWidget.setRange(-10000., 10000.);
  162. sliderWidget.setValue(10.);
  163. ctkLinearValueProxy proxy;
  164. proxy.setCoefficient(10.);
  165. sliderWidget.setValueProxy(&proxy);
  166. QCOMPARE(sliderWidget.value(), 10.);
  167. QCOMPARE(sliderWidget.spinBox()->displayedValue(), 100.);
  168. QFETCH(double, newCoefficient);
  169. proxy.setCoefficient(newCoefficient);
  170. QFETCH(double, expectedDisplayedValue);
  171. QCOMPARE(sliderWidget.value(), 10.);
  172. QCOMPARE(sliderWidget.spinBox()->displayedValue(),
  173. expectedDisplayedValue);
  174. }
  175. //-----------------------------------------------------------------------------
  176. void ctkSliderWidgetValueProxyTester::testSetCoefficient_data()
  177. {
  178. QTest::addColumn<double>("newCoefficient");
  179. QTest::addColumn<double>("expectedDisplayedValue");
  180. QTest::newRow("100") << 100.0 << 1000.;
  181. QTest::newRow("10") << 10.0 << 100.;
  182. QTest::newRow("1") << 1.0 << 10.;
  183. QTest::newRow("0.10") << 0.1 << 1.;
  184. QTest::newRow("-10") << -10.0 << -100.;
  185. }
  186. // ----------------------------------------------------------------------------
  187. CTK_TEST_MAIN(ctkSliderWidgetValueProxyTest)
  188. #include "moc_ctkSliderWidgetValueProxyTest.cpp"