tests/auto/qapplication/tst_qapplication.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   104     void args_data();
   104     void args_data();
   105     void args();
   105     void args();
   106 
   106 
   107     void lastWindowClosed();
   107     void lastWindowClosed();
   108     void quitOnLastWindowClosed();
   108     void quitOnLastWindowClosed();
       
   109     void closeAllWindows();
   109     void testDeleteLater();
   110     void testDeleteLater();
   110     void testDeleteLaterProcessEvents();
   111     void testDeleteLaterProcessEvents();
   111 
   112 
   112     void libraryPaths();
   113     void libraryPaths();
   113     void libraryPaths_qt_plugin_path();
   114     void libraryPaths_qt_plugin_path();
   186 // Testing get/set functions
   187 // Testing get/set functions
   187 void tst_QApplication::getSetCheck()
   188 void tst_QApplication::getSetCheck()
   188 {
   189 {
   189     int argc = 0;
   190     int argc = 0;
   190     QApplication obj1(argc, 0, QApplication::GuiServer);
   191     QApplication obj1(argc, 0, QApplication::GuiServer);
   191     // QInputContext * QApplication::inputContext()
       
   192     // void QApplication::setInputContext(QInputContext *)
       
   193     MyInputContext *var1 = new MyInputContext;
   192     MyInputContext *var1 = new MyInputContext;
       
   193 
       
   194     // QApplication takes ownership, so check for reparenting:
   194     obj1.setInputContext(var1);
   195     obj1.setInputContext(var1);
   195     QCOMPARE((QInputContext *)var1, obj1.inputContext());
   196     QCOMPARE(var1->parent(), static_cast<QObject *>(&obj1));
       
   197 
       
   198     // Test for self-assignment:
       
   199     obj1.setInputContext(obj1.inputContext());
       
   200     QVERIFY(obj1.inputContext());
       
   201     QCOMPARE(static_cast<QInputContext *>(var1), obj1.inputContext());
       
   202 
       
   203     // Resetting the input context to 0 is not allowed:
   196     QTest::ignoreMessage(QtWarningMsg, "QApplication::setInputContext: called with 0 input context");
   204     QTest::ignoreMessage(QtWarningMsg, "QApplication::setInputContext: called with 0 input context");
   197     obj1.setInputContext((QInputContext *)0);
   205     obj1.setInputContext(0);
   198     QCOMPARE((QInputContext *)var1, obj1.inputContext());
   206 
   199     // delete var1; // No delete, since QApplication takes ownership
   207     QCOMPARE(static_cast<QInputContext *>(var1), obj1.inputContext());
   200 }
   208 }
   201 
   209 
   202 class CloseEventTestWindow : public QWidget
   210 class CloseEventTestWindow : public QWidget
   203 {
   211 {
   204 public:
   212 public:
   743         QCOMPARE(timerSpy.count(), 1);
   751         QCOMPARE(timerSpy.count(), 1);
   744         QCOMPARE(appSpy.count(), 2);
   752         QCOMPARE(appSpy.count(), 2);
   745     }
   753     }
   746 }
   754 }
   747 
   755 
       
   756 class PromptOnCloseWidget : public QWidget
       
   757 {
       
   758 public:
       
   759     void closeEvent(QCloseEvent *event)
       
   760     {
       
   761         QMessageBox *messageBox = new QMessageBox(this);
       
   762         messageBox->setWindowTitle("Unsaved data");
       
   763         messageBox->setText("Would you like to save or discard your current data?");
       
   764         messageBox->setStandardButtons(QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
       
   765         messageBox->setDefaultButton(QMessageBox::Save);
       
   766 
       
   767         messageBox->show();
       
   768         QTest::qWaitForWindowShown(messageBox);
       
   769 
       
   770         // verify that all windows are visible
       
   771         foreach (QWidget *w, qApp->topLevelWidgets())
       
   772             QVERIFY(w->isVisible());
       
   773         // flush event queue
       
   774         qApp->processEvents();
       
   775         // close all windows
       
   776         qApp->closeAllWindows();
       
   777 
       
   778         if (messageBox->standardButton(messageBox->clickedButton()) == QMessageBox::Cancel)
       
   779             event->ignore();
       
   780         else
       
   781             event->accept();
       
   782 
       
   783         delete messageBox;
       
   784     }
       
   785 };
       
   786 
       
   787 void tst_QApplication::closeAllWindows()
       
   788 {
       
   789     int argc = 0;
       
   790     QApplication app(argc, 0, QApplication::GuiServer);
       
   791 
       
   792     // create some windows
       
   793     new QWidget;
       
   794     new QWidget;
       
   795     new QWidget;
       
   796 
       
   797     // show all windows
       
   798     foreach (QWidget *w, app.topLevelWidgets()) {
       
   799         w->show();
       
   800         QTest::qWaitForWindowShown(w);
       
   801     }
       
   802     // verify that they are visible
       
   803     foreach (QWidget *w, app.topLevelWidgets())
       
   804         QVERIFY(w->isVisible());
       
   805     // empty event queue
       
   806     app.processEvents();
       
   807     // close all windows
       
   808     app.closeAllWindows();
       
   809     // all windows should no longer be visible
       
   810     foreach (QWidget *w, app.topLevelWidgets())
       
   811         QVERIFY(!w->isVisible());
       
   812 
       
   813     // add a window that prompts the user when closed
       
   814     PromptOnCloseWidget *promptOnCloseWidget = new PromptOnCloseWidget;
       
   815     // show all windows
       
   816     foreach (QWidget *w, app.topLevelWidgets()) {
       
   817         w->show();
       
   818         QTest::qWaitForWindowShown(w);
       
   819     }
       
   820     // close the last window to open the prompt (eventloop recurses)
       
   821     promptOnCloseWidget->close();
       
   822     // all windows should not be visible, except the one that opened the prompt
       
   823     foreach (QWidget *w, app.topLevelWidgets()) {
       
   824         if (w == promptOnCloseWidget)
       
   825             QVERIFY(w->isVisible());
       
   826         else
       
   827             QVERIFY(!w->isVisible());
       
   828     }
       
   829 
       
   830     qDeleteAll(app.topLevelWidgets());
       
   831 }
       
   832 
   748 bool isPathListIncluded(const QStringList &l, const QStringList &r)
   833 bool isPathListIncluded(const QStringList &l, const QStringList &r)
   749 {
   834 {
   750     int size = r.count();
   835     int size = r.count();
   751     if (size > l.count())
   836     if (size > l.count())
   752         return false;
   837         return false;