From bffbcfd0ccc863884701e79a260b1b05f9dc8bb0 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 11 Jan 2012 16:20:29 +0100 Subject: Stabilize Language-Change test. - Introduce separate state machine class with a timer instead of using semi-synchronous qWait(). Also invoke closeAllWindows() repeatedly should the file dialog be slow on Windows. - Use QTemporaryDir for test data to avoid conflicts with remains from previous tests. Change-Id: Ibd95176b44ff20d6f326dc3139fb386472f64c2b Reviewed-by: Rohan McGovern --- .../other/languagechange/tst_languagechange.cpp | 82 ++++++++++++++++------ 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/tests/auto/other/languagechange/tst_languagechange.cpp b/tests/auto/other/languagechange/tst_languagechange.cpp index f6a0341ac5..12b35795fe 100644 --- a/tests/auto/other/languagechange/tst_languagechange.cpp +++ b/tests/auto/other/languagechange/tst_languagechange.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -116,16 +117,53 @@ public: virtual bool isEmpty() const { return false; } -public slots: - void install() { - QCoreApplication::installTranslator(this); - QTest::qWait(2500); - QApplication::closeAllWindows(); - } public: mutable QSet m_translations; }; +// Install the translator and close all application windows after a while to +// quit the event loop. +class LanguageTestStateMachine : public QObject +{ + Q_OBJECT +public: + LanguageTestStateMachine(QTranslator *translator); + void start() { m_timer.start(); } + +private slots: + void timeout(); + +private: + enum State { InstallTranslator, CloseDialog }; + + QTimer m_timer; + QTranslator *m_translator; + State m_state; +}; + +LanguageTestStateMachine::LanguageTestStateMachine(QTranslator *translator) : + m_translator(translator), m_state(InstallTranslator) +{ + m_timer.setInterval(500); + connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout())); +} + +void LanguageTestStateMachine::timeout() +{ + switch (m_state) { + case InstallTranslator: + m_timer.stop(); + QCoreApplication::installTranslator(m_translator); + m_timer.setInterval(2500); + m_timer.start(); + m_state = CloseDialog; + break; + case CloseDialog: // Close repeatedly in case file dialog is slow. + QApplication::closeAllWindows(); + break; + } +} + enum DialogType { InputDialog = 1, ColorDialog, @@ -208,17 +246,20 @@ void tst_languageChange::retranslatability() "get proper widget layout."); TransformTranslator translator; - QTimer::singleShot(500, &translator, SLOT(install())); + LanguageTestStateMachine stateMachine(&translator); + switch (dialogType) { case InputDialog: - (void)QInputDialog::getInteger(0, QLatin1String("title"), QLatin1String("label")); + stateMachine.start(); + QInputDialog::getInteger(0, QLatin1String("title"), QLatin1String("label")); break; case ColorDialog: #ifdef Q_OS_MAC QSKIP("The native color dialog is used on Mac OS"); #else - (void)QColorDialog::getColor(); + stateMachine.start(); + QColorDialog::getColor(); #endif break; case FileDialog: { @@ -227,29 +268,30 @@ void tst_languageChange::retranslatability() #endif QFileDialog dlg; dlg.setOption(QFileDialog::DontUseNativeDialog); - QString tmpParentDir = QDir::tempPath(); - if (!tmpParentDir.endsWith(QLatin1Char('/'))) - tmpParentDir += QLatin1Char('/'); - tmpParentDir += QStringLiteral("languagechangetestdir"); - const QString tmpDir = tmpParentDir + QStringLiteral("/finaldir"); - const QString fooName = tmpParentDir + QStringLiteral("/foo"); + QString tempDirPattern = QDir::tempPath(); + if (!tempDirPattern.endsWith(QLatin1Char('/'))) + tempDirPattern += QLatin1Char('/'); + tempDirPattern += QStringLiteral("languagechangetestdirXXXXXX"); + QTemporaryDir temporaryDir(tempDirPattern); + temporaryDir.setAutoRemove(true); + QVERIFY(temporaryDir.isValid()); + const QString finalDir = temporaryDir.path() + QStringLiteral("/finaldir"); + const QString fooName = temporaryDir.path() + QStringLiteral("/foo"); QDir dir; - QCOMPARE(dir.mkpath(tmpDir), true); + QVERIFY(dir.mkpath(finalDir)); QFile fooFile(fooName); QVERIFY(fooFile.open(QIODevice::WriteOnly|QIODevice::Text)); fooFile.write("test"); fooFile.close(); - dlg.setDirectory(tmpParentDir); + dlg.setDirectory(temporaryDir.path()); #ifdef Q_OS_WINCE dlg.setDirectory("\\Windows"); #endif dlg.setFileMode(QFileDialog::ExistingFiles); dlg.setViewMode(QFileDialog::Detail); + stateMachine.start(); dlg.exec(); QTest::qWait(3000); - QCOMPARE(QFile::remove(fooName), true); - QCOMPARE(dir.rmdir(tmpDir), true); - QCOMPARE(dir.rmdir(tmpParentDir), true); break; } } -- cgit v1.2.3