ctkDoubleSliderValueProxyTest.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.commontk.org/LICENSE
  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 <QTest>
  17. // CTK includes
  18. #include "ctkDoubleSlider.h"
  19. #include "ctkLinearValueProxy.h"
  20. #include "ctkTest.h"
  21. #include "ctkValueProxy.h"
  22. // STD includes
  23. #include <limits>
  24. namespace
  25. {
  26. //-----------------------------------------------------------------------------
  27. void getSpyReport(QSignalSpy& spy, double expectedValue)
  28. {
  29. QCOMPARE(spy.count(), 1);
  30. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  31. ctkTest::COMPARE(arguments.at(0).toDouble(), expectedValue);
  32. }
  33. } // end namespace
  34. //-----------------------------------------------------------------------------
  35. class ctkDoubleSliderValueProxyTester: public QObject
  36. {
  37. Q_OBJECT
  38. private slots:
  39. void testSetValueProxy();
  40. void testSetValue();
  41. void testSetValue_data();
  42. void testSetSliderPosition();
  43. void testSetSliderPosition_data();
  44. };
  45. //-----------------------------------------------------------------------------
  46. void ctkDoubleSliderValueProxyTester::testSetValueProxy()
  47. {
  48. ctkDoubleSlider slider;
  49. slider.setRange(-200., 200.);
  50. slider.setValue(-32.6);
  51. ctkLinearValueProxy proxy;
  52. proxy.setCoefficient(-1.);
  53. proxy.setOffset(20.);
  54. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  55. QSignalSpy rangeSpy(&slider, SIGNAL(rangeChanged(double,double)));
  56. slider.setValueProxy(&proxy);
  57. QCOMPARE(valueSpy.count(), 0);
  58. QCOMPARE(rangeSpy.count(), 0);
  59. }
  60. //-----------------------------------------------------------------------------
  61. void ctkDoubleSliderValueProxyTester::testSetValue()
  62. {
  63. ctkDoubleSlider slider;
  64. slider.setRange(-200., 200.);
  65. slider.setSingleStep(0.01);
  66. slider.setValue(-32.6);
  67. QFETCH(double, coefficient);
  68. QFETCH(double, offset);
  69. ctkLinearValueProxy proxy;
  70. proxy.setCoefficient(coefficient);
  71. proxy.setOffset(offset);
  72. slider.setValueProxy(&proxy);
  73. // Spy
  74. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  75. // Test
  76. QFETCH(double, value);
  77. slider.setValue(value);
  78. QFETCH(double, expectedValue);
  79. getSpyReport(valueSpy, expectedValue);
  80. ctkTest::COMPARE(slider.value(), expectedValue);
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ctkDoubleSliderValueProxyTester::testSetValue_data()
  84. {
  85. QTest::addColumn<double>("coefficient");
  86. QTest::addColumn<double>("offset");
  87. QTest::addColumn<double>("value");
  88. QTest::addColumn<double>("expectedValue");
  89. //---------------------------------------------------------------------------
  90. // Offset
  91. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1;
  92. QTest::newRow("Offset only: less than min")
  93. << 1. << 42.19 << -510. << -200.;
  94. QTest::newRow("Offset only: less than min but ok with offset")
  95. << 1. << 42.19 << -230. << -200.;
  96. QTest::newRow("Offset only: less than min with offset")
  97. << 1. << -42.19 << -190. << -190.;
  98. QTest::newRow("Offset only: more than max with offset")
  99. << 1. << 42.19 << 160. << 160.;
  100. QTest::newRow("Offset only: more than max")
  101. << 1. << -42.19 << 65010.0 << 200.;
  102. QTest::newRow("Offset only: less than max but ok with offset")
  103. << 1. << -42.19 << 229.1 << 200.;
  104. // coeff // offset // value // expectedValue
  105. //---------------------------------------------------------------------------
  106. // Coefficient
  107. QTest::newRow("Coeff only") << 5. << 0. << 0.1 << 0.1;
  108. QTest::newRow("Coeff only: less than min")
  109. << 5. << 0. << -510. << -200.;
  110. QTest::newRow("Coeff only: less than min but ok with coeff")
  111. << 0.5 << 0. << -230. << -200.;
  112. QTest::newRow("Coeff only: less than min with coeff")
  113. << 5. << 0. << -190. << -190.;
  114. QTest::newRow("Coeff only: more than max with coeff")
  115. << 5. << 0. << 160. << 160.;
  116. QTest::newRow("Coeff only: more than max")
  117. << 5. << 0. << 65010. << 200.;
  118. QTest::newRow("Offset only: less than max but ok with coeff")
  119. << 0.5 << 0. << 229.2 << 200.;
  120. // coeff // offset // value // expectedValue
  121. //---------------------------------------------------------------------------
  122. // Linear
  123. QTest::newRow("Linear") << 5. << 0. << 0.1 << 0.1;
  124. QTest::newRow("Linear: less than min")
  125. << 5.0 << 12.0 << -510. << -200.;
  126. QTest::newRow("Linear: less than min but ok with function")
  127. << 0.5 << 12.0 << -230. << -200.;
  128. QTest::newRow("Linear: less than min with function")
  129. << 5.0 << 12.0 << -61.5 << -61.5;
  130. QTest::newRow("Linear: more than max with function")
  131. << 5.0 << 12.0 << 160. << 160.;
  132. QTest::newRow("Linear: more than max")
  133. << 5.0 << 12.0 << 65010. << 200.;
  134. QTest::newRow("Offset only: less than max but ok with function")
  135. << 0.5 << 12.0 << 229.2 << 200.;
  136. }
  137. //-----------------------------------------------------------------------------
  138. void ctkDoubleSliderValueProxyTester::testSetSliderPosition()
  139. {
  140. // Setup
  141. ctkDoubleSlider slider;
  142. slider.setRange(-200., 200.);
  143. slider.setSingleStep(0.01);
  144. slider.setValue(-32.6);
  145. QFETCH(double, coefficient);
  146. QFETCH(double, offset);
  147. ctkLinearValueProxy proxy;
  148. proxy.setCoefficient(coefficient);
  149. proxy.setOffset(offset);
  150. slider.setValueProxy(&proxy);
  151. // Spy
  152. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  153. // Test
  154. QFETCH(double, sliderPosition);
  155. slider.setSliderPosition(sliderPosition);
  156. QFETCH(double, expectedSliderPosition);
  157. QFETCH(double, expectedValue);
  158. getSpyReport(valueSpy, expectedValue);
  159. ctkTest::COMPARE(slider.value(), expectedValue);
  160. ctkTest::COMPARE(slider.sliderPosition(), expectedSliderPosition);
  161. }
  162. //-----------------------------------------------------------------------------
  163. void ctkDoubleSliderValueProxyTester::testSetSliderPosition_data()
  164. {
  165. QTest::addColumn<double>("coefficient");
  166. QTest::addColumn<double>("offset");
  167. QTest::addColumn<double>("sliderPosition");
  168. QTest::addColumn<double>("expectedValue");
  169. QTest::addColumn<double>("expectedSliderPosition");
  170. //---------------------------------------------------------------------------
  171. // Offset
  172. QTest::newRow("Offset only") << 1. << 42.19 << 0.1 << 0.1 << 0.1;
  173. QTest::newRow("Offset only: less than min")
  174. << 1. << 42.19 << -510. << -200. << -200.;
  175. QTest::newRow("Offset only: more than max")
  176. << 1. << -42.19 << 65010. << 200. << 200.;
  177. //---------------------------------------------------------------------------
  178. // Coefficient
  179. QTest::newRow("Coeff only") << 5. << 0. << 5. << 5. << 5.;
  180. QTest::newRow("Coeff only: less than min")
  181. << 5. << 0. << -1010. << -200. << -200.;
  182. QTest::newRow("Coeff only: more than max")
  183. << 5. << 0. << 65010. << 200. << 200.;
  184. //---------------------------------------------------------------------------
  185. // Linear
  186. QTest::newRow("Linear") << 5. << 12. << 42. << 42. << 42.;
  187. QTest::newRow("Linear: less than min")
  188. << 5. << 12. << -5010. << -200. << -200.;
  189. QTest::newRow("Linear: more than max")
  190. << 5. << 12. << 65010. << 200. << 200.;
  191. }
  192. // ----------------------------------------------------------------------------
  193. CTK_TEST_MAIN(ctkDoubleSliderValueProxyTest)
  194. #include "moc_ctkDoubleSliderValueProxyTest.cpp"