ctkDoubleRangeSliderValueProxyTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 "ctkDoubleRangeSlider.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. QCOMPARE(arguments.at(0).toDouble(), expectedValue);
  32. }
  33. //-----------------------------------------------------------------------------
  34. class CustomSpy : public QObject
  35. {
  36. Q_OBJECT
  37. public:
  38. CustomSpy()
  39. {
  40. this->AcknowledgedSignals = 0;
  41. }
  42. public slots:
  43. void onValuesChanged(double min, double max)
  44. {
  45. ++this->AcknowledgedSignals;
  46. this->MinSpyData.append(min);
  47. this->MaxSpyData.append(max);
  48. }
  49. public:
  50. void getSpyReport(double min, double max)
  51. {
  52. QCOMPARE(this->AcknowledgedSignals, 1);
  53. QCOMPARE(this->MinSpyData.size(), 1);
  54. ctkTest::COMPARE(this->MinSpyData[0], min);
  55. QCOMPARE(this->MaxSpyData.size(), 1);
  56. ctkTest::COMPARE(this->MaxSpyData[0], max);
  57. }
  58. QList<double> MinSpyData;
  59. QList<double> MaxSpyData;
  60. int AcknowledgedSignals;
  61. };
  62. } // end namespace
  63. //-----------------------------------------------------------------------------
  64. class ctkDoubleRangeSliderValueProxyTester: public QObject
  65. {
  66. Q_OBJECT
  67. private slots:
  68. void testSetValues();
  69. void testSetValues_data();
  70. void testSetMinValue();
  71. void testSetMinValue_data() { testSetValueCommonData(); };
  72. void testSetMaxValue();
  73. void testSetMaxValue_data(){ testSetValueCommonData(); };
  74. void testSetMinPosition();
  75. void testSetMinPosition_data() { testSetPositionCommonData(); };
  76. void testSetMaxPosition();
  77. void testSetMaxPosition_data() { testSetPositionCommonData(); };
  78. private:
  79. void testSetValueCommonData();
  80. void testSetPositionCommonData();
  81. };
  82. //-----------------------------------------------------------------------------
  83. void ctkDoubleRangeSliderValueProxyTester::testSetValues()
  84. {
  85. // Setup
  86. ctkDoubleRangeSlider slider;
  87. slider.setMinimum(-200);
  88. slider.setMaximum(200);
  89. slider.setSingleStep(0.01);
  90. slider.setMinimumValue(-32.6);
  91. slider.setMaximumValue(32.6);
  92. QFETCH(double, coefficient);
  93. QFETCH(double, offset);
  94. ctkLinearValueProxy proxy;
  95. proxy.setCoefficient(coefficient);
  96. proxy.setOffset(offset);
  97. slider.setValueProxy(&proxy);
  98. // Spy
  99. CustomSpy valuesSpy;
  100. QObject::connect(&slider, SIGNAL(valuesChanged(double, double)),
  101. &valuesSpy, SLOT(onValuesChanged(double, double)));
  102. // Test
  103. QFETCH(double, min);
  104. QFETCH(double, max);
  105. slider.setValues(min, max);
  106. QFETCH(double, expectedMin);
  107. QFETCH(double, expectedMax);
  108. valuesSpy.getSpyReport(expectedMin, expectedMax);
  109. QCOMPARE(slider.minimumValue(), expectedMin);
  110. QCOMPARE(slider.maximumValue(), expectedMax);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void ctkDoubleRangeSliderValueProxyTester::testSetValues_data()
  114. {
  115. QTest::addColumn<double>("coefficient");
  116. QTest::addColumn<double>("offset");
  117. QTest::addColumn<double>("min");
  118. QTest::addColumn<double>("expectedMin");
  119. QTest::addColumn<double>("max");
  120. QTest::addColumn<double>("expectedMax");
  121. //---------------------------------------------------------------------------
  122. // Offset
  123. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1 << 0.2 << 0.2;
  124. QTest::newRow("Offset only: max+offset < min+offset < -200")
  125. << 1. << -42.19 << -160. << -190.9 << -190.9 << -160.;
  126. QTest::newRow("Offset only: max+offset < -200 < min+offset")
  127. << 1. << -42.19 << -0.1 << -160.9 << -160.9 << -0.1;
  128. QTest::newRow("Offset only: -200 < max+offset < min+offset")
  129. << 1. << 42.19 << -0.1 << -130.9 << -130.9 << -0.1;
  130. QTest::newRow("Offset only: 200 < max+offset < min+offset")
  131. << 1. << 42.19 << 160. << 160. << 190.9 << 190.9;
  132. QTest::newRow("Offset only: max+offset < 200 < min+offset")
  133. << 1. << 42.19 << 160.9 << -0.9 << -0.9 << 160.9;
  134. QTest::newRow("Offset only: max+offset < min+offset < 200")
  135. << 1. << 42.19 << 130.6 << -13.9 << -13.9 << 130.6;
  136. //---------------------------------------------------------------------------
  137. // Coefficient
  138. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1 << 0.2 << 0.2;
  139. QTest::newRow("Coeff only: max*coeff < min*coeff < -200")
  140. << 5. << 0. << -160. << -190.9 << -190.9 << -160.;
  141. QTest::newRow("Coeff only: max*coeff < -200 < min*coeff")
  142. << 5. << 0. << -0.1 << -160.9 << -160.9 << -0.1;
  143. QTest::newRow("Coeff only: -200 < max*coeff < min*coeff")
  144. << 5. << 0. << -0.1 << -20.9 << -20.9 << -0.1;
  145. QTest::newRow("Coeff only: 200 < max*coeff < min*coeff")
  146. << 5. << 0. << 160. << 160. << 190.9 << 190.9;
  147. QTest::newRow("Coeff only: max*coeff < 200 < min*coeff")
  148. << 5. << 0. << 160.9 << -0.9 << -0.9 << 160.9;
  149. QTest::newRow("Coeff only: max*coeff < min*coeff < 200")
  150. << 5. << 0. << 13.6 << -13.9 << -13.9 << 13.6;
  151. //---------------------------------------------------------------------------
  152. // Linear
  153. QTest::newRow("Linear") << 5.0 << 12.0 << 0.1 << 0.1 << 0.2 << 0.2;
  154. QTest::newRow("Linear:f(max) < f(min) < -200")
  155. << 5. << 12. << -160. << -190.9 << -190.9 << -160.;
  156. QTest::newRow("Linear: f(max) < -200 < f(min)")
  157. << 5. << 12. << -0.1 << -160.9 << -160.9 << -0.1;
  158. QTest::newRow("Linear: -200 < f(max) < f(min)")
  159. << 5. << 12. << -0.1 << -20.9 << -20.9 << -0.1;
  160. QTest::newRow("Linear: 200 < f(max) < f(min)")
  161. << 5. << 12. << 160.0 << 160.0 << 190.9 << 190.9;
  162. QTest::newRow("Linear: f(max) < 200 < f(min)")
  163. << 5. << 12. << 160.9 << -0.9 << -0.9 << 160.9;
  164. QTest::newRow("Linear: f(max) < f(min) < 200")
  165. << 5. << 12. << 13.6 << -13.9 << -13.9 << 13.6;
  166. }
  167. //-----------------------------------------------------------------------------
  168. void ctkDoubleRangeSliderValueProxyTester::testSetMinValue()
  169. {
  170. // Setup
  171. ctkDoubleRangeSlider slider;
  172. slider.setMinimum(-200);
  173. slider.setMaximum(200);
  174. slider.setSingleStep(0.01);
  175. slider.setMinimumValue(-32.6);
  176. QFETCH(double, coefficient);
  177. QFETCH(double, offset);
  178. ctkLinearValueProxy proxy;
  179. proxy.setCoefficient(coefficient);
  180. proxy.setOffset(offset);
  181. slider.setValueProxy(&proxy);
  182. // Spy
  183. QSignalSpy valueSpy(&slider, SIGNAL(minimumValueChanged(double)));
  184. // Test
  185. QFETCH(double, value);
  186. slider.setMinimumValue(value);
  187. QFETCH(double, expectedValue);
  188. getSpyReport(valueSpy, expectedValue);
  189. QCOMPARE(slider.minimumValue(), expectedValue);
  190. }
  191. //-----------------------------------------------------------------------------
  192. void ctkDoubleRangeSliderValueProxyTester::testSetMaxValue()
  193. {
  194. // Setup
  195. ctkDoubleRangeSlider slider;
  196. slider.setMinimum(-200);
  197. slider.setMaximum(200);
  198. slider.setSingleStep(0.01);
  199. slider.setMaximumValue(-32.6);
  200. QFETCH(double, coefficient);
  201. QFETCH(double, offset);
  202. ctkLinearValueProxy proxy;
  203. proxy.setCoefficient(coefficient);
  204. proxy.setOffset(offset);
  205. slider.setValueProxy(&proxy);
  206. // Spy
  207. QSignalSpy valueSpy(&slider, SIGNAL(maximumValueChanged(double)));
  208. // Test
  209. QFETCH(double, value);
  210. slider.setMaximumValue(value);
  211. QFETCH(double, expectedValue);
  212. getSpyReport(valueSpy, expectedValue);
  213. ctkTest::COMPARE(slider.maximumValue(), expectedValue);
  214. }
  215. //-----------------------------------------------------------------------------
  216. void ctkDoubleRangeSliderValueProxyTester::testSetValueCommonData()
  217. {
  218. QTest::addColumn<double>("coefficient");
  219. QTest::addColumn<double>("offset");
  220. QTest::addColumn<double>("value");
  221. QTest::addColumn<double>("expectedValue");
  222. //---------------------------------------------------------------------------
  223. // Offset
  224. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1;
  225. QTest::newRow("Offset only: less than min")
  226. << 1. << 42.19 << -510. << -200.;
  227. QTest::newRow("Offset only: less than min but ok with offset")
  228. << 1. << 42.19 << -230. << -200.;
  229. QTest::newRow("Offset only: less than min with offset")
  230. << 1. << -42.19 << -190. << -190.;
  231. QTest::newRow("Offset only: more than max with offset")
  232. << 1. << 42.19 << 160. << 160.;
  233. QTest::newRow("Offset only: more than max")
  234. << 1. << -42.19 << 65010.0 << 200.;
  235. QTest::newRow("Offset only: less than max but ok with offset")
  236. << 1. << -42.19 << 229.1 << 200.;
  237. //---------------------------------------------------------------------------
  238. // Coefficient
  239. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1;
  240. QTest::newRow("Coeff only: less than min")
  241. << 5. << 0. << -510. << -200.;
  242. QTest::newRow("Coeff only: less than min but ok with coeff")
  243. << 0.5 << 0. << -230. << -200.;
  244. QTest::newRow("Coeff only: less than min with coeff")
  245. << 5.0 << 0. << -190. << -190.;
  246. QTest::newRow("Coeff only: more than max with coeff")
  247. << 5. << 0. << 160. << 160.;
  248. QTest::newRow("Coeff only: more than max")
  249. << 5. << 0. << 65010. << 200.;
  250. QTest::newRow("Offset only: less than max but ok with coeff")
  251. << 0.5 << 0. << 229.2 << 200.;
  252. //---------------------------------------------------------------------------
  253. // Linear
  254. QTest::newRow("Linear") << 5.0 << 0.0 << 0.1 << 0.1;
  255. QTest::newRow("Linear: less than min")
  256. << 5. << 12. << -510.0 << -200.;
  257. QTest::newRow("Linear: less than min but ok with function")
  258. << 0.5 << 12. << -230.0 << -200.;
  259. QTest::newRow("Linear: less than min with function")
  260. << 5. << 12. << -61.5 << -61.5;
  261. QTest::newRow("Linear: more than max with function")
  262. << 5. << 12. << 160. << 160.;
  263. QTest::newRow("Linear: more than max")
  264. << 5. << 12. << 65010. << 200.;
  265. QTest::newRow("Linear: less than max but ok with function")
  266. << 0.5 << 12. << 229.2 << 200.;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void ctkDoubleRangeSliderValueProxyTester::testSetMinPosition()
  270. {
  271. // Setup
  272. ctkDoubleRangeSlider slider;
  273. slider.setMinimum(-200);
  274. slider.setMaximum(200);
  275. slider.setSingleStep(0.01);
  276. slider.setMinimumValue(-32.6);
  277. QFETCH(double, coefficient);
  278. QFETCH(double, offset);
  279. ctkLinearValueProxy proxy;
  280. proxy.setCoefficient(coefficient);
  281. proxy.setOffset(offset);
  282. slider.setValueProxy(&proxy);
  283. // Spy
  284. QSignalSpy valueSpy(&slider, SIGNAL(minimumValueChanged(double)));
  285. // Test
  286. QFETCH(double, sliderPosition);
  287. slider.setMinimumPosition(sliderPosition);
  288. QFETCH(double, expectedSliderPosition);
  289. QFETCH(double, expectedValue);
  290. getSpyReport(valueSpy, expectedValue);
  291. ctkTest::COMPARE(slider.minimumValue(), expectedValue);
  292. ctkTest::COMPARE(slider.minimumPosition(), expectedSliderPosition);
  293. }
  294. //-----------------------------------------------------------------------------
  295. void ctkDoubleRangeSliderValueProxyTester::testSetMaxPosition()
  296. {
  297. // Setup
  298. ctkDoubleRangeSlider slider;
  299. slider.setRange(-200., 200.);
  300. slider.setSingleStep(0.01);
  301. slider.setMaximumValue(-32.6);
  302. QFETCH(double, coefficient);
  303. QFETCH(double, offset);
  304. ctkLinearValueProxy proxy;
  305. proxy.setCoefficient(coefficient);
  306. proxy.setOffset(offset);
  307. slider.setValueProxy(&proxy);
  308. // Spy
  309. QSignalSpy valueSpy(&slider, SIGNAL(maximumValueChanged(double)));
  310. // Test
  311. QFETCH(double, sliderPosition);
  312. slider.setMaximumPosition(sliderPosition);
  313. QFETCH(double, expectedSliderPosition);
  314. QFETCH(double, expectedValue);
  315. getSpyReport(valueSpy, expectedValue);
  316. ctkTest::COMPARE(slider.maximumValue(), expectedValue);
  317. ctkTest::COMPARE(slider.maximumPosition(), expectedSliderPosition);
  318. }
  319. //-----------------------------------------------------------------------------
  320. void ctkDoubleRangeSliderValueProxyTester::testSetPositionCommonData()
  321. {
  322. QTest::addColumn<double>("coefficient");
  323. QTest::addColumn<double>("offset");
  324. QTest::addColumn<double>("sliderPosition");
  325. QTest::addColumn<double>("expectedValue");
  326. QTest::addColumn<double>("expectedSliderPosition");
  327. //---------------------------------------------------------------------------
  328. // Offset
  329. QTest::newRow("Offset only") << 1. << 42.19 << 0.1 << 0.1 << 0.1;
  330. QTest::newRow("Offset only: less than min")
  331. << 1. << 42.19 << -510. << -200. << -200.;
  332. QTest::newRow("Offset only: more than max")
  333. << 1. << -42.19 << 65010. << 200. << 200.;
  334. //---------------------------------------------------------------------------
  335. // Coefficient
  336. QTest::newRow("Coeff only") << 5. << 0. << 5. << 5. << 5.;
  337. QTest::newRow("Coeff only: less than min")
  338. << 5. << 0. << -1010. << -200. << -200.;
  339. QTest::newRow("Coeff only: more than max")
  340. << 5. << 0. << 65010. << 200. << 200.;
  341. //---------------------------------------------------------------------------
  342. // Linear
  343. QTest::newRow("Linear") << 5. << 12. << 42. << 42. << 42.;
  344. QTest::newRow("Linear: less than min")
  345. << 5. << 12. << -5010. << -200. << -200.;
  346. QTest::newRow("Linear: more than max")
  347. << 5. << 12. << 65010. << 200. << 200.;
  348. }
  349. // ----------------------------------------------------------------------------
  350. CTK_TEST_MAIN(ctkDoubleRangeSliderValueProxyTest)
  351. #include "moc_ctkDoubleRangeSliderValueProxyTest.cpp"