ctkRangeWidgetTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // CTK includes
  17. #include "ctkRangeWidget.h"
  18. #include "ctkTest.h"
  19. // STD includes
  20. #include <limits>
  21. // ----------------------------------------------------------------------------
  22. class ctkRangeWidgetTester: public QObject
  23. {
  24. Q_OBJECT
  25. private slots:
  26. void testUI();
  27. void testSetMinimumValue();
  28. void testSetMinimumValue_data();
  29. void testSetMaximumValue();
  30. void testSetMaximumValue_data();
  31. void testSetValues();
  32. void testSetValues_data();
  33. void testSetRange();
  34. void testSetRange_data();
  35. void testSetRangeSetValues();
  36. void testSetRangeSetValues_data();
  37. };
  38. // ----------------------------------------------------------------------------
  39. void ctkRangeWidgetTester::testUI()
  40. {
  41. ctkRangeWidget rangeWidget;
  42. rangeWidget.show();
  43. #if (QT_VERSION >= 0x50000)
  44. QTest::qWaitForWindowActive(&rangeWidget);
  45. #else
  46. QTest::qWaitForWindowShown(&rangeWidget);
  47. #endif
  48. //qApp->exec();
  49. }
  50. //-----------------------------------------------------------------------------
  51. void ctkRangeWidgetTester::testSetMinimumValue()
  52. {
  53. ctkRangeWidget rangeWidget;
  54. rangeWidget.setValues(25., 75.);
  55. QSignalSpy lowerValueChangedSpy(&rangeWidget,
  56. SIGNAL(minimumValueChanged(double)));
  57. QSignalSpy valuesChangedSpy(&rangeWidget,
  58. SIGNAL(valuesChanged(double,double)));
  59. QFETCH(double, lowerValue);
  60. rangeWidget.setMinimumValue(lowerValue);
  61. QFETCH(double, expectedLowerValue);
  62. QCOMPARE(rangeWidget.minimumValue(), expectedLowerValue);
  63. const bool lowerValueChanged = expectedLowerValue != 25.;
  64. QCOMPARE(lowerValueChangedSpy.count(), lowerValueChanged ? 1 : 0);
  65. QCOMPARE(valuesChangedSpy.count(), lowerValueChanged ? 1 : 0);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ctkRangeWidgetTester::testSetMinimumValue_data()
  69. {
  70. QTest::addColumn<double>("lowerValue");
  71. QTest::addColumn<double>("expectedLowerValue");
  72. QTest::newRow("1. -> 1.]") << 1. << 1.;
  73. QTest::newRow("100. -> 75.]") << 100. << 75.;
  74. QTest::newRow("-1. -> 0.]") << -1. << 0.;
  75. QTest::newRow("min -> min") << std::numeric_limits<double>::min()
  76. << std::numeric_limits<double>::min();
  77. QTest::newRow("max -> 75.") << std::numeric_limits<double>::max() << 75.;
  78. QTest::newRow("-inf -> 0.") << -std::numeric_limits<double>::infinity() << 0.;
  79. QTest::newRow("inf -> 75.") << std::numeric_limits<double>::infinity() << 75.;
  80. QTest::newRow("NaN -> 75.") << std::numeric_limits<double>::quiet_NaN() << 0.;
  81. }
  82. //-----------------------------------------------------------------------------
  83. void ctkRangeWidgetTester::testSetMaximumValue()
  84. {
  85. ctkRangeWidget rangeWidget;
  86. rangeWidget.setValues(25., 75.);
  87. QSignalSpy upperValueChangedSpy(&rangeWidget,
  88. SIGNAL(maximumValueChanged(double)));
  89. QSignalSpy valuesChangedSpy(&rangeWidget,
  90. SIGNAL(valuesChanged(double,double)));
  91. QFETCH(double, upperValue);
  92. rangeWidget.setMaximumValue(upperValue);
  93. QFETCH(double, expectedUpperValue);
  94. QCOMPARE(rangeWidget.maximumValue(), expectedUpperValue);
  95. const bool upperValueChanged = expectedUpperValue != 75.;
  96. QCOMPARE(upperValueChangedSpy.count(), upperValueChanged ? 1 : 0);
  97. QCOMPARE(valuesChangedSpy.count(), upperValueChanged ? 1 : 0);
  98. }
  99. //-----------------------------------------------------------------------------
  100. void ctkRangeWidgetTester::testSetMaximumValue_data()
  101. {
  102. QTest::addColumn<double>("upperValue");
  103. QTest::addColumn<double>("expectedUpperValue");
  104. QTest::newRow("99. -> 99.]") << 99. << 99.;
  105. QTest::newRow("0. -> 25.]") << 0. << 25.;
  106. QTest::newRow("100. -> 99.]") << 100. << 99.;
  107. QTest::newRow("min -> 25.") << std::numeric_limits<double>::min() << 25.;
  108. QTest::newRow("max -> 99.") << std::numeric_limits<double>::max() << 99.;
  109. QTest::newRow("-inf -> 25.") << -std::numeric_limits<double>::infinity() << 25.;
  110. QTest::newRow("inf -> 99.") << std::numeric_limits<double>::infinity() << 99.;
  111. QTest::newRow("NaN -> 99.") << std::numeric_limits<double>::quiet_NaN() << 25.;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void ctkRangeWidgetTester::testSetValues()
  115. {
  116. ctkRangeWidget rangeWidget;
  117. rangeWidget.setValues(25., 75.);
  118. QSignalSpy lowerValueChangedSpy(&rangeWidget,
  119. SIGNAL(minimumValueChanged(double)));
  120. QSignalSpy upperValueChangedSpy(&rangeWidget,
  121. SIGNAL(maximumValueChanged(double)));
  122. QSignalSpy valuesChangedSpy(&rangeWidget,
  123. SIGNAL(valuesChanged(double,double)));
  124. QFETCH(double, lowerValue);
  125. QFETCH(double, upperValue);
  126. rangeWidget.setValues(lowerValue, upperValue);
  127. QFETCH(double, expectedLowerValue);
  128. QFETCH(double, expectedUpperValue);
  129. QCOMPARE(rangeWidget.minimumValue(), expectedLowerValue);
  130. QCOMPARE(rangeWidget.maximumValue(), expectedUpperValue);
  131. const bool lowerValueChanged = expectedLowerValue != 25.;
  132. const bool upperValueChanged = expectedUpperValue != 75.;
  133. QCOMPARE(lowerValueChangedSpy.count(), lowerValueChanged ? 1 : 0);
  134. QCOMPARE(upperValueChangedSpy.count(), upperValueChanged ? 1 : 0);
  135. // \todo fix the valuesChanged signal count.
  136. //QCOMPARE(valuesChangedSpy.count(),
  137. // (lowerValueChanged || upperValueChanged) ? 1 : 0);
  138. }
  139. //-----------------------------------------------------------------------------
  140. void ctkRangeWidgetTester::testSetValues_data()
  141. {
  142. QTest::addColumn<double>("lowerValue");
  143. QTest::addColumn<double>("upperValue");
  144. QTest::addColumn<double>("expectedLowerValue");
  145. QTest::addColumn<double>("expectedUpperValue");
  146. QTest::newRow("[1.,10.] -> [1., 10.]") << 1. << 10. << 1. << 10.;
  147. QTest::newRow("[98.,99.] -> [98., 99.]") << 98. << 99. << 98. << 99.;
  148. QTest::newRow("[1.,1.] -> [1., 1.]") << 1. << 1. << 1. << 1.;
  149. QTest::newRow("[10.,1.] -> [1., 10.]") << 10. << 1. << 1. << 10.;
  150. QTest::newRow("[-1.,100.] -> [0., 99.]") << -1. << 100. << 0. << 99.;
  151. QTest::newRow("[min,max] -> [min, 99.]")
  152. << std::numeric_limits<double>::min()
  153. << std::numeric_limits<double>::max()
  154. << std::numeric_limits<double>::min() << 99.;
  155. QTest::newRow("[-inf,inf] -> [0., 99.]")
  156. << -std::numeric_limits<double>::infinity()
  157. << std::numeric_limits<double>::infinity() << 0. << 99.;
  158. QTest::newRow("[NaN,NaN] -> [99., 99.]")
  159. << std::numeric_limits<double>::quiet_NaN()
  160. << std::numeric_limits<double>::quiet_NaN() << 0. << 0.;
  161. }
  162. //-----------------------------------------------------------------------------
  163. void ctkRangeWidgetTester::testSetRange()
  164. {
  165. ctkRangeWidget rangeWidget;
  166. rangeWidget.setValues(25., 75.);
  167. QSignalSpy lowerValueChangedSpy(&rangeWidget,
  168. SIGNAL(minimumValueChanged(double)));
  169. QSignalSpy upperValueChangedSpy(&rangeWidget,
  170. SIGNAL(maximumValueChanged(double)));
  171. QSignalSpy rangeChangedSpy(&rangeWidget,
  172. SIGNAL(rangeChanged(double,double)));
  173. QFETCH(double, minimum);
  174. QFETCH(double, maximum);
  175. rangeWidget.setRange(minimum, maximum);
  176. QFETCH(double, expectedMinimum);
  177. QFETCH(double, expectedMaximum);
  178. ctkTest::COMPARE(rangeWidget.minimum(), expectedMinimum);
  179. ctkTest::COMPARE(rangeWidget.maximum(), expectedMaximum);
  180. const bool minimumChanged = expectedMinimum != 0.;
  181. const bool maximumChanged = expectedMaximum != 99.;
  182. QCOMPARE(rangeChangedSpy.count(),(minimumChanged || maximumChanged) ? 1 : 0);
  183. QFETCH(double, expectedLowerValue);
  184. QFETCH(double, expectedUpperValue);
  185. ctkTest::COMPARE(rangeWidget.minimumValue(), expectedLowerValue);
  186. ctkTest::COMPARE(rangeWidget.maximumValue(), expectedUpperValue);
  187. const bool lowerValueChanged = expectedLowerValue != 25.;
  188. const bool upperValueChanged = expectedUpperValue != 75.;
  189. QCOMPARE(lowerValueChangedSpy.count(), lowerValueChanged ? 1 : 0);
  190. QCOMPARE(upperValueChangedSpy.count(), upperValueChanged ? 1 : 0);
  191. }
  192. //-----------------------------------------------------------------------------
  193. void ctkRangeWidgetTester::testSetRange_data()
  194. {
  195. QTest::addColumn<double>("minimum");
  196. QTest::addColumn<double>("maximum");
  197. QTest::addColumn<double>("expectedMinimum");
  198. QTest::addColumn<double>("expectedMaximum");
  199. QTest::addColumn<double>("expectedLowerValue");
  200. QTest::addColumn<double>("expectedUpperValue");
  201. QTest::newRow("[1.,98.]") << 1. << 98. << 1. << 98. << 25. << 75.;
  202. QTest::newRow("[10.0123,99.99]") << 10.0123 << 99.99 << 10.0123 << 99.99 << 25. << 75.;
  203. QTest::newRow("[0.,2050.9876.]") << 0. << 2050.9876 << 0. << 2050.9876 << 25. << 75.;
  204. QTest::newRow("[-1.,101.]") << -1. << 101. << -1. << 101. << 25. << 75.;
  205. QTest::newRow("[1.,50.]") << 1. << 50. << 1. << 50. << 25. << 50.;
  206. QTest::newRow("[1., 50.5678]") << 1. << 50.5678 << 1. << 50.5678 << 25. << 50.5678 ;
  207. QTest::newRow("[50.,99.]") << 50. << 99. << 50. << 99. << 50. << 75. ;
  208. QTest::newRow("[50.5678,99.]") << 50.5678 << 99. << 50.5678 << 99. << 50.5678 << 75. ;
  209. QTest::newRow("[1.,10.]") << 1. << 10. << 1. << 10. << 10. << 10.;
  210. QTest::newRow("[90.,99.]") << 90. << 99. << 90. << 99. << 90. << 90.;
  211. QTest::newRow("[min,max]")
  212. << std::numeric_limits<double>::min()
  213. << std::numeric_limits<double>::max()
  214. << std::numeric_limits<double>::min()
  215. << std::numeric_limits<double>::max()
  216. << 25. << 75.;
  217. QTest::newRow("[-inf,inf]")
  218. << -std::numeric_limits<double>::infinity()
  219. << std::numeric_limits<double>::infinity()
  220. << -std::numeric_limits<double>::infinity()
  221. << std::numeric_limits<double>::infinity()
  222. << 25. << 75.;
  223. QTest::newRow("[NaN,NaN]")
  224. << std::numeric_limits<double>::quiet_NaN()
  225. << std::numeric_limits<double>::quiet_NaN()
  226. << std::numeric_limits<double>::quiet_NaN()
  227. << std::numeric_limits<double>::quiet_NaN()
  228. << std::numeric_limits<double>::quiet_NaN()
  229. << std::numeric_limits<double>::quiet_NaN();
  230. }
  231. //-----------------------------------------------------------------------------
  232. void ctkRangeWidgetTester::testSetRangeSetValues()
  233. {
  234. ctkRangeWidget rangeWidget;
  235. QFETCH(double, minimum);
  236. QFETCH(double, maximum);
  237. rangeWidget.setRange(minimum, maximum);
  238. rangeWidget.setDecimals(0);
  239. rangeWidget.setSingleStep(std::numeric_limits<double>::infinity());
  240. QFETCH(double, lowerValue);
  241. QFETCH(double, upperValue);
  242. rangeWidget.setValues(lowerValue, upperValue);
  243. }
  244. //-----------------------------------------------------------------------------
  245. void ctkRangeWidgetTester::testSetRangeSetValues_data()
  246. {
  247. QTest::addColumn<double>("minimum");
  248. QTest::addColumn<double>("maximum");
  249. QTest::addColumn<double>("lowerValue");
  250. QTest::addColumn<double>("upperValue");
  251. QTest::newRow("[-inf,inf,-inf,inf]")
  252. << -std::numeric_limits<double>::infinity()
  253. << std::numeric_limits<double>::infinity()
  254. << -std::numeric_limits<double>::infinity()
  255. << std::numeric_limits<double>::infinity();
  256. }
  257. // ----------------------------------------------------------------------------
  258. CTK_TEST_MAIN(ctkRangeWidgetTest)
  259. #include "moc_ctkRangeWidgetTest.cpp"