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 | |
33 | #include "qcheckbox.h" |
34 | #include <qapplication.h> |
35 | #include <qpixmap.h> |
36 | #include <qdatetime.h> |
37 | #include <qcheckbox.h> |
38 | |
39 | class tst_QCheckBox : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | private slots: |
44 | void initTestCase(); |
45 | void cleanupTestCase(); |
46 | void init(); |
47 | void cleanup(); |
48 | |
49 | void setChecked(); |
50 | void setTriState(); |
51 | void setText_data(); |
52 | void setText(); |
53 | void isToggleButton(); |
54 | void setDown(); |
55 | void setAutoRepeat(); |
56 | void toggle(); |
57 | void pressed(); |
58 | void toggled(); |
59 | void stateChanged(); |
60 | void foregroundRole(); |
61 | void minimumSizeHint(); |
62 | |
63 | protected slots: |
64 | void onClicked(); |
65 | void onToggled( bool on ); |
66 | void onPressed(); |
67 | void onReleased(); |
68 | void onStateChanged( int state ); |
69 | |
70 | private: |
71 | uint click_count; |
72 | uint toggle_count; |
73 | uint press_count; |
74 | uint release_count; |
75 | int cur_state; |
76 | QCheckBox *testWidget; |
77 | }; |
78 | |
79 | void tst_QCheckBox::initTestCase() |
80 | { |
81 | if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland" ), cs: Qt::CaseInsensitive)) |
82 | QSKIP("Wayland: This fails. Figure out why." ); |
83 | |
84 | // Create the test class |
85 | testWidget = new QCheckBox(0); |
86 | testWidget->setObjectName("testObject" ); |
87 | testWidget->resize( w: 200, h: 200 ); |
88 | testWidget->show(); |
89 | QVERIFY(QTest::qWaitForWindowActive(testWidget)); |
90 | } |
91 | |
92 | void tst_QCheckBox::cleanupTestCase() |
93 | { |
94 | delete testWidget; |
95 | testWidget = 0; |
96 | } |
97 | |
98 | void tst_QCheckBox::init() |
99 | { |
100 | testWidget->setTristate( false ); |
101 | testWidget->setChecked( false ); |
102 | testWidget->setAutoRepeat( false ); |
103 | } |
104 | |
105 | void tst_QCheckBox::cleanup() |
106 | { |
107 | disconnect(sender: testWidget, SIGNAL(pressed()), receiver: this, SLOT(onPressed())); |
108 | disconnect(sender: testWidget, SIGNAL(released()), receiver: this, SLOT(onReleased())); |
109 | disconnect(sender: testWidget, SIGNAL(clicked()), receiver: this, SLOT(onClicked())); |
110 | disconnect(sender: testWidget, SIGNAL(toggled(bool)), receiver: this, SLOT(onToggled(bool))); |
111 | } |
112 | |
113 | void tst_QCheckBox::onClicked() |
114 | { |
115 | click_count++; |
116 | } |
117 | |
118 | void tst_QCheckBox::onPressed() |
119 | { |
120 | press_count++; |
121 | } |
122 | |
123 | void tst_QCheckBox::onReleased() |
124 | { |
125 | release_count++; |
126 | } |
127 | |
128 | void tst_QCheckBox::onToggled( bool /*on*/ ) |
129 | { |
130 | toggle_count++; |
131 | } |
132 | |
133 | // *************************************************** |
134 | |
135 | void tst_QCheckBox::setChecked() |
136 | { |
137 | testWidget->setChecked( true ); |
138 | QVERIFY( testWidget->isChecked() ); |
139 | QVERIFY( testWidget->isChecked() ); |
140 | QVERIFY( testWidget->checkState() == Qt::Checked ); |
141 | |
142 | testWidget->setChecked( false ); |
143 | QVERIFY( !testWidget->isChecked() ); |
144 | QVERIFY( !testWidget->isChecked() ); |
145 | QVERIFY( testWidget->checkState() == Qt::Unchecked ); |
146 | |
147 | testWidget->setChecked( false ); |
148 | QTest::keyClick( widget: testWidget, key: ' ' ); |
149 | QVERIFY( testWidget->isChecked() ); |
150 | |
151 | QTest::keyClick( widget: testWidget, key: ' ' ); |
152 | QVERIFY( !testWidget->isChecked() ); |
153 | } |
154 | |
155 | void tst_QCheckBox::setTriState() |
156 | { |
157 | testWidget->setTristate( true ); |
158 | QVERIFY( testWidget->isTristate() ); |
159 | QVERIFY( testWidget->checkState() == Qt::Unchecked ); |
160 | |
161 | testWidget->setCheckState(Qt::PartiallyChecked); |
162 | QVERIFY( testWidget->checkState() == Qt::PartiallyChecked ); |
163 | |
164 | testWidget->setChecked( true ); |
165 | QVERIFY( testWidget->isChecked() ); |
166 | QVERIFY( testWidget->checkState() == Qt::Checked ); |
167 | |
168 | testWidget->setChecked( false ); |
169 | QVERIFY( !testWidget->isChecked() ); |
170 | QVERIFY( testWidget->checkState() == Qt::Unchecked ); |
171 | |
172 | testWidget->setCheckState(Qt::PartiallyChecked); |
173 | QVERIFY( testWidget->checkState() == Qt::PartiallyChecked ); |
174 | |
175 | testWidget->setTristate( false ); |
176 | QVERIFY( !testWidget->isTristate() ); |
177 | |
178 | testWidget->setCheckState(Qt::PartiallyChecked); |
179 | QVERIFY( testWidget->checkState() == Qt::PartiallyChecked ); |
180 | |
181 | testWidget->setChecked( true ); |
182 | QVERIFY( testWidget->checkState() == Qt::Checked ); |
183 | |
184 | testWidget->setChecked( false ); |
185 | QVERIFY( testWidget->checkState() == Qt::Unchecked ); |
186 | } |
187 | |
188 | void tst_QCheckBox::setText_data() |
189 | { |
190 | QTest::addColumn<QString>(name: "s1" ); |
191 | |
192 | QTest::newRow( dataTag: "data0" ) << QString("This is a text" ); |
193 | QTest::newRow( dataTag: "data1" ) << QString("A" ); |
194 | QTest::newRow( dataTag: "data2" ) << QString("ABCDEFG " ); |
195 | QTest::newRow( dataTag: "data3" ) << QString("Text\nwith a cr-lf" ); |
196 | QTest::newRow( dataTag: "data4" ) << QString("" ); |
197 | } |
198 | |
199 | void tst_QCheckBox::setText() |
200 | { |
201 | QFETCH( QString, s1 ); |
202 | testWidget->setText( s1 ); |
203 | QCOMPARE( testWidget->text(), s1 ); |
204 | } |
205 | |
206 | void tst_QCheckBox::setDown() |
207 | { |
208 | testWidget->setDown( true ); |
209 | QVERIFY( testWidget->isDown() ); |
210 | |
211 | testWidget->setDown( false ); |
212 | QVERIFY( !testWidget->isDown() ); |
213 | } |
214 | |
215 | void tst_QCheckBox::setAutoRepeat() |
216 | { |
217 | // setAutoRepeat has no effect on toggle buttons |
218 | QVERIFY( testWidget->isCheckable() ); |
219 | } |
220 | |
221 | void tst_QCheckBox::toggle() |
222 | { |
223 | bool cur_state; |
224 | cur_state = testWidget->isChecked(); |
225 | testWidget->toggle(); |
226 | QVERIFY( cur_state != testWidget->isChecked() ); |
227 | |
228 | cur_state = testWidget->isChecked(); |
229 | testWidget->toggle(); |
230 | QVERIFY( cur_state != testWidget->isChecked() ); |
231 | |
232 | cur_state = testWidget->isChecked(); |
233 | testWidget->toggle(); |
234 | QVERIFY( cur_state != testWidget->isChecked() ); |
235 | } |
236 | |
237 | void tst_QCheckBox::pressed() |
238 | { |
239 | connect(sender: testWidget, SIGNAL(pressed()), receiver: this, SLOT(onPressed())); |
240 | connect(sender: testWidget, SIGNAL(released()), receiver: this, SLOT(onReleased())); |
241 | press_count = 0; |
242 | release_count = 0; |
243 | testWidget->setDown(false); |
244 | QVERIFY( !testWidget->isChecked() ); |
245 | |
246 | QTest::keyPress( widget: testWidget, key: Qt::Key_Space ); |
247 | QVERIFY( press_count == 1 ); |
248 | QVERIFY( release_count == 0 ); |
249 | QVERIFY( !testWidget->isChecked() ); |
250 | |
251 | QTest::keyRelease( widget: testWidget, key: Qt::Key_Space ); |
252 | QVERIFY( press_count == 1 ); |
253 | QVERIFY( release_count == 1 ); |
254 | QVERIFY( testWidget->isChecked() ); |
255 | } |
256 | |
257 | void tst_QCheckBox::toggled() |
258 | { |
259 | connect(sender: testWidget, SIGNAL(toggled(bool)), receiver: this, SLOT(onToggled(bool))); |
260 | click_count = 0; |
261 | toggle_count = 0; |
262 | testWidget->toggle(); |
263 | QCOMPARE( toggle_count, (uint)1 ); |
264 | |
265 | testWidget->toggle(); |
266 | QCOMPARE( toggle_count, (uint)2 ); |
267 | |
268 | testWidget->toggle(); |
269 | QCOMPARE( toggle_count, (uint)3 ); |
270 | |
271 | QCOMPARE( click_count, (uint)0 ); |
272 | } |
273 | |
274 | void tst_QCheckBox::onStateChanged( int state ) |
275 | { |
276 | cur_state = state; |
277 | } |
278 | |
279 | void tst_QCheckBox::stateChanged() |
280 | { |
281 | QSignalSpy stateChangedSpy(testWidget, SIGNAL(stateChanged(int))); |
282 | connect(sender: testWidget, SIGNAL(stateChanged(int)), receiver: this, SLOT(onStateChanged(int))); |
283 | cur_state = -1; |
284 | testWidget->setChecked( true ); |
285 | qApp->processEvents(); |
286 | QCOMPARE( cur_state, (int)2 ); |
287 | |
288 | cur_state = -1; |
289 | testWidget->setChecked( false ); |
290 | qApp->processEvents(); |
291 | QCOMPARE( cur_state, (int)0 ); |
292 | |
293 | cur_state = -1; |
294 | testWidget->setCheckState(Qt::PartiallyChecked); |
295 | qApp->processEvents(); |
296 | QCOMPARE( cur_state, (int)1 ); |
297 | |
298 | QCOMPARE(stateChangedSpy.count(), 3); |
299 | testWidget->setCheckState(Qt::PartiallyChecked); |
300 | qApp->processEvents(); |
301 | QCOMPARE(stateChangedSpy.count(), 3); |
302 | } |
303 | |
304 | void tst_QCheckBox::isToggleButton() |
305 | { |
306 | QVERIFY( testWidget->isCheckable() ); |
307 | } |
308 | |
309 | void tst_QCheckBox::foregroundRole() |
310 | { |
311 | QCOMPARE(testWidget->foregroundRole(), QPalette::WindowText); |
312 | } |
313 | |
314 | void tst_QCheckBox::minimumSizeHint() |
315 | { |
316 | QCheckBox box(tr(s: "CheckBox's sizeHint is the same as it's minimumSizeHint" )); |
317 | QCOMPARE(box.sizeHint(), box.minimumSizeHint()); |
318 | } |
319 | |
320 | QTEST_MAIN(tst_QCheckBox) |
321 | #include "tst_qcheckbox.moc" |
322 | |