tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 #include <QtTest/QtTest>
       
    42 #include <QtGui/QPushButton>
       
    43 #include <QtGui/QStyle>
       
    44 #include <QtGui/QLayout>
       
    45 #include <QtGui/QDialog>
       
    46 #include <QtGui/QAction>
       
    47 #include <qdialogbuttonbox.h>
       
    48 #include <limits.h>
       
    49 
       
    50 //TESTED_CLASS=
       
    51 //TESTED_FILES=
       
    52 
       
    53 Q_DECLARE_METATYPE(QList<int>)
       
    54 Q_DECLARE_METATYPE(QDialogButtonBox::ButtonRole)
       
    55 Q_DECLARE_METATYPE(QDialogButtonBox::StandardButton)
       
    56 Q_DECLARE_METATYPE(QDialogButtonBox::StandardButtons)
       
    57 Q_DECLARE_METATYPE(QAbstractButton*)
       
    58 
       
    59 class tst_QDialogButtonBox : public QObject
       
    60 {
       
    61     Q_OBJECT
       
    62 public:
       
    63     tst_QDialogButtonBox();
       
    64     ~tst_QDialogButtonBox();
       
    65 
       
    66 
       
    67 public slots:
       
    68     void buttonClicked1(QAbstractButton *);
       
    69     void acceptClicked();
       
    70     void rejectClicked();
       
    71     void helpRequestedClicked();
       
    72 
       
    73 private slots:
       
    74     void standardButtons();
       
    75     void testConstructor1();
       
    76     void testConstrurtor2();
       
    77     void testConstrurtor2_data();
       
    78     void testConstructor3();
       
    79     void testConstructor3_data();
       
    80     void setOrientation_data();
       
    81     void setOrientation();
       
    82     void addButton1_data();
       
    83     void addButton1();
       
    84     void addButton2_data();
       
    85     void addButton2();
       
    86     void addButton3_data();
       
    87     void addButton3();
       
    88     void clear_data();
       
    89     void clear();
       
    90     void removeButton_data();
       
    91     void removeButton();
       
    92     void buttonRole_data();
       
    93     void buttonRole();
       
    94     void setStandardButtons_data();
       
    95     void setStandardButtons();
       
    96     void layoutReuse();
       
    97 
       
    98 
       
    99     // Skip these tests, buttons is used in every test thus far.
       
   100 //    void buttons_data();
       
   101 //    void buttons();
       
   102 
       
   103     void testDelete();
       
   104     void testRemove();
       
   105     void testMultipleAdd();
       
   106     void testStandardButtonMapping_data();
       
   107     void testStandardButtonMapping();
       
   108     void testSignals_data();
       
   109     void testSignals();
       
   110     void testSignalOrder();
       
   111     void testDefaultButton_data();
       
   112     void testDefaultButton();
       
   113     void testS60SoftKeys();
       
   114 
       
   115     void task191642_default();
       
   116 private:
       
   117     qint64 timeStamp;
       
   118     qint64 buttonClicked1TimeStamp;
       
   119     qint64 acceptTimeStamp;
       
   120     qint64 rejectTimeStamp;
       
   121     qint64 helpRequestedTimeStamp;
       
   122 };
       
   123 
       
   124 tst_QDialogButtonBox::tst_QDialogButtonBox()
       
   125 {
       
   126 }
       
   127 
       
   128 tst_QDialogButtonBox::~tst_QDialogButtonBox()
       
   129 {
       
   130 }
       
   131 
       
   132 void tst_QDialogButtonBox::testConstructor1()
       
   133 {
       
   134     QDialogButtonBox buttonbox;
       
   135     QCOMPARE(buttonbox.orientation(), Qt::Horizontal);
       
   136 
       
   137     QCOMPARE(buttonbox.buttons().count(), 0);
       
   138 }
       
   139 
       
   140 void tst_QDialogButtonBox::layoutReuse()
       
   141 {
       
   142     QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok);
       
   143     QPointer<QLayout> layout = box->layout();
       
   144     box->setCenterButtons(!box->centerButtons());
       
   145     QVERIFY(layout == box->layout());
       
   146     QEvent event(QEvent::StyleChange);
       
   147     QApplication::sendEvent(box, &event);
       
   148     QVERIFY(layout == box->layout());
       
   149     box->setOrientation(box->orientation() == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal);
       
   150     QVERIFY(layout == 0);
       
   151     QVERIFY(layout != box->layout());
       
   152     delete box;
       
   153 }
       
   154 
       
   155 void tst_QDialogButtonBox::testConstrurtor2_data()
       
   156 {
       
   157     QTest::addColumn<int>("orientation");
       
   158 
       
   159     QTest::newRow("horizontal") << int(Qt::Horizontal);
       
   160     QTest::newRow("vertical") << int(Qt::Vertical);
       
   161 }
       
   162 
       
   163 void tst_QDialogButtonBox::testConstrurtor2()
       
   164 {
       
   165     QFETCH(int, orientation);
       
   166     Qt::Orientation orient = Qt::Orientation(orientation);
       
   167     QDialogButtonBox buttonBox(orient);
       
   168 
       
   169     QCOMPARE(buttonBox.orientation(), orient);
       
   170     QCOMPARE(buttonBox.buttons().count(), 0);
       
   171 }
       
   172 
       
   173 void tst_QDialogButtonBox::testConstructor3_data()
       
   174 {
       
   175     QTest::addColumn<int>("orientation");
       
   176     QTest::addColumn<QDialogButtonBox::StandardButtons>("buttons");
       
   177     QTest::addColumn<int>("buttonCount");
       
   178 
       
   179     QTest::newRow("nothing") << int(Qt::Horizontal) << (QDialogButtonBox::StandardButtons)0 << 0;
       
   180     QTest::newRow("only 1") << int(Qt::Horizontal) << QDialogButtonBox::StandardButtons(QDialogButtonBox::Ok) << 1;
       
   181     QTest::newRow("only 1.. twice") << int(Qt::Horizontal)
       
   182                         << (QDialogButtonBox::Ok | QDialogButtonBox::Ok)
       
   183                         << 1;
       
   184     QTest::newRow("only 2") << int(Qt::Horizontal)
       
   185             << (QDialogButtonBox::Ok | QDialogButtonBox::Cancel)
       
   186             << 2;
       
   187     QTest::newRow("two different things") << int(Qt::Horizontal)
       
   188             << (QDialogButtonBox::Save | QDialogButtonBox::Close)
       
   189             << 2;
       
   190     QTest::newRow("three") << int(Qt::Horizontal)
       
   191             << (QDialogButtonBox::Ok
       
   192                     | QDialogButtonBox::Cancel
       
   193                     | QDialogButtonBox::Help)
       
   194             << 3;
       
   195     QTest::newRow("everything") << int(Qt::Vertical)
       
   196             << (QDialogButtonBox::StandardButtons)UINT_MAX
       
   197             << 18;
       
   198 }
       
   199 
       
   200 void tst_QDialogButtonBox::testConstructor3()
       
   201 {
       
   202     QFETCH(int, orientation);
       
   203     QFETCH(QDialogButtonBox::StandardButtons, buttons);
       
   204 
       
   205     QDialogButtonBox buttonBox(buttons, (Qt::Orientation)orientation);
       
   206     QCOMPARE(int(buttonBox.orientation()), orientation);
       
   207     QTEST(buttonBox.buttons().count(), "buttonCount");
       
   208 }
       
   209 
       
   210 void tst_QDialogButtonBox::setOrientation_data()
       
   211 {
       
   212     QTest::addColumn<int>("orientation");
       
   213 
       
   214     QTest::newRow("Horizontal") << int(Qt::Horizontal);
       
   215     QTest::newRow("Vertical") << int(Qt::Vertical);
       
   216 }
       
   217 
       
   218 void tst_QDialogButtonBox::setOrientation()
       
   219 {
       
   220     QFETCH(int, orientation);
       
   221     QDialogButtonBox buttonBox;
       
   222     QCOMPARE(int(buttonBox.orientation()), int(Qt::Horizontal));
       
   223 
       
   224     buttonBox.setOrientation(Qt::Orientation(orientation));
       
   225     QCOMPARE(int(buttonBox.orientation()), orientation);
       
   226 }
       
   227 
       
   228 /*
       
   229 void tst_QDialogButtonBox::setLayoutPolicy_data()
       
   230 {
       
   231     QTest::addColumn<int>("layoutPolicy");
       
   232 
       
   233     QTest::newRow("win") << int(QDialogButtonBox::WinLayout);
       
   234     QTest::newRow("mac") << int(QDialogButtonBox::MacLayout);
       
   235     QTest::newRow("kde") << int(QDialogButtonBox::KdeLayout);
       
   236     QTest::newRow("gnome") << int(QDialogButtonBox::GnomeLayout);
       
   237 
       
   238 }
       
   239 
       
   240 void tst_QDialogButtonBox::setLayoutPolicy()
       
   241 {
       
   242     QFETCH(int, layoutPolicy);
       
   243 
       
   244     QDialogButtonBox buttonBox;
       
   245     QCOMPARE(int(buttonBox.layoutPolicy()),
       
   246              int(buttonBox.style()->styleHint(QStyle::SH_DialogButtonLayout)));
       
   247     buttonBox.setLayoutPolicy(QDialogButtonBox::ButtonLayout(layoutPolicy));
       
   248     QCOMPARE(int(buttonBox.layoutPolicy()), layoutPolicy);
       
   249 }
       
   250 */
       
   251 
       
   252 void tst_QDialogButtonBox::addButton1_data()
       
   253 {
       
   254     QTest::addColumn<QDialogButtonBox::ButtonRole>("role");
       
   255     QTest::addColumn<int>("totalCount");
       
   256 
       
   257     QTest::newRow("InvalidRole") << QDialogButtonBox::InvalidRole << 0;
       
   258     QTest::newRow("AcceptRole") << QDialogButtonBox::AcceptRole << 1;
       
   259     QTest::newRow("RejectRole") << QDialogButtonBox::RejectRole << 1;
       
   260     QTest::newRow("DestructiveRole") << QDialogButtonBox::DestructiveRole << 1;
       
   261     QTest::newRow("ActionRole") << QDialogButtonBox::ActionRole << 1;
       
   262     QTest::newRow("HelpRole") << QDialogButtonBox::HelpRole << 1;
       
   263     QTest::newRow("WackyValue") << (QDialogButtonBox::ButtonRole)-1 << 0;
       
   264 }
       
   265 
       
   266 void tst_QDialogButtonBox::addButton1()
       
   267 {
       
   268     QFETCH(QDialogButtonBox::ButtonRole, role);
       
   269     QDialogButtonBox buttonBox;
       
   270     QCOMPARE(buttonBox.buttons().count(), 0);
       
   271     QPushButton *button = new QPushButton();
       
   272     buttonBox.addButton(button, role);
       
   273     QTEST(buttonBox.buttons().count(), "totalCount");
       
   274     QList<QAbstractButton *> children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   275     QTEST(children.count(), "totalCount");
       
   276     delete button;
       
   277 }
       
   278 
       
   279 void tst_QDialogButtonBox::addButton2_data()
       
   280 {
       
   281     QTest::addColumn<QString>("text");
       
   282     QTest::addColumn<QDialogButtonBox::ButtonRole>("role");
       
   283     QTest::addColumn<int>("totalCount");
       
   284     QTest::newRow("InvalidRole") << QString("foo") << QDialogButtonBox::InvalidRole << 0;
       
   285     QTest::newRow("AcceptRole") << QString("foo") << QDialogButtonBox::AcceptRole << 1;
       
   286     QTest::newRow("RejectRole") << QString("foo") << QDialogButtonBox::RejectRole << 1;
       
   287     QTest::newRow("DestructiveRole") << QString("foo") << QDialogButtonBox::DestructiveRole << 1;
       
   288     QTest::newRow("ActionRole") << QString("foo") << QDialogButtonBox::ActionRole << 1;
       
   289     QTest::newRow("HelpRole") << QString("foo") << QDialogButtonBox::HelpRole << 1;
       
   290     QTest::newRow("WackyValue") << QString("foo") << (QDialogButtonBox::ButtonRole)-1 << 0;
       
   291 }
       
   292 
       
   293 void tst_QDialogButtonBox::addButton2()
       
   294 {
       
   295     QFETCH(QString, text);
       
   296     QFETCH(QDialogButtonBox::ButtonRole, role);
       
   297     QDialogButtonBox buttonBox;
       
   298     QCOMPARE(buttonBox.buttons().count(), 0);
       
   299     buttonBox.addButton(text, role);
       
   300     QTEST(buttonBox.buttons().count(), "totalCount");
       
   301     QList<QAbstractButton *> children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   302     QTEST(children.count(), "totalCount");
       
   303 }
       
   304 
       
   305 void tst_QDialogButtonBox::addButton3_data()
       
   306 {
       
   307     QTest::addColumn<QDialogButtonBox::StandardButton>("button");
       
   308     QTest::addColumn<int>("totalCount");
       
   309     QTest::newRow("Ok") << QDialogButtonBox::Ok << 1;
       
   310     QTest::newRow("Open") << QDialogButtonBox::Open << 1;
       
   311     QTest::newRow("Save") << QDialogButtonBox::Save << 1;
       
   312     QTest::newRow("Cancel") << QDialogButtonBox::Cancel << 1;
       
   313     QTest::newRow("Close") << QDialogButtonBox::Close << 1;
       
   314     QTest::newRow("Discard") << QDialogButtonBox::Discard << 1;
       
   315     QTest::newRow("Apply") << QDialogButtonBox::Apply << 1;
       
   316     QTest::newRow("Reset") << QDialogButtonBox::Reset << 1;
       
   317     QTest::newRow("Help") << QDialogButtonBox::Help << 1;
       
   318     QTest::newRow("noButton") << (QDialogButtonBox::StandardButton)0 << 0;
       
   319 }
       
   320 
       
   321 void tst_QDialogButtonBox::addButton3()
       
   322 {
       
   323     QFETCH(QDialogButtonBox::StandardButton, button);
       
   324     QDialogButtonBox buttonBox;
       
   325     QCOMPARE(buttonBox.buttons().count(), 0);
       
   326     buttonBox.addButton(button);
       
   327     QTEST(buttonBox.buttons().count(), "totalCount");
       
   328     QList<QAbstractButton *> children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   329     QTEST(children.count(), "totalCount");
       
   330 }
       
   331 
       
   332 void tst_QDialogButtonBox::clear_data()
       
   333 {
       
   334     QTest::addColumn<int>("rolesToAdd");
       
   335 
       
   336     QTest::newRow("nothing") << 0;
       
   337     QTest::newRow("one") << 1;
       
   338     QTest::newRow("all") << int(QDialogButtonBox::NRoles);
       
   339 }
       
   340 
       
   341 void tst_QDialogButtonBox::clear()
       
   342 {
       
   343     QFETCH(int, rolesToAdd);
       
   344 
       
   345     QDialogButtonBox buttonBox;
       
   346     for (int i = 1; i < rolesToAdd; ++i)
       
   347         buttonBox.addButton("Happy", QDialogButtonBox::ButtonRole(i));
       
   348     buttonBox.clear();
       
   349     QCOMPARE(buttonBox.buttons().count(), 0);
       
   350     QList<QAbstractButton *> children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   351     QCOMPARE(children.count(), 0);
       
   352 }
       
   353 
       
   354 void tst_QDialogButtonBox::removeButton_data()
       
   355 {
       
   356     QTest::addColumn<QDialogButtonBox::ButtonRole>("roleToAdd");
       
   357     QTest::addColumn<int>("expectedCount");
       
   358     QTest::newRow("no button added") << QDialogButtonBox::InvalidRole << 0;
       
   359     QTest::newRow("a button") << QDialogButtonBox::AcceptRole << 1;
       
   360 }
       
   361 
       
   362 void tst_QDialogButtonBox::removeButton()
       
   363 {
       
   364     QFETCH(QDialogButtonBox::ButtonRole, roleToAdd);
       
   365 
       
   366     QDialogButtonBox buttonBox;
       
   367     QCOMPARE(buttonBox.buttons().count(), 0);
       
   368     QPushButton *button = new QPushButton("RemoveButton test");
       
   369     buttonBox.addButton(button, roleToAdd);
       
   370     QTEST(buttonBox.buttons().count(), "expectedCount");
       
   371 
       
   372     buttonBox.removeButton(button);
       
   373     QCOMPARE(buttonBox.buttons().count(), 0);
       
   374     delete button;
       
   375 }
       
   376 
       
   377 void tst_QDialogButtonBox::testDelete()
       
   378 {
       
   379     QDialogButtonBox buttonBox;
       
   380     QCOMPARE(buttonBox.buttons().count(), 0);
       
   381 
       
   382     QPushButton *deleteMe = new QPushButton("Happy");
       
   383     buttonBox.addButton(deleteMe, QDialogButtonBox::HelpRole);
       
   384     QCOMPARE(buttonBox.buttons().count(), 1);
       
   385     QList<QAbstractButton *> children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   386     QCOMPARE(children.count(), 1);
       
   387 
       
   388     delete deleteMe;
       
   389     children = qFindChildren<QAbstractButton *>(&buttonBox);
       
   390     QCOMPARE(children.count(), 0);
       
   391     QCOMPARE(buttonBox.buttons().count(), 0);
       
   392 }
       
   393 
       
   394 void tst_QDialogButtonBox::testMultipleAdd()
       
   395 {
       
   396     // Add a button into the thing multiple times.
       
   397     QDialogButtonBox buttonBox;
       
   398     QCOMPARE(buttonBox.buttons().count(), 0);
       
   399 
       
   400     QPushButton *button = new QPushButton("Foo away");
       
   401     buttonBox.addButton(button, QDialogButtonBox::AcceptRole);
       
   402     QCOMPARE(buttonBox.buttons().count(), 1);
       
   403     QCOMPARE(buttonBox.buttonRole(button), QDialogButtonBox::AcceptRole);
       
   404     buttonBox.addButton(button, QDialogButtonBox::AcceptRole);
       
   405     QCOMPARE(buttonBox.buttons().count(), 1);
       
   406     QCOMPARE(buttonBox.buttonRole(button), QDialogButtonBox::AcceptRole);
       
   407 
       
   408     // Add it again with a different role
       
   409     buttonBox.addButton(button, QDialogButtonBox::RejectRole);
       
   410     QCOMPARE(buttonBox.buttons().count(), 1);
       
   411     QCOMPARE(buttonBox.buttonRole(button), QDialogButtonBox::RejectRole);
       
   412 
       
   413     // Add it as an "invalid" role
       
   414     buttonBox.addButton(button, QDialogButtonBox::InvalidRole);
       
   415     QCOMPARE(buttonBox.buttons().count(), 1);
       
   416     QCOMPARE(buttonBox.buttonRole(button), QDialogButtonBox::RejectRole);
       
   417 }
       
   418 
       
   419 void tst_QDialogButtonBox::buttonRole_data()
       
   420 {
       
   421     QTest::addColumn<QDialogButtonBox::ButtonRole>("roleToAdd");
       
   422     QTest::addColumn<QDialogButtonBox::ButtonRole>("expectedRole");
       
   423 
       
   424     QTest::newRow("AcceptRole stuff") << QDialogButtonBox::AcceptRole
       
   425                                       << QDialogButtonBox::AcceptRole;
       
   426     QTest::newRow("Nothing") << QDialogButtonBox::InvalidRole << QDialogButtonBox::InvalidRole;
       
   427     QTest::newRow("bad stuff") << (QDialogButtonBox::ButtonRole)-1 << QDialogButtonBox::InvalidRole;
       
   428 }
       
   429 
       
   430 void tst_QDialogButtonBox::buttonRole()
       
   431 {
       
   432     QFETCH(QDialogButtonBox::ButtonRole, roleToAdd);
       
   433     QDialogButtonBox buttonBox;
       
   434     QAbstractButton *button = buttonBox.addButton("Here's a button", roleToAdd);
       
   435     QTEST(buttonBox.buttonRole(button), "expectedRole");
       
   436 }
       
   437 
       
   438 void tst_QDialogButtonBox::testStandardButtonMapping_data()
       
   439 {
       
   440     QTest::addColumn<QDialogButtonBox::StandardButton>("button");
       
   441     QTest::addColumn<QDialogButtonBox::ButtonRole>("expectedRole");
       
   442     QTest::addColumn<QString>("expectedText");
       
   443 
       
   444     int layoutPolicy = qApp->style()->styleHint(QStyle::SH_DialogButtonLayout);
       
   445 
       
   446     QTest::newRow("QDialogButtonBox::Ok") << QDialogButtonBox::Ok
       
   447                                           << QDialogButtonBox::AcceptRole
       
   448                                           << QDialogButtonBox::tr("OK");
       
   449     QTest::newRow("QDialogButtonBox::Open") << QDialogButtonBox::Open
       
   450                                             << QDialogButtonBox::AcceptRole
       
   451                                             << QDialogButtonBox::tr("Open");
       
   452     QTest::newRow("QDialogButtonBox::Save") << QDialogButtonBox::Save
       
   453                                             << QDialogButtonBox::AcceptRole
       
   454                                             << QDialogButtonBox::tr("Save");
       
   455     QTest::newRow("QDialogButtonBox::Cancel") << QDialogButtonBox::Cancel
       
   456                                               << QDialogButtonBox::RejectRole
       
   457                                               << QDialogButtonBox::tr("Cancel");
       
   458     QTest::newRow("QDialogButtonBox::Close") << QDialogButtonBox::Close
       
   459                                              << QDialogButtonBox::RejectRole
       
   460                                              << QDialogButtonBox::tr("Close");
       
   461     if (layoutPolicy == QDialogButtonBox::MacLayout) {
       
   462         QTest::newRow("QDialogButtonBox::Discard") << QDialogButtonBox::Discard
       
   463                                                     << QDialogButtonBox::DestructiveRole
       
   464                                                     << QDialogButtonBox::tr("Don't Save");
       
   465     } else if (layoutPolicy == QDialogButtonBox::GnomeLayout) {
       
   466         QTest::newRow("QDialogButtonBox::Discard")
       
   467             << QDialogButtonBox::Discard
       
   468             << QDialogButtonBox::DestructiveRole
       
   469             << QDialogButtonBox::tr("Close without Saving");
       
   470     } else {
       
   471         QTest::newRow("QDialogButtonBox::Discard") << QDialogButtonBox::Discard
       
   472                                                     << QDialogButtonBox::DestructiveRole
       
   473                                                     << QDialogButtonBox::tr("Discard");
       
   474     }
       
   475     QTest::newRow("QDialogButtonBox::Apply") << QDialogButtonBox::Apply
       
   476                                              << QDialogButtonBox::ApplyRole
       
   477                                              << QDialogButtonBox::tr("Apply");
       
   478     QTest::newRow("QDialogButtonBox::Reset") << QDialogButtonBox::Reset
       
   479                                              << QDialogButtonBox::ResetRole
       
   480                                              << QDialogButtonBox::tr("Reset");
       
   481     QTest::newRow("QDialogButtonBox::Help") << QDialogButtonBox::Help
       
   482                                             << QDialogButtonBox::HelpRole
       
   483                                             << QDialogButtonBox::tr("Help");
       
   484 }
       
   485 
       
   486 void tst_QDialogButtonBox::testStandardButtonMapping()
       
   487 {
       
   488     QFETCH(QDialogButtonBox::StandardButton, button);
       
   489     QDialogButtonBox buttonBox;
       
   490 
       
   491     QAbstractButton *theButton = buttonBox.addButton(button);
       
   492     QTEST(buttonBox.buttonRole(theButton), "expectedRole");
       
   493     QString textWithoutMnemonic = theButton->text().remove("&");
       
   494     QTEST(textWithoutMnemonic, "expectedText");
       
   495 }
       
   496 
       
   497 void tst_QDialogButtonBox::testSignals_data()
       
   498 {
       
   499     QTest::addColumn<QDialogButtonBox::ButtonRole>("buttonToClick");
       
   500     QTest::addColumn<int>("clicked2Count");
       
   501     QTest::addColumn<int>("acceptCount");
       
   502     QTest::addColumn<int>("rejectCount");
       
   503     QTest::addColumn<int>("helpRequestedCount");
       
   504 
       
   505     QTest::newRow("nothing") << QDialogButtonBox::InvalidRole << 0 << 0 << 0 << 0;
       
   506     QTest::newRow("accept") << QDialogButtonBox::AcceptRole << 1 << 1 << 0 << 0;
       
   507     QTest::newRow("reject") << QDialogButtonBox::RejectRole << 1 << 0 << 1 << 0;
       
   508     QTest::newRow("destructive") << QDialogButtonBox::DestructiveRole << 1 << 0 << 0 << 0;
       
   509     QTest::newRow("Action") << QDialogButtonBox::ActionRole << 1 << 0 << 0 << 0;
       
   510     QTest::newRow("Help") << QDialogButtonBox::HelpRole << 1 << 0 << 0 << 1;
       
   511 }
       
   512 
       
   513 void tst_QDialogButtonBox::testSignals()
       
   514 {
       
   515     QFETCH(QDialogButtonBox::ButtonRole, buttonToClick);
       
   516     QDialogButtonBox buttonBox;
       
   517     qRegisterMetaType<QAbstractButton *>("QAbstractButton*");
       
   518     QSignalSpy clicked2(&buttonBox, SIGNAL(clicked(QAbstractButton*)));
       
   519     QSignalSpy accept(&buttonBox, SIGNAL(accepted()));
       
   520     QSignalSpy reject(&buttonBox, SIGNAL(rejected()));
       
   521     QSignalSpy helpRequested(&buttonBox, SIGNAL(helpRequested()));
       
   522 
       
   523     QPushButton *clickMe = 0;
       
   524     for (int i = QDialogButtonBox::AcceptRole; i < QDialogButtonBox::NRoles; ++i) {
       
   525         QPushButton *button = buttonBox.addButton(QString::number(i),
       
   526                                                   QDialogButtonBox::ButtonRole(i));
       
   527 
       
   528         if (i == buttonToClick)
       
   529             clickMe = button;
       
   530     }
       
   531     if (clickMe) {
       
   532         clickMe->animateClick(0);
       
   533         QTest::qWait(100);
       
   534     }
       
   535 
       
   536     QTEST(clicked2.count(), "clicked2Count");
       
   537     if (clicked2.count() > 0)
       
   538         QCOMPARE(qvariant_cast<QAbstractButton *>(clicked2.at(0).at(0)), (QAbstractButton *)clickMe);
       
   539 
       
   540     QTEST(accept.count(), "acceptCount");
       
   541     QTEST(reject.count(), "rejectCount");
       
   542     QTEST(helpRequested.count(), "helpRequestedCount");
       
   543 }
       
   544 
       
   545 void tst_QDialogButtonBox::testSignalOrder()
       
   546 {
       
   547     const qint64 longLongZero = 0;
       
   548     buttonClicked1TimeStamp = acceptTimeStamp
       
   549             = rejectTimeStamp = helpRequestedTimeStamp = timeStamp = 0;
       
   550     QDialogButtonBox buttonBox;
       
   551     connect(&buttonBox, SIGNAL(clicked(QAbstractButton *)),
       
   552             this, SLOT(buttonClicked1(QAbstractButton *)));
       
   553     connect(&buttonBox, SIGNAL(accepted()), this, SLOT(acceptClicked()));
       
   554     connect(&buttonBox, SIGNAL(rejected()), this, SLOT(rejectClicked()));
       
   555     connect(&buttonBox, SIGNAL(helpRequested()), this, SLOT(helpRequestedClicked()));
       
   556 
       
   557     QPushButton *acceptButton = buttonBox.addButton("OK", QDialogButtonBox::AcceptRole);
       
   558     QPushButton *rejectButton = buttonBox.addButton("Cancel", QDialogButtonBox::RejectRole);
       
   559     QPushButton *actionButton = buttonBox.addButton("Action This", QDialogButtonBox::ActionRole);
       
   560     QPushButton *helpButton = buttonBox.addButton("Help Me!", QDialogButtonBox::HelpRole);
       
   561 
       
   562     // Try accept
       
   563     acceptButton->animateClick(0);
       
   564     QTest::qWait(100);
       
   565     QCOMPARE(rejectTimeStamp, longLongZero);
       
   566     QCOMPARE(helpRequestedTimeStamp, longLongZero);
       
   567 
       
   568     QVERIFY(buttonClicked1TimeStamp < acceptTimeStamp);
       
   569     acceptTimeStamp = 0;
       
   570 
       
   571     rejectButton->animateClick(0);
       
   572     QTest::qWait(100);
       
   573     QCOMPARE(acceptTimeStamp, longLongZero);
       
   574     QCOMPARE(helpRequestedTimeStamp, longLongZero);
       
   575     QVERIFY(buttonClicked1TimeStamp < rejectTimeStamp);
       
   576 
       
   577     rejectTimeStamp = 0;
       
   578     actionButton->animateClick(0);
       
   579     QTest::qWait(100);
       
   580     QCOMPARE(acceptTimeStamp, longLongZero);
       
   581     QCOMPARE(rejectTimeStamp, longLongZero);
       
   582     QCOMPARE(helpRequestedTimeStamp, longLongZero);
       
   583 
       
   584     helpButton->animateClick(0);
       
   585     QTest::qWait(100);
       
   586     QCOMPARE(acceptTimeStamp, longLongZero);
       
   587     QCOMPARE(rejectTimeStamp, longLongZero);
       
   588     QVERIFY(buttonClicked1TimeStamp < helpRequestedTimeStamp);
       
   589 }
       
   590 
       
   591 void tst_QDialogButtonBox::buttonClicked1(QAbstractButton *)
       
   592 {
       
   593     buttonClicked1TimeStamp = ++timeStamp;
       
   594 }
       
   595 
       
   596 void tst_QDialogButtonBox::acceptClicked()
       
   597 {
       
   598     acceptTimeStamp = ++timeStamp;
       
   599 }
       
   600 
       
   601 void tst_QDialogButtonBox::rejectClicked()
       
   602 {
       
   603     rejectTimeStamp = ++timeStamp;
       
   604 }
       
   605 
       
   606 void tst_QDialogButtonBox::helpRequestedClicked()
       
   607 {
       
   608     helpRequestedTimeStamp = ++timeStamp;
       
   609 }
       
   610 
       
   611 void tst_QDialogButtonBox::setStandardButtons_data()
       
   612 {
       
   613     QTest::addColumn<QDialogButtonBox::StandardButtons>("buttonsToAdd");
       
   614     QTest::addColumn<QDialogButtonBox::StandardButtons>("expectedResult");
       
   615 
       
   616     QTest::newRow("Nothing") << QDialogButtonBox::StandardButtons(QDialogButtonBox::NoButton)
       
   617                              << QDialogButtonBox::StandardButtons(QDialogButtonBox::NoButton);
       
   618     QTest::newRow("Everything") << (QDialogButtonBox::StandardButtons)0xffffffff
       
   619                                 << (QDialogButtonBox::Ok
       
   620                                    | QDialogButtonBox::Open
       
   621                                    | QDialogButtonBox::Save
       
   622                                    | QDialogButtonBox::Cancel
       
   623                                    | QDialogButtonBox::Close
       
   624                                    | QDialogButtonBox::Discard
       
   625                                    | QDialogButtonBox::Apply
       
   626                                    | QDialogButtonBox::Reset
       
   627                                    | QDialogButtonBox::Help
       
   628                                    | QDialogButtonBox::Yes
       
   629                                    | QDialogButtonBox::YesToAll
       
   630                                    | QDialogButtonBox::No
       
   631                                    | QDialogButtonBox::NoToAll
       
   632                                    | QDialogButtonBox::SaveAll
       
   633                                    | QDialogButtonBox::Abort
       
   634                                    | QDialogButtonBox::Retry
       
   635                                    | QDialogButtonBox::Ignore
       
   636                                    | QDialogButtonBox::RestoreDefaults
       
   637                                    );
       
   638     QTest::newRow("Simple thing") << QDialogButtonBox::StandardButtons(QDialogButtonBox::Help)
       
   639                                   << QDialogButtonBox::StandardButtons(QDialogButtonBox::Help);
       
   640     QTest::newRow("Standard thing") << (QDialogButtonBox::Ok | QDialogButtonBox::Cancel)
       
   641                                     << (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   642 }
       
   643 
       
   644 void tst_QDialogButtonBox::setStandardButtons()
       
   645 {
       
   646     QFETCH(QDialogButtonBox::StandardButtons, buttonsToAdd);
       
   647     QDialogButtonBox buttonBox;
       
   648     buttonBox.setStandardButtons(buttonsToAdd);
       
   649     QTEST(buttonBox.standardButtons(), "expectedResult");
       
   650 }
       
   651 
       
   652 void tst_QDialogButtonBox::standardButtons()
       
   653 {
       
   654     // Test various cases of setting StandardButtons
       
   655     QDialogButtonBox buttonBox;
       
   656 
       
   657     QCOMPARE(buttonBox.standardButtons(),
       
   658              QDialogButtonBox::StandardButtons(QDialogButtonBox::NoButton));
       
   659 
       
   660     // Set some buttons
       
   661     buttonBox.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   662     QCOMPARE(buttonBox.standardButtons(), QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   663 
       
   664     // Now add a button
       
   665     buttonBox.addButton(QDialogButtonBox::Apply);
       
   666     QCOMPARE(buttonBox.standardButtons(),
       
   667              QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
       
   668 
       
   669     // Set the standard buttons to other things
       
   670     buttonBox.setStandardButtons(QDialogButtonBox::Save | QDialogButtonBox::Cancel
       
   671                                  | QDialogButtonBox::Discard);
       
   672     QCOMPARE(buttonBox.standardButtons(), QDialogButtonBox::Save | QDialogButtonBox::Cancel
       
   673                                           | QDialogButtonBox::Discard);
       
   674     QCOMPARE(buttonBox.buttons().size(), 3);
       
   675 
       
   676     // Add another button (not a standard one)
       
   677     buttonBox.addButton(QLatin1String("Help"), QDialogButtonBox::HelpRole);
       
   678     QCOMPARE(buttonBox.standardButtons(), QDialogButtonBox::Save | QDialogButtonBox::Cancel
       
   679                                           | QDialogButtonBox::Discard);
       
   680     QCOMPARE(buttonBox.buttons().size(), 4);
       
   681 
       
   682     // Finally, check if we construct with standard buttons that they show up.
       
   683     QDialogButtonBox buttonBox2(QDialogButtonBox::Open | QDialogButtonBox::Reset);
       
   684     QCOMPARE(buttonBox2.standardButtons(), QDialogButtonBox::Open | QDialogButtonBox::Reset);
       
   685 }
       
   686 
       
   687 void tst_QDialogButtonBox::testRemove()
       
   688 {
       
   689     // Make sure that removing a button and clicking it, doesn't trigger any latent signals
       
   690     QDialogButtonBox buttonBox;
       
   691     QSignalSpy clicked(&buttonBox, SIGNAL(clicked(QAbstractButton*)));
       
   692 
       
   693     QAbstractButton *button = buttonBox.addButton(QDialogButtonBox::Ok);
       
   694 
       
   695     buttonBox.removeButton(button);
       
   696 
       
   697     button->animateClick(0);
       
   698     QTest::qWait(100);
       
   699     QCOMPARE(clicked.count(), 0);
       
   700     delete button;
       
   701 }
       
   702 
       
   703 void tst_QDialogButtonBox::testDefaultButton_data()
       
   704 {
       
   705     QTest::addColumn<int>("whenToSetDefault");  // -1 Do nothing, 0 after accept, 1 before accept
       
   706     QTest::addColumn<int>("buttonToBeDefault");
       
   707     QTest::addColumn<int>("indexThatIsDefault");
       
   708 
       
   709     QTest::newRow("do nothing First Accept implicit") << -1 << -1 << 0;
       
   710     QTest::newRow("First accept explicit before add") << 1 << 0 << 0;
       
   711     QTest::newRow("First accept explicit after add") << 0 << 0 << 0;
       
   712     QTest::newRow("second accept explicit before add") << 1 << 1 << 1;
       
   713     QTest::newRow("second accept explicit after add") << 0 << 1 << 1;
       
   714     QTest::newRow("third accept explicit befare add") << 1 << 2 << 2;
       
   715     QTest::newRow("third accept explicit after add") << 0 << 2 << 2;
       
   716 }
       
   717 
       
   718 void tst_QDialogButtonBox::testS60SoftKeys()
       
   719 {
       
   720 #ifdef Q_WS_S60
       
   721     QDialog dialog(0);
       
   722     QDialogButtonBox buttonBox(&dialog);
       
   723     buttonBox.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
       
   724     dialog.show();
       
   725     
       
   726     int softkeyCount = 0;
       
   727     QList<QAction *> actions = dialog.actions();
       
   728     foreach (QAction *action, actions) {
       
   729         if (action->softKeyRole() != QAction::NoSoftKey)
       
   730             softkeyCount++;
       
   731     }
       
   732     QCOMPARE( softkeyCount, 2);
       
   733 
       
   734     QDialog dialog2(0);
       
   735     QDialogButtonBox buttonBox2(&dialog2);
       
   736     buttonBox2.setStandardButtons(QDialogButtonBox::Cancel);
       
   737     dialog2.show();
       
   738 
       
   739     int softkeyCount2 = 0;
       
   740     QList<QAction *> actions2 = dialog2.actions();
       
   741     foreach (QAction *action, actions2) {
       
   742         if (action->softKeyRole() != QAction::NoSoftKey)
       
   743             softkeyCount2++;
       
   744     }
       
   745     QCOMPARE( softkeyCount2, 1);
       
   746     
       
   747 #else
       
   748     QSKIP("S60-specific test", SkipAll );
       
   749 #endif
       
   750 }
       
   751 
       
   752 void tst_QDialogButtonBox::testDefaultButton()
       
   753 {
       
   754     QFETCH(int, whenToSetDefault);
       
   755     QFETCH(int, buttonToBeDefault);
       
   756     QFETCH(int, indexThatIsDefault);
       
   757     QDialogButtonBox buttonBox;
       
   758     QPushButton *buttonArray[] = { new QPushButton("Foo"), new QPushButton("Bar"), new QPushButton("Baz") };
       
   759 
       
   760     for (int i = 0; i < 3; ++i) {
       
   761         if (whenToSetDefault == 1 && i == buttonToBeDefault)
       
   762             buttonArray[i]->setDefault(true);
       
   763         buttonBox.addButton(buttonArray[i], QDialogButtonBox::AcceptRole);
       
   764         if (whenToSetDefault == 0 && i == buttonToBeDefault)
       
   765             buttonArray[i]->setDefault(true);
       
   766     }
       
   767     buttonBox.show();
       
   768 
       
   769     for (int i = 0; i < 3; ++i) {
       
   770         if (i == indexThatIsDefault)
       
   771             QVERIFY(buttonArray[i]->isDefault());
       
   772         else
       
   773             QVERIFY(!buttonArray[i]->isDefault());
       
   774     }
       
   775 }
       
   776 
       
   777 void tst_QDialogButtonBox::task191642_default()
       
   778 {
       
   779     QDialog dlg;
       
   780     QPushButton *def = new QPushButton(&dlg);
       
   781     QSignalSpy clicked(def, SIGNAL(clicked(bool)));
       
   782     def->setDefault(true);
       
   783     QDialogButtonBox *bb = new QDialogButtonBox(&dlg);
       
   784     bb->addButton(QDialogButtonBox::Ok);
       
   785     bb->addButton(QDialogButtonBox::Cancel);
       
   786     bb->addButton(QDialogButtonBox::Help);
       
   787 
       
   788     dlg.show();
       
   789     QTest::qWait(10);
       
   790     QVERIFY(def->isDefault());
       
   791     QTest::keyPress( &dlg, Qt::Key_Enter );
       
   792     QTest::qWait(100);
       
   793     QCOMPARE(clicked.count(), 1);
       
   794 }
       
   795 
       
   796 QTEST_MAIN(tst_QDialogButtonBox)
       
   797 #include "tst_qdialogbuttonbox.moc"