util/tests/auto/qcommandlinkbutton/tst_qcommandlinkbutton.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <QPainter>
       
    55 
       
    56 Q_DECLARE_METATYPE(QCommandLinkButton*)
       
    57 
       
    58 //TESTED_CLASS=
       
    59 //TESTED_FILES=
       
    60 
       
    61 class tst_QCommandLinkButton : public QObject
       
    62 {
       
    63 Q_OBJECT
       
    64 public:
       
    65     tst_QCommandLinkButton();
       
    66     virtual ~tst_QCommandLinkButton();
       
    67 
       
    68 
       
    69 public slots:
       
    70     void initTestCase();
       
    71     void cleanupTestCase();
       
    72     void init();
       
    73     void cleanup();
       
    74 private slots:
       
    75     void getSetCheck();
       
    76     void pressed();
       
    77     void setAccel();
       
    78     void isCheckable();
       
    79     void setDown();
       
    80     void popupCrash();
       
    81     void isChecked();
       
    82     void animateClick();
       
    83     void toggle();
       
    84     void clicked();
       
    85     void toggled();
       
    86     void defaultAndAutoDefault();
       
    87     void setAutoRepeat();
       
    88     void heightForWithWithIcon();
       
    89 
       
    90 protected slots:
       
    91     void resetCounters();
       
    92     void onClicked();
       
    93     void onToggled( bool on );
       
    94     void onPressed();
       
    95     void onReleased();
       
    96     void helperSlotDelete();
       
    97 
       
    98 private:
       
    99     uint click_count;
       
   100     uint toggle_count;
       
   101     uint press_count;
       
   102     uint release_count;
       
   103 
       
   104     QCommandLinkButton *testWidget;
       
   105 };
       
   106 
       
   107 // Testing get/set functions
       
   108 void tst_QCommandLinkButton::getSetCheck()
       
   109 {
       
   110     QCommandLinkButton obj1;
       
   111 
       
   112 	QString text("mytext");
       
   113 	QVERIFY(obj1.description().isEmpty());
       
   114 	obj1.setDescription(text);
       
   115 	QVERIFY(obj1.description() == text);
       
   116 
       
   117 	QVERIFY(obj1.text().isEmpty());
       
   118 	obj1.setText(text);
       
   119 	QVERIFY(obj1.text() == text);
       
   120 
       
   121 
       
   122     QMenu *var1 = new QMenu;
       
   123     obj1.setMenu(var1);
       
   124     QCOMPARE(var1, obj1.menu());
       
   125     obj1.setMenu((QMenu *)0);
       
   126     QCOMPARE((QMenu *)0, obj1.menu());
       
   127     delete var1;
       
   128 }
       
   129 
       
   130 tst_QCommandLinkButton::tst_QCommandLinkButton()
       
   131 {
       
   132 }
       
   133 
       
   134 tst_QCommandLinkButton::~tst_QCommandLinkButton()
       
   135 {
       
   136 }
       
   137 
       
   138 void tst_QCommandLinkButton::initTestCase()
       
   139 {
       
   140     // Create the test class
       
   141     testWidget = new QCommandLinkButton( "&Start", 0 );
       
   142     testWidget->setObjectName("testWidget");
       
   143     testWidget->resize( 200, 200 );
       
   144     testWidget->show();
       
   145 
       
   146     connect( testWidget, SIGNAL(clicked()), this, SLOT(onClicked()) );
       
   147     connect( testWidget, SIGNAL(pressed()), this, SLOT(onPressed()) );
       
   148     connect( testWidget, SIGNAL(released()), this, SLOT(onReleased()) );
       
   149     connect( testWidget, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)) );
       
   150 }
       
   151 
       
   152 void tst_QCommandLinkButton::cleanupTestCase()
       
   153 {
       
   154     delete testWidget;
       
   155     testWidget = 0;
       
   156 }
       
   157 
       
   158 void tst_QCommandLinkButton::init()
       
   159 {
       
   160     testWidget->setAutoRepeat( FALSE );
       
   161     testWidget->setDown( FALSE );
       
   162     testWidget->setText("Test");
       
   163     testWidget->setDescription("Description text.");
       
   164     testWidget->setEnabled( TRUE );
       
   165     QKeySequence seq;
       
   166     testWidget->setShortcut( seq );
       
   167 
       
   168     resetCounters();
       
   169 }
       
   170 
       
   171 void tst_QCommandLinkButton::cleanup()
       
   172 {
       
   173 }
       
   174 
       
   175 
       
   176 void tst_QCommandLinkButton::resetCounters()
       
   177 {
       
   178     toggle_count = 0;
       
   179     press_count = 0;
       
   180     release_count = 0;
       
   181     click_count = 0;
       
   182 }
       
   183 
       
   184 void tst_QCommandLinkButton::onClicked()
       
   185 {
       
   186     click_count++;
       
   187 }
       
   188 
       
   189 void tst_QCommandLinkButton::onToggled( bool /*on*/ )
       
   190 {
       
   191     toggle_count++;
       
   192 }
       
   193 
       
   194 void tst_QCommandLinkButton::onPressed()
       
   195 {
       
   196     press_count++;
       
   197 }
       
   198 
       
   199 void tst_QCommandLinkButton::onReleased()
       
   200 {
       
   201     release_count++;
       
   202 }
       
   203 
       
   204 void tst_QCommandLinkButton::setAutoRepeat()
       
   205 {
       
   206     // Give the last tests time to finish - i.e., wait for the window close
       
   207     // and deactivate to avoid a race condition here. We can't add this to the
       
   208     // end of the defaultAndAutoDefault test, since any failure in that test
       
   209     // will return out of that function.
       
   210     QTest::qWait( 1000 );
       
   211 
       
   212     // If this changes, this test must be completely revised.
       
   213     QVERIFY( !testWidget->isCheckable() );
       
   214 
       
   215     // verify autorepeat is off by default.
       
   216     QCommandLinkButton tmp( 0 );
       
   217     tmp.setObjectName("tmp");
       
   218     QVERIFY( !tmp.autoRepeat() );
       
   219 
       
   220     // check if we can toggle the mode
       
   221     testWidget->setAutoRepeat( TRUE );
       
   222     QVERIFY( testWidget->autoRepeat() );
       
   223 
       
   224     testWidget->setAutoRepeat( FALSE );
       
   225     QVERIFY( !testWidget->autoRepeat() );
       
   226 
       
   227     resetCounters();
       
   228 
       
   229     // check that the button is down if we press space and not in autorepeat
       
   230     testWidget->setDown( FALSE );
       
   231     testWidget->setAutoRepeat( FALSE );
       
   232     QTest::keyPress( testWidget, Qt::Key_Space );
       
   233 
       
   234     QTest::qWait( 300 );
       
   235 
       
   236     QVERIFY( testWidget->isDown() );
       
   237     QVERIFY( toggle_count == 0 );
       
   238     QVERIFY( press_count == 1 );
       
   239     QVERIFY( release_count == 0 );
       
   240     QVERIFY( click_count == 0 );
       
   241 
       
   242     QTest::keyRelease( testWidget, Qt::Key_Space );
       
   243     resetCounters();
       
   244 
       
   245     // check that the button is down if we press space while in autorepeat
       
   246     // we can't actually confirm how many times it is fired, more than 1 is enough.
       
   247 
       
   248     testWidget->setDown( FALSE );
       
   249     testWidget->setAutoRepeat( TRUE );
       
   250     QTest::keyPress( testWidget, Qt::Key_Space );
       
   251     QTest::qWait(900);
       
   252     QVERIFY( testWidget->isDown() );
       
   253     QVERIFY( toggle_count == 0 );
       
   254     QTest::keyRelease( testWidget, Qt::Key_Space );
       
   255     QVERIFY(press_count == release_count);
       
   256     QVERIFY(release_count == click_count);
       
   257     QVERIFY(press_count > 1);
       
   258 
       
   259     // #### shouldn't I check here to see if multiple signals have been fired???
       
   260 
       
   261     // check that pressing ENTER has no effect
       
   262     resetCounters();
       
   263     testWidget->setDown( FALSE );
       
   264     testWidget->setAutoRepeat( FALSE );
       
   265     QTest::keyPress( testWidget, Qt::Key_Enter );
       
   266 
       
   267     QTest::qWait( 300 );
       
   268 
       
   269     QVERIFY( !testWidget->isDown() );
       
   270     QVERIFY( toggle_count == 0 );
       
   271     QVERIFY( press_count == 0 );
       
   272     QVERIFY( release_count == 0 );
       
   273     QVERIFY( click_count == 0 );
       
   274     QTest::keyRelease( testWidget, Qt::Key_Enter );
       
   275 
       
   276     // check that pressing ENTER has no effect
       
   277     resetCounters();
       
   278     testWidget->setDown( FALSE );
       
   279     testWidget->setAutoRepeat( TRUE );
       
   280     QTest::keyClick( testWidget, Qt::Key_Enter );
       
   281     QTest::qWait( 300 );
       
   282     QVERIFY( !testWidget->isDown() );
       
   283     QVERIFY( toggle_count == 0 );
       
   284     QVERIFY( press_count == 0 );
       
   285     QVERIFY( release_count == 0 );
       
   286     QVERIFY( click_count == 0 );
       
   287 }
       
   288 
       
   289 void tst_QCommandLinkButton::pressed()
       
   290 {
       
   291     QTest::keyPress( testWidget, ' ' );
       
   292 //    QTest::qWait( 300 );
       
   293     QCOMPARE( press_count, (uint)1 );
       
   294     QCOMPARE( release_count, (uint)0 );
       
   295 
       
   296     QTest::keyRelease( testWidget, ' ' );
       
   297 //    QTest::qWait( 300 );
       
   298     QCOMPARE( press_count, (uint)1 );
       
   299     QCOMPARE( release_count, (uint)1 );
       
   300 
       
   301     QTest::keyPress( testWidget,Qt::Key_Enter );
       
   302 //    QTest::qWait( 300 );
       
   303     QCOMPARE( press_count, (uint)1 );
       
   304     QCOMPARE( release_count, (uint)1 );
       
   305 
       
   306     testWidget->setAutoDefault(true);
       
   307     QTest::keyPress( testWidget,Qt::Key_Enter );
       
   308 //    QTest::qWait( 300 );
       
   309     QCOMPARE( press_count, (uint)2 );
       
   310     QCOMPARE( release_count, (uint)2 );
       
   311     testWidget->setAutoDefault(false);
       
   312 
       
   313 }
       
   314 
       
   315 
       
   316 
       
   317 void tst_QCommandLinkButton::isCheckable()
       
   318 {
       
   319     QVERIFY( !testWidget->isCheckable() );
       
   320 }
       
   321 
       
   322 void tst_QCommandLinkButton::setDown()
       
   323 {
       
   324     testWidget->setDown( FALSE );
       
   325     QVERIFY( !testWidget->isDown() );
       
   326 
       
   327     testWidget->setDown( TRUE );
       
   328     QVERIFY( testWidget->isDown() );
       
   329 
       
   330     testWidget->setDown( TRUE );
       
   331     QTest::keyClick( testWidget, Qt::Key_Escape );
       
   332     QVERIFY( !testWidget->isDown() );
       
   333 }
       
   334 
       
   335 void tst_QCommandLinkButton::isChecked()
       
   336 {
       
   337     testWidget->setDown( FALSE );
       
   338     QVERIFY( !testWidget->isChecked() );
       
   339 
       
   340     testWidget->setDown( TRUE );
       
   341     QVERIFY( !testWidget->isChecked() );
       
   342 
       
   343     testWidget->setDown( FALSE );
       
   344     testWidget->toggle();
       
   345     QVERIFY( testWidget->isChecked() == testWidget->isCheckable() );
       
   346 }
       
   347 
       
   348 void tst_QCommandLinkButton::toggle()
       
   349 {
       
   350     // the pushbutton shouldn't toggle the button.
       
   351     testWidget->toggle();
       
   352     QVERIFY( testWidget->isChecked() == FALSE );
       
   353 }
       
   354 
       
   355 void tst_QCommandLinkButton::toggled()
       
   356 {
       
   357     // the pushbutton shouldn't send a toggled signal when we call the toggle slot.
       
   358     QVERIFY( !testWidget->isCheckable() );
       
   359 
       
   360     testWidget->toggle();
       
   361     QVERIFY( toggle_count == 0 );
       
   362 
       
   363     // do it again, just to be shure
       
   364     resetCounters();
       
   365     testWidget->toggle();
       
   366     QVERIFY( toggle_count == 0 );
       
   367 
       
   368     // finally check that we can toggle using the mouse
       
   369     resetCounters();
       
   370     QTest::mousePress( testWidget, Qt::LeftButton );
       
   371     QVERIFY( toggle_count == 0 );
       
   372     QVERIFY( click_count == 0 );
       
   373 
       
   374     QTest::mouseRelease( testWidget, Qt::LeftButton );
       
   375     QVERIFY( click_count == 1 );
       
   376 }
       
   377 
       
   378 /*
       
   379     If we press an accelerator key we ONLY get a pressed signal and
       
   380     NOT a released or clicked signal.
       
   381 */
       
   382 
       
   383 void tst_QCommandLinkButton::setAccel()
       
   384 {
       
   385     testWidget->setText("&AccelTest");
       
   386     QKeySequence seq( Qt::ALT + Qt::Key_A );
       
   387     testWidget->setShortcut( seq );
       
   388 
       
   389     // The shortcut will not be activated unless the button is in a active
       
   390     // window and has focus
       
   391     testWidget->setFocus();
       
   392     for (int i = 0; !testWidget->isActiveWindow() && i < 1000; ++i) {
       
   393         testWidget->activateWindow();
       
   394         QApplication::instance()->processEvents();
       
   395         QTest::qWait(100);
       
   396     }
       
   397 
       
   398     QVERIFY(testWidget->isActiveWindow());
       
   399 
       
   400     QTest::keyClick( testWidget, 'A', Qt::AltModifier );
       
   401     QTest::qWait( 500 );
       
   402     QVERIFY( click_count == 1 );
       
   403     QVERIFY( press_count == 1 );
       
   404     QVERIFY( release_count == 1 );
       
   405     QVERIFY( toggle_count == 0 );
       
   406 
       
   407     // wait 200 ms because setAccel uses animateClick.
       
   408     // if we don't wait this may screw up a next test.
       
   409     QTest::qWait(200);
       
   410 }
       
   411 
       
   412 void tst_QCommandLinkButton::animateClick()
       
   413 {
       
   414     QVERIFY( !testWidget->isDown() );
       
   415     testWidget->animateClick();
       
   416     QVERIFY( testWidget->isDown() );
       
   417     QTest::qWait( 200 );
       
   418     QVERIFY( !testWidget->isDown() );
       
   419 
       
   420     QVERIFY( click_count == 1 );
       
   421     QVERIFY( press_count == 1 );
       
   422     QVERIFY( release_count == 1 );
       
   423     QVERIFY( toggle_count == 0 );
       
   424 }
       
   425 
       
   426 void tst_QCommandLinkButton::clicked()
       
   427 {
       
   428     QTest::mousePress( testWidget, Qt::LeftButton );
       
   429     QVERIFY( press_count == 1 );
       
   430     QVERIFY( release_count == 0 );
       
   431 
       
   432     QTest::mouseRelease( testWidget, Qt::LeftButton );
       
   433     QCOMPARE( press_count, (uint)1 );
       
   434     QCOMPARE( release_count, (uint)1 );
       
   435 
       
   436     press_count = 0;
       
   437     release_count = 0;
       
   438     testWidget->setDown(FALSE);
       
   439     for (uint i=0; i<10; i++)
       
   440         QTest::mouseClick( testWidget, Qt::LeftButton );
       
   441     QCOMPARE( press_count, (uint)10 );
       
   442     QCOMPARE( release_count, (uint)10 );
       
   443 }
       
   444 
       
   445 QCommandLinkButton *pb = 0;
       
   446 void tst_QCommandLinkButton::helperSlotDelete()
       
   447 {
       
   448     delete pb;
       
   449     pb = 0;
       
   450 }
       
   451 
       
   452 void tst_QCommandLinkButton::popupCrash()
       
   453 {
       
   454     pb = new QCommandLinkButton("foo", "description");
       
   455     QMenu *menu = new QMenu("bar", pb);
       
   456     pb->setMenu(menu);
       
   457     QTimer::singleShot(1000, this, SLOT(helperSlotDelete()));
       
   458     pb->show();
       
   459     pb->click();
       
   460 }
       
   461 
       
   462 void tst_QCommandLinkButton::defaultAndAutoDefault()
       
   463 {
       
   464     {
       
   465     // Adding buttons directly to QDialog
       
   466     QDialog dialog;
       
   467 
       
   468     QCommandLinkButton button1(&dialog);
       
   469     QVERIFY(button1.autoDefault());
       
   470     QVERIFY(!button1.isDefault());
       
   471 
       
   472     QCommandLinkButton button2(&dialog);
       
   473     QVERIFY(button2.autoDefault());
       
   474     QVERIFY(!button2.isDefault());
       
   475 
       
   476     button1.setDefault(true);
       
   477     QVERIFY(button1.autoDefault());
       
   478     QVERIFY(button1.isDefault());
       
   479     QVERIFY(button2.autoDefault());
       
   480     QVERIFY(!button2.isDefault());
       
   481 
       
   482     dialog.show();
       
   483     QVERIFY(dialog.isVisible());
       
   484 
       
   485     QObject::connect(&button1, SIGNAL(clicked()), &dialog, SLOT(hide()));
       
   486     QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
       
   487     QApplication::sendEvent(&dialog, &event);
       
   488     QVERIFY(!dialog.isVisible());
       
   489     }
       
   490 
       
   491     {
       
   492     // Adding buttons to QDialog through a layout
       
   493     QDialog dialog;
       
   494 
       
   495 	QCommandLinkButton button3;
       
   496 	button3.setAutoDefault(false);
       
   497 
       
   498     QCommandLinkButton button1;
       
   499     QVERIFY(!button1.autoDefault());
       
   500     QVERIFY(!button1.isDefault());
       
   501 
       
   502     QCommandLinkButton button2;
       
   503     QVERIFY(!button2.autoDefault());
       
   504     QVERIFY(!button2.isDefault());
       
   505 
       
   506     button1.setDefault(true);
       
   507     QVERIFY(!button1.autoDefault());
       
   508     QVERIFY(button1.isDefault());
       
   509     QVERIFY(!button2.autoDefault());
       
   510     QVERIFY(!button2.isDefault());
       
   511 
       
   512     QGridLayout layout;
       
   513     layout.addWidget(&button3, 0, 3);
       
   514     layout.addWidget(&button2, 0, 2);
       
   515     layout.addWidget(&button1, 0, 1);
       
   516     dialog.setLayout(&layout);
       
   517 	button3.setFocus();
       
   518     QVERIFY(button1.autoDefault());
       
   519     QVERIFY(button1.isDefault());
       
   520     QVERIFY(button2.autoDefault());
       
   521     QVERIFY(!button2.isDefault());
       
   522 
       
   523     dialog.show();
       
   524     QVERIFY(dialog.isVisible());
       
   525 
       
   526     QObject::connect(&button1, SIGNAL(clicked()), &dialog, SLOT(hide()));
       
   527     QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
       
   528     QApplication::sendEvent(&dialog, &event);
       
   529     QVERIFY(!dialog.isVisible());
       
   530     }
       
   531 
       
   532     {
       
   533     // autoDefault behavior.
       
   534     QDialog dialog;
       
   535     QCommandLinkButton button2(&dialog);
       
   536     QCommandLinkButton button1(&dialog);
       
   537     dialog.show();
       
   538     QVERIFY(dialog.isVisible());
       
   539 
       
   540     // No default button is set, and button2 is the first autoDefault button
       
   541     // that is next in the tab order
       
   542     QObject::connect(&button2, SIGNAL(clicked()), &dialog, SLOT(hide()));
       
   543     QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
       
   544     QApplication::sendEvent(&dialog, &event);
       
   545     QVERIFY(!dialog.isVisible());
       
   546 
       
   547     // Reparenting
       
   548     QVERIFY(button2.autoDefault());
       
   549     button2.setParent(0);
       
   550     QVERIFY(!button2.autoDefault());
       
   551     button2.setAutoDefault(false);
       
   552     button2.setParent(&dialog);
       
   553     QVERIFY(!button2.autoDefault());
       
   554 
       
   555     button1.setAutoDefault(true);
       
   556     button1.setParent(0);
       
   557     QVERIFY(button1.autoDefault());
       
   558     }
       
   559 }
       
   560 
       
   561 void tst_QCommandLinkButton::heightForWithWithIcon()
       
   562 {
       
   563     QWidget mainWin;
       
   564 
       
   565     QPixmap pixmap(64, 64);
       
   566     {
       
   567         pixmap.fill(Qt::white);
       
   568         QPainter painter(&pixmap);
       
   569         painter.setBrush(Qt::black);
       
   570         painter.drawEllipse(0, 0, 63, 63);
       
   571     }
       
   572 
       
   573     QCommandLinkButton *largeIconButton = new QCommandLinkButton(QString("Large Icon"),
       
   574                     QString("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris nibh lectus, adipiscing eu."),
       
   575                     &mainWin);
       
   576     largeIconButton->setIconSize(QSize(64, 64));
       
   577     largeIconButton->setIcon(pixmap);
       
   578 
       
   579     QVBoxLayout *layout = new QVBoxLayout();
       
   580     layout->addWidget(largeIconButton);
       
   581     layout->addStretch();
       
   582     mainWin.setLayout(layout);
       
   583     mainWin.showMaximized();
       
   584     QTest::qWaitForWindowShown(&mainWin);
       
   585     QVERIFY(largeIconButton->height() > 68);  //enough room for the icon
       
   586 
       
   587 }
       
   588 
       
   589 QTEST_MAIN(tst_QCommandLinkButton)
       
   590 #include "tst_qcommandlinkbutton.moc"