1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 //#define QT_TST_QAPP_DEBUG
46 #include <QtTest/QtTest>
48 #include <QtCore/QAbstractEventDispatcher>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QDir>
51 #include <QtCore/QProcess>
52 #include <QtCore/private/qeventloop_p.h>
54 #include <QtGui/QFontDatabase>
55 #include <QtGui/QClipboard>
57 #include <QtWidgets/QApplication>
58 #include <QtWidgets/QMessageBox>
59 #include <QtWidgets/QStyleFactory>
60 #include <QtWidgets/QHBoxLayout>
61 #include <QtWidgets/QPushButton>
62 #include <QtWidgets/QLineEdit>
63 #include <QtWidgets/QLabel>
64 #include <QtWidgets/QMainWindow>
65 #include <QtWidgets/private/qapplication_p.h>
66 #include <QtWidgets/private/qstylesheetstyle_p.h>
73 extern bool Q_GUI_EXPORT qt_tab_all_widgets; // from qapplication.cpp
76 class tst_QApplication : public QObject
82 virtual ~tst_QApplication();
89 void sendEventsOnProcessEvents(); // this must be the first test
105 void lastWindowClosed();
106 void quitOnLastWindowClosed();
107 void closeAllWindows();
108 void testDeleteLater();
109 void testDeleteLaterProcessEvents();
112 void libraryPaths_qt_plugin_path();
113 void libraryPaths_qt_plugin_path_2();
115 void sendPostedEvents();
118 void desktopSettingsAware();
120 void setActiveWindow();
125 void execAfterExit();
127 void wheelScrollLines();
134 void topLevelWidgets();
138 void windowsCommandLine_data();
139 void windowsCommandLine();
141 void touchEventPropagation();
146 void globalStaticObjectDestruction(); // run this last
148 void abortQuitOnShow();
151 class EventSpy : public QObject
156 QList<int> recordedEvents;
157 bool eventFilter(QObject *, QEvent *event)
159 recordedEvents.append(event->type());
164 void tst_QApplication::initTestCase()
166 // chdir to our testdata path and execute helper apps relative to that.
167 const QString testdataDir = QFileInfo(QFINDTESTDATA("desktopsettingsaware")).absolutePath();
168 QVERIFY2(QDir::setCurrent(testdataDir), qPrintable("Could not chdir to " + testdataDir));
171 void tst_QApplication::sendEventsOnProcessEvents()
174 QApplication app(argc, 0, QApplication::GuiServer);
177 app.installEventFilter(&spy);
179 QCoreApplication::postEvent(&app, new QEvent(QEvent::Type(QEvent::User + 1)));
180 QCoreApplication::processEvents();
181 QVERIFY(spy.recordedEvents.contains(QEvent::User + 1));
185 class CloseEventTestWindow : public QWidget
188 CloseEventTestWindow(QWidget *parent = 0)
193 void closeEvent(QCloseEvent *event)
205 tst_QApplication::tst_QApplication()
208 // Clean up environment previously to launching test
209 qputenv("QT_PLUGIN_PATH", QByteArray());
213 tst_QApplication::~tst_QApplication()
218 void tst_QApplication::init()
220 // TODO: Add initialization code here.
221 // This will be executed immediately before each test is run.
224 void tst_QApplication::cleanup()
226 // TODO: Add cleanup code here.
227 // This will be executed immediately after each test is run.
230 void tst_QApplication::staticSetup()
234 QStyle *style = QStyleFactory::create(QLatin1String("Windows"));
236 QApplication::setStyle(style);
239 QApplication::setPalette(pal);
242 QApplication::setFont(font);*/
245 QApplication app(argc, 0, QApplication::GuiServer);
249 // QApp subclass that exits the event loop after 150ms
250 class TestApplication : public QApplication
253 TestApplication( int &argc, char **argv )
254 : QApplication( argc, argv, QApplication::GuiServer )
259 void timerEvent( QTimerEvent * )
265 void tst_QApplication::alert()
268 QApplication app(argc, 0, QApplication::GuiServer);
273 app.alert(&widget, 100);
277 qt_x11_wait_for_window_manager(&widget);
278 qt_x11_wait_for_window_manager(&widget2);
281 app.alert(&widget, -1);
282 app.alert(&widget, 250);
283 widget2.activateWindow();
284 QApplication::setActiveWindow(&widget2);
285 app.alert(&widget, 0);
286 widget.activateWindow();
287 QApplication::setActiveWindow(&widget);
288 app.alert(&widget, 200);
292 void tst_QApplication::multiple_data()
294 QTest::addColumn<QStringList>("features");
296 // return a list of things to try
297 QTest::newRow( "data0" ) << QStringList( "" );
298 QTest::newRow( "data1" ) << QStringList( "QFont" );
299 QTest::newRow( "data2" ) << QStringList( "QPixmap" );
300 QTest::newRow( "data3" ) << QStringList( "QWidget" );
303 void tst_QApplication::multiple()
305 QFETCH(QStringList,features);
310 TestApplication app( argc, 0 );
312 if ( features.contains( "QFont" ) ) {
313 // create font and force loading
314 QFont font( "Arial", 12 );
315 QFontInfo finfo( font );
318 if ( features.contains( "QPixmap" ) ) {
319 QPixmap pix( 100, 100 );
320 pix.fill( Qt::black );
322 if ( features.contains( "QWidget" ) ) {
326 QVERIFY(!app.exec());
330 void tst_QApplication::nonGui()
333 // ### This is only to allow us to generate a test report for now.
334 QSKIP("This test shuts down the window manager on HP-UX.");
338 QApplication app(argc, 0, false);
339 QCOMPARE(qApp, &app);
342 void tst_QApplication::setFont_data()
344 QTest::addColumn<QString>("family");
345 QTest::addColumn<int>("pointsize");
346 QTest::addColumn<bool>("beforeAppConstructor");
349 QApplication app(argc, 0, QApplication::GuiServer); // Needed for QFontDatabase
353 QStringList families = fdb.families();
354 for (QStringList::const_iterator itr = families.begin();
355 itr != families.end();
358 QString family = *itr;
359 QStringList styles = fdb.styles(family);
360 if (styles.size() > 0) {
361 QString style = styles.first();
362 QList<int> sizes = fdb.pointSizes(family, style);
364 sizes = fdb.standardSizes();
365 if (sizes.size() > 0) {
366 QTest::newRow(QString("data%1a").arg(cnt).toLatin1().constData())
370 QTest::newRow(QString("data%1b").arg(cnt).toLatin1().constData())
380 QTest::newRow("nonexistingfont after") << "nosuchfont_probably_quiteunlikely"
382 QTest::newRow("nonexistingfont before") << "nosuchfont_probably_quiteunlikely"
385 QTest::newRow("largescaleable after") << "smoothtimes" << 100 << false;
386 QTest::newRow("largescaleable before") << "smoothtimes" << 100 << true;
388 QTest::newRow("largeunscaleale after") << "helvetica" << 100 << false;
389 QTest::newRow("largeunscaleale before") << "helvetica" << 100 << true;
392 void tst_QApplication::setFont()
394 QFETCH( QString, family );
395 QFETCH( int, pointsize );
396 QFETCH( bool, beforeAppConstructor );
398 QFont font( family, pointsize );
399 if (beforeAppConstructor) {
400 QApplication::setFont( font );
401 QCOMPARE(QApplication::font(), font);
405 QApplication app( argc, 0, QApplication::GuiServer );
406 if (!beforeAppConstructor)
407 QApplication::setFont( font );
409 QCOMPARE( app.font(), font );
412 void tst_QApplication::args_data()
414 QTest::addColumn<int>("argc_in");
415 QTest::addColumn<QString>("args_in");
416 QTest::addColumn<int>("argc_out");
417 QTest::addColumn<QString>("args_out");
419 QTest::newRow( "App name" ) << 1 << "/usr/bin/appname" << 1 << "/usr/bin/appname";
420 QTest::newRow( "No arguments" ) << 0 << QString() << 0 << QString();
421 QTest::newRow( "App name, style" ) << 3 << "/usr/bin/appname -style motif" << 1 << "/usr/bin/appname";
422 QTest::newRow( "App name, style, arbitrary, reverse" ) << 5 << "/usr/bin/appname -style motif -arbitrary -reverse"
423 << 2 << "/usr/bin/appname -arbitrary";
426 void tst_QApplication::task109149()
429 QApplication app(argc, 0, QApplication::GuiServer);
430 QApplication::setFont(QFont("helvetica", 100));
433 w.setWindowTitle("hello");
439 static char ** QString2cstrings( const QString &args )
441 static QList<QByteArray> cache;
445 QStringList list = args.split(' ');;
446 argarray = new char*[list.count()+1];
448 for (i = 0; i < (int)list.count(); ++i ) {
449 QByteArray l1 = list[i].toLatin1();
450 argarray[i] = l1.data();
458 static QString cstrings2QString( char **args )
474 void tst_QApplication::args()
476 QFETCH( int, argc_in );
477 QFETCH( QString, args_in );
478 QFETCH( int, argc_out );
479 QFETCH( QString, args_out );
481 char **argv = QString2cstrings( args_in );
483 QApplication app( argc_in, argv, QApplication::GuiServer );
484 QString argv_out = cstrings2QString(argv);
486 QCOMPARE( argc_in, argc_out );
487 QCOMPARE( argv_out, args_out );
492 class CloseWidget : public QWidget
502 void timerEvent(QTimerEvent *)
509 void tst_QApplication::lastWindowClosed()
512 QApplication app(argc, 0, QApplication::GuiServer);
514 QSignalSpy spy(&app, SIGNAL(lastWindowClosed()));
516 QPointer<QDialog> dialog = new QDialog;
517 QVERIFY(dialog->testAttribute(Qt::WA_QuitOnClose));
518 QTimer::singleShot(1000, dialog, SLOT(accept()));
521 QCOMPARE(spy.count(), 0);
523 QPointer<CloseWidget>widget = new CloseWidget;
524 QVERIFY(widget->testAttribute(Qt::WA_QuitOnClose));
526 QObject::connect(&app, SIGNAL(lastWindowClosed()), widget, SLOT(deleteLater()));
529 QCOMPARE(spy.count(), 1);
534 // show 3 windows, close them, should only get lastWindowClosed once
542 QTimer::singleShot(1000, &app, SLOT(closeAllWindows()));
544 QCOMPARE(spy.count(), 1);
547 class QuitOnLastWindowClosedDialog : public QDialog
551 QPushButton *okButton;
553 QuitOnLastWindowClosedDialog()
555 QHBoxLayout *hbox = new QHBoxLayout(this);
556 okButton = new QPushButton("&ok", this);
558 hbox->addWidget(okButton);
559 connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
560 connect(okButton, SIGNAL(clicked()), this, SLOT(ok_clicked()));
569 connect(&timer, SIGNAL(timeout()), &other, SLOT(accept()));
570 QSignalSpy spy(&timer, SIGNAL(timeout()));
571 QSignalSpy appSpy(qApp, SIGNAL(lastWindowClosed()));
576 // verify that the eventloop ran and let the timer fire
577 QCOMPARE(spy.count(), 1);
578 QCOMPARE(appSpy.count(), 1);
582 class QuitOnLastWindowClosedWindow : public QWidget
587 QuitOnLastWindowClosedWindow()
591 void execDialogThenShow()
595 connect(&timer1, SIGNAL(timeout()), &dialog, SLOT(accept()));
596 QSignalSpy spy1(&timer1, SIGNAL(timeout()));
597 timer1.setSingleShot(true);
600 QCOMPARE(spy1.count(), 1);
606 void tst_QApplication::quitOnLastWindowClosed()
610 QApplication app(argc, 0, QApplication::GuiServer);
612 QuitOnLastWindowClosedDialog d;
614 QTimer::singleShot(1000, d.okButton, SLOT(animateClick()));
616 QSignalSpy appSpy(&app, SIGNAL(lastWindowClosed()));
619 // lastWindowClosed() signal should only be sent after the last dialog is closed
620 QCOMPARE(appSpy.count(), 2);
624 QApplication app(argc, 0, QApplication::GuiServer);
625 QSignalSpy appSpy(&app, SIGNAL(lastWindowClosed()));
629 connect(&timer1, SIGNAL(timeout()), &dialog, SLOT(accept()));
630 QSignalSpy spy1(&timer1, SIGNAL(timeout()));
631 timer1.setSingleShot(true);
634 QCOMPARE(spy1.count(), 1);
635 QCOMPARE(appSpy.count(), 0);
638 connect(&timer2, SIGNAL(timeout()), &app, SLOT(quit()));
639 QSignalSpy spy2(&timer2, SIGNAL(timeout()));
640 timer2.setSingleShot(true);
642 int returnValue = app.exec();
643 QCOMPARE(returnValue, 0);
644 QCOMPARE(spy2.count(), 1);
645 QCOMPARE(appSpy.count(), 0);
649 QApplication app(argc, 0, QApplication::GuiServer);
651 timer.setInterval(100);
653 QSignalSpy spy(&app, SIGNAL(aboutToQuit()));
654 QSignalSpy spy2(&timer, SIGNAL(timeout()));
656 QPointer<QMainWindow> mainWindow = new QMainWindow;
657 QPointer<QDialog> dialog = new QDialog(mainWindow);
659 QVERIFY(app.quitOnLastWindowClosed());
660 QVERIFY(mainWindow->testAttribute(Qt::WA_QuitOnClose));
661 QVERIFY(dialog->testAttribute(Qt::WA_QuitOnClose));
667 QTimer::singleShot(1000, mainWindow, SLOT(close())); // This should quit the application
668 QTimer::singleShot(2000, &app, SLOT(quit())); // This makes sure we quit even if it didn't
672 QCOMPARE(spy.count(), 1);
673 QVERIFY(spy2.count() < 15); // Should be around 10 if closing caused the quit
677 QApplication app(argc, 0, QApplication::GuiServer);
679 timer.setInterval(100);
681 QSignalSpy spy(&app, SIGNAL(aboutToQuit()));
682 QSignalSpy spy2(&timer, SIGNAL(timeout()));
684 QPointer<CloseEventTestWindow> mainWindow = new CloseEventTestWindow;
686 QVERIFY(app.quitOnLastWindowClosed());
687 QVERIFY(mainWindow->testAttribute(Qt::WA_QuitOnClose));
692 QTimer::singleShot(1000, mainWindow, SLOT(close())); // This should quit the application
693 QTimer::singleShot(2000, &app, SLOT(quit())); // This makes sure we quit even if it didn't
697 QCOMPARE(spy.count(), 1);
698 QVERIFY(spy2.count() > 15); // Should be around 20 if closing did not caused the quit
702 QApplication app(argc, 0, QApplication::GuiServer);
703 QSignalSpy appSpy(&app, SIGNAL(lastWindowClosed()));
705 // exec a dialog for 1 second, then show the window
706 QuitOnLastWindowClosedWindow window;
707 QTimer::singleShot(0, &window, SLOT(execDialogThenShow()));
710 QSignalSpy timerSpy(&timer, SIGNAL(timeout()));
711 connect(&timer, SIGNAL(timeout()), &window, SLOT(close()));
712 timer.setSingleShot(true);
714 int returnValue = app.exec();
715 QCOMPARE(returnValue, 0);
716 // failure here means the timer above didn't fire, and the
717 // quit was caused the the dialog being closed (not the window)
718 QCOMPARE(timerSpy.count(), 1);
719 QCOMPARE(appSpy.count(), 2);
723 class PromptOnCloseWidget : public QWidget
726 void closeEvent(QCloseEvent *event)
728 QMessageBox *messageBox = new QMessageBox(this);
729 messageBox->setWindowTitle("Unsaved data");
730 messageBox->setText("Would you like to save or discard your current data?");
731 messageBox->setStandardButtons(QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
732 messageBox->setDefaultButton(QMessageBox::Save);
735 QTest::qWaitForWindowShown(messageBox);
737 // verify that all windows are visible
738 foreach (QWidget *w, qApp->topLevelWidgets())
739 QVERIFY(w->isVisible());
741 qApp->processEvents();
743 qApp->closeAllWindows();
745 if (messageBox->standardButton(messageBox->clickedButton()) == QMessageBox::Cancel)
754 void tst_QApplication::closeAllWindows()
757 QApplication app(argc, 0, QApplication::GuiServer);
759 // create some windows
765 foreach (QWidget *w, app.topLevelWidgets()) {
767 QTest::qWaitForWindowShown(w);
769 // verify that they are visible
770 foreach (QWidget *w, app.topLevelWidgets())
771 QVERIFY(w->isVisible());
775 app.closeAllWindows();
776 // all windows should no longer be visible
777 foreach (QWidget *w, app.topLevelWidgets())
778 QVERIFY(!w->isVisible());
780 // add a window that prompts the user when closed
781 PromptOnCloseWidget *promptOnCloseWidget = new PromptOnCloseWidget;
783 foreach (QWidget *w, app.topLevelWidgets()) {
785 QTest::qWaitForWindowShown(w);
787 // close the last window to open the prompt (eventloop recurses)
788 promptOnCloseWidget->close();
789 // all windows should not be visible, except the one that opened the prompt
790 foreach (QWidget *w, app.topLevelWidgets()) {
791 if (w == promptOnCloseWidget)
792 QVERIFY(w->isVisible());
794 QVERIFY(!w->isVisible());
797 qDeleteAll(app.topLevelWidgets());
800 bool isPathListIncluded(const QStringList &l, const QStringList &r)
802 int size = r.count();
803 if (size > l.count())
805 #if defined (Q_OS_WIN)
806 Qt::CaseSensitivity cs = Qt::CaseInsensitive;
808 Qt::CaseSensitivity cs = Qt::CaseSensitive;
811 for ( ; i < l.count() && j < r.count(); ++i) {
812 if (QDir::toNativeSeparators(l[i]).compare(QDir::toNativeSeparators(r[j]), cs) == 0) {
817 return j == r.count();
820 #define QT_TST_QAPP_DEBUG
821 void tst_QApplication::libraryPaths()
825 QString testDir = QFileInfo(QFINDTESTDATA("test/test.pro")).absolutePath();
827 // On Windows CE we need QApplication object to have valid
828 // current Path. Therefore we need to identify it ourselves
829 // here for the test.
831 wchar_t module_name[MAX_PATH];
832 GetModuleFileName(0, module_name, MAX_PATH);
833 filePath = QString::fromWCharArray(module_name);
834 QString testDir = filePath.path() + "/test";
836 QApplication::setLibraryPaths(QStringList() << testDir);
837 QCOMPARE(QApplication::libraryPaths(), (QStringList() << testDir));
839 // creating QApplication adds the applicationDirPath to the libraryPath
841 QApplication app(argc, &argv0, QApplication::GuiServer);
842 QString appDirPath = QDir(app.applicationDirPath()).canonicalPath();
844 QStringList actual = QApplication::libraryPaths();
846 QStringList expected = QSet<QString>::fromList((QStringList() << testDir << appDirPath)).toList();
849 QVERIFY2(isPathListIncluded(actual, expected),
850 qPrintable("actual:\n - " + actual.join("\n - ") +
851 "\nexpected:\n - " + expected.join("\n - ")));
854 // creating QApplication adds the applicationDirPath and plugin install path to the libraryPath
856 QApplication app(argc, &argv0, QApplication::GuiServer);
857 QString appDirPath = app.applicationDirPath();
858 QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath);
860 QStringList actual = QApplication::libraryPaths();
863 QStringList expected = QSet<QString>::fromList((QStringList() << installPathPlugins << appDirPath)).toList();
866 QVERIFY2(isPathListIncluded(actual, expected),
867 qPrintable("actual:\n - " + actual.join("\n - ") +
868 "\nexpected:\n - " + expected.join("\n - ")));
870 // setting the library paths overrides everything
871 QString testDir = QFileInfo(QFINDTESTDATA("test/test.pro")).absolutePath();
872 QApplication::setLibraryPaths(QStringList() << testDir);
873 QVERIFY2(isPathListIncluded(QApplication::libraryPaths(), (QStringList() << testDir)),
874 qPrintable("actual:\n - " + QApplication::libraryPaths().join("\n - ") +
875 "\nexpected:\n - " + testDir));
878 #ifdef QT_TST_QAPP_DEBUG
879 qDebug() << "Initial library path:" << QApplication::libraryPaths();
882 int count = QApplication::libraryPaths().count();
884 // this test doesn't work if KDE 4 is installed
885 QCOMPARE(count, 1); // before creating QApplication, only the PluginsPath is in the libraryPaths()
887 QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath);
888 QApplication::addLibraryPath(installPathPlugins);
889 #ifdef QT_TST_QAPP_DEBUG
890 qDebug() << "installPathPlugins" << installPathPlugins;
891 qDebug() << "After adding plugins path:" << QApplication::libraryPaths();
893 QCOMPARE(QApplication::libraryPaths().count(), count);
894 QString testDir = QFileInfo(QFINDTESTDATA("test/test.pro")).absolutePath();
895 QApplication::addLibraryPath(testDir);
896 QCOMPARE(QApplication::libraryPaths().count(), count + 1);
898 // creating QApplication adds the applicationDirPath to the libraryPath
900 QApplication app(argc, &argv0, QApplication::GuiServer);
901 QString appDirPath = app.applicationDirPath();
902 qDebug() << QApplication::libraryPaths();
903 // On Windows CE these are identical and might also be the case for other
905 if (appDirPath != installPathPlugins)
906 QCOMPARE(QApplication::libraryPaths().count(), count + 2);
910 QApplication app(argc, &argv0, QApplication::GuiServer);
912 #ifdef QT_TST_QAPP_DEBUG
913 qDebug() << "Initial library path:" << app.libraryPaths();
915 int count = app.libraryPaths().count();
916 QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath);
917 app.addLibraryPath(installPathPlugins);
918 #ifdef QT_TST_QAPP_DEBUG
919 qDebug() << "installPathPlugins" << installPathPlugins;
920 qDebug() << "After adding plugins path:" << app.libraryPaths();
922 QCOMPARE(app.libraryPaths().count(), count);
924 QString appDirPath = app.applicationDirPath();
926 app.addLibraryPath(appDirPath);
928 app.addLibraryPath(appDirPath + "/../..");
930 app.addLibraryPath(appDirPath + "/..");
932 #ifdef QT_TST_QAPP_DEBUG
933 qDebug() << "appDirPath" << appDirPath;
934 qDebug() << "After adding appDirPath && appDirPath + /..:" << app.libraryPaths();
936 QCOMPARE(app.libraryPaths().count(), count + 1);
938 app.addLibraryPath(appDirPath + "/../MacOS");
940 app.addLibraryPath(appDirPath + "/tmp/..");
942 #ifdef QT_TST_QAPP_DEBUG
943 qDebug() << "After adding appDirPath + /tmp/..:" << app.libraryPaths();
945 QCOMPARE(app.libraryPaths().count(), count + 1);
949 void tst_QApplication::libraryPaths_qt_plugin_path()
953 QApplication app(argc, &argv0, QApplication::GuiServer);
954 QString appDirPath = app.applicationDirPath();
956 // Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
957 QString installPathPluginsDeCanon = appDirPath + QString::fromLatin1("/tmp/..");
958 QByteArray ascii = QFile::encodeName(installPathPluginsDeCanon);
959 qputenv("QT_PLUGIN_PATH", ascii);
961 QVERIFY(!app.libraryPaths().contains(appDirPath + QString::fromLatin1("/tmp/..")));
964 void tst_QApplication::libraryPaths_qt_plugin_path_2()
967 QByteArray validPath = QDir("/tmp").canonicalPath().toLatin1();
968 QByteArray nonExistentPath = "/nonexistent";
969 QByteArray pluginPath = validPath + ":" + nonExistentPath;
970 #elif defined(Q_OS_WIN)
972 QByteArray validPath = "/Temp";
973 QByteArray nonExistentPath = "/nonexistent";
974 QByteArray pluginPath = validPath + ";" + nonExistentPath;
976 QByteArray validPath = "C:\\windows";
977 QByteArray nonExistentPath = "Z:\\nonexistent";
978 QByteArray pluginPath = validPath + ";" + nonExistentPath;
983 // Our hook into libraryPaths() initialization: Set the QT_PLUGIN_PATH environment variable
984 qputenv("QT_PLUGIN_PATH", pluginPath);
988 QApplication app(argc, &argv0, QApplication::GuiServer);
990 // library path list should contain the default plus the one valid path
991 QStringList expected =
993 << QLibraryInfo::location(QLibraryInfo::PluginsPath)
994 << QDir(app.applicationDirPath()).canonicalPath()
995 << QDir(QDir::fromNativeSeparators(QString::fromLatin1(validPath))).canonicalPath();
997 expected = QSet<QString>::fromList(expected).toList();
999 QVERIFY2(isPathListIncluded(app.libraryPaths(), expected),
1000 qPrintable("actual:\n - " + app.libraryPaths().join("\n - ") +
1001 "\nexpected:\n - " + expected.join("\n - ")));
1007 QApplication app(argc, &argv0, QApplication::GuiServer);
1009 // library paths are initialized by the QApplication, setting
1010 // the environment variable here doesn't work
1011 qputenv("QT_PLUGIN_PATH", pluginPath);
1013 // library path list should contain the default
1014 QStringList expected =
1016 << QLibraryInfo::location(QLibraryInfo::PluginsPath)
1017 << app.applicationDirPath();
1019 expected = QSet<QString>::fromList(expected).toList();
1021 QVERIFY(isPathListIncluded(app.libraryPaths(), expected));
1023 qputenv("QT_PLUGIN_PATH", QByteArray());
1027 class SendPostedEventsTester : public QObject
1031 QList<int> eventSpy;
1032 bool event(QEvent *e);
1037 bool SendPostedEventsTester::event(QEvent *e)
1039 eventSpy.append(e->type());
1040 return QObject::event(e);
1043 void SendPostedEventsTester::doTest()
1045 QPointer<SendPostedEventsTester> p = this;
1046 QApplication::postEvent(this, new QEvent(QEvent::User));
1047 // DeferredDelete should not be delivered until returning from this function
1048 QApplication::postEvent(this, new QDeferredDeleteEvent());
1050 QEventLoop eventLoop;
1051 QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection);
1055 QCOMPARE(eventSpy.count(), 2);
1056 QCOMPARE(eventSpy.at(0), int(QEvent::MetaCall));
1057 QCOMPARE(eventSpy.at(1), int(QEvent::User));
1061 void tst_QApplication::sendPostedEvents()
1064 QApplication app(argc, 0, QApplication::GuiServer);
1065 SendPostedEventsTester *tester = new SendPostedEventsTester;
1066 QMetaObject::invokeMethod(tester, "doTest", Qt::QueuedConnection);
1067 QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
1068 QPointer<SendPostedEventsTester> p = tester;
1073 void tst_QApplication::thread()
1075 QThread *currentThread = QThread::currentThread();
1076 // no app, but still have a valid thread
1077 QVERIFY(currentThread != 0);
1079 // the thread should be running and not finished
1080 QVERIFY(currentThread->isRunning());
1081 QVERIFY(!currentThread->isFinished());
1083 // this should probably be in the tst_QObject::thread() test, but
1084 // we put it here since we want to make sure that objects created
1085 // *before* the QApplication has a thread
1087 QObject child(&object);
1088 QVERIFY(object.thread() == currentThread);
1089 QVERIFY(child.thread() == currentThread);
1093 QApplication app(argc, 0, QApplication::GuiServer);
1095 // current thread still valid
1096 QVERIFY(QThread::currentThread() != 0);
1097 // thread should be the same as before
1098 QCOMPARE(QThread::currentThread(), currentThread);
1100 // app's thread should be the current thread
1101 QCOMPARE(app.thread(), currentThread);
1103 // the thread should still be running and not finished
1104 QVERIFY(currentThread->isRunning());
1105 QVERIFY(!currentThread->isFinished());
1107 QTestEventLoop::instance().enterLoop(1);
1110 // app dead, current thread still valid
1111 QVERIFY(QThread::currentThread() != 0);
1112 QCOMPARE(QThread::currentThread(), currentThread);
1114 // the thread should still be running and not finished
1115 QVERIFY(currentThread->isRunning());
1116 QVERIFY(!currentThread->isFinished());
1118 // should still have a thread
1119 QVERIFY(object.thread() == currentThread);
1120 QVERIFY(child.thread() == currentThread);
1122 // do the test again, making sure that the thread is the same as
1126 QApplication app(argc, 0, QApplication::GuiServer);
1128 // current thread still valid
1129 QVERIFY(QThread::currentThread() != 0);
1130 // thread should be the same as before
1131 QCOMPARE(QThread::currentThread(), currentThread);
1133 // app's thread should be the current thread
1134 QCOMPARE(app.thread(), currentThread);
1136 // the thread should be running and not finished
1137 QVERIFY(currentThread->isRunning());
1138 QVERIFY(!currentThread->isFinished());
1140 // should still have a thread
1141 QVERIFY(object.thread() == currentThread);
1142 QVERIFY(child.thread() == currentThread);
1144 QTestEventLoop::instance().enterLoop(1);
1147 // app dead, current thread still valid
1148 QVERIFY(QThread::currentThread() != 0);
1149 QCOMPARE(QThread::currentThread(), currentThread);
1151 // the thread should still be running and not finished
1152 QVERIFY(currentThread->isRunning());
1153 QVERIFY(!currentThread->isFinished());
1155 // should still have a thread
1156 QVERIFY(object.thread() == currentThread);
1157 QVERIFY(child.thread() == currentThread);
1160 class DeleteLaterWidget : public QWidget
1164 DeleteLaterWidget(QApplication *_app, QWidget *parent = 0)
1165 : QWidget(parent) { app = _app; child_deleted = false; }
1172 void checkDeleteLater();
1173 void childDeleted() { child_deleted = true; }
1177 void DeleteLaterWidget::runTest()
1179 QObject *stillAlive = qFindChild<QObject*>(this, "deleteLater");
1181 QWidget *w = new QWidget(this);
1182 connect(w, SIGNAL(destroyed()), this, SLOT(childDeleted()));
1185 QVERIFY(!child_deleted);
1188 QTimer::singleShot(500, &dlg, SLOT(reject()));
1191 QVERIFY(!child_deleted);
1192 app->processEvents();
1193 QVERIFY(!child_deleted);
1195 QTimer::singleShot(500, this, SLOT(checkDeleteLater()));
1197 app->processEvents();
1199 QVERIFY(!stillAlive); // verify at the end to make test terminate
1202 void DeleteLaterWidget::checkDeleteLater()
1204 QVERIFY(child_deleted);
1209 void tst_QApplication::testDeleteLater()
1212 QSKIP("This test fails and then hangs on Mac OS X, see QTBUG-24318");
1215 QApplication app(argc, 0, QApplication::GuiServer);
1216 connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
1218 DeleteLaterWidget *wgt = new DeleteLaterWidget(&app);
1219 QTimer::singleShot(500, wgt, SLOT(runTest()));
1221 QObject *object = new QObject(wgt);
1222 object->setObjectName("deleteLater");
1223 object->deleteLater();
1225 QObject *stillAlive = qFindChild<QObject*>(wgt, "deleteLater");
1226 QVERIFY(stillAlive);
1234 class EventLoopNester : public QObject
1238 void deleteLaterAndEnterLoop()
1240 QEventLoop eventLoop;
1241 QPointer<QObject> p(this);
1244 DeferredDelete events are compressed, meaning this second
1245 deleteLater() will *not* delete the object in the nested
1248 QMetaObject::invokeMethod(this, "deleteLater", Qt::QueuedConnection);
1249 QTimer::singleShot(1000, &eventLoop, SLOT(quit()));
1253 void deleteLaterAndExitLoop()
1255 // Check that 'p' is not deleted before exec returns, since the call
1256 // to QEventLoop::quit() should stop 'eventLoop' from processing
1257 // any more events (that is, delete later) until we return to the
1258 // _current_ event loop:
1259 QEventLoop eventLoop;
1260 QPointer<QObject> p(this);
1261 QMetaObject::invokeMethod(this, "deleteLater", Qt::QueuedConnection);
1262 QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection);
1264 QVERIFY(p); // not dead yet
1267 void processEventsOnly()
1269 QApplication::processEvents();
1271 void sendPostedEventsWithDeferredDelete()
1273 QApplication::sendPostedEvents(0, QEvent::DeferredDelete);
1276 void deleteLaterAndProcessEvents()
1278 QEventLoop eventLoop;
1280 QPointer<QObject> p = this;
1283 // trying to delete this object in a deeper eventloop just won't work
1284 QMetaObject::invokeMethod(this,
1285 "processEventsOnly",
1286 Qt::QueuedConnection);
1287 QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection);
1290 QMetaObject::invokeMethod(this,
1291 "sendPostedEventsWithDeferredDelete",
1292 Qt::QueuedConnection);
1293 QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection);
1297 // trying to delete it from this eventloop still doesn't work
1298 QApplication::processEvents();
1301 // however, it *will* work with this magic incantation
1302 QApplication::sendPostedEvents(0, QEvent::DeferredDelete);
1307 void tst_QApplication::testDeleteLaterProcessEvents()
1311 // Calling processEvents() with no event dispatcher does nothing.
1312 QObject *object = new QObject;
1313 QPointer<QObject> p(object);
1314 object->deleteLater();
1315 QApplication::processEvents();
1320 QApplication app(argc, 0, QApplication::GuiServer);
1321 // If you call processEvents() with an event dispatcher present, but
1322 // outside any event loops, deferred deletes are not processed unless
1323 // sendPostedEvents(0, DeferredDelete) is called.
1324 object = new QObject;
1326 object->deleteLater();
1327 app.processEvents();
1329 QApplication::sendPostedEvents(0, QEvent::DeferredDelete);
1332 // If you call deleteLater() on an object when there is no parent
1333 // event loop, and then enter an event loop, the object will get
1335 object = new QObject;
1337 object->deleteLater();
1339 QTimer::singleShot(1000, &loop, SLOT(quit()));
1344 // When an object is in an event loop, then calls deleteLater() and enters
1345 // an event loop recursively, it should not die until the parent event
1347 QApplication app(argc, 0, QApplication::GuiServer);
1349 EventLoopNester *nester = new EventLoopNester;
1351 QTimer::singleShot(3000, &loop, SLOT(quit()));
1352 QTimer::singleShot(0, nester, SLOT(deleteLaterAndEnterLoop()));
1359 // When the event loop that calls deleteLater() is exited
1360 // immediately, the object should die when returning to the
1361 // parent event loop
1362 QApplication app(argc, 0, QApplication::GuiServer);
1364 EventLoopNester *nester = new EventLoopNester;
1366 QTimer::singleShot(3000, &loop, SLOT(quit()));
1367 QTimer::singleShot(0, nester, SLOT(deleteLaterAndExitLoop()));
1374 // when the event loop that calls deleteLater() also calls
1375 // processEvents() immediately afterwards, the object should
1376 // not die until the parent loop continues
1377 QApplication app(argc, 0, QApplication::GuiServer);
1379 EventLoopNester *nester = new EventLoopNester();
1381 QTimer::singleShot(3000, &loop, SLOT(quit()));
1382 QTimer::singleShot(0, nester, SLOT(deleteLaterAndProcessEvents()));
1390 Test for crash with QApplication::setDesktopSettingsAware(false).
1392 void tst_QApplication::desktopSettingsAware()
1394 #ifndef QT_NO_PROCESS
1397 // We need an application object for QFINDTESTDATA to work
1398 // properly in all cases.
1400 QCoreApplication app(argc, 0);
1401 path = QFINDTESTDATA("desktopsettingsaware/");
1403 QVERIFY2(!path.isEmpty(), "Cannot locate desktopsettingsaware helper application");
1404 path += "desktopsettingsaware";
1407 QApplication tmpApp(argc, 0, QApplication::GuiServer);
1409 QProcess testProcess;
1410 testProcess.start(path);
1411 QVERIFY2(testProcess.waitForStarted(),
1412 qPrintable(QString::fromLatin1("Cannot start '%1': %2").arg(path, testProcess.errorString())));
1413 QVERIFY(testProcess.waitForFinished(10000));
1414 QCOMPARE(int(testProcess.state()), int(QProcess::NotRunning));
1415 QVERIFY(int(testProcess.error()) != int(QProcess::Crashed));
1419 void tst_QApplication::setActiveWindow()
1422 QApplication MyApp(argc, 0, QApplication::GuiServer);
1424 QWidget* w = new QWidget;
1425 QVBoxLayout* layout = new QVBoxLayout(w);
1427 QLineEdit* pb1 = new QLineEdit("Testbutton1", w);
1428 QLineEdit* pb2 = new QLineEdit("Test Line Edit", w);
1430 layout->addWidget(pb1);
1431 layout->addWidget(pb2);
1438 QApplication::setActiveWindow(w); // needs this on twm (focus follows mouse)
1439 QVERIFY(pb1->hasFocus());
1444 /* This might fail on some X11 window managers? */
1445 void tst_QApplication::focusChanged()
1448 QApplication app(argc, 0, QApplication::GuiServer);
1450 QSignalSpy spy(&app, SIGNAL(focusChanged(QWidget *, QWidget *)));
1455 QHBoxLayout hbox1(&parent1);
1456 QLabel lb1(&parent1);
1457 QLineEdit le1(&parent1);
1458 QPushButton pb1(&parent1);
1459 hbox1.addWidget(&lb1);
1460 hbox1.addWidget(&le1);
1461 hbox1.addWidget(&pb1);
1463 QCOMPARE(spy.count(), 0);
1466 QApplication::setActiveWindow(&parent1); // needs this on twm (focus follows mouse)
1467 QCOMPARE(spy.count(), 1);
1468 QCOMPARE(spy.at(0).count(), 2);
1469 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1470 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1471 QVERIFY(now == &le1);
1472 QVERIFY(now == QApplication::focusWidget());
1475 QCOMPARE(spy.count(), 0);
1478 QCOMPARE(spy.count(), 1);
1479 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1480 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1481 QVERIFY(now == &pb1);
1482 QVERIFY(now == QApplication::focusWidget());
1483 QVERIFY(old == &le1);
1487 QCOMPARE(spy.count(), 1);
1488 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1489 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1490 QVERIFY(now == &lb1);
1491 QVERIFY(now == QApplication::focusWidget());
1492 QVERIFY(old == &pb1);
1496 QCOMPARE(spy.count(), 1);
1497 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1498 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1500 QVERIFY(now == QApplication::focusWidget());
1501 QVERIFY(old == &lb1);
1505 QHBoxLayout hbox2(&parent2);
1506 QLabel lb2(&parent2);
1507 QLineEdit le2(&parent2);
1508 QPushButton pb2(&parent2);
1509 hbox2.addWidget(&lb2);
1510 hbox2.addWidget(&le2);
1511 hbox2.addWidget(&pb2);
1514 QApplication::setActiveWindow(&parent2); // needs this on twm (focus follows mouse)
1515 QVERIFY(spy.count() > 0); // one for deactivation, one for activation on Windows
1516 old = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(0));
1517 now = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(1));
1518 QVERIFY(now == &le2);
1519 QVERIFY(now == QApplication::focusWidget());
1523 QTestKeyEvent tab(QTest::Press, Qt::Key_Tab, 0, 0);
1524 QTestKeyEvent backtab(QTest::Press, Qt::Key_Backtab, 0, 0);
1525 QTestMouseEvent click(QTest::MouseClick, Qt::LeftButton, 0, QPoint(5, 5), 0);
1527 bool tabAllControls = true;
1529 // Mac has two modes, one where you tab to everything, one where you can
1530 // only tab to input controls, here's what we get. Determine which ones we
1532 QSettings appleSettings(QLatin1String("apple.com"));
1533 QVariant appleValue = appleSettings.value(QLatin1String("AppleKeyboardUIMode"), 0);
1534 tabAllControls = (appleValue.toInt() & 0x2);
1535 if (!tabAllControls) {
1536 QEXPECT_FAIL("", "QTBUG-24372 Mac tab key \"Text boxes and lists only\" vs "
1537 "\"All controls\" setting is not respected in Qt5", Abort);
1541 // make sure Qt's idea of tabbing between widgets matches what we think it should
1542 QCOMPARE(qt_tab_all_widgets, tabAllControls);
1545 if (!tabAllControls) {
1546 QVERIFY(spy.count() == 0);
1547 QVERIFY(now == QApplication::focusWidget());
1549 QVERIFY(spy.count() > 0);
1550 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1551 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1552 QVERIFY(now == &pb2);
1553 QVERIFY(now == QApplication::focusWidget());
1554 QVERIFY(old == &le2);
1558 if (!tabAllControls) {
1559 QVERIFY(spy.count() == 0);
1560 QVERIFY(now == QApplication::focusWidget());
1563 QVERIFY(spy.count() > 0);
1564 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1565 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1566 QVERIFY(now == &le2);
1567 QVERIFY(now == QApplication::focusWidget());
1568 QVERIFY(old == &pb2);
1572 if (!tabAllControls) {
1573 QVERIFY(spy.count() == 0);
1574 QVERIFY(now == QApplication::focusWidget());
1576 backtab.simulate(now);
1577 QVERIFY(spy.count() > 0);
1578 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1579 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1580 QVERIFY(now == &pb2);
1581 QVERIFY(now == QApplication::focusWidget());
1582 QVERIFY(old == &le2);
1587 if (!tabAllControls) {
1588 QVERIFY(spy.count() == 0);
1589 QVERIFY(now == QApplication::focusWidget());
1592 backtab.simulate(now);
1593 QVERIFY(spy.count() > 0);
1594 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1595 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1596 QVERIFY(now == &le2);
1597 QVERIFY(now == QApplication::focusWidget());
1598 QVERIFY(old == &pb2);
1602 click.simulate(old);
1603 if (!(pb2.focusPolicy() & Qt::ClickFocus)) {
1604 QVERIFY(spy.count() == 0);
1605 QVERIFY(now == QApplication::focusWidget());
1607 QVERIFY(spy.count() > 0);
1608 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1609 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1610 QVERIFY(now == &pb2);
1611 QVERIFY(now == QApplication::focusWidget());
1612 QVERIFY(old == &le2);
1615 click.simulate(old);
1616 QVERIFY(spy.count() > 0);
1617 old = qvariant_cast<QWidget*>(spy.at(0).at(0));
1618 now = qvariant_cast<QWidget*>(spy.at(0).at(1));
1619 QVERIFY(now == &le2);
1620 QVERIFY(now == QApplication::focusWidget());
1621 QVERIFY(old == &pb2);
1625 parent1.activateWindow();
1626 QApplication::setActiveWindow(&parent1); // needs this on twm (focus follows mouse)
1627 QVERIFY(spy.count() == 1 || spy.count() == 2); // one for deactivation, one for activation on Windows
1629 //on windows, the change of focus is made in 2 steps
1630 //(the focusChanged SIGNAL is emitted twice)
1632 old = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(0));
1634 old = qvariant_cast<QWidget*>(spy.at(spy.count()-2).at(0));
1635 now = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(1));
1636 QVERIFY(now == &le1);
1637 QVERIFY(now == QApplication::focusWidget());
1638 QVERIFY(old == &le2);
1642 class LineEdit : public QLineEdit
1645 LineEdit(QWidget *parent = 0) : QLineEdit(parent) { }
1648 void focusOutEvent(QFocusEvent *e) {
1649 QLineEdit::focusOutEvent(e);
1650 if (objectName() == "le1")
1654 void focusInEvent(QFocusEvent *e) {
1655 QLineEdit::focusInEvent(e);
1656 if (objectName() == "le2")
1661 void tst_QApplication::focusOut()
1664 QApplication app(argc, &argv0, QApplication::GuiServer);
1666 // Tests the case where the style pointer changes when on focus in/out
1667 // (the above is the case when the stylesheet changes)
1669 QLineEdit *le1 = new LineEdit(&w);
1670 le1->setObjectName("le1");
1671 le1->setStyleSheet("background: #fee");
1674 QLineEdit *le2 = new LineEdit(&w);
1675 le2->setObjectName("le2");
1676 le2->setStyleSheet("background: #fee");
1677 le2->move(100, 100);
1685 void tst_QApplication::execAfterExit()
1688 QApplication app(argc, &argv0, QApplication::GuiServer);
1689 QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
1690 // this should be ignored, as exec() will reset the exitCode
1691 QApplication::exit(1);
1692 int exitCode = app.exec();
1693 QCOMPARE(exitCode, 0);
1695 // the quitNow flag should have been reset, so we can spin an
1696 // eventloop after QApplication::exec() returns
1697 QEventLoop eventLoop;
1698 QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection);
1699 exitCode = eventLoop.exec();
1700 QCOMPARE(exitCode, 0);
1703 void tst_QApplication::wheelScrollLines()
1706 QApplication app(argc, &argv0, QApplication::GuiServer);
1707 // If wheelScrollLines returns 0, the mose wheel will be disabled.
1708 QVERIFY(app.wheelScrollLines() > 0);
1711 void tst_QApplication::style()
1716 QApplication app(argc, &argv0, QApplication::GuiServer);
1717 QPointer<QStyle> style = app.style();
1718 app.setStyle(new QWindowsStyle);
1719 QVERIFY(style.isNull());
1722 QApplication app(argc, &argv0, QApplication::GuiServer);
1724 // qApp style can never be 0
1725 QVERIFY(QApplication::style() != 0);
1728 void tst_QApplication::allWidgets()
1731 QApplication app(argc, &argv0, QApplication::GuiServer);
1732 QWidget *w = new QWidget;
1733 QVERIFY(app.allWidgets().contains(w)); // uncreate widget test
1734 QVERIFY(app.allWidgets().contains(w)); // created widget test
1737 QVERIFY(!app.allWidgets().contains(w)); // removal test
1740 void tst_QApplication::topLevelWidgets()
1743 QApplication app(argc, &argv0, QApplication::GuiServer);
1744 QWidget *w = new QWidget;
1746 #ifndef QT_NO_CLIPBOARD
1747 QClipboard *clipboard = QApplication::clipboard();
1748 QString originalText = clipboard->text();
1749 clipboard->setText(QString("newText"));
1751 app.processEvents();
1752 QVERIFY(QApplication::topLevelWidgets().contains(w));
1753 QCOMPARE(QApplication::topLevelWidgets().count(), 1);
1756 app.processEvents();
1757 QCOMPARE(QApplication::topLevelWidgets().count(), 0);
1762 void tst_QApplication::setAttribute()
1765 QApplication app(argc, &argv0, QApplication::GuiServer);
1766 QVERIFY(!QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation));
1767 QWidget *w = new QWidget;
1768 QVERIFY(!w->testAttribute(Qt::WA_WState_Created));
1771 QApplication::setAttribute(Qt::AA_ImmediateWidgetCreation);
1772 QVERIFY(QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation));
1774 QVERIFY(w->testAttribute(Qt::WA_WState_Created));
1775 QWidget *w2 = new QWidget(w);
1777 QVERIFY(w2->testAttribute(Qt::WA_WState_Created));
1781 QApplication::setAttribute(Qt::AA_ImmediateWidgetCreation, false);
1782 QVERIFY(!QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation));
1784 QVERIFY(!w->testAttribute(Qt::WA_WState_Created));
1788 void tst_QApplication::windowsCommandLine_data()
1790 #if defined(Q_OS_WIN)
1791 QTest::addColumn<QString>("args");
1792 QTest::addColumn<QString>("expected");
1794 QTest::newRow("hello world")
1795 << QString("Hello \"World\"")
1796 << QString("Hello \"World\"");
1797 QTest::newRow("sql")
1798 << QString("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PNR' AND TABLE_TYPE = 'VIEW' ORDER BY TABLE_NAME")
1799 << QString("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PNR' AND TABLE_TYPE = 'VIEW' ORDER BY TABLE_NAME");
1803 void tst_QApplication::windowsCommandLine()
1805 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
1806 QFETCH(QString, args);
1807 QFETCH(QString, expected);
1809 QProcess testProcess;
1810 const QString path = QStringLiteral("wincmdline/wincmdline");
1811 testProcess.start(path, QStringList(args));
1812 QVERIFY2(testProcess.waitForStarted(),
1813 qPrintable(QString::fromLatin1("Cannot start '%1': %2").arg(path, testProcess.errorString())));
1814 QVERIFY(testProcess.waitForFinished(10000));
1815 QByteArray error = testProcess.readAllStandardError();
1816 QString procError(error);
1817 QCOMPARE(procError, expected);
1821 class TouchEventPropagationTestWidget : public QWidget
1826 bool seenTouchEvent, acceptTouchEvent, seenMouseEvent, acceptMouseEvent;
1828 TouchEventPropagationTestWidget(QWidget *parent = 0)
1829 : QWidget(parent), seenTouchEvent(false), acceptTouchEvent(false), seenMouseEvent(false), acceptMouseEvent(false)
1834 seenTouchEvent = acceptTouchEvent = seenMouseEvent = acceptMouseEvent = false;
1837 bool event(QEvent *event)
1839 switch (event->type()) {
1840 case QEvent::MouseButtonPress:
1841 case QEvent::MouseMove:
1842 case QEvent::MouseButtonRelease:
1843 // qDebug() << objectName() << "seenMouseEvent = true";
1844 seenMouseEvent = true;
1845 event->setAccepted(acceptMouseEvent);
1847 case QEvent::TouchBegin:
1848 case QEvent::TouchUpdate:
1849 case QEvent::TouchEnd:
1850 // qDebug() << objectName() << "seenTouchEvent = true";
1851 seenTouchEvent = true;
1852 event->setAccepted(acceptTouchEvent);
1855 return QWidget::event(event);
1861 void tst_QApplication::touchEventPropagation()
1864 QApplication app(argc, &argv0, QApplication::GuiServer);
1866 QList<QTouchEvent::TouchPoint> pressedTouchPoints;
1867 QTouchEvent::TouchPoint press(0);
1868 press.setState(Qt::TouchPointPressed);
1869 pressedTouchPoints << press;
1871 QList<QTouchEvent::TouchPoint> releasedTouchPoints;
1872 QTouchEvent::TouchPoint release(0);
1873 release.setState(Qt::TouchPointReleased);
1874 releasedTouchPoints << release;
1876 QTouchDevice *device = new QTouchDevice;
1877 device->setType(QTouchDevice::TouchScreen);
1878 QWindowSystemInterface::registerTouchDevice(device);
1881 // touch event behavior on a window
1882 TouchEventPropagationTestWidget window;
1883 window.setObjectName("1. window");
1884 window.show(); // Must have an explicitly specified QWindow for handleTouchEvent,
1885 // passing 0 would result in using topLevelAt() which is not ok in this case
1886 // as the screen position in the point is bogus.
1887 QTest::qWaitForWindowShown(&window);
1888 // QPA always takes screen positions and since we map the TouchPoint back to QPA's structure first,
1889 // we must ensure there is a screen position in the TouchPoint that maps to a local 0, 0.
1890 pressedTouchPoints[0].setScreenPos(window.mapToGlobal(QPoint(0, 0)));
1891 releasedTouchPoints[0].setScreenPos(window.mapToGlobal(QPoint(0, 0)));
1893 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1896 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1897 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1900 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1901 QCoreApplication::processEvents();
1902 QVERIFY(!window.seenTouchEvent);
1903 QVERIFY(!window.seenMouseEvent);
1906 window.setAttribute(Qt::WA_AcceptTouchEvents);
1907 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1910 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1911 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1914 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1915 QCoreApplication::processEvents();
1916 QVERIFY(window.seenTouchEvent);
1917 QVERIFY(!window.seenMouseEvent);
1920 window.acceptTouchEvent = true;
1921 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1924 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1925 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1928 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1929 QCoreApplication::processEvents();
1930 QVERIFY(window.seenTouchEvent);
1931 QVERIFY(!window.seenMouseEvent);
1935 // touch event behavior on a window with a child widget
1936 TouchEventPropagationTestWidget window;
1937 window.setObjectName("2. window");
1938 TouchEventPropagationTestWidget widget(&window);
1939 widget.setObjectName("2. widget");
1941 QTest::qWaitForWindowShown(&window);
1942 pressedTouchPoints[0].setScreenPos(window.mapToGlobal(QPoint(0, 0)));
1943 releasedTouchPoints[0].setScreenPos(window.mapToGlobal(QPoint(0, 0)));
1945 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1948 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1949 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1952 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1953 QCoreApplication::processEvents();
1954 QVERIFY(!widget.seenTouchEvent);
1955 QVERIFY(!widget.seenMouseEvent);
1956 QVERIFY(!window.seenTouchEvent);
1957 QVERIFY(!window.seenMouseEvent);
1961 widget.setAttribute(Qt::WA_AcceptTouchEvents);
1962 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1965 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1966 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1969 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1970 QCoreApplication::processEvents();
1971 QVERIFY(widget.seenTouchEvent);
1972 QVERIFY(!widget.seenMouseEvent);
1973 QVERIFY(!window.seenTouchEvent);
1974 QVERIFY(!window.seenMouseEvent);
1978 widget.acceptMouseEvent = true;
1979 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1982 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
1983 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1986 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
1987 QCoreApplication::processEvents();
1988 QVERIFY(widget.seenTouchEvent);
1989 QVERIFY(!widget.seenMouseEvent);
1990 QVERIFY(!window.seenTouchEvent);
1991 QVERIFY(!window.seenMouseEvent);
1995 widget.acceptTouchEvent = true;
1996 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
1999 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
2000 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2003 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
2004 QCoreApplication::processEvents();
2005 QVERIFY(widget.seenTouchEvent);
2006 QVERIFY(!widget.seenMouseEvent);
2007 QVERIFY(!window.seenTouchEvent);
2008 QVERIFY(!window.seenMouseEvent);
2012 widget.setAttribute(Qt::WA_AcceptTouchEvents, false);
2013 window.setAttribute(Qt::WA_AcceptTouchEvents);
2014 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2017 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
2018 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2021 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
2022 QCoreApplication::processEvents();
2023 QVERIFY(!widget.seenTouchEvent);
2024 QVERIFY(!widget.seenMouseEvent);
2025 QVERIFY(window.seenTouchEvent);
2026 QVERIFY(!window.seenMouseEvent);
2030 window.acceptTouchEvent = true;
2031 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2034 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
2035 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2038 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
2039 QCoreApplication::processEvents();
2040 QVERIFY(!widget.seenTouchEvent);
2041 QVERIFY(!widget.seenMouseEvent);
2042 QVERIFY(window.seenTouchEvent);
2043 QVERIFY(!window.seenMouseEvent);
2047 widget.acceptMouseEvent = true; // doesn't matter, touch events are propagated first
2048 window.acceptTouchEvent = true;
2049 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2052 QTest::QTouchEventSequence::touchPointList(pressedTouchPoints));
2053 QWindowSystemInterface::handleTouchEvent(window.windowHandle(),
2056 QTest::QTouchEventSequence::touchPointList(releasedTouchPoints));
2057 QCoreApplication::processEvents();
2058 QVERIFY(!widget.seenTouchEvent);
2059 QVERIFY(!widget.seenMouseEvent);
2060 QVERIFY(window.seenTouchEvent);
2061 QVERIFY(!window.seenMouseEvent);
2065 void tst_QApplication::qtbug_12673()
2069 // We need an application object for QFINDTESTDATA to work
2070 // properly in all cases.
2072 QCoreApplication app(argc, 0);
2073 path = QFINDTESTDATA("modal/");
2075 QVERIFY2(!path.isEmpty(), "Cannot locate modal helper application");
2078 QProcess testProcess;
2079 QStringList arguments;
2080 testProcess.start(path, arguments);
2081 QVERIFY2(testProcess.waitForStarted(),
2082 qPrintable(QString::fromLatin1("Cannot start '%1': %2").arg(path, testProcess.errorString())));
2083 QVERIFY(testProcess.waitForFinished(20000));
2084 QCOMPARE(testProcess.exitStatus(), QProcess::NormalExit);
2087 class NoQuitOnHideWidget : public QWidget
2091 explicit NoQuitOnHideWidget(QWidget *parent = 0)
2094 QTimer::singleShot(0, this, SLOT(hide()));
2095 QTimer::singleShot(500, this, SLOT(exitApp()));
2104 void tst_QApplication::noQuitOnHide()
2107 QApplication app(argc, 0);
2108 QWidget *window1 = new NoQuitOnHideWidget;
2110 QCOMPARE(app.exec(), 1);
2113 class ShowCloseShowWidget : public QWidget
2117 ShowCloseShowWidget(bool showAgain, QWidget *parent = 0)
2118 : QWidget(parent), showAgain(showAgain)
2120 QTimer::singleShot(0, this, SLOT(doClose()));
2121 QTimer::singleShot(500, this, SLOT(exitApp()));
2139 void tst_QApplication::abortQuitOnShow()
2142 QApplication app(argc, 0);
2143 QWidget *window1 = new ShowCloseShowWidget(false);
2145 QCOMPARE(app.exec(), 0);
2147 QWidget *window2 = new ShowCloseShowWidget(true);
2149 QCOMPARE(app.exec(), 1);
2153 This test is meant to ensure that certain objects (public & commonly used)
2154 can safely be used in a Q_GLOBAL_STATIC such that their destructors are
2155 executed *after* the destruction of QApplication.
2157 Q_GLOBAL_STATIC(QLocale, tst_qapp_locale);
2158 Q_GLOBAL_STATIC(QProcess, tst_qapp_process);
2159 Q_GLOBAL_STATIC(QFileSystemWatcher, tst_qapp_fileSystemWatcher);
2160 #ifndef QT_NO_SHAREDMEMORY
2161 Q_GLOBAL_STATIC(QSharedMemory, tst_qapp_sharedMemory);
2163 Q_GLOBAL_STATIC(QElapsedTimer, tst_qapp_elapsedTimer);
2164 Q_GLOBAL_STATIC(QMutex, tst_qapp_mutex);
2165 Q_GLOBAL_STATIC(QWidget, tst_qapp_widget);
2166 Q_GLOBAL_STATIC(QPixmap, tst_qapp_pixmap);
2167 Q_GLOBAL_STATIC(QFont, tst_qapp_font);
2168 Q_GLOBAL_STATIC(QRegion, tst_qapp_region);
2169 Q_GLOBAL_STATIC(QFontDatabase, tst_qapp_fontDatabase);
2170 Q_GLOBAL_STATIC(QCursor, tst_qapp_cursor);
2172 void tst_QApplication::globalStaticObjectDestruction()
2175 QApplication app(argc, &argv0, QApplication::GuiServer);
2176 QVERIFY(tst_qapp_locale());
2177 QVERIFY(tst_qapp_process());
2178 QVERIFY(tst_qapp_fileSystemWatcher());
2179 #ifndef QT_NO_SHAREDMEMORY
2180 QVERIFY(tst_qapp_sharedMemory());
2182 QVERIFY(tst_qapp_elapsedTimer());
2183 QVERIFY(tst_qapp_mutex());
2184 QVERIFY(tst_qapp_widget());
2185 QVERIFY(tst_qapp_pixmap());
2186 QVERIFY(tst_qapp_font());
2187 QVERIFY(tst_qapp_region());
2188 QVERIFY(tst_qapp_fontDatabase());
2189 QVERIFY(tst_qapp_cursor());
2192 //QTEST_APPLESS_MAIN(tst_QApplication)
2193 int main(int argc, char *argv[])
2195 tst_QApplication tc;
2197 return QTest::qExec(&tc, argc, argv);
2200 #include "tst_qapplication.moc"