tests/auto/qmessagebox/tst_qmessagebox.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 <QMessageBox>
       
    43 #include <QDebug>
       
    44 #include <QPair>
       
    45 #include <QList>
       
    46 #include <QPointer>
       
    47 #include <QTimer>
       
    48 #include <QApplication>
       
    49 #include <QPushButton>
       
    50 #if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
       
    51 #include <QMacStyle>
       
    52 #endif
       
    53 #if !defined(QT_NO_STYLE_CLEANLOOKS)
       
    54 #include <QCleanlooksStyle>
       
    55 #endif
       
    56 
       
    57 //TESTED_CLASS=
       
    58 //TESTED_FILES=
       
    59 
       
    60 #define CONVENIENCE_FUNC_SYMS(func) \
       
    61     { \
       
    62         int x1 = QMessageBox::func(0, "Foo", "Bar"); \
       
    63         int x3 = QMessageBox::func(0, "Foo", "Bar", "Save"); \
       
    64         int x6 = QMessageBox::func(0, "Foo", "Bar", "Save", "Save As"); \
       
    65         int x7 = QMessageBox::func(0, "Foo", "Bar", "Save", "Save As", "Dont Save"); \
       
    66         int x8 = QMessageBox::func(0, "Foo", "Bar", "Save", "Save As", "Dont Save", 1); \
       
    67         int x9 = QMessageBox::func(0, "Foo", "Bar", "Save", "Save As", "Dont Save", 1, 2); \
       
    68         int x10 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::YesAll, QMessageBox::Yes); \
       
    69         int x11 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::YesAll, QMessageBox::Yes, \
       
    70                                     QMessageBox::No); \
       
    71         qDebug("%d %d %d %d %d %d %d %d", x1, x3, x6, x7, x8, x9, x10, x11); \
       
    72         { \
       
    73         int x4 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, (int)QMessageBox::No); \
       
    74         int x5 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, (int)QMessageBox::No); \
       
    75         int x6 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, (int)QMessageBox::No); \
       
    76         int x7 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, QMessageBox::No); \
       
    77         int x8 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, QMessageBox::No); \
       
    78         int x9 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No); \
       
    79         int x10 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, (int)QMessageBox::No, (int)QMessageBox::Ok); \
       
    80         int x11 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, (int)QMessageBox::No, (int)QMessageBox::Ok); \
       
    81         int x12 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, (int)QMessageBox::No, (int)QMessageBox::Ok); \
       
    82         int x13 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, QMessageBox::No, (int)QMessageBox::Ok); \
       
    83         int x14 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, QMessageBox::No, (int)QMessageBox::Ok); \
       
    84         int x15 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, (int)QMessageBox::Ok); \
       
    85         int x16 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, (int)QMessageBox::No, QMessageBox::Ok); \
       
    86         int x17 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, (int)QMessageBox::No, QMessageBox::Ok); \
       
    87         int x18 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, (int)QMessageBox::No, QMessageBox::Ok); \
       
    88         int x19 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes, QMessageBox::No, QMessageBox::Ok); \
       
    89         int x20 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes, QMessageBox::No, QMessageBox::Ok); \
       
    90         int x21 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Ok); \
       
    91         qDebug("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21); \
       
    92         } \
       
    93     }
       
    94 
       
    95 #define CONVENIENCE_FUNC_SYMS_EXTRA(func) \
       
    96     { \
       
    97         int x1 = QMessageBox::func(0, "Foo", "Bar", (int)QMessageBox::Yes); \
       
    98         int x2 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes); \
       
    99         int x3 = QMessageBox::func(0, "Foo", "Bar", QMessageBox::Yes | QMessageBox::Default); \
       
   100         qDebug("%d %d %d", x1, x2, x3); \
       
   101     }
       
   102 
       
   103 class tst_QMessageBox : public QObject
       
   104 {
       
   105     Q_OBJECT
       
   106 public:
       
   107     tst_QMessageBox();
       
   108     int exec(QMessageBox *msgBox, int key = -1);
       
   109     int sendReturn();
       
   110 
       
   111 public slots:
       
   112     void sendKey();
       
   113 
       
   114 private slots:
       
   115     void sanityTest();
       
   116     void defaultButton();
       
   117     void escapeButton();
       
   118     void button();
       
   119     void statics();
       
   120     void about();
       
   121     void detailsText();
       
   122 
       
   123     void shortcut();
       
   124 
       
   125     void staticSourceCompat();
       
   126     void staticBinaryCompat();
       
   127     void instanceSourceCompat();
       
   128     void instanceBinaryCompat();
       
   129 
       
   130     void testSymbols();
       
   131     void incorrectDefaultButton();
       
   132     void updateSize();
       
   133 
       
   134     void setInformativeText();
       
   135     void iconPixmap();
       
   136 
       
   137 private:
       
   138     int keyToSend;
       
   139 };
       
   140 
       
   141 tst_QMessageBox::tst_QMessageBox() : keyToSend(-1)
       
   142 {
       
   143 }
       
   144 
       
   145 int tst_QMessageBox::exec(QMessageBox *msgBox, int key)
       
   146 {
       
   147     if (key == -1) {
       
   148         QTimer::singleShot(1000, msgBox, SLOT(close()));
       
   149     } else {
       
   150         keyToSend = key;
       
   151         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   152     }
       
   153     return msgBox->exec();
       
   154 }
       
   155 
       
   156 int tst_QMessageBox::sendReturn()
       
   157 {
       
   158     keyToSend = Qt::Key_Return;
       
   159     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   160     return 0;
       
   161 }
       
   162 
       
   163 void tst_QMessageBox::sendKey()
       
   164 {
       
   165     if (keyToSend == -2) {
       
   166         QApplication::activeModalWidget()->close();
       
   167         return;
       
   168     }
       
   169     if (keyToSend == -1)
       
   170         return;
       
   171     QKeyEvent *ke = new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier);
       
   172     qApp->postEvent(QApplication::activeModalWidget(), ke);
       
   173     keyToSend = -1;
       
   174 }
       
   175 
       
   176 void tst_QMessageBox::sanityTest()
       
   177 {
       
   178     QMessageBox msgBox;
       
   179     msgBox.setText("This is insane");
       
   180     for (int i = 0; i < 10; i++)
       
   181         msgBox.setIcon(QMessageBox::Icon(i));
       
   182     msgBox.setIconPixmap(QPixmap());
       
   183     msgBox.setIconPixmap(QPixmap("whatever.png"));
       
   184     msgBox.setTextFormat(Qt::RichText);
       
   185     msgBox.setTextFormat(Qt::PlainText);
       
   186     exec(&msgBox);
       
   187 }
       
   188 
       
   189 void tst_QMessageBox::button()
       
   190 {
       
   191     QMessageBox msgBox;
       
   192     msgBox.addButton("retry", QMessageBox::DestructiveRole);
       
   193     QVERIFY(msgBox.button(QMessageBox::Ok) == 0); // not added yet
       
   194     QPushButton *b1 = msgBox.addButton(QMessageBox::Ok);
       
   195     QCOMPARE(msgBox.button(QMessageBox::Ok), (QAbstractButton *)b1);  // just added
       
   196     QCOMPARE(msgBox.standardButton(b1), QMessageBox::Ok);
       
   197     msgBox.addButton(QMessageBox::Cancel);
       
   198     QCOMPARE(msgBox.standardButtons(), QMessageBox::Ok | QMessageBox::Cancel);
       
   199 
       
   200     // remove the cancel, should not exist anymore
       
   201     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
       
   202     QVERIFY(msgBox.button(QMessageBox::Cancel) == 0);
       
   203     QVERIFY(msgBox.button(QMessageBox::Yes) != 0);
       
   204 
       
   205     // should not crash
       
   206     QPushButton *b4 = new QPushButton;
       
   207     msgBox.addButton(b4, QMessageBox::DestructiveRole);
       
   208     msgBox.addButton(0, QMessageBox::ActionRole);
       
   209 }
       
   210 
       
   211 void tst_QMessageBox::defaultButton()
       
   212 {
       
   213     QMessageBox msgBox;
       
   214     QVERIFY(msgBox.defaultButton() == 0);
       
   215     msgBox.addButton(QMessageBox::Ok);
       
   216     msgBox.addButton(QMessageBox::Cancel);
       
   217     QVERIFY(msgBox.defaultButton() == 0);
       
   218     QPushButton pushButton;
       
   219     msgBox.setDefaultButton(&pushButton);
       
   220     QVERIFY(msgBox.defaultButton() == 0); // we have not added it yet
       
   221     QPushButton *retryButton = msgBox.addButton(QMessageBox::Retry);
       
   222     msgBox.setDefaultButton(retryButton);
       
   223     QCOMPARE(msgBox.defaultButton(), retryButton);
       
   224     exec(&msgBox);
       
   225     QCOMPARE(msgBox.clickedButton(), msgBox.button(QMessageBox::Cancel));
       
   226 
       
   227     exec(&msgBox, Qt::Key_Enter);
       
   228     QCOMPARE(msgBox.clickedButton(), (QAbstractButton *)retryButton);
       
   229 
       
   230     QAbstractButton *okButton = msgBox.button(QMessageBox::Ok);
       
   231     msgBox.setDefaultButton(QMessageBox::Ok);
       
   232     QCOMPARE(msgBox.defaultButton(), (QPushButton *)okButton);
       
   233     exec(&msgBox, Qt::Key_Enter);
       
   234     QCOMPARE(msgBox.clickedButton(), okButton);
       
   235     msgBox.setDefaultButton(QMessageBox::Yes); // its not in there!
       
   236     QVERIFY(msgBox.defaultButton() == okButton);
       
   237     msgBox.removeButton(okButton);
       
   238     delete okButton;
       
   239     okButton = 0;
       
   240     QVERIFY(msgBox.defaultButton() == 0);
       
   241     msgBox.setDefaultButton(QMessageBox::Ok);
       
   242     QVERIFY(msgBox.defaultButton() == 0);
       
   243 }
       
   244 
       
   245 void tst_QMessageBox::escapeButton()
       
   246 {
       
   247     QMessageBox msgBox;
       
   248     QVERIFY(msgBox.escapeButton() == 0);
       
   249     msgBox.addButton(QMessageBox::Ok);
       
   250     exec(&msgBox);
       
   251     QVERIFY(msgBox.clickedButton() == msgBox.button(QMessageBox::Ok)); // auto detected (one button only)
       
   252     msgBox.addButton(QMessageBox::Cancel);
       
   253     QVERIFY(msgBox.escapeButton() == 0);
       
   254     QPushButton invalidButton;
       
   255     msgBox.setEscapeButton(&invalidButton);
       
   256     QVERIFY(msgBox.escapeButton() == 0);
       
   257     QAbstractButton *retryButton = msgBox.addButton(QMessageBox::Retry);
       
   258 
       
   259     exec(&msgBox);
       
   260     QVERIFY(msgBox.clickedButton() == msgBox.button(QMessageBox::Cancel)); // auto detected (cancel)
       
   261 
       
   262     msgBox.setEscapeButton(retryButton);
       
   263     QCOMPARE(msgBox.escapeButton(), (QAbstractButton *)retryButton);
       
   264 
       
   265     // with escape
       
   266     exec(&msgBox, Qt::Key_Escape);
       
   267     QCOMPARE(msgBox.clickedButton(), retryButton);
       
   268 
       
   269     // with close
       
   270     exec(&msgBox);
       
   271     QCOMPARE(msgBox.clickedButton(), (QAbstractButton *)retryButton);
       
   272 
       
   273     QAbstractButton *okButton = msgBox.button(QMessageBox::Ok);
       
   274     msgBox.setEscapeButton(QMessageBox::Ok);
       
   275     QCOMPARE(msgBox.escapeButton(), okButton);
       
   276     exec(&msgBox, Qt::Key_Escape);
       
   277     QCOMPARE(msgBox.clickedButton(), okButton);
       
   278     msgBox.setEscapeButton(QMessageBox::Yes); // its not in there!
       
   279     QVERIFY(msgBox.escapeButton() == okButton);
       
   280     msgBox.removeButton(okButton);
       
   281     delete okButton;
       
   282     okButton = 0;
       
   283     QVERIFY(msgBox.escapeButton() == 0);
       
   284     msgBox.setEscapeButton(QMessageBox::Ok);
       
   285     QVERIFY(msgBox.escapeButton() == 0);
       
   286 
       
   287     QMessageBox msgBox2;
       
   288     msgBox2.addButton(QMessageBox::Yes);
       
   289     msgBox2.addButton(QMessageBox::No);
       
   290     exec(&msgBox2);
       
   291     QVERIFY(msgBox2.clickedButton() == msgBox2.button(QMessageBox::No)); // auto detected (one No button only)
       
   292 
       
   293     QPushButton *rejectButton = new QPushButton;
       
   294     msgBox2.addButton(rejectButton, QMessageBox::RejectRole);
       
   295     exec(&msgBox2);
       
   296     QVERIFY(msgBox2.clickedButton() == rejectButton); // auto detected (one reject button only)
       
   297 
       
   298     msgBox2.addButton(new QPushButton, QMessageBox::RejectRole);
       
   299     exec(&msgBox2);
       
   300     QVERIFY(msgBox2.clickedButton() == msgBox2.button(QMessageBox::No)); // auto detected (one No button only)
       
   301 }
       
   302 
       
   303 void tst_QMessageBox::statics()
       
   304 {
       
   305     QMessageBox::StandardButton (*statics[4])(QWidget *, const QString &,
       
   306          const QString&, QMessageBox::StandardButtons buttons,
       
   307          QMessageBox::StandardButton);
       
   308 
       
   309     statics[0] = QMessageBox::information;
       
   310     statics[1] = QMessageBox::critical;
       
   311     statics[2] = QMessageBox::question;
       
   312     statics[3] = QMessageBox::warning;
       
   313 
       
   314     for (int i = 0; i < 4; i++) {
       
   315         keyToSend = Qt::Key_Escape;
       
   316         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   317         QMessageBox::StandardButton sb = (*statics[i])(0, "caption",
       
   318             "text", QMessageBox::Yes | QMessageBox::No | QMessageBox::Help | QMessageBox::Cancel,
       
   319            QMessageBox::NoButton);
       
   320         QCOMPARE(sb, QMessageBox::Cancel);
       
   321 
       
   322         keyToSend = -2; // close()
       
   323         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   324         sb = (*statics[i])(0, "caption",
       
   325            "text", QMessageBox::Yes | QMessageBox::No | QMessageBox::Help | QMessageBox::Cancel,
       
   326            QMessageBox::NoButton);
       
   327         QCOMPARE(sb, QMessageBox::Cancel);
       
   328 
       
   329         keyToSend = Qt::Key_Enter;
       
   330         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   331         sb = (*statics[i])(0, "caption",
       
   332            "text", QMessageBox::Yes | QMessageBox::No | QMessageBox::Help,
       
   333            QMessageBox::Yes);
       
   334         QCOMPARE(sb, QMessageBox::Yes);
       
   335 
       
   336         keyToSend = Qt::Key_Enter;
       
   337         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   338         sb = (*statics[i])(0, "caption",
       
   339            "text", QMessageBox::Yes | QMessageBox::No | QMessageBox::Help,
       
   340             QMessageBox::No);
       
   341         QCOMPARE(sb, QMessageBox::No);
       
   342     }
       
   343 }
       
   344 
       
   345 void tst_QMessageBox::shortcut()
       
   346 {
       
   347 #ifdef Q_WS_MAC
       
   348     QSKIP("shortcuts are not used on MAC OS X", SkipAll);
       
   349 #endif
       
   350     QMessageBox msgBox;
       
   351     msgBox.addButton("O&k", QMessageBox::YesRole);
       
   352     msgBox.addButton("&No", QMessageBox::YesRole);
       
   353     msgBox.addButton("&Maybe", QMessageBox::YesRole);
       
   354     QCOMPARE(exec(&msgBox, Qt::Key_M), 2);
       
   355 }
       
   356 
       
   357 void tst_QMessageBox::about()
       
   358 {
       
   359     keyToSend = Qt::Key_Escape;
       
   360     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   361     QMessageBox::about(0, "Caption", "This is an auto test");
       
   362 
       
   363 #if !defined(Q_OS_WINCE)
       
   364     keyToSend = Qt::Key_Enter;
       
   365 #else
       
   366     keyToSend = Qt::Key_Escape;
       
   367 #endif
       
   368     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   369     QMessageBox::aboutQt(0, "Caption");
       
   370 }
       
   371 
       
   372 // Old message box enums
       
   373 const int Old_Ok = 1;
       
   374 const int Old_Cancel = 2;
       
   375 const int Old_Yes = 3;
       
   376 const int Old_No = 4;
       
   377 const int Old_Abort = 5;
       
   378 const int Old_Retry = 6;
       
   379 const int Old_Ignore = 7;
       
   380 const int Old_YesAll = 8;
       
   381 const int Old_NoAll = 9;
       
   382 const int Old_Default = 0x100;
       
   383 const int Old_Escape = 0x200;
       
   384 
       
   385 void tst_QMessageBox::staticSourceCompat()
       
   386 {
       
   387     int ret;
       
   388 
       
   389     // source compat tests for < 4.2
       
   390     keyToSend = Qt::Key_Enter;
       
   391     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   392     ret = QMessageBox::information(0, "title", "text", QMessageBox::Yes, QMessageBox::No);
       
   393     int expectedButton = int(QMessageBox::Yes);
       
   394 #if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
       
   395     if (qobject_cast<QMacStyle *>(qApp->style()))
       
   396         expectedButton = int(QMessageBox::No);
       
   397 #elif !defined(QT_NO_STYLE_CLEANLOOKS)
       
   398     if (qobject_cast<QCleanlooksStyle *>(qApp->style()))
       
   399         expectedButton = int(QMessageBox::No);
       
   400 #endif
       
   401     QCOMPARE(ret, expectedButton);
       
   402 
       
   403     keyToSend = Qt::Key_Enter;
       
   404     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   405     ret = QMessageBox::information(0, "title", "text", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No);
       
   406     QCOMPARE(ret, int(QMessageBox::Yes));
       
   407 
       
   408     keyToSend = Qt::Key_Enter;
       
   409     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   410     ret = QMessageBox::information(0, "title", "text", QMessageBox::Yes, QMessageBox::No | QMessageBox::Default);
       
   411     QCOMPARE(ret, int(QMessageBox::No));
       
   412 
       
   413     keyToSend = Qt::Key_Enter;
       
   414     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   415     ret = QMessageBox::information(0, "title", "text", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape);
       
   416     QCOMPARE(ret, int(QMessageBox::Yes));
       
   417 
       
   418     keyToSend = Qt::Key_Enter;
       
   419     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   420     ret = QMessageBox::information(0, "title", "text", QMessageBox::Yes | QMessageBox::Escape, QMessageBox::No | QMessageBox::Default);
       
   421     QCOMPARE(ret, int(QMessageBox::No));
       
   422 
       
   423     // the button text versions
       
   424     keyToSend = Qt::Key_Enter;
       
   425     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   426     ret = QMessageBox::information(0, "title", "text", "Yes", "No", QString(), 1);
       
   427     QCOMPARE(ret, 1);
       
   428 
       
   429     if (0) { // dont run these tests since the dialog wont close!
       
   430         keyToSend = Qt::Key_Escape;
       
   431         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   432         ret = QMessageBox::information(0, "title", "text", "Yes", "No", QString(), 1);
       
   433         QCOMPARE(ret, -1);
       
   434 
       
   435         keyToSend = Qt::Key_Escape;
       
   436         QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   437         ret = QMessageBox::information(0, "title", "text", "Yes", "No", QString(), 0, 1);
       
   438         QCOMPARE(ret, 1);
       
   439     }
       
   440 }
       
   441 
       
   442 void tst_QMessageBox::instanceSourceCompat()
       
   443 {
       
   444      QMessageBox mb("Application name here",
       
   445                     "Saving the file will overwrite the original file on the disk.\n"
       
   446                     "Do you really want to save?",
       
   447                     QMessageBox::Information,
       
   448                     QMessageBox::Yes | QMessageBox::Default,
       
   449                     QMessageBox::No,
       
   450                     QMessageBox::Cancel | QMessageBox::Escape);
       
   451     mb.setButtonText(QMessageBox::Yes, "Save");
       
   452     mb.setButtonText(QMessageBox::No, "Discard");
       
   453     mb.addButton("&Revert", QMessageBox::RejectRole);
       
   454     mb.addButton("&Zoo", QMessageBox::ActionRole);
       
   455 
       
   456     QCOMPARE(exec(&mb, Qt::Key_Enter), int(QMessageBox::Yes));
       
   457     QCOMPARE(exec(&mb, Qt::Key_Escape), int(QMessageBox::Cancel));
       
   458 #ifdef Q_WS_MAC
       
   459     QSKIP("mnemonics are not used on the MAC OS X", SkipAll);
       
   460 #endif
       
   461     QCOMPARE(exec(&mb, Qt::ALT + Qt::Key_R), 0);
       
   462     QCOMPARE(exec(&mb, Qt::ALT + Qt::Key_Z), 1);
       
   463 }
       
   464 
       
   465 void tst_QMessageBox::staticBinaryCompat()
       
   466 {
       
   467     int ret;
       
   468 
       
   469     // binary compat tests for < 4.2
       
   470     keyToSend = Qt::Key_Enter;
       
   471     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   472     ret = QMessageBox::information(0, "title", "text", Old_Yes, Old_No, 0);
       
   473     int expectedButton = int(Old_Yes);
       
   474 #if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
       
   475     if (qobject_cast<QMacStyle *>(qApp->style()))
       
   476         expectedButton = int(Old_No);
       
   477 #elif !defined(QT_NO_STYLE_CLEANLOOKS)
       
   478     if (qobject_cast<QCleanlooksStyle *>(qApp->style()))
       
   479         expectedButton = int(Old_No);
       
   480 #endif
       
   481     QCOMPARE(ret, expectedButton);
       
   482 
       
   483     keyToSend = Qt::Key_Escape;
       
   484     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   485     ret = QMessageBox::information(0, "title", "text", Old_Yes | Old_Escape, Old_No, 0);
       
   486     QCOMPARE(ret, int(Old_Yes));
       
   487 
       
   488     keyToSend = Qt::Key_Enter;
       
   489     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   490     ret = QMessageBox::information(0, "title", "text", Old_Yes | Old_Default, Old_No, 0);
       
   491     QCOMPARE(ret, int(Old_Yes));
       
   492 
       
   493 #if 0
       
   494     keyToSend = Qt::Key_Escape;
       
   495     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   496     ret = QMessageBox::information(0, "title", "text", Old_Yes, Old_No | Old_Default, 0);
       
   497     QCOMPARE(ret, -1);
       
   498 #endif
       
   499 
       
   500     keyToSend = Qt::Key_Escape;
       
   501     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   502     ret = QMessageBox::information(0, "title", "text", Old_Yes | Old_Escape, Old_No | Old_Default, 0);
       
   503     QCOMPARE(ret, Old_Yes);
       
   504 
       
   505     keyToSend = Qt::Key_Escape;
       
   506     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   507     ret = QMessageBox::information(0, "title", "text", Old_Yes | Old_Default, Old_No | Old_Escape, 0);
       
   508     QCOMPARE(ret, Old_No);
       
   509 
       
   510 }
       
   511 
       
   512 void tst_QMessageBox::instanceBinaryCompat()
       
   513 {
       
   514      QMessageBox mb("Application name here",
       
   515                     "Saving the file will overwrite the original file on the disk.\n"
       
   516                     "Do you really want to save?",
       
   517                     QMessageBox::Information,
       
   518                     Old_Yes | Old_Default,
       
   519                     Old_No,
       
   520                     Old_Cancel | Old_Escape);
       
   521     mb.setButtonText(Old_Yes, "Save");
       
   522     mb.setButtonText(Old_No, "Discard");
       
   523     QCOMPARE(exec(&mb, Qt::Key_Enter), int(Old_Yes));
       
   524     QCOMPARE(exec(&mb, Qt::Key_Escape), int(Old_Cancel));
       
   525 }
       
   526 
       
   527 void tst_QMessageBox::testSymbols()
       
   528 {
       
   529     return;
       
   530 
       
   531     QMessageBox::Icon icon;
       
   532     icon = QMessageBox::NoIcon;
       
   533     icon = QMessageBox::Information;
       
   534     icon = QMessageBox::Warning;
       
   535     icon = QMessageBox::Critical;
       
   536     icon = QMessageBox::Question;
       
   537 
       
   538     QMessageBox mb1;
       
   539     QMessageBox mb2(0);
       
   540     QMessageBox mb3(&mb1);
       
   541     QMessageBox mb3b("title", "text", QMessageBox::Critical, int(QMessageBox::Yes),
       
   542                      int(QMessageBox::No), int(QMessageBox::Cancel), &mb1, Qt::Dialog);
       
   543 
       
   544     QMessageBox::Button button = QMessageBox::NoButton;
       
   545     button = QMessageBox::Ok;
       
   546     button = QMessageBox::Cancel;
       
   547     button = QMessageBox::Yes;
       
   548     button = QMessageBox::No;
       
   549     button = QMessageBox::Abort;
       
   550     button = QMessageBox::Retry;
       
   551     button = QMessageBox::Ignore;
       
   552     button = QMessageBox::YesAll;
       
   553     button = QMessageBox::NoAll;
       
   554     button = QMessageBox::ButtonMask;
       
   555     button = QMessageBox::Default;
       
   556     button = QMessageBox::Escape;
       
   557     button = QMessageBox::FlagMask;
       
   558 
       
   559     mb1.setText("Foo");
       
   560     QString text = mb1.text();
       
   561     Q_ASSERT(text == "Foo");
       
   562 
       
   563     icon = mb1.icon();
       
   564     Q_ASSERT(icon == QMessageBox::NoIcon);
       
   565     mb1.setIcon(QMessageBox::Question);
       
   566     Q_ASSERT(mb1.icon() == QMessageBox::Question);
       
   567 
       
   568     QPixmap iconPixmap = mb1.iconPixmap();
       
   569     mb1.setIconPixmap(iconPixmap);
       
   570     Q_ASSERT(mb1.icon() == QMessageBox::NoIcon);
       
   571 
       
   572     QString bt0 = mb1.buttonText(QMessageBox::Ok);
       
   573     QString bt1 = mb1.buttonText(QMessageBox::Cancel);
       
   574     QString bt2 = mb1.buttonText(QMessageBox::Ok | QMessageBox::Default);
       
   575 
       
   576     Q_ASSERT(bt0 == "OK");
       
   577     Q_ASSERT(bt1.isEmpty());
       
   578     Q_ASSERT(bt2.isEmpty());
       
   579 
       
   580     mb2.setButtonText(QMessageBox::Cancel, "Foo");
       
   581     mb2.setButtonText(QMessageBox::Ok, "Bar");
       
   582     mb2.setButtonText(QMessageBox::Ok | QMessageBox::Default, "Baz");
       
   583 
       
   584     Q_ASSERT(mb2.buttonText(QMessageBox::Cancel).isEmpty());
       
   585     Q_ASSERT(mb2.buttonText(QMessageBox::Ok) == "Bar");
       
   586 
       
   587     Q_ASSERT(mb3b.buttonText(QMessageBox::Yes).endsWith("Yes"));
       
   588     Q_ASSERT(mb3b.buttonText(QMessageBox::YesAll).isEmpty());
       
   589     Q_ASSERT(mb3b.buttonText(QMessageBox::Ok).isEmpty());
       
   590 
       
   591     mb3b.setButtonText(QMessageBox::Yes, "Blah");
       
   592     mb3b.setButtonText(QMessageBox::YesAll, "Zoo");
       
   593     mb3b.setButtonText(QMessageBox::Ok, "Zoo");
       
   594 
       
   595     Q_ASSERT(mb3b.buttonText(QMessageBox::Yes) == "Blah");
       
   596     Q_ASSERT(mb3b.buttonText(QMessageBox::YesAll).isEmpty());
       
   597     Q_ASSERT(mb3b.buttonText(QMessageBox::Ok).isEmpty());
       
   598 
       
   599     Q_ASSERT(mb1.textFormat() == Qt::AutoText);
       
   600     mb1.setTextFormat(Qt::PlainText);
       
   601     Q_ASSERT(mb1.textFormat() == Qt::PlainText);
       
   602 
       
   603     CONVENIENCE_FUNC_SYMS(information);
       
   604     CONVENIENCE_FUNC_SYMS_EXTRA(information);
       
   605     CONVENIENCE_FUNC_SYMS(question);
       
   606     CONVENIENCE_FUNC_SYMS_EXTRA(question);
       
   607     CONVENIENCE_FUNC_SYMS(warning);
       
   608     CONVENIENCE_FUNC_SYMS(critical);
       
   609 
       
   610     QSize sizeHint = mb1.sizeHint();
       
   611     Q_ASSERT(sizeHint.width() > 20 && sizeHint.height() > 20);
       
   612 
       
   613 #ifdef QT3_SUPPORT
       
   614      //test QT3_SUPPORT stuff
       
   615 
       
   616     QMessageBox mb4("title", "text", icon, QMessageBox::Yes, QMessageBox::No | QMessageBox::Default,
       
   617                     QMessageBox::Cancel, &mb1, "name", true, Qt::Dialog);
       
   618     QMessageBox mb5(&mb1, "name");
       
   619 
       
   620     QPixmap pm = QMessageBox::standardIcon(QMessageBox::Question, Qt::GUIStyle(1));
       
   621     QPixmap pm2 = QMessageBox::standardIcon(QMessageBox::Question);
       
   622 
       
   623     Q_ASSERT(pm.toImage() == iconPixmap.toImage());
       
   624     Q_ASSERT(pm2.toImage() == iconPixmap.toImage());
       
   625 
       
   626     int ret1 = QMessageBox::message("title", "text");
       
   627     int ret2 = QMessageBox::message("title", "text", "OK");
       
   628     int ret3 = QMessageBox::message("title", "text", "OK", &mb1);
       
   629     int ret4 = QMessageBox::message("title", "text", "OK", &mb1, "name");
       
   630     qDebug("%d %d %d %d", ret1, ret2, ret3, ret4);
       
   631 
       
   632     bool ret5 = QMessageBox::query("title", "text");
       
   633     bool ret6 = QMessageBox::query("title", "text", "Ja");
       
   634     bool ret7 = QMessageBox::query("title", "text", "Ja", "Nein");
       
   635     bool ret8 = QMessageBox::query("title", "text", "Ja", "Nein", &mb1);
       
   636     bool ret9 = QMessageBox::query("title", "text", "Ja", "Nein", &mb1, "name");
       
   637     qDebug("%d %d %d %d %d", ret5, ret6, ret7, ret8, ret9);
       
   638 
       
   639     Q_UNUSED(ret1);
       
   640     Q_UNUSED(ret5);
       
   641 
       
   642     QPixmap pm3 = QMessageBox::standardIcon(QMessageBox::NoIcon);
       
   643     Q_ASSERT(pm3.isNull());
       
   644 
       
   645     pm3 = QMessageBox::standardIcon(QMessageBox::Information);
       
   646     Q_ASSERT(!pm3.isNull());
       
   647 #endif //QT3_SUPPORT
       
   648 
       
   649     QMessageBox::about(&mb1, "title", "text");
       
   650     QMessageBox::aboutQt(&mb1);
       
   651     QMessageBox::aboutQt(&mb1, "title");
       
   652 }
       
   653 
       
   654 void tst_QMessageBox::detailsText()
       
   655 {
       
   656     QMessageBox box;
       
   657     QString text("This is the details text.");
       
   658     box.setDetailedText(text);
       
   659     QCOMPARE(box.detailedText(), text);
       
   660 }
       
   661 
       
   662 void tst_QMessageBox::incorrectDefaultButton()
       
   663 {
       
   664     keyToSend = Qt::Key_Escape;
       
   665     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   666     //Do not crash here
       
   667     QTest::ignoreMessage(QtWarningMsg, "QDialogButtonBox::createButton: Invalid ButtonRole, button not added");
       
   668     QMessageBox::question( 0, "", "I've been hit!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Save );
       
   669 
       
   670     keyToSend = Qt::Key_Escape;
       
   671     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   672     QTest::ignoreMessage(QtWarningMsg, "QDialogButtonBox::createButton: Invalid ButtonRole, button not added");
       
   673     QMessageBox::question( 0, "", "I've been hit!",QFlag(QMessageBox::Ok | QMessageBox::Cancel),QMessageBox::Save );
       
   674 
       
   675     keyToSend = Qt::Key_Escape;
       
   676     QTimer::singleShot(1000, this, SLOT(sendKey()));
       
   677     QTest::ignoreMessage(QtWarningMsg, "QDialogButtonBox::createButton: Invalid ButtonRole, button not added");
       
   678     QTest::ignoreMessage(QtWarningMsg, "QDialogButtonBox::createButton: Invalid ButtonRole, button not added");
       
   679     //do not crash here -> call old function of QMessageBox in this case
       
   680     QMessageBox::question( 0, "", "I've been hit!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Save | QMessageBox::Cancel,QMessageBox::Ok);
       
   681 }
       
   682 
       
   683 void tst_QMessageBox::updateSize()
       
   684 {
       
   685     QMessageBox box;
       
   686 #ifdef Q_WS_S60
       
   687     // In S60 messagebox is always occupies maximum width, i.e. screen width
       
   688     // so we need to have long enough text to split over several line
       
   689     box.setText("This is awesome long text");
       
   690 #else
       
   691     box.setText("This is awesome");
       
   692 #endif
       
   693     box.show();
       
   694     QSize oldSize = box.size();
       
   695     QString longText;
       
   696     for (int i = 0; i < 20; i++)
       
   697         longText += box.text();
       
   698     box.setText(longText);
       
   699     QVERIFY(box.size() != oldSize); // should have grown
       
   700     QVERIFY(box.width() > oldSize.width() || box.height() > oldSize.height());
       
   701     oldSize = box.size();
       
   702 #ifndef Q_WS_S60
       
   703     // In S60 dialogs buttons are in softkey area -> message box size does not change
       
   704     box.setStandardButtons(QMessageBox::StandardButtons(0xFFFF));
       
   705     QVERIFY(box.size() != oldSize); // should have grown
       
   706     QVERIFY(box.width() > oldSize.width() || box.height() > oldSize.height());
       
   707 #endif
       
   708 }
       
   709 
       
   710 void tst_QMessageBox::setInformativeText()
       
   711 {
       
   712     QMessageBox msgbox(QMessageBox::Warning, "", "", QMessageBox::Ok);
       
   713     QString itext = "This is a very long message and it should make the dialog have enough width to fit this message in";
       
   714     msgbox.setInformativeText(itext);
       
   715     msgbox.show();
       
   716     QCOMPARE(msgbox.informativeText(), itext);
       
   717     QVERIFY2(msgbox.width() > 190, //verify it's big enough (task181688)
       
   718              qPrintable(QString("%1 > 190").arg(msgbox.width())));
       
   719 }
       
   720 
       
   721 void tst_QMessageBox::iconPixmap()
       
   722 {
       
   723     QMessageBox messageBox;
       
   724     QCOMPARE(messageBox.iconPixmap(), QPixmap());
       
   725 }
       
   726 
       
   727 QTEST_MAIN(tst_QMessageBox)
       
   728 #include "tst_qmessagebox.moc"