1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
47
#include <qapplication.h>
49
#include <qstyleoption.h>
50
#include <qkeysequence.h>
52
#include <qgridlayout.h>
53
#include <qabstractbutton.h>
55
class tst_QAbstractButton : public QObject
60
tst_QAbstractButton();
61
virtual ~tst_QAbstractButton();
66
void cleanupTestCase();
70
void setAutoRepeat_data();
87
void shortcutEvents();
88
void stopRepeatTimer();
90
#ifdef QT_KEYPAD_NAVIGATION
96
void onToggled( bool on );
107
QAbstractButton *testWidget;
110
// QAbstractButton is an abstract class in 4.0
111
class MyButton : public QAbstractButton
114
MyButton(QWidget *p = 0) : QAbstractButton(p) {}
115
void paintEvent(QPaintEvent *)
119
p.fillRect(r, isDown() ? Qt::black : (isChecked() ? Qt::lightGray : Qt::white));
120
p.setPen(isDown() ? Qt::white : Qt::black);
122
p.drawText(r, Qt::AlignCenter | Qt::TextShowMnemonic, text());
124
r.adjust(2, 2, -2, -2);
125
QStyleOptionFocusRect opt;
127
opt.palette = palette();
128
opt.state = QStyle::State_None;
129
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, &p, this);
136
QSize sizeHint() const
139
if (!text().isEmpty())
140
sh += fontMetrics().boundingRect(text()).size();
144
void resetTimerEvents() { timerEvents = 0; }
145
int timerEventCount() const { return timerEvents; }
151
void timerEvent(QTimerEvent *event)
154
QAbstractButton::timerEvent(event);
158
tst_QAbstractButton::tst_QAbstractButton()
162
tst_QAbstractButton::~tst_QAbstractButton()
166
void tst_QAbstractButton::initTestCase()
168
testWidget = new MyButton(0);
169
testWidget->setObjectName("testObject");
170
testWidget->resize( 200, 200 );
173
connect( testWidget, SIGNAL(clicked()), this, SLOT(onClicked()) );
174
connect( testWidget, SIGNAL(pressed()), this, SLOT(onPressed()) );
175
connect( testWidget, SIGNAL(released()), this, SLOT(onReleased()) );
176
connect( testWidget, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)) );
179
void tst_QAbstractButton::cleanupTestCase()
184
void tst_QAbstractButton::init()
186
testWidget->setText("Test");
187
testWidget->setEnabled( true );
188
testWidget->setDown( false );
189
testWidget->setAutoRepeat( false );
191
testWidget->setShortcut( seq );
199
void tst_QAbstractButton::cleanup()
203
void tst_QAbstractButton::resetValues()
211
void tst_QAbstractButton::onClicked()
216
void tst_QAbstractButton::onToggled( bool /*on*/ )
221
void tst_QAbstractButton::onPressed()
226
void tst_QAbstractButton::onReleased()
231
void tst_QAbstractButton::setAutoRepeat_data()
233
QTest::addColumn<int>("mode");
234
QTest::newRow("mode 0") << 0;
235
QTest::newRow("mode 1") << 1;
236
QTest::newRow("mode 2") << 2;
237
QTest::newRow("mode 3") << 3;
238
QTest::newRow("mode 4") << 4;
239
QTest::newRow("mode 5") << 5;
240
QTest::newRow("mode 6") << 6;
243
#define REPEAT_DELAY 1000
248
void tst_QAbstractButton::setAutoRepeat()
252
//FIXME: temp code to check that the test fails consistenly
258
QVERIFY( !testWidget->isCheckable() );
261
// check if we can toggle the mode
262
testWidget->setAutoRepeat( true );
263
QVERIFY( testWidget->autoRepeat() );
265
testWidget->setAutoRepeat( false );
266
QVERIFY( !testWidget->autoRepeat() );
269
// check that the button is down if we press space and not in autorepeat
270
testWidget->setDown( false );
271
testWidget->setAutoRepeat( false );
272
QTest::keyPress( testWidget, Qt::Key_Space );
274
QTest::qWait( REPEAT_DELAY );
276
QVERIFY( release_count == 0 );
277
QVERIFY( testWidget->isDown() );
278
QVERIFY( toggle_count == 0 );
279
QVERIFY( press_count == 1 );
280
QVERIFY( click_count == 0 );
282
QTest::keyRelease( testWidget, Qt::Key_Space );
283
QVERIFY( click_count == 1 );
284
QVERIFY( release_count == 1 );
287
// check that the button is down if we press space while in autorepeat
288
testWidget->setDown(false);
289
testWidget->setAutoRepeat(true);
290
QTest::keyPress(testWidget, Qt::Key_Space);
291
QTest::qWait(REPEAT_DELAY);
292
QVERIFY(testWidget->isDown());
293
QTest::keyRelease(testWidget, Qt::Key_Space);
294
QVERIFY(release_count == press_count);
295
QVERIFY(toggle_count == 0);
296
QVERIFY(press_count == click_count);
297
QVERIFY(click_count > 1);
300
// check that pressing ENTER has no effect when autorepeat is false
301
testWidget->setDown( false );
302
testWidget->setAutoRepeat( false );
303
QTest::keyPress( testWidget, Qt::Key_Enter );
305
QTest::qWait( REPEAT_DELAY );
307
QVERIFY( !testWidget->isDown() );
308
QVERIFY( toggle_count == 0 );
309
QVERIFY( press_count == 0 );
310
QVERIFY( release_count == 0 );
311
QVERIFY( click_count == 0 );
312
QTest::keyRelease( testWidget, Qt::Key_Enter );
314
QVERIFY( click_count == 0 );
317
// check that pressing ENTER has no effect when autorepeat is true
318
testWidget->setDown( false );
319
testWidget->setAutoRepeat( true );
320
QTest::keyPress( testWidget, Qt::Key_Enter );
322
QTest::qWait( REPEAT_DELAY );
324
QVERIFY( !testWidget->isDown() );
325
QVERIFY( toggle_count == 0 );
326
QVERIFY( press_count == 0 );
327
QVERIFY( release_count == 0 );
328
QVERIFY( click_count == 0 );
330
QTest::keyRelease( testWidget, Qt::Key_Enter );
332
QVERIFY( click_count == 0 );
335
// verify autorepeat is off by default.
337
tmp.setObjectName("tmp" );
338
QVERIFY( !tmp.autoRepeat() );
343
void tst_QAbstractButton::pressed()
345
// pressed/released signals expected for a QAbstractButton
346
QTest::keyPress( testWidget, ' ' );
347
QCOMPARE( press_count, (uint)1 );
350
void tst_QAbstractButton::released()
352
// pressed/released signals expected for a QAbstractButton
353
QTest::keyPress( testWidget, ' ' );
354
QTest::keyRelease( testWidget, ' ' );
355
QCOMPARE( release_count, (uint)1 );
358
void tst_QAbstractButton::setText()
360
testWidget->setText("");
361
QCOMPARE( testWidget->text(), QString("") );
362
testWidget->setText("simple");
363
QCOMPARE( testWidget->text(), QString("simple") );
364
testWidget->setText("&ersand");
365
QCOMPARE( testWidget->text(), QString("&ersand") );
366
#ifndef Q_OS_MAC // no mneonics on Mac.
367
QCOMPARE( testWidget->shortcut(), QKeySequence("ALT+A"));
369
testWidget->setText("te&st");
370
QCOMPARE( testWidget->text(), QString("te&st") );
371
#ifndef Q_OS_MAC // no mneonics on Mac.
372
QCOMPARE( testWidget->shortcut(), QKeySequence("ALT+S"));
374
testWidget->setText("foo");
375
QCOMPARE( testWidget->text(), QString("foo") );
376
#ifndef Q_OS_MAC // no mneonics on Mac.
377
QCOMPARE( testWidget->shortcut(), QKeySequence());
381
void tst_QAbstractButton::setIcon()
383
const char *test1_xpm[] = {
397
QPixmap p(test1_xpm);
398
testWidget->setIcon( p );
399
QCOMPARE( testWidget->icon().pixmap(12, 8), p );
403
const char *test2_xpm[] = {
417
int currentHeight = testWidget->height();
418
int currentWidth = testWidget->width();
420
QPixmap p2( test2_xpm );
421
for ( int a = 0; a<5; a++ )
422
testWidget->setIcon( p2 );
424
QCOMPARE( testWidget->icon().pixmap(12, 8), p2 );
426
QCOMPARE( testWidget->height(), currentHeight );
427
QCOMPARE( testWidget->width(), currentWidth );
430
void tst_QAbstractButton::setEnabled()
432
testWidget->setEnabled( false );
433
QVERIFY( !testWidget->isEnabled() );
434
// QTEST( testWidget, "disabled" );
436
testWidget->setEnabled( true );
437
QVERIFY( testWidget->isEnabled() );
438
// QTEST( testWidget, "enabled" );
441
void tst_QAbstractButton::isCheckable()
443
QVERIFY( !testWidget->isCheckable() );
446
void tst_QAbstractButton::setDown()
448
testWidget->setDown( false );
449
QVERIFY( !testWidget->isDown() );
451
testWidget->setDown( true );
453
QVERIFY( testWidget->isDown() );
455
testWidget->setDown( true );
457
// add some debugging stuff
458
QWidget *grab = QWidget::keyboardGrabber();
459
if (grab != 0 && grab != testWidget)
460
qDebug( "testWidget != keyboardGrabber" );
461
grab = qApp->focusWidget();
462
if (grab != 0 && grab != testWidget)
463
qDebug( "testWidget != focusWidget" );
465
QTest::keyClick( testWidget, Qt::Key_Escape );
466
QVERIFY( !testWidget->isDown() );
469
void tst_QAbstractButton::isChecked()
471
testWidget->setDown( false );
472
QVERIFY( !testWidget->isChecked() );
474
testWidget->setDown( true );
475
QVERIFY( !testWidget->isChecked() );
477
testWidget->setDown( false );
478
testWidget->toggle();
479
QVERIFY( testWidget->isChecked() == testWidget->isCheckable() );
482
void tst_QAbstractButton::toggled()
484
testWidget->toggle();
485
QVERIFY( toggle_count == 0 );
487
QTest::mousePress( testWidget, Qt::LeftButton );
488
QVERIFY( toggle_count == 0 );
489
QVERIFY( click_count == 0 );
491
QTest::mouseRelease( testWidget, Qt::LeftButton );
492
QVERIFY( click_count == 1 );
495
void tst_QAbstractButton::setShortcut()
497
QKeySequence seq( Qt::Key_A );
498
testWidget->setShortcut( seq );
499
QApplication::setActiveWindow(testWidget);
501
// must be active to get shortcuts
502
for (int i = 0; !testWidget->isActiveWindow() && i < 100; ++i) {
503
testWidget->activateWindow();
504
QApplication::instance()->processEvents();
507
QVERIFY(testWidget->isActiveWindow());
509
QTest::keyClick( testWidget, 'A' );
510
QTest::qWait(300); // Animate click takes time
511
QCOMPARE(click_count, (uint)1);
512
QCOMPARE(press_count, (uint)1); // Press is part of a click
513
QCOMPARE(release_count,(uint)1); // Release is part of a click
515
QVERIFY( toggle_count == 0 );
518
// QTest::keyPress( testWidget, 'A' );
519
// QTest::qWait(10000);
520
// QTest::keyRelease( testWidget, 'A' );
521
// QCOMPARE(click_count, (uint)1);
522
// QCOMPARE(press_count, (uint)1);
523
// QCOMPARE(release_count,(uint)1);
525
// qDebug() << click_count;
529
void tst_QAbstractButton::animateClick()
531
testWidget->animateClick();
532
QVERIFY( testWidget->isDown() );
533
qApp->processEvents();
534
QVERIFY( testWidget->isDown() );
536
qApp->processEvents();
537
QVERIFY( !testWidget->isDown() );
540
void tst_QAbstractButton::shortcutEvents()
543
QSignalSpy pressedSpy(&button, SIGNAL(pressed()));
544
QSignalSpy releasedSpy(&button, SIGNAL(released()));
545
QSignalSpy clickedSpy(&button, SIGNAL(clicked(bool)));
547
for (int i = 0; i < 4; ++i) {
548
QKeySequence sequence;
549
// Default shortcutId for QAbstractButton is 0, so the shortcut event will work.
550
QShortcutEvent event(sequence, /*shortcutId*/ 0);
551
QApplication::sendEvent(&button, &event);
556
QTest::qWait(1000); // ensure animate timer is expired
558
QCOMPARE(pressedSpy.count(), 3);
559
QCOMPARE(releasedSpy.count(), 3);
560
QCOMPARE(clickedSpy.count(), 3);
563
void tst_QAbstractButton::stopRepeatTimer()
566
button.setAutoRepeat(true);
568
// Mouse trigger case:
569
button.resetTimerEvents();
570
QTest::mousePress(&button, Qt::LeftButton);
572
QVERIFY(button.timerEventCount() > 0);
574
QTest::mouseRelease(&button, Qt::LeftButton);
575
button.resetTimerEvents();
577
QCOMPARE(button.timerEventCount(), 0);
580
button.resetTimerEvents();
581
QTest::keyPress(&button, Qt::Key_Space);
583
QVERIFY(button.timerEventCount() > 0);
585
QTest::keyRelease(&button, Qt::Key_Space);
586
button.resetTimerEvents();
588
QCOMPARE(button.timerEventCount(), 0);
591
#ifdef QT_KEYPAD_NAVIGATION
592
void tst_QAbstractButton::keyNavigation()
594
QApplication::setNavigationMode(Qt::NavigationModeKeypadDirectional);
597
QGridLayout *layout = new QGridLayout(&widget);
598
QAbstractButton *buttons[3][3];
599
for(int y = 0; y < 3; y++) {
600
for(int x = 0; x < 3; x++) {
601
buttons[y][x] = new MyButton(&widget);
602
buttons[y][x]->setFocusPolicy(Qt::StrongFocus);
603
layout->addWidget(buttons[y][x], y, x);
608
qApp->setActiveWindow(&widget);
609
widget.activateWindow();
612
buttons[1][1]->setFocus();
614
QVERIFY(buttons[1][1]->hasFocus());
615
QTest::keyPress(buttons[1][1], Qt::Key_Up);
617
QVERIFY(buttons[0][1]->hasFocus());
618
QTest::keyPress(buttons[0][1], Qt::Key_Down);
620
QVERIFY(buttons[1][1]->hasFocus());
621
QTest::keyPress(buttons[1][1], Qt::Key_Left);
623
QVERIFY(buttons[1][0]->hasFocus());
624
QTest::keyPress(buttons[1][0], Qt::Key_Down);
626
QVERIFY(buttons[2][0]->hasFocus());
627
QTest::keyPress(buttons[2][0], Qt::Key_Right);
629
QVERIFY(buttons[2][1]->hasFocus());
630
QTest::keyPress(buttons[2][1], Qt::Key_Right);
632
QVERIFY(buttons[2][2]->hasFocus());
633
QTest::keyPress(buttons[2][2], Qt::Key_Up);
635
QVERIFY(buttons[1][2]->hasFocus());
636
QTest::keyPress(buttons[1][2], Qt::Key_Up);
638
QVERIFY(buttons[0][2]->hasFocus());
639
buttons[0][1]->hide();
640
QTest::keyPress(buttons[0][2], Qt::Key_Left);
642
QTest::keyPress(buttons[0][2], Qt::Key_Left);
643
QEXPECT_FAIL("", "QTBUG-22286" ,Abort);
644
QVERIFY(buttons[0][0]->hasFocus());
648
QTEST_MAIN(tst_QAbstractButton)
649
#include "tst_qabstractbutton.moc"