diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-01-07 16:13:16 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-01-15 10:44:48 +0000 |
commit | bc315ce05298cf500f45f3a897b0f7c0408fd611 (patch) | |
tree | 7560fbe0e18f63dcc62a5e9d0f328b3cb2b67bec /tests/auto/widgets/qwebenginescript | |
parent | 4bf31a52de2f9c8d049d2fd7410b9cfb88d41168 (diff) |
Add API to set WebChannel on isolated world
Make it possible to set a web-channel so that it can only be accessed
by private scripts.
Pulls in needed API extension in 3rdparty.
Task-number: QTBUG-50318
Change-Id: I61bcce5c318dffe0a406ee8cddf31f58a021c22c
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
Diffstat (limited to 'tests/auto/widgets/qwebenginescript')
-rw-r--r-- | tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp b/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp index 3231293bf..ad10234f4 100644 --- a/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp +++ b/tests/auto/widgets/qwebenginescript/tst_qwebenginescript.cpp @@ -24,6 +24,7 @@ #include <qwebenginescriptcollection.h> #include <qwebengineview.h> #include "../util.h" +#include <QWebChannel> class tst_QWebEngineScript: public QObject { Q_OBJECT @@ -34,7 +35,8 @@ private Q_SLOTS: void injectionPoint_data(); void scriptWorld(); void scriptModifications(); - + void webChannel_data(); + void webChannel(); }; void tst_QWebEngineScript::domEditing() @@ -150,6 +152,72 @@ void tst_QWebEngineScript::scriptModifications() QVERIFY(page.scripts().count() == 0); } +class TestObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) +public: + TestObject(QObject *parent = 0) : QObject(parent) { } + + void setText(const QString &text) + { + if (text == m_text) + return; + m_text = text; + emit textChanged(text); + } + + QString text() const { return m_text; } + +signals: + void textChanged(const QString &text); + +private: + QString m_text; +}; + + +void tst_QWebEngineScript::webChannel_data() +{ + QTest::addColumn<int>("worldId"); + QTest::newRow("MainWorld") << static_cast<int>(QWebEngineScript::MainWorld); + QTest::newRow("ApplicationWorld") << static_cast<int>(QWebEngineScript::ApplicationWorld); +} + +void tst_QWebEngineScript::webChannel() +{ + QFETCH(int, worldId); + QWebEnginePage page; + TestObject testObject; + QScopedPointer<QWebChannel> channel(new QWebChannel(this)); + channel->registerObject(QStringLiteral("object"), &testObject); + page.setWebChannel(channel.data(), worldId); + + QFile qwebchanneljs(":/qwebchannel.js"); + QVERIFY(qwebchanneljs.exists()); + qwebchanneljs.open(QFile::ReadOnly); + QByteArray scriptSrc = qwebchanneljs.readAll(); + qwebchanneljs.close(); + QWebEngineScript script; + script.setInjectionPoint(QWebEngineScript::DocumentCreation); + script.setWorldId(worldId); + script.setSourceCode(QString::fromLatin1(scriptSrc)); + page.scripts().insert(script); + page.setHtml(QStringLiteral("<html><body></body></html>")); + waitForSignal(&page, SIGNAL(loadFinished(bool))); + page.runJavaScript(QLatin1String( + "new QWebChannel(qt.webChannelTransport," + " function(channel) {" + " channel.objects.object.text = 'test';" + " }" + ");"), worldId); + waitForSignal(&testObject, SIGNAL(textChanged(QString))); + QCOMPARE(testObject.text(), QStringLiteral("test")); + + if (worldId != QWebEngineScript::MainWorld) + QCOMPARE(evaluateJavaScriptSync(&page, "qt.webChannelTransport"), QVariant(QVariant::Invalid)); +} + QTEST_MAIN(tst_QWebEngineScript) #include "tst_qwebenginescript.moc" |