aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2018-03-28 16:49:19 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-04-09 13:42:24 +0000
commit51b73e0bb68812d78315af032546750d04656c02 (patch)
treeb9d5c063af458ed62786cc95d89c132543dbeb3f /tests
parentccad6b577016c8a0986f56b2656471896b5817ea (diff)
Fix XMLHttpRequest when used with QQmlEngine::evaluate
Our XHR implementation insists on a valid QQmlContext when processing callbacks. This is to protect against callbacks being triggered after dynamic QML contexts such as delegates have been destroyed. Unfortunately those checks are too strict and make it impossible to use XHR from within plain JS scripts (where v4->callingQmlContext() will return a null pointer). Dispatching the callbacks in functions that are directly called from QML/JS is safe and something we can do unconditionally. This applies to the callbacks triggered from abort() and open() for example. When we're called from QNetworkAccessManager we should enforce the continued existence of a QML context only if it was present at send() time. Task-number: QTBUG-67337 Change-Id: I8235f6ef407adc3eaeeff4eee72238ba6750afb2 Reviewed-by: Michael Brasser <michael.brasser@live.com> Reviewed-by: Valery Kotov <vkotov@luxoft.com> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmlxmlhttprequest/data/noqmlcontext.js11
-rw-r--r--tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp26
2 files changed, 37 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/noqmlcontext.js b/tests/auto/qml/qqmlxmlhttprequest/data/noqmlcontext.js
new file mode 100644
index 0000000000..adb7269310
--- /dev/null
+++ b/tests/auto/qml/qqmlxmlhttprequest/data/noqmlcontext.js
@@ -0,0 +1,11 @@
+(function(url, resultCollector) {
+ var x = new XMLHttpRequest;
+ x.open("GET", url);
+ x.setRequestHeader("Accept-Language","en-US");
+ x.onreadystatechange = function() {
+ if (x.readyState == XMLHttpRequest.DONE) {
+ resultCollector.responseText = x.responseText
+ }
+ }
+ x.send()
+})
diff --git a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp
index 59716acc0d..ecce6515ed 100644
--- a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp
+++ b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp
@@ -108,6 +108,8 @@ private slots:
void text();
void cdata();
+ void noQmlContext();
+
// Crashes
// void outstanding_request_at_shutdown();
@@ -1243,6 +1245,30 @@ void tst_qqmlxmlhttprequest::cdata()
QCOMPARE(object->property("status").toInt(), 200);
}
+void tst_qqmlxmlhttprequest::noQmlContext()
+{
+ TestHTTPServer server;
+ QVERIFY2(server.listen(), qPrintable(server.errorString()));
+ QVERIFY(server.wait(testFileUrl("open_network.expect"),
+ testFileUrl("open_network.reply"),
+ testFileUrl("testdocument.html")));
+ QUrl url = server.urlString(QStringLiteral("/testdocument.html"));
+
+ QQmlEngine engine;
+
+ QFile f(testFile("noqmlcontext.js"));
+ QVERIFY(f.open(QIODevice::ReadOnly));
+ QString script = QString::fromUtf8(f.readAll());
+ QJSValue testFunction = engine.evaluate(script);
+ QVERIFY(testFunction.isCallable());
+
+ QJSValue resultCollector = engine.newObject();
+
+ testFunction.call(QJSValueList() << url.toString() << resultCollector);
+
+ QTRY_COMPARE(resultCollector.property("responseText").toString(), "QML Rocks!\n");
+ }
+
void tst_qqmlxmlhttprequest::stateChangeCallingContext()
{
#ifdef Q_OS_WIN