From 35da68a15c12586415484def802839e67372eac1 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 25 Sep 2015 10:30:59 +0200 Subject: Provide a threaded TestHTTPServer This allows us to do blocking operations that interact with the test server in the main thread. The threaded server is used in tests that don't explicitly require asynchronous operation. Change-Id: Ibcb28e79a1114cb9cfb812e86aae0a1af71c569e Reviewed-by: Simon Hausmann --- .../auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 4 +- tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 24 ++----- .../qml/qqmlmoduleplugin/tst_qqmlmoduleplugin.cpp | 4 +- .../tst_qquickanimatedimage.cpp | 4 +- tests/auto/quick/qquickloader/tst_qquickloader.cpp | 21 ++---- .../quick/qquicktextedit/tst_qquicktextedit.cpp | 17 +++-- .../quick/qquicktextinput/tst_qquicktextinput.cpp | 5 +- tests/auto/shared/testhttpserver.cpp | 79 ++++++++++++++++++++-- tests/auto/shared/testhttpserver.h | 28 ++++++++ 9 files changed, 127 insertions(+), 59 deletions(-) (limited to 'tests') diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index f238bf6b90..b30dfcbd0b 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -4203,9 +4203,7 @@ void tst_qqmlecmascript::importScripts() QFETCH(QStringList, propertyNames); QFETCH(QVariantList, propertyValues); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory() + "/remote"); + ThreadedTestHTTPServer server(dataDirectory() + "/remote"); QStringList importPathList = engine.importPathList(); diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 3c7584bfa1..f1e12a0c00 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -2543,9 +2543,7 @@ void tst_qqmllanguage::basicRemote() QFETCH(QString, type); QFETCH(QString, error); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); url = server.baseUrl().resolved(url); @@ -2590,9 +2588,7 @@ void tst_qqmllanguage::importsRemote() QFETCH(QString, type); QFETCH(QString, error); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); qml.replace(QStringLiteral("{{ServerBaseUrl}}"), server.baseUrl().toString()); @@ -2685,9 +2681,7 @@ void tst_qqmllanguage::importsInstalledRemote() QFETCH(QString, type); QFETCH(QString, error); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QString serverdir = server.urlString("/lib/"); engine.setImportPathList(QStringList(defaultImportPathList) << serverdir); @@ -2752,9 +2746,7 @@ void tst_qqmllanguage::importsPath() QFETCH(QString, qml); QFETCH(QString, value); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); for (int i = 0; i < importPath.count(); ++i) importPath[i].replace(QStringLiteral("{{ServerBaseUrl}}"), server.baseUrl().toString()); @@ -3386,9 +3378,7 @@ void tst_qqmllanguage::registeredCompositeTypeWithAttachedProperty() // QTBUG-18268 void tst_qqmllanguage::remoteLoadCrash() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QQmlComponent component(&engine); component.setData("import QtQuick 2.0; Text {}", server.url("/remoteLoadCrash.qml")); @@ -3877,9 +3867,7 @@ void tst_qqmllanguage::compositeSingletonQmlDirError() // Load a remote composite singleton type via qmldir that defines the type as a singleton void tst_qqmllanguage::compositeSingletonRemote() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QFile f(testFile("singletonTest15.qml")); QVERIFY(f.open(QIODevice::ReadOnly)); diff --git a/tests/auto/qml/qqmlmoduleplugin/tst_qqmlmoduleplugin.cpp b/tests/auto/qml/qqmlmoduleplugin/tst_qqmlmoduleplugin.cpp index 77fc67a1ac..89477609ca 100644 --- a/tests/auto/qml/qqmlmoduleplugin/tst_qqmlmoduleplugin.cpp +++ b/tests/auto/qml/qqmlmoduleplugin/tst_qqmlmoduleplugin.cpp @@ -239,9 +239,7 @@ void tst_qqmlmoduleplugin::importPluginWithQmlFile() void tst_qqmlmoduleplugin::remoteImportWithQuotedUrl() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(m_dataImportsDirectory); + ThreadedTestHTTPServer server(m_dataImportsDirectory); QQmlEngine engine; QQmlComponent component(&engine); diff --git a/tests/auto/quick/qquickanimatedimage/tst_qquickanimatedimage.cpp b/tests/auto/quick/qquickanimatedimage/tst_qquickanimatedimage.cpp index 45694f38dd..f4c37b4d66 100644 --- a/tests/auto/quick/qquickanimatedimage/tst_qquickanimatedimage.cpp +++ b/tests/auto/quick/qquickanimatedimage/tst_qquickanimatedimage.cpp @@ -252,9 +252,7 @@ void tst_qquickanimatedimage::remote() QFETCH(QString, fileName); QFETCH(bool, paused); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QQmlEngine engine; QQmlComponent component(&engine, server.url(fileName)); diff --git a/tests/auto/quick/qquickloader/tst_qquickloader.cpp b/tests/auto/quick/qquickloader/tst_qquickloader.cpp index e72b38e06c..f4fab1d79f 100644 --- a/tests/auto/quick/qquickloader/tst_qquickloader.cpp +++ b/tests/auto/quick/qquickloader/tst_qquickloader.cpp @@ -435,9 +435,7 @@ void tst_QQuickLoader::noResize() void tst_QQuickLoader::networkRequestUrl() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QQmlComponent component(&engine); const QString qml = "import QtQuick 2.0\nLoader { property int signalCount : 0; source: \"" + server.baseUrl().toString() + "/Rect120x60.qml\"; onLoaded: signalCount += 1 }"; @@ -460,9 +458,7 @@ void tst_QQuickLoader::networkRequestUrl() /* XXX Component waits until all dependencies are loaded. Is this actually possible? */ void tst_QQuickLoader::networkComponent() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory(), TestHTTPServer::Delay); + ThreadedTestHTTPServer server(dataDirectory(), TestHTTPServer::Delay); QQmlComponent component(&engine); const QString qml = "import QtQuick 2.0\n" @@ -471,8 +467,9 @@ void tst_QQuickLoader::networkComponent() " Component { id: comp; NW.Rect120x60 {} }\n" " Loader { sourceComponent: comp } }"; component.setData(qml.toUtf8(), dataDirectory()); - QCOMPARE(component.status(), QQmlComponent::Loading); - server.sendDelayedItem(); + // The component may be loaded synchronously or asynchronously, so we cannot test for + // status == Loading here. Also, it makes no sense to instruct the server to send here + // because in the synchronous case we're already done loading. QTRY_COMPARE(component.status(), QQmlComponent::Ready); QQuickItem *item = qobject_cast(component.create()); @@ -492,9 +489,7 @@ void tst_QQuickLoader::networkComponent() void tst_QQuickLoader::failNetworkRequest() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); QTest::ignoreMessage(QtWarningMsg, QString(server.baseUrl().toString() + "/IDontExist.qml: File not found").toUtf8()); @@ -708,9 +703,7 @@ void tst_QQuickLoader::initialPropertyValues() QFETCH(QStringList, propertyNames); QFETCH(QVariantList, propertyValues); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory()); + ThreadedTestHTTPServer server(dataDirectory()); foreach (const QString &warning, expectedWarnings) QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData()); diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index ece9a0841b..e4473b9540 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -2599,9 +2599,7 @@ void tst_qquicktextedit::cursorDelegate() void tst_qquicktextedit::remoteCursorDelegate() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory(), TestHTTPServer::Delay); + ThreadedTestHTTPServer server(dataDirectory(), TestHTTPServer::Delay); QQuickView view; @@ -2737,20 +2735,21 @@ void tst_qquicktextedit::delegateLoading() QFETCH(QString, qmlfile); QFETCH(QString, error); - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(testFile("httpfail"), TestHTTPServer::Disconnect); - server.serveDirectory(testFile("httpslow"), TestHTTPServer::Delay); - server.serveDirectory(testFile("http")); + QHash dirs; + dirs[testFile("httpfail")] = TestHTTPServer::Disconnect; + dirs[testFile("httpslow")] = TestHTTPServer::Delay; + dirs[testFile("http")] = TestHTTPServer::Normal; + ThreadedTestHTTPServer server(dirs); error.replace(QStringLiteral("{{ServerBaseUrl}}"), server.baseUrl().toString()); + if (!error.isEmpty()) + QTest::ignoreMessage(QtWarningMsg, error.toUtf8()); QQuickView view(server.url(qmlfile)); view.show(); view.requestActivate(); if (!error.isEmpty()) { - QTest::ignoreMessage(QtWarningMsg, error.toUtf8()); QTRY_VERIFY(view.status()==QQuickView::Error); QTRY_VERIFY(!view.rootObject()); // there is fail item inside this test } else { diff --git a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp index ec62d1a57b..493c03d00e 100644 --- a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp @@ -2857,10 +2857,7 @@ void tst_qquicktextinput::cursorDelegate() void tst_qquicktextinput::remoteCursorDelegate() { - TestHTTPServer server; - QVERIFY2(server.listen(), qPrintable(server.errorString())); - server.serveDirectory(dataDirectory(), TestHTTPServer::Delay); - + ThreadedTestHTTPServer server(dataDirectory(), TestHTTPServer::Delay); QQuickView view; QQmlComponent component(view.engine(), server.url("/RemoteCursor.qml")); diff --git a/tests/auto/shared/testhttpserver.cpp b/tests/auto/shared/testhttpserver.cpp index 6c466293b6..80ce10cacd 100644 --- a/tests/auto/shared/testhttpserver.cpp +++ b/tests/auto/shared/testhttpserver.cpp @@ -36,6 +36,7 @@ #include #include #include +#include /*! \internal @@ -80,6 +81,16 @@ The following request urls will then result in the appropriate action: \row \li http://localhost:14445/slowMain.qml \li slowMain.qml returned after 500ms \endtable */ + +static QUrl localHostUrl(quint16 port) +{ + QUrl url; + url.setScheme(QStringLiteral("http")); + url.setHost(QStringLiteral("127.0.0.1")); + url.setPort(port); + return url; +} + TestHTTPServer::TestHTTPServer() : m_state(AwaitingHeader) { @@ -93,11 +104,12 @@ bool TestHTTPServer::listen() QUrl TestHTTPServer::baseUrl() const { - QUrl url; - url.setScheme(QStringLiteral("http")); - url.setHost(QStringLiteral("127.0.0.1")); - url.setPort(m_server.serverPort()); - return url; + return localHostUrl(m_server.serverPort()); +} + +quint16 TestHTTPServer::port() const +{ + return m_server.serverPort(); } QUrl TestHTTPServer::url(const QString &documentPath) const @@ -377,3 +389,60 @@ void TestHTTPServer::serveGET(QTcpSocket *socket, const QByteArray &data) socket->disconnectFromHost(); } } + +ThreadedTestHTTPServer::ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode) : + m_port(0) +{ + m_dirs[dir] = mode; + start(); +} + +ThreadedTestHTTPServer::ThreadedTestHTTPServer(const QHash &dirs) : + m_dirs(dirs), m_port(0) +{ + start(); +} + +ThreadedTestHTTPServer::~ThreadedTestHTTPServer() +{ + quit(); + wait(); +} + +QUrl ThreadedTestHTTPServer::baseUrl() const +{ + return localHostUrl(m_port); +} + +QUrl ThreadedTestHTTPServer::url(const QString &documentPath) const +{ + return baseUrl().resolved(documentPath); +} + +QString ThreadedTestHTTPServer::urlString(const QString &documentPath) const +{ + return url(documentPath).toString(); +} + +void ThreadedTestHTTPServer::run() +{ + TestHTTPServer server; + { + QMutexLocker locker(&m_mutex); + QVERIFY2(server.listen(), qPrintable(server.errorString())); + m_port = server.port(); + for (QHash::ConstIterator i = m_dirs.constBegin(); + i != m_dirs.constEnd(); ++i) { + server.serveDirectory(i.key(), i.value()); + } + m_condition.wakeAll(); + } + exec(); +} + +void ThreadedTestHTTPServer::start() +{ + QMutexLocker locker(&m_mutex); + QThread::start(); + m_condition.wait(&m_mutex); +} diff --git a/tests/auto/shared/testhttpserver.h b/tests/auto/shared/testhttpserver.h index 0fc8e4a79c..bf826b247b 100644 --- a/tests/auto/shared/testhttpserver.h +++ b/tests/auto/shared/testhttpserver.h @@ -37,6 +37,9 @@ #include #include #include +#include +#include +#include class TestHTTPServer : public QObject { @@ -45,6 +48,7 @@ public: TestHTTPServer(); bool listen(); + quint16 port() const; QUrl baseUrl() const; QUrl url(const QString &documentPath) const; QString urlString(const QString &documentPath) const; @@ -100,5 +104,29 @@ private: QTcpServer m_server; }; +class ThreadedTestHTTPServer : public QThread +{ + Q_OBJECT +public: + ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode = TestHTTPServer::Normal); + ThreadedTestHTTPServer(const QHash &dirs); + ~ThreadedTestHTTPServer(); + + QUrl baseUrl() const; + QUrl url(const QString &documentPath) const; + QString urlString(const QString &documentPath) const; + +protected: + void run(); + +private: + void start(); + + QHash m_dirs; + quint16 m_port; + QMutex m_mutex; + QWaitCondition m_condition; +}; + #endif // TESTHTTPSERVER_H -- cgit v1.2.3