1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.commontk.org/LICENSE
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 // Qt includes
22 #include <QApplication>
23 #include <QTest>
24 
25 // CTK includes
26 #include "ctkDoubleSlider.h"
27 #include "ctkTest.h"
28 
29 //-----------------------------------------------------------------------------
30 class ctkDoubleSliderTester: public QObject
31 {
32   Q_OBJECT
33 private slots:
34   void testUI();
35 
36   void testRange();
37   void testRange_data();
38 
39   void testSingleStep();
40   void testSingleStep_data();
41 };
42 
43 // ----------------------------------------------------------------------------
testUI()44 void ctkDoubleSliderTester::testUI()
45 {
46   ctkDoubleSlider slider;
47   slider.show();
48 #if (QT_VERSION >= 0x50000)
49   bool result = QTest::qWaitForWindowActive(&slider);
50   Q_UNUSED(result);
51 #else
52   QTest::qWaitForWindowShown(&slider);
53 #endif
54   // qApp->exec();
55 }
56 
57 // ----------------------------------------------------------------------------
testRange()58 void ctkDoubleSliderTester::testRange()
59 {
60   ctkDoubleSlider slider;
61 
62   QFETCH(double, minimum);
63   QFETCH(double, maximum);
64   slider.setRange(minimum, maximum);
65 
66   QFETCH(double, expectedMinimum);
67   QFETCH(double, expectedMaximum);
68   QFETCH(double, expectedValue);
69 
70   QCOMPARE(slider.minimum(), expectedMinimum);
71   QCOMPARE(slider.maximum(), expectedMaximum);
72   QCOMPARE(slider.value(), expectedValue);
73 }
74 
75 // ----------------------------------------------------------------------------
testRange_data()76 void ctkDoubleSliderTester::testRange_data()
77 {
78   QTest::addColumn<double>("minimum");
79   QTest::addColumn<double>("maximum");
80   QTest::addColumn<double>("expectedMinimum");
81   QTest::addColumn<double>("expectedMaximum");
82   QTest::addColumn<double>("expectedValue");
83 
84   QTest::newRow("[20.123,20.1234]") << 20.123 << 20.1234 << 20.123 << 20.1234 << 20.123;
85 }
86 
87 // ----------------------------------------------------------------------------
testSingleStep()88 void ctkDoubleSliderTester::testSingleStep()
89 {
90   ctkDoubleSlider slider;
91   slider.setValue(50.);
92 
93   QFETCH(double, minimum);
94   QFETCH(double, maximum);
95   slider.setRange(minimum, maximum);
96 
97   QFETCH(double, singleStep);
98   slider.setSingleStep(singleStep);
99 
100   QFETCH(double, expectedValue);
101   QCOMPARE(slider.value(), expectedValue);
102 }
103 
104 // ----------------------------------------------------------------------------
testSingleStep_data()105 void ctkDoubleSliderTester::testSingleStep_data()
106 {
107   QTest::addColumn<double>("minimum");
108   QTest::addColumn<double>("maximum");
109   QTest::addColumn<double>("singleStep");
110   QTest::addColumn<double>("expectedValue");
111 
112   QTest::newRow("1.") << 0. << 100. << 1. << 50.;
113   QTest::newRow("100.") << 0. << 100. << 100. << 50.;
114   QTest::newRow("0.1") << 0. << 100. << 0.1 << 50.;
115   QTest::newRow("min") << 0. << 100. << std::numeric_limits<double>::min() << 50.;
116   QTest::newRow("max") << 0. << 100. << std::numeric_limits<double>::max() << 50.;
117   QTest::newRow("-max") << 0. << 100. << -std::numeric_limits<double>::max() << 50.;
118   QTest::newRow("-inf") << 0. << 100. << -std::numeric_limits<double>::infinity() << 50.;
119   QTest::newRow("inf") << 0. << 100. << std::numeric_limits<double>::infinity() << 50.;
120   QTest::newRow("NaN") << 0. << 100. << std::numeric_limits<double>::quiet_NaN() << 50.;
121 }
122 
123 // ----------------------------------------------------------------------------
124 CTK_TEST_MAIN(ctkDoubleSliderTest)
125 #include "moc_ctkDoubleSliderTest.cpp"
126