|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 |
|
45 |
|
46 #include "qcommandlinkbutton.h" |
|
47 #include <qapplication.h> |
|
48 |
|
49 #include <qcommandlinkbutton.h> |
|
50 #include <qmenu.h> |
|
51 #include <qtimer.h> |
|
52 #include <QDialog> |
|
53 #include <QGridLayout> |
|
54 |
|
55 Q_DECLARE_METATYPE(QCommandLinkButton*) |
|
56 |
|
57 //TESTED_CLASS= |
|
58 //TESTED_FILES= |
|
59 |
|
60 class tst_QCommandLinkButton : public QObject |
|
61 { |
|
62 Q_OBJECT |
|
63 public: |
|
64 tst_QCommandLinkButton(); |
|
65 virtual ~tst_QCommandLinkButton(); |
|
66 |
|
67 |
|
68 public slots: |
|
69 void initTestCase(); |
|
70 void cleanupTestCase(); |
|
71 void init(); |
|
72 void cleanup(); |
|
73 private slots: |
|
74 void getSetCheck(); |
|
75 void pressed(); |
|
76 void setAccel(); |
|
77 void isCheckable(); |
|
78 void setDown(); |
|
79 void popupCrash(); |
|
80 void isChecked(); |
|
81 void animateClick(); |
|
82 void toggle(); |
|
83 void clicked(); |
|
84 void toggled(); |
|
85 void defaultAndAutoDefault(); |
|
86 void setAutoRepeat(); |
|
87 |
|
88 protected slots: |
|
89 void resetCounters(); |
|
90 void onClicked(); |
|
91 void onToggled( bool on ); |
|
92 void onPressed(); |
|
93 void onReleased(); |
|
94 void helperSlotDelete(); |
|
95 |
|
96 private: |
|
97 uint click_count; |
|
98 uint toggle_count; |
|
99 uint press_count; |
|
100 uint release_count; |
|
101 |
|
102 QCommandLinkButton *testWidget; |
|
103 }; |
|
104 |
|
105 // Testing get/set functions |
|
106 void tst_QCommandLinkButton::getSetCheck() |
|
107 { |
|
108 QCommandLinkButton obj1; |
|
109 |
|
110 QString text("mytext"); |
|
111 QVERIFY(obj1.description().isEmpty()); |
|
112 obj1.setDescription(text); |
|
113 QVERIFY(obj1.description() == text); |
|
114 |
|
115 QVERIFY(obj1.text().isEmpty()); |
|
116 obj1.setText(text); |
|
117 QVERIFY(obj1.text() == text); |
|
118 |
|
119 |
|
120 QMenu *var1 = new QMenu; |
|
121 obj1.setMenu(var1); |
|
122 QCOMPARE(var1, obj1.menu()); |
|
123 obj1.setMenu((QMenu *)0); |
|
124 QCOMPARE((QMenu *)0, obj1.menu()); |
|
125 delete var1; |
|
126 } |
|
127 |
|
128 tst_QCommandLinkButton::tst_QCommandLinkButton() |
|
129 { |
|
130 } |
|
131 |
|
132 tst_QCommandLinkButton::~tst_QCommandLinkButton() |
|
133 { |
|
134 } |
|
135 |
|
136 void tst_QCommandLinkButton::initTestCase() |
|
137 { |
|
138 // Create the test class |
|
139 testWidget = new QCommandLinkButton( "&Start", 0 ); |
|
140 testWidget->setObjectName("testWidget"); |
|
141 testWidget->resize( 200, 200 ); |
|
142 testWidget->show(); |
|
143 |
|
144 connect( testWidget, SIGNAL(clicked()), this, SLOT(onClicked()) ); |
|
145 connect( testWidget, SIGNAL(pressed()), this, SLOT(onPressed()) ); |
|
146 connect( testWidget, SIGNAL(released()), this, SLOT(onReleased()) ); |
|
147 connect( testWidget, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)) ); |
|
148 } |
|
149 |
|
150 void tst_QCommandLinkButton::cleanupTestCase() |
|
151 { |
|
152 delete testWidget; |
|
153 testWidget = 0; |
|
154 } |
|
155 |
|
156 void tst_QCommandLinkButton::init() |
|
157 { |
|
158 testWidget->setAutoRepeat( FALSE ); |
|
159 testWidget->setDown( FALSE ); |
|
160 testWidget->setText("Test"); |
|
161 testWidget->setDescription("Description text."); |
|
162 testWidget->setEnabled( TRUE ); |
|
163 QKeySequence seq; |
|
164 testWidget->setShortcut( seq ); |
|
165 |
|
166 resetCounters(); |
|
167 } |
|
168 |
|
169 void tst_QCommandLinkButton::cleanup() |
|
170 { |
|
171 } |
|
172 |
|
173 |
|
174 void tst_QCommandLinkButton::resetCounters() |
|
175 { |
|
176 toggle_count = 0; |
|
177 press_count = 0; |
|
178 release_count = 0; |
|
179 click_count = 0; |
|
180 } |
|
181 |
|
182 void tst_QCommandLinkButton::onClicked() |
|
183 { |
|
184 click_count++; |
|
185 } |
|
186 |
|
187 void tst_QCommandLinkButton::onToggled( bool /*on*/ ) |
|
188 { |
|
189 toggle_count++; |
|
190 } |
|
191 |
|
192 void tst_QCommandLinkButton::onPressed() |
|
193 { |
|
194 press_count++; |
|
195 } |
|
196 |
|
197 void tst_QCommandLinkButton::onReleased() |
|
198 { |
|
199 release_count++; |
|
200 } |
|
201 |
|
202 void tst_QCommandLinkButton::setAutoRepeat() |
|
203 { |
|
204 // Give the last tests time to finish - i.e., wait for the window close |
|
205 // and deactivate to avoid a race condition here. We can't add this to the |
|
206 // end of the defaultAndAutoDefault test, since any failure in that test |
|
207 // will return out of that function. |
|
208 QTest::qWait( 1000 ); |
|
209 |
|
210 // If this changes, this test must be completely revised. |
|
211 QVERIFY( !testWidget->isCheckable() ); |
|
212 |
|
213 // verify autorepeat is off by default. |
|
214 QCommandLinkButton tmp( 0 ); |
|
215 tmp.setObjectName("tmp"); |
|
216 QVERIFY( !tmp.autoRepeat() ); |
|
217 |
|
218 // check if we can toggle the mode |
|
219 testWidget->setAutoRepeat( TRUE ); |
|
220 QVERIFY( testWidget->autoRepeat() ); |
|
221 |
|
222 testWidget->setAutoRepeat( FALSE ); |
|
223 QVERIFY( !testWidget->autoRepeat() ); |
|
224 |
|
225 resetCounters(); |
|
226 |
|
227 // check that the button is down if we press space and not in autorepeat |
|
228 testWidget->setDown( FALSE ); |
|
229 testWidget->setAutoRepeat( FALSE ); |
|
230 QTest::keyPress( testWidget, Qt::Key_Space ); |
|
231 |
|
232 QTest::qWait( 300 ); |
|
233 |
|
234 QVERIFY( testWidget->isDown() ); |
|
235 QVERIFY( toggle_count == 0 ); |
|
236 QVERIFY( press_count == 1 ); |
|
237 QVERIFY( release_count == 0 ); |
|
238 QVERIFY( click_count == 0 ); |
|
239 |
|
240 QTest::keyRelease( testWidget, Qt::Key_Space ); |
|
241 resetCounters(); |
|
242 |
|
243 // check that the button is down if we press space while in autorepeat |
|
244 // we can't actually confirm how many times it is fired, more than 1 is enough. |
|
245 |
|
246 testWidget->setDown( FALSE ); |
|
247 testWidget->setAutoRepeat( TRUE ); |
|
248 QTest::keyPress( testWidget, Qt::Key_Space ); |
|
249 QTest::qWait(900); |
|
250 QVERIFY( testWidget->isDown() ); |
|
251 QVERIFY( toggle_count == 0 ); |
|
252 QTest::keyRelease( testWidget, Qt::Key_Space ); |
|
253 QVERIFY(press_count == release_count); |
|
254 QVERIFY(release_count == click_count); |
|
255 QVERIFY(press_count > 1); |
|
256 |
|
257 // #### shouldn't I check here to see if multiple signals have been fired??? |
|
258 |
|
259 // check that pressing ENTER has no effect |
|
260 resetCounters(); |
|
261 testWidget->setDown( FALSE ); |
|
262 testWidget->setAutoRepeat( FALSE ); |
|
263 QTest::keyPress( testWidget, Qt::Key_Enter ); |
|
264 |
|
265 QTest::qWait( 300 ); |
|
266 |
|
267 QVERIFY( !testWidget->isDown() ); |
|
268 QVERIFY( toggle_count == 0 ); |
|
269 QVERIFY( press_count == 0 ); |
|
270 QVERIFY( release_count == 0 ); |
|
271 QVERIFY( click_count == 0 ); |
|
272 QTest::keyRelease( testWidget, Qt::Key_Enter ); |
|
273 |
|
274 // check that pressing ENTER has no effect |
|
275 resetCounters(); |
|
276 testWidget->setDown( FALSE ); |
|
277 testWidget->setAutoRepeat( TRUE ); |
|
278 QTest::keyClick( testWidget, Qt::Key_Enter ); |
|
279 QTest::qWait( 300 ); |
|
280 QVERIFY( !testWidget->isDown() ); |
|
281 QVERIFY( toggle_count == 0 ); |
|
282 QVERIFY( press_count == 0 ); |
|
283 QVERIFY( release_count == 0 ); |
|
284 QVERIFY( click_count == 0 ); |
|
285 } |
|
286 |
|
287 void tst_QCommandLinkButton::pressed() |
|
288 { |
|
289 QTest::keyPress( testWidget, ' ' ); |
|
290 // QTest::qWait( 300 ); |
|
291 QCOMPARE( press_count, (uint)1 ); |
|
292 QCOMPARE( release_count, (uint)0 ); |
|
293 |
|
294 QTest::keyRelease( testWidget, ' ' ); |
|
295 // QTest::qWait( 300 ); |
|
296 QCOMPARE( press_count, (uint)1 ); |
|
297 QCOMPARE( release_count, (uint)1 ); |
|
298 |
|
299 QTest::keyPress( testWidget,Qt::Key_Enter ); |
|
300 // QTest::qWait( 300 ); |
|
301 QCOMPARE( press_count, (uint)1 ); |
|
302 QCOMPARE( release_count, (uint)1 ); |
|
303 |
|
304 testWidget->setAutoDefault(true); |
|
305 QTest::keyPress( testWidget,Qt::Key_Enter ); |
|
306 // QTest::qWait( 300 ); |
|
307 QCOMPARE( press_count, (uint)2 ); |
|
308 QCOMPARE( release_count, (uint)2 ); |
|
309 testWidget->setAutoDefault(false); |
|
310 |
|
311 } |
|
312 |
|
313 |
|
314 |
|
315 void tst_QCommandLinkButton::isCheckable() |
|
316 { |
|
317 QVERIFY( !testWidget->isCheckable() ); |
|
318 } |
|
319 |
|
320 void tst_QCommandLinkButton::setDown() |
|
321 { |
|
322 testWidget->setDown( FALSE ); |
|
323 QVERIFY( !testWidget->isDown() ); |
|
324 |
|
325 testWidget->setDown( TRUE ); |
|
326 QVERIFY( testWidget->isDown() ); |
|
327 |
|
328 testWidget->setDown( TRUE ); |
|
329 QTest::keyClick( testWidget, Qt::Key_Escape ); |
|
330 QVERIFY( !testWidget->isDown() ); |
|
331 } |
|
332 |
|
333 void tst_QCommandLinkButton::isChecked() |
|
334 { |
|
335 testWidget->setDown( FALSE ); |
|
336 QVERIFY( !testWidget->isChecked() ); |
|
337 |
|
338 testWidget->setDown( TRUE ); |
|
339 QVERIFY( !testWidget->isChecked() ); |
|
340 |
|
341 testWidget->setDown( FALSE ); |
|
342 testWidget->toggle(); |
|
343 QVERIFY( testWidget->isChecked() == testWidget->isCheckable() ); |
|
344 } |
|
345 |
|
346 void tst_QCommandLinkButton::toggle() |
|
347 { |
|
348 // the pushbutton shouldn't toggle the button. |
|
349 testWidget->toggle(); |
|
350 QVERIFY( testWidget->isChecked() == FALSE ); |
|
351 } |
|
352 |
|
353 void tst_QCommandLinkButton::toggled() |
|
354 { |
|
355 // the pushbutton shouldn't send a toggled signal when we call the toggle slot. |
|
356 QVERIFY( !testWidget->isCheckable() ); |
|
357 |
|
358 testWidget->toggle(); |
|
359 QVERIFY( toggle_count == 0 ); |
|
360 |
|
361 // do it again, just to be shure |
|
362 resetCounters(); |
|
363 testWidget->toggle(); |
|
364 QVERIFY( toggle_count == 0 ); |
|
365 |
|
366 // finally check that we can toggle using the mouse |
|
367 resetCounters(); |
|
368 QTest::mousePress( testWidget, Qt::LeftButton ); |
|
369 QVERIFY( toggle_count == 0 ); |
|
370 QVERIFY( click_count == 0 ); |
|
371 |
|
372 QTest::mouseRelease( testWidget, Qt::LeftButton ); |
|
373 QVERIFY( click_count == 1 ); |
|
374 } |
|
375 |
|
376 /* |
|
377 If we press an accelerator key we ONLY get a pressed signal and |
|
378 NOT a released or clicked signal. |
|
379 */ |
|
380 |
|
381 void tst_QCommandLinkButton::setAccel() |
|
382 { |
|
383 testWidget->setText("&AccelTest"); |
|
384 QKeySequence seq( Qt::ALT + Qt::Key_A ); |
|
385 testWidget->setShortcut( seq ); |
|
386 |
|
387 // The shortcut will not be activated unless the button is in a active |
|
388 // window and has focus |
|
389 testWidget->setFocus(); |
|
390 for (int i = 0; !testWidget->isActiveWindow() && i < 1000; ++i) { |
|
391 testWidget->activateWindow(); |
|
392 QApplication::instance()->processEvents(); |
|
393 QTest::qWait(100); |
|
394 } |
|
395 |
|
396 QVERIFY(testWidget->isActiveWindow()); |
|
397 |
|
398 QTest::keyClick( testWidget, 'A', Qt::AltModifier ); |
|
399 QTest::qWait( 500 ); |
|
400 QVERIFY( click_count == 1 ); |
|
401 QVERIFY( press_count == 1 ); |
|
402 QVERIFY( release_count == 1 ); |
|
403 QVERIFY( toggle_count == 0 ); |
|
404 |
|
405 // wait 200 ms because setAccel uses animateClick. |
|
406 // if we don't wait this may screw up a next test. |
|
407 QTest::qWait(200); |
|
408 } |
|
409 |
|
410 void tst_QCommandLinkButton::animateClick() |
|
411 { |
|
412 QVERIFY( !testWidget->isDown() ); |
|
413 testWidget->animateClick(); |
|
414 QVERIFY( testWidget->isDown() ); |
|
415 QTest::qWait( 200 ); |
|
416 QVERIFY( !testWidget->isDown() ); |
|
417 |
|
418 QVERIFY( click_count == 1 ); |
|
419 QVERIFY( press_count == 1 ); |
|
420 QVERIFY( release_count == 1 ); |
|
421 QVERIFY( toggle_count == 0 ); |
|
422 } |
|
423 |
|
424 void tst_QCommandLinkButton::clicked() |
|
425 { |
|
426 QTest::mousePress( testWidget, Qt::LeftButton ); |
|
427 QVERIFY( press_count == 1 ); |
|
428 QVERIFY( release_count == 0 ); |
|
429 |
|
430 QTest::mouseRelease( testWidget, Qt::LeftButton ); |
|
431 QCOMPARE( press_count, (uint)1 ); |
|
432 QCOMPARE( release_count, (uint)1 ); |
|
433 |
|
434 press_count = 0; |
|
435 release_count = 0; |
|
436 testWidget->setDown(FALSE); |
|
437 for (uint i=0; i<10; i++) |
|
438 QTest::mouseClick( testWidget, Qt::LeftButton ); |
|
439 QCOMPARE( press_count, (uint)10 ); |
|
440 QCOMPARE( release_count, (uint)10 ); |
|
441 } |
|
442 |
|
443 QCommandLinkButton *pb = 0; |
|
444 void tst_QCommandLinkButton::helperSlotDelete() |
|
445 { |
|
446 delete pb; |
|
447 pb = 0; |
|
448 } |
|
449 |
|
450 void tst_QCommandLinkButton::popupCrash() |
|
451 { |
|
452 pb = new QCommandLinkButton("foo", "description"); |
|
453 QMenu *menu = new QMenu("bar", pb); |
|
454 pb->setMenu(menu); |
|
455 QTimer::singleShot(1000, this, SLOT(helperSlotDelete())); |
|
456 pb->show(); |
|
457 pb->click(); |
|
458 } |
|
459 |
|
460 void tst_QCommandLinkButton::defaultAndAutoDefault() |
|
461 { |
|
462 { |
|
463 // Adding buttons directly to QDialog |
|
464 QDialog dialog; |
|
465 |
|
466 QCommandLinkButton button1(&dialog); |
|
467 QVERIFY(button1.autoDefault()); |
|
468 QVERIFY(!button1.isDefault()); |
|
469 |
|
470 QCommandLinkButton button2(&dialog); |
|
471 QVERIFY(button2.autoDefault()); |
|
472 QVERIFY(!button2.isDefault()); |
|
473 |
|
474 button1.setDefault(true); |
|
475 QVERIFY(button1.autoDefault()); |
|
476 QVERIFY(button1.isDefault()); |
|
477 QVERIFY(button2.autoDefault()); |
|
478 QVERIFY(!button2.isDefault()); |
|
479 |
|
480 dialog.show(); |
|
481 QVERIFY(dialog.isVisible()); |
|
482 |
|
483 QObject::connect(&button1, SIGNAL(clicked()), &dialog, SLOT(hide())); |
|
484 QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); |
|
485 QApplication::sendEvent(&dialog, &event); |
|
486 QVERIFY(!dialog.isVisible()); |
|
487 } |
|
488 |
|
489 { |
|
490 // Adding buttons to QDialog through a layout |
|
491 QDialog dialog; |
|
492 |
|
493 QCommandLinkButton button3; |
|
494 button3.setAutoDefault(false); |
|
495 |
|
496 QCommandLinkButton button1; |
|
497 QVERIFY(!button1.autoDefault()); |
|
498 QVERIFY(!button1.isDefault()); |
|
499 |
|
500 QCommandLinkButton button2; |
|
501 QVERIFY(!button2.autoDefault()); |
|
502 QVERIFY(!button2.isDefault()); |
|
503 |
|
504 button1.setDefault(true); |
|
505 QVERIFY(!button1.autoDefault()); |
|
506 QVERIFY(button1.isDefault()); |
|
507 QVERIFY(!button2.autoDefault()); |
|
508 QVERIFY(!button2.isDefault()); |
|
509 |
|
510 QGridLayout layout; |
|
511 layout.addWidget(&button3, 0, 3); |
|
512 layout.addWidget(&button2, 0, 2); |
|
513 layout.addWidget(&button1, 0, 1); |
|
514 dialog.setLayout(&layout); |
|
515 button3.setFocus(); |
|
516 QVERIFY(button1.autoDefault()); |
|
517 QVERIFY(button1.isDefault()); |
|
518 QVERIFY(button2.autoDefault()); |
|
519 QVERIFY(!button2.isDefault()); |
|
520 |
|
521 dialog.show(); |
|
522 QVERIFY(dialog.isVisible()); |
|
523 |
|
524 QObject::connect(&button1, SIGNAL(clicked()), &dialog, SLOT(hide())); |
|
525 QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); |
|
526 QApplication::sendEvent(&dialog, &event); |
|
527 QVERIFY(!dialog.isVisible()); |
|
528 } |
|
529 |
|
530 { |
|
531 // autoDefault behavior. |
|
532 QDialog dialog; |
|
533 QCommandLinkButton button2(&dialog); |
|
534 QCommandLinkButton button1(&dialog); |
|
535 dialog.show(); |
|
536 QVERIFY(dialog.isVisible()); |
|
537 |
|
538 // No default button is set, and button2 is the first autoDefault button |
|
539 // that is next in the tab order |
|
540 QObject::connect(&button2, SIGNAL(clicked()), &dialog, SLOT(hide())); |
|
541 QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); |
|
542 QApplication::sendEvent(&dialog, &event); |
|
543 QVERIFY(!dialog.isVisible()); |
|
544 |
|
545 // Reparenting |
|
546 QVERIFY(button2.autoDefault()); |
|
547 button2.setParent(0); |
|
548 QVERIFY(!button2.autoDefault()); |
|
549 button2.setAutoDefault(false); |
|
550 button2.setParent(&dialog); |
|
551 QVERIFY(!button2.autoDefault()); |
|
552 |
|
553 button1.setAutoDefault(true); |
|
554 button1.setParent(0); |
|
555 QVERIFY(button1.autoDefault()); |
|
556 } |
|
557 } |
|
558 |
|
559 QTEST_MAIN(tst_QCommandLinkButton) |
|
560 #include "tst_qcommandlinkbutton.moc" |