1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | |
32 | #include <QDial> |
33 | |
34 | class tst_QDial : public QObject |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | tst_QDial(); |
39 | |
40 | private slots: |
41 | void getSetCheck(); |
42 | void valueChanged(); |
43 | void sliderMoved(); |
44 | void wrappingCheck(); |
45 | }; |
46 | |
47 | // Testing get/set functions |
48 | void tst_QDial::getSetCheck() |
49 | { |
50 | QDial obj1; |
51 | // bool QDial::notchesVisible() |
52 | // void QDial::setNotchesVisible(bool) |
53 | obj1.setNotchesVisible(false); |
54 | QCOMPARE(false, obj1.notchesVisible()); |
55 | obj1.setNotchesVisible(true); |
56 | QCOMPARE(true, obj1.notchesVisible()); |
57 | |
58 | // bool QDial::wrapping() |
59 | // void QDial::setWrapping(bool) |
60 | obj1.setWrapping(false); |
61 | QCOMPARE(false, obj1.wrapping()); |
62 | obj1.setWrapping(true); |
63 | QCOMPARE(true, obj1.wrapping()); |
64 | } |
65 | |
66 | tst_QDial::tst_QDial() |
67 | { |
68 | } |
69 | |
70 | void tst_QDial::valueChanged() |
71 | { |
72 | QDial dial; |
73 | dial.setMinimum(0); |
74 | dial.setMaximum(100); |
75 | QSignalSpy spy(&dial, SIGNAL(valueChanged(int))); |
76 | dial.setValue(50); |
77 | QCOMPARE(spy.count(), 1); |
78 | spy.clear(); |
79 | dial.setValue(25); |
80 | QCOMPARE(spy.count(), 1); |
81 | spy.clear(); |
82 | // repeat! |
83 | dial.setValue(25); |
84 | QCOMPARE(spy.count(), 0); |
85 | } |
86 | |
87 | void tst_QDial::sliderMoved() |
88 | { |
89 | //this tests that when dragging the arrow that the sliderMoved signal is emitted |
90 | //even if tracking is set to false |
91 | QDial dial; |
92 | dial.setTracking(false); |
93 | dial.setMinimum(0); |
94 | dial.setMaximum(100); |
95 | |
96 | dial.show(); |
97 | |
98 | QPoint init(dial.width()/4, dial.height()/2); |
99 | |
100 | QMouseEvent pressevent(QEvent::MouseButtonPress, init, |
101 | Qt::LeftButton, Qt::LeftButton, {}); |
102 | qApp->sendEvent(receiver: &dial, event: &pressevent); |
103 | |
104 | QSignalSpy sliderspy(&dial, SIGNAL(sliderMoved(int))); |
105 | QSignalSpy valuespy(&dial, SIGNAL(valueChanged(int))); |
106 | |
107 | |
108 | { //move on top of the slider |
109 | init = QPoint(dial.width()/2, dial.height()/4); |
110 | QMouseEvent moveevent(QEvent::MouseMove, init, |
111 | Qt::LeftButton, Qt::LeftButton, {}); |
112 | qApp->sendEvent(receiver: &dial, event: &moveevent); |
113 | QCOMPARE( sliderspy.count(), 1); |
114 | QCOMPARE( valuespy.count(), 0); |
115 | } |
116 | |
117 | |
118 | { //move on the right of the slider |
119 | init = QPoint(dial.width()*3/4, dial.height()/2); |
120 | QMouseEvent moveevent(QEvent::MouseMove, init, |
121 | Qt::LeftButton, Qt::LeftButton, {}); |
122 | qApp->sendEvent(receiver: &dial, event: &moveevent); |
123 | QCOMPARE( sliderspy.count(), 2); |
124 | QCOMPARE( valuespy.count(), 0); |
125 | } |
126 | |
127 | QMouseEvent releaseevent(QEvent::MouseButtonRelease, init, |
128 | Qt::LeftButton, Qt::LeftButton, {}); |
129 | qApp->sendEvent(receiver: &dial, event: &releaseevent); |
130 | QCOMPARE( valuespy.count(), 1); // valuechanged signal should be called at this point |
131 | |
132 | } |
133 | |
134 | void tst_QDial::wrappingCheck() |
135 | { |
136 | //This tests if dial will wrap past the maximum value back to the minimum |
137 | //and vice versa when changing the value with a keypress |
138 | QDial dial; |
139 | dial.setMinimum(0); |
140 | dial.setMaximum(100); |
141 | dial.setSingleStep(1); |
142 | dial.setWrapping(true); |
143 | dial.setValue(99); |
144 | dial.show(); |
145 | |
146 | { //set value to maximum but do not wrap |
147 | QTest::keyPress(widget: &dial, key: Qt::Key_Up); |
148 | QCOMPARE( dial.value(), 100); |
149 | } |
150 | |
151 | { //step up once more and wrap clockwise to minimum + 1 |
152 | QTest::keyPress(widget: &dial, key: Qt::Key_Up); |
153 | QCOMPARE( dial.value(), 1); |
154 | } |
155 | |
156 | { //step down once, and wrap anti-clockwise to minimum, then again to maximum - 1 |
157 | QTest::keyPress(widget: &dial, key: Qt::Key_Down); |
158 | QCOMPARE( dial.value(), 0); |
159 | |
160 | QTest::keyPress(widget: &dial, key: Qt::Key_Down); |
161 | QCOMPARE( dial.value(), 99); |
162 | } |
163 | |
164 | { //when wrapping property is false no wrapping will occur |
165 | dial.setWrapping(false); |
166 | dial.setValue(100); |
167 | |
168 | QTest::keyPress(widget: &dial, key: Qt::Key_Up); |
169 | QCOMPARE( dial.value(), 100); |
170 | |
171 | dial.setValue(0); |
172 | QTest::keyPress(widget: &dial, key: Qt::Key_Down); |
173 | QCOMPARE( dial.value(), 0); |
174 | } |
175 | |
176 | { //When the step is really big or small, wrapping should still behave |
177 | dial.setWrapping(true); |
178 | dial.setValue(dial.minimum()); |
179 | dial.setSingleStep(305); |
180 | |
181 | QTest::keyPress(widget: &dial, key: Qt::Key_Up); |
182 | QCOMPARE( dial.value(), 5); |
183 | |
184 | dial.setValue(dial.minimum()); |
185 | QTest::keyPress(widget: &dial, key: Qt::Key_Down); |
186 | QCOMPARE( dial.value(), 95); |
187 | |
188 | dial.setMinimum(-30); |
189 | dial.setMaximum(-4); |
190 | dial.setSingleStep(200); |
191 | dial.setValue(dial.minimum()); |
192 | QTest::keyPress(widget: &dial, key: Qt::Key_Down); |
193 | QCOMPARE( dial.value(), -22); |
194 | } |
195 | } |
196 | |
197 | QTEST_MAIN(tst_QDial) |
198 | #include "tst_qdial.moc" |
199 | |