From f8beab1d7390095114e1d5f02097751dd1b07345 Mon Sep 17 00:00:00 2001 From: Tasuku Suzuki Date: Sun, 30 Nov 2014 01:40:42 +0900 Subject: support XMLHttpRequest sync mode [ChangeLog][QtQml][XMLHttpRequest] Supported synchronous requests Change-Id: Ia38fcf97f212a14657bb519240d8406368a72390 Reviewed-by: Simon Hausmann --- .../auto/qml/qqmlxmlhttprequest/data/open_sync.qml | 14 ++++---- .../qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp | 39 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml b/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml index 0f31c966fa..3c73141954 100644 --- a/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml +++ b/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml @@ -1,17 +1,15 @@ import QtQuick 2.0 QtObject { - property bool exceptionThrown: false + property url url + property string responseText Component.onCompleted: { var x = new XMLHttpRequest; - - try { - x.open("GET", "http://www.qt-project.org", false); - } catch (e) { - if (e.code == DOMException.NOT_SUPPORTED_ERR) - exceptionThrown = true; - } + x.open("GET", url, false); + x.setRequestHeader("Accept-Language", "en-US"); + x.send(); + responseText = x.responseText; } } diff --git a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp index 441893a853..0d70e47b3d 100644 --- a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp +++ b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include "testhttpserver.h" #include "../../shared/util.h" @@ -282,14 +283,46 @@ void tst_qqmlxmlhttprequest::open_invalid_method() QCOMPARE(object->property("exceptionThrown").toBool(), true); } -// Test that calling XMLHttpRequest.open() with sync raises an exception +class TestThreadedHTTPServer : public QObject +{ + Q_OBJECT +public: + TestThreadedHTTPServer(const QUrl &expectUrl, const QUrl &replyUrl, const QUrl &bodyUrl) + : m_server(Q_NULLPTR) { + moveToThread(&m_thread); + m_thread.start(); + QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection, Q_ARG(QUrl, expectUrl), Q_ARG(QUrl, replyUrl), Q_ARG(QUrl, bodyUrl)); + } + ~TestThreadedHTTPServer() { + m_server->deleteLater(); + m_thread.exit(); + m_thread.wait(); + } + +private slots: + void start(const QUrl &expectUrl, const QUrl &replyUrl, const QUrl &bodyUrl) { + m_server = new TestHTTPServer; + QVERIFY2(m_server->listen(SERVER_PORT), qPrintable(m_server->errorString())); + QVERIFY(m_server->wait(expectUrl, replyUrl, bodyUrl)); + } + +private: + TestHTTPServer *m_server; + QThread m_thread; +}; + +// Test that calling XMLHttpRequest.open() with sync void tst_qqmlxmlhttprequest::open_sync() { + TestThreadedHTTPServer server(testFileUrl("open_network.expect"), testFileUrl("open_network.reply"), testFileUrl("testdocument.html")); + QQmlComponent component(&engine, testFileUrl("open_sync.qml")); - QScopedPointer object(component.create()); + QScopedPointer object(component.beginCreate(engine.rootContext())); QVERIFY(!object.isNull()); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); - QCOMPARE(object->property("exceptionThrown").toBool(), true); + QCOMPARE(object->property("responseText").toString(), QStringLiteral("QML Rocks!\n")); } // Calling with incorrect arg count raises an exception -- cgit v1.2.3