diff options
Diffstat (limited to 'tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp')
-rw-r--r-- | tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp | 292 |
1 files changed, 235 insertions, 57 deletions
diff --git a/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp b/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp index be9e59b8c..9ba13589f 100644 --- a/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp +++ b/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp @@ -25,11 +25,33 @@ #include <qwebenginescriptcollection.h> #include <qwebenginesettings.h> #include <qwebengineview.h> -#include "../util.h" +#include <util.h> #if QT_CONFIG(webengine_webchannel) #include <QWebChannel> #endif +static bool verifyOrder(QStringList orderList) +{ + QStringList expected = { + "DocumentCreation", + "DOMContentLoaded", + "DocumentReady", + "load", + "Deferred" + }; + + if (orderList.size() != 5) + return false; + if (orderList.at(4) == "load (timeout)") { + if (orderList.at(3) != "Deferred") + return false; + expected[3] = "Deferred"; + expected[4] = "load (timeout)"; + } + + return orderList == expected; +} + class tst_QWebEngineScript: public QObject { Q_OBJECT @@ -48,9 +70,13 @@ private Q_SLOTS: void webChannelWithExistingQtObject(); void navigation(); void webChannelWithBadString(); + void webChannelWithJavaScriptDisabled(); #endif void noTransportWithoutWebChannel(); void scriptsInNestedIframes(); + void matchQrcUrl(); + void injectionOrder(); + void reloadWithSubframes(); }; void tst_QWebEngineScript::domEditing() @@ -101,15 +127,28 @@ void tst_QWebEngineScript::loadEvents() script.setRunsOnSubFrames(true); if (injectionPoint == QWebEngineScript::DocumentCreation) { script.setSourceCode(QStringLiteral(R"( - var log = ["DocumentCreation"]; - for (let type of ["DOMContentLoaded", "load"]) { - window.addEventListener(type, () => log.push(type)); + var log = ['DocumentCreation']; + var timestamps = {'DocumentCreation': Date.now()}; + for (let type of ['DOMContentLoaded', 'load']) { + window.addEventListener(type, () => { + timestamps[type] = Date.now(); + if (type === 'load' && log.includes('Deferred') && timestamps['Deferred'] - timestamps['DOMContentLoaded'] > 500) + log.push(type + ' (timeout)'); + else + log.push(type); + }); } )")); } else if (injectionPoint == QWebEngineScript::DocumentReady) { - script.setSourceCode(QStringLiteral(R"(log.push("DocumentReady"))")); + script.setSourceCode(QStringLiteral(R"( + timestamps['DocumentReady'] = Date.now(); + log.push('DocumentReady'); + )")); } else { - script.setSourceCode(QStringLiteral(R"(log.push("Deferred"))")); + script.setSourceCode(QStringLiteral(R"( + timestamps['Deferred'] = Date.now(); + log.push('Deferred'); + )")); } return script; } @@ -144,49 +183,49 @@ void tst_QWebEngineScript::loadEvents() profile.pages.emplace_back(profile); Page &page = profile.pages.back(); - const QStringList expected = { - "DocumentCreation", - "DOMContentLoaded", - "DocumentReady", - "load", - "Deferred" - }; - // Single frame / setHtml page.setHtml(QStringLiteral("<!DOCTYPE html><html><head><title>mr</title></head><body></body></html>")); - QTRY_COMPARE(page.spy.count(), 1); - QCOMPARE(page.spy.takeFirst().value(0).toBool(), true); - QCOMPARE(page.eval("window.log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList(), expected); + QTRY_COMPARE_WITH_TIMEOUT(page.spy.size(), 1, 20000); + QVERIFY(page.spy.takeFirst().value(0).toBool()); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); + + // After discard + page.setLifecycleState(QWebEnginePage::LifecycleState::Discarded); + page.setLifecycleState(QWebEnginePage::LifecycleState::Active); + QTRY_COMPARE_WITH_TIMEOUT(page.spy.size(), 1, 20000); + QVERIFY(page.spy.takeFirst().value(0).toBool()); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); // Multiple frames page.load(QUrl("qrc:/resources/test_iframe_main.html")); - QTRY_COMPARE(page.spy.count(), 1); - QCOMPARE(page.spy.takeFirst().value(0).toBool(), true); - QCOMPARE(page.eval("window.log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList(), expected); - QCOMPARE(page.eval("window[0].log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(page.eval("window[0].log", QWebEngineScript::ApplicationWorld).toStringList(), expected); - QCOMPARE(page.eval("window[0][0].log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(page.eval("window[0][0].log", QWebEngineScript::ApplicationWorld).toStringList(), expected); + QTRY_COMPARE_WITH_TIMEOUT(page.spy.size(), 1, 20000); + QVERIFY(page.spy.takeFirst().value(0).toBool()); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window[0].log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window[0].log", QWebEngineScript::ApplicationWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window[0][0].log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window[0][0].log", QWebEngineScript::ApplicationWorld).toStringList())); // Cross-process navigation page.load(QUrl("chrome://gpu")); - QTRY_COMPARE(page.spy.count(), 1); - QCOMPARE(page.spy.takeFirst().value(0).toBool(), true); - QCOMPARE(page.eval("window.log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList(), expected); + QTRY_COMPARE_WITH_TIMEOUT(page.spy.size(), 1, 20000); + QVERIFY(page.spy.takeFirst().value(0).toBool()); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(page.eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); // Using window.open from JS QVERIFY(profile.pages.size() == 1); page.load(QUrl("qrc:/resources/test_window_open.html")); - QTRY_VERIFY(profile.pages.size() == 2); - QTRY_COMPARE(profile.pages.front().spy.count(), 1); - QTRY_COMPARE(profile.pages.back().spy.count(), 1); - QCOMPARE(profile.pages.front().eval("window.log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(profile.pages.front().eval("window.log", QWebEngineScript::ApplicationWorld).toStringList(), expected); - QCOMPARE(profile.pages.back().eval("window.log", QWebEngineScript::MainWorld).toStringList(), expected); - QCOMPARE(profile.pages.back().eval("window.log", QWebEngineScript::ApplicationWorld).toStringList(), expected); + QTRY_COMPARE(profile.pages.size(), 2u); + QTRY_COMPARE(profile.pages.front().spy.size(), 1); + QTRY_COMPARE(profile.pages.back().spy.size(), 1); + QVERIFY(verifyOrder(profile.pages.front().eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(profile.pages.front().eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); + QVERIFY(verifyOrder(profile.pages.back().eval("window.log", QWebEngineScript::MainWorld).toStringList())); + QVERIFY(verifyOrder(profile.pages.back().eval("window.log", QWebEngineScript::ApplicationWorld).toStringList())); } void tst_QWebEngineScript::scriptWorld_data() @@ -233,7 +272,7 @@ void tst_QWebEngineScript::scriptDisabled() page.scripts().insert(script); page.load(QUrl("about:blank")); QSignalSpy spy(&page, &QWebEnginePage::loadFinished); - QTRY_COMPARE(spy.count(), 1); + QTRY_COMPARE(spy.size(), 1); QCOMPARE(spy.takeFirst().value(0).toBool(), true); // MainWorld scripts are disabled by the setting... QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "foo", QWebEngineScript::MainWorld), QVariant()); @@ -242,7 +281,7 @@ void tst_QWebEngineScript::scriptDisabled() page.scripts().clear(); page.scripts().insert(script); page.load(QUrl("about:blank")); - QTRY_COMPARE(spy.count(), 1); + QTRY_COMPARE(spy.size(), 1); QCOMPARE(spy.takeFirst().value(0).toBool(), true); // ...but ApplicationWorld scripts should still work QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "foo", QWebEngineScript::MainWorld), QVariant()); @@ -260,7 +299,7 @@ void tst_QWebEngineScript::viewSource() page.scripts().insert(script); page.load(QUrl("view-source:about:blank")); QSignalSpy spy(&page, &QWebEnginePage::loadFinished); - QTRY_COMPARE(spy.count(), 1); + QTRY_COMPARE(spy.size(), 1); QCOMPARE(spy.takeFirst().value(0).toBool(), true); QCOMPARE(evaluateJavaScriptSync(&page, "foo"), QVariant(42)); } @@ -286,8 +325,8 @@ void tst_QWebEngineScript::scriptModifications() QVERIFY(spyFinished.wait()); QCOMPARE(evaluateJavaScriptSync(&page, "document.body.innerText"), QVariant::fromValue(QStringLiteral("SUCCESS"))); QVERIFY(page.scripts().count() == 1); - QWebEngineScript s = page.scripts().findScript(QStringLiteral("String1")); - QVERIFY(page.scripts().remove(s)); + QList<QWebEngineScript> s = page.scripts().find(QStringLiteral("String1")); + QVERIFY(page.scripts().remove(s.first())); QVERIFY(page.scripts().count() == 0); } @@ -379,7 +418,7 @@ void tst_QWebEngineScript::webChannel() QCOMPARE(testObject.text(), QStringLiteral("test")); if (worldId != QWebEngineScript::MainWorld) - QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant(QVariant::Invalid)); + QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant()); } #endif void tst_QWebEngineScript::noTransportWithoutWebChannel() @@ -387,11 +426,11 @@ void tst_QWebEngineScript::noTransportWithoutWebChannel() QWebEnginePage page; page.setHtml(QStringLiteral("<html><body></body></html>")); - QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant(QVariant::Invalid)); + QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant()); page.triggerAction(QWebEnginePage::Reload); QSignalSpy spyFinished(&page, &QWebEnginePage::loadFinished); QVERIFY(spyFinished.wait()); - QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant(QVariant::Invalid)); + QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant()); } void tst_QWebEngineScript::scriptsInNestedIframes() @@ -419,7 +458,7 @@ void tst_QWebEngineScript::scriptsInNestedIframes() QSignalSpy spyFinished(&page, &QWebEnginePage::loadFinished); page.load(QUrl("qrc:/resources/test_iframe_main.html")); view.show(); - QVERIFY(spyFinished.wait()); + QTRY_VERIFY_WITH_TIMEOUT(spyFinished.size() > 0, 20000); // Check that main frame has modified content. QCOMPARE( @@ -452,9 +491,9 @@ void tst_QWebEngineScript::webChannelResettingAndUnsetting() // There should be no webChannelTransport yet. QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::MainWorld), - QVariant(QVariant::Invalid)); + QVariant()); QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::ApplicationWorld), - QVariant(QVariant::Invalid)); + QVariant()); QWebChannel channel; page.setWebChannel(&channel, QWebEngineScript::MainWorld); @@ -463,13 +502,13 @@ void tst_QWebEngineScript::webChannelResettingAndUnsetting() QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::MainWorld), QVariant(QVariantMap())); QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::ApplicationWorld), - QVariant(QVariant::Invalid)); + QVariant()); page.setWebChannel(&channel, QWebEngineScript::ApplicationWorld); // Now it should have moved to ApplicationWorld. QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::MainWorld), - QVariant(QVariant::Invalid)); + QVariant()); QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::ApplicationWorld), QVariant(QVariantMap())); @@ -477,9 +516,9 @@ void tst_QWebEngineScript::webChannelResettingAndUnsetting() // And now it should be gone again. QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::MainWorld), - QVariant(QVariant::Invalid)); + QVariant()); QCOMPARE(evaluateJavaScriptSyncInWorld(&page, "qt.webChannelTransport", QWebEngineScript::ApplicationWorld), - QVariant(QVariant::Invalid)); + QVariant()); } void tst_QWebEngineScript::webChannelWithExistingQtObject() @@ -487,7 +526,7 @@ void tst_QWebEngineScript::webChannelWithExistingQtObject() QWebEnginePage page; evaluateJavaScriptSync(&page, "qt = 42"); - QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant(QVariant::Invalid)); + QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant()); QWebChannel channel; page.setWebChannel(&channel); @@ -519,18 +558,23 @@ void tst_QWebEngineScript::navigation() QString url1 = QStringLiteral("about:blank"); page.setUrl(url1); - QTRY_COMPARE(spyTextChanged.count(), 1); + QTRY_COMPARE(spyTextChanged.size(), 1); QCOMPARE(testObject.text(), url1); QString url2 = QStringLiteral("chrome://gpu/"); page.setUrl(url2); - QTRY_COMPARE(spyTextChanged.count(), 2); + QTRY_COMPARE(spyTextChanged.size(), 2); QCOMPARE(testObject.text(), url2); QString url3 = QStringLiteral("qrc:/resources/test_iframe_main.html"); page.setUrl(url3); - QTRY_COMPARE(spyTextChanged.count(), 3); + QTRY_COMPARE(spyTextChanged.size(), 3); QCOMPARE(testObject.text(), url3); + + page.setLifecycleState(QWebEnginePage::LifecycleState::Discarded); + page.setUrl(url1); + QTRY_COMPARE(spyTextChanged.size(), 4); + QCOMPARE(testObject.text(), url1); } // Try to set TestObject::text to an invalid UTF-16 string. @@ -546,9 +590,143 @@ void tst_QWebEngineScript::webChannelWithBadString() page.setWebChannel(&channel); page.setUrl(QStringLiteral("qrc:/resources/webChannelWithBadString.html")); QVERIFY(hostSpy.wait(20000)); - QCOMPARE(host.text(), QString(QChar(QChar::ReplacementCharacter))); + // expect 0xD800 see https://chromium-review.googlesource.com/c/1282993 + QChar data(0xd800); + QCOMPARE(host.text(), data); +} + +void tst_QWebEngineScript::webChannelWithJavaScriptDisabled() +{ + QWebEnginePage page; + QSignalSpy spyFinished(&page, &QWebEnginePage::loadFinished); + // JavaScript disabled in main world + page.settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false); + + TestObject testObject; + QScopedPointer<QWebChannel> channel(new QWebChannel(this)); + channel->registerObject(QStringLiteral("object"), &testObject); + page.setWebChannel(channel.data(), QWebEngineScript::ApplicationWorld); + + QWebEngineScript script = webChannelScript(); + script.setWorldId(QWebEngineScript::ApplicationWorld); + page.scripts().insert(script); + + page.setHtml(QStringLiteral("<html><body></body></html>")); + QVERIFY(spyFinished.wait()); + + QSignalSpy spyTextChanged(&testObject, &TestObject::textChanged); + page.runJavaScript(QLatin1String( + "new QWebChannel(qt.webChannelTransport," + " function(channel) {" + " channel.objects.object.text = 'test';" + " }" + ");"), QWebEngineScript::ApplicationWorld); + QVERIFY(spyTextChanged.wait()); + QCOMPARE(testObject.text(), QStringLiteral("test")); } #endif + +void tst_QWebEngineScript::matchQrcUrl() +{ + QWebEngineProfile profile; + QWebEnginePage page(&profile); + QWebEngineScript s; + s.setInjectionPoint(QWebEngineScript::DocumentReady); + s.setWorldId(QWebEngineScript::MainWorld); + s.setSourceCode(QStringLiteral(R"( +// ==UserScript== +// @match qrc:/*title_b.html +// ==/UserScript== + +document.title = 'New title'; + )")); + page.scripts().insert(s); + loadSync(&page, QUrl("qrc:/resources/title_a.html")); + QCOMPARE(page.title(), "A"); + loadSync(&page, QUrl("qrc:/resources/title_b.html")); + QCOMPARE(page.title(), "New title"); +} + +// Add many scripts and check order of execution. +void tst_QWebEngineScript::injectionOrder() +{ + QWebEngineProfile profile; + class Page : public QWebEnginePage + { + public: + Page(QWebEngineProfile *profile) : QWebEnginePage(profile) {} + QVector<QString> log; + + protected: + void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel, const QString &message, int, + const QString &) override + { + log.append(message); + } + } page(&profile); + QWebEngineScript::InjectionPoint points[] = { + QWebEngineScript::DocumentCreation, + QWebEngineScript::DocumentReady, + QWebEngineScript::Deferred, + }; + int nPoints = 3; + int nCollections = 2; + int nScripts = 5; + + QVector<QString> expected; + for (int iPoint = 0; iPoint != nPoints; ++iPoint) { + for (int iCollection = 0; iCollection != nCollections; ++iCollection) { + for (int iScript = 0; iScript != nScripts; ++iScript) { + QWebEngineScript script; + script.setName(QString("%1%2%3").arg(iPoint).arg(iCollection).arg(iScript)); + expected.append(script.name()); + script.setInjectionPoint(points[iPoint]); + script.setWorldId(QWebEngineScript::MainWorld); + script.setSourceCode(QStringLiteral("console.error('%1');").arg(script.name())); + if (iCollection == 0) + profile.scripts()->insert(script); + else + page.scripts().insert(script); + } + } + } + + page.load(QUrl("qrc:/resources/test_iframe_inner.html")); + QTRY_COMPARE(page.log, expected); +} + +void tst_QWebEngineScript::reloadWithSubframes() +{ + class Page : public QWebEnginePage + { + public: + Page() : QWebEnginePage() {} + QVector<QString> log; + + protected: + void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel, const QString &message, int, + const QString &) override + { + log.append(message); + } + } page; + + QWebEngineScript s; + s.setInjectionPoint(QWebEngineScript::DocumentCreation); + s.setSourceCode(QStringLiteral("console.log('Hello');")); + page.scripts().insert(s); + + page.setHtml(QStringLiteral("<body>" + " <h1>Test scripts working on reload </h1>" + " <iframe src='about://blank'>" + " </iframe>" + "</body>")); + QTRY_COMPARE(page.log.size(), 1); + + page.triggerAction(QWebEnginePage::Reload); + QTRY_COMPARE(page.log.size(), 2); +} + QTEST_MAIN(tst_QWebEngineScript) #include "tst_qwebenginescript.moc" |