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 <simon.hausmann@theqtcompany.com>
This commit is contained in:
parent
9e15fb156a
commit
35da68a15c
|
@ -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();
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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<QQuickItem*>(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());
|
||||
|
|
|
@ -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<QString, TestHTTPServer::Mode> 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 {
|
||||
|
|
|
@ -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"));
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QTimer>
|
||||
#include <QTest>
|
||||
|
||||
/*!
|
||||
\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<QString, TestHTTPServer::Mode> &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<QString, TestHTTPServer::Mode>::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);
|
||||
}
|
||||
|
|
|
@ -37,6 +37,9 @@
|
|||
#include <QTcpServer>
|
||||
#include <QUrl>
|
||||
#include <QPair>
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
|
||||
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<QString, TestHTTPServer::Mode> &dirs);
|
||||
~ThreadedTestHTTPServer();
|
||||
|
||||
QUrl baseUrl() const;
|
||||
QUrl url(const QString &documentPath) const;
|
||||
QString urlString(const QString &documentPath) const;
|
||||
|
||||
protected:
|
||||
void run();
|
||||
|
||||
private:
|
||||
void start();
|
||||
|
||||
QHash<QString, TestHTTPServer::Mode> m_dirs;
|
||||
quint16 m_port;
|
||||
QMutex m_mutex;
|
||||
QWaitCondition m_condition;
|
||||
};
|
||||
|
||||
#endif // TESTHTTPSERVER_H
|
||||
|
||||
|
|
Loading…
Reference in New Issue