summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-01-10 14:21:05 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-06 16:32:20 +0100
commit54f66cc7a1e17155e90a1d3b5c33f627dbd0d50f (patch)
tree6dd4b35eda74487977bfae5bb0b81a133dfc1bf4 /tests
parent05bafd509ca302fc63465fece7cd0c33ec602e31 (diff)
downloadqtwebchannel-54f66cc7a1e17155e90a1d3b5c33f627dbd0d50f.tar.gz
Make the underlying transport mechanism of the webchannel pluggable.
This enables us to optionally use navigator.qt instead of a WebSocket, which is nicer setup-wise and is also slightly faster: navigator.qt: 284.0 msecs per iteration (total: 2,840, iterations: 10) WebSocket: 295.8 msecs per iteration (total: 2,959, iterations: 10) The baseline is ca. 203 msecs, which would mean a performance boost of ca. 12.7%. Furthermore, this sets the fundation to eventually add a WebEngine transport mechanism. The WebViewTransport should also be removed and instead the WebView itself should directly implement the WebChannelTransportInterface. Change-Id: I368bb27e38ffa2f17ffeb7f5ae695690f6f5ad21 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/qml/WebChannelTest.qml38
-rw-r--r--tests/qml/data/bench_init.html4
-rw-r--r--tests/qml/data/disconnect.html4
-rw-r--r--tests/qml/data/grouping.html4
-rw-r--r--tests/qml/data/method.html4
-rw-r--r--tests/qml/data/property.html4
-rw-r--r--tests/qml/data/receiveRaw.html6
-rw-r--r--tests/qml/data/respond.html6
-rw-r--r--tests/qml/data/send.html6
-rw-r--r--tests/qml/data/signal.html4
-rw-r--r--tests/qml/data/testsetup.js47
-rw-r--r--tests/qml/data/wrapper.html4
-rw-r--r--tests/qml/tst_bench.qml13
-rw-r--r--tests/qml/tst_webchannel.qml8
-rw-r--r--tests/webchannel/tst_webchannel.cpp28
-rw-r--r--tests/webchannel/tst_webchannel.h23
16 files changed, 141 insertions, 62 deletions
diff --git a/tests/qml/WebChannelTest.qml b/tests/qml/WebChannelTest.qml
index 1f84c1b..b3121d3 100644
--- a/tests/qml/WebChannelTest.qml
+++ b/tests/qml/WebChannelTest.qml
@@ -47,15 +47,25 @@ import QtWebKit 3.0
import QtWebKit.experimental 1.0
TestCase {
+ property var lastLoadStatus
+ property bool useWebViewTransport: false
+
// only run after the webchannel has finished initialization
- when: webChannel.baseUrl != ""
+ when: webSocketTransport.baseUrl != ""
- property var lastLoadStatus
+ WebViewTransport {
+ id: webViewTransport
+ webViewExperimental: view.experimental
+ }
+ WebSocketTransport {
+ id: webSocketTransport
+ }
WebView {
id: view
experimental.preferences.developerExtrasEnabled: true
+ experimental.preferences.navigatorQtObjectEnabled: true
onLoadingChanged: {
// NOTE: we cannot use spy.signalArguments nor save the loadRequest anywhere, as it gets
@@ -70,6 +80,7 @@ TestCase {
signalName: "onLoadingChanged"
}
}
+ property var view: view
WebChannel {
id: webChannel
@@ -78,23 +89,23 @@ TestCase {
SignalSpy {
id: rawMessageSpy
- target: webChannel
- signalName: "onRawMessageReceived"
+ target: useWebViewTransport ? webViewTransport : webSocketTransport;
+ signalName: "onMessageReceived"
}
property var rawMessageSpy: rawMessageSpy
property var rawMessageIdx: 0;
- SignalSpy {
- id: pongSpy
- target: webChannel
- signalName: "onPongReceived"
- }
- property var pongSpy: pongSpy
-
function loadUrl(url)
{
- verify(webChannel.baseUrl != "", "webChannel.baseUrl is empty");
- view.url = "data/" + url + "?webChannelBaseUrl=" + webChannel.baseUrl;
+ if (useWebViewTransport) {
+ webChannel.disconnectFrom(webSocketTransport);
+ webChannel.connectTo(webViewTransport);
+ } else {
+ webChannel.disconnectFrom(webViewTransport);
+ webChannel.connectTo(webSocketTransport);
+ }
+ verify(useWebViewTransport || webSocketTransport.baseUrl != "", "webSocketTransport.baseUrl is empty");
+ view.url = "data/" + url + (!useWebViewTransport ? "?webChannelBaseUrl=" + webSocketTransport.baseUrl : "");
// now wait for page to finish loading
do {
loadingSpy.wait(500);
@@ -108,7 +119,6 @@ TestCase {
loadingSpy.clear();
rawMessageSpy.clear();
rawMessageIdx = 0;
- pongSpy.clear();
}
function awaitRawMessage()
diff --git a/tests/qml/data/bench_init.html b/tests/qml/data/bench_init.html
index cb1b488..7d3af5b 100644
--- a/tests/qml/data/bench_init.html
+++ b/tests/qml/data/bench_init.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {});
+ createWebChannel(function(channel) {});
//END SETUP
</script>
</head>
diff --git a/tests/qml/data/disconnect.html b/tests/qml/data/disconnect.html
index dc771b9..a9a479c 100644
--- a/tests/qml/data/disconnect.html
+++ b/tests/qml/data/disconnect.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
myObj.mySignal.connect(function(arg) {
channel.exec({label: "mySignalReceived", args: [arg]});
myObj.mySignal.disconnect(this);
diff --git a/tests/qml/data/grouping.html b/tests/qml/data/grouping.html
index 2a6c158..60fa0f8 100644
--- a/tests/qml/data/grouping.html
+++ b/tests/qml/data/grouping.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- window.channel = new QWebChannel(baseUrl, function(channel) {
+ window.channel = createWebChannel(function(channel) {
channel.subscribe("Qt.propertyUpdate", function() {
channel.exec({label: "gotPropertyUpdate", values: [myObj.myProperty(), myOtherObj.foo(), myOtherObj.bar()]});
});
diff --git a/tests/qml/data/method.html b/tests/qml/data/method.html
index 04048a6..6dbaa90 100644
--- a/tests/qml/data/method.html
+++ b/tests/qml/data/method.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
channel.subscribe("invokeMethod", function(arg) {
myObj.myMethod(arg);
});
diff --git a/tests/qml/data/property.html b/tests/qml/data/property.html
index 5d03118..9565aaa 100644
--- a/tests/qml/data/property.html
+++ b/tests/qml/data/property.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
channel.exec({label: "init", value: myObj.myProperty()});
myObj.myPropertyChanged.connect(function() {
channel.exec({label: "changed", value: myObj.myProperty()});
diff --git a/tests/qml/data/receiveRaw.html b/tests/qml/data/receiveRaw.html
index 7c98bab..139b2b1 100644
--- a/tests/qml/data/receiveRaw.html
+++ b/tests/qml/data/receiveRaw.html
@@ -1,12 +1,12 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
channel.send("foobar");
- }, true);
+ }, true /* raw */);
//END SETUP
</script>
</head>
diff --git a/tests/qml/data/respond.html b/tests/qml/data/respond.html
index 6c8db0d..5f0e7fc 100644
--- a/tests/qml/data/respond.html
+++ b/tests/qml/data/respond.html
@@ -1,14 +1,14 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
channel.exec("foobar", function(response) {
channel.send("received:"+response);
});
- }, true);
+ }, true /* raw */);
//END SETUP
</script>
</head>
diff --git a/tests/qml/data/send.html b/tests/qml/data/send.html
index 7d601a8..c60fbf4 100644
--- a/tests/qml/data/send.html
+++ b/tests/qml/data/send.html
@@ -1,14 +1,14 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
channel.subscribe("myMessage", function(payload) {
channel.send("myMessagePong:" + payload);
});
- }, true);
+ }, true /* raw */);
//END SETUP
</script>
</head>
diff --git a/tests/qml/data/signal.html b/tests/qml/data/signal.html
index 92b0ed5..bdce0c7 100644
--- a/tests/qml/data/signal.html
+++ b/tests/qml/data/signal.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
myObj.mySignal.connect(function(arg) {
channel.exec({label: "signalReceived", value: arg});
});
diff --git a/tests/qml/data/testsetup.js b/tests/qml/data/testsetup.js
new file mode 100644
index 0000000..c0db83a
--- /dev/null
+++ b/tests/qml/data/testsetup.js
@@ -0,0 +1,47 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+window.createWebChannel = function(callback, raw)
+{
+ var baseUrlMatch = /[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search);
+ var transport = baseUrlMatch ? baseUrlMatch[1] : navigator.qt;
+ return new QWebChannel(transport, callback, raw);
+}
diff --git a/tests/qml/data/wrapper.html b/tests/qml/data/wrapper.html
index 9c4386c..a556486 100644
--- a/tests/qml/data/wrapper.html
+++ b/tests/qml/data/wrapper.html
@@ -1,10 +1,10 @@
<html>
<head>
<script type="text/javascript" src="qrc:///qwebchannel/qwebchannel.js"></script>
+ <script type="text/javascript" src="testsetup.js"></script>
<script type="text/javascript">
//BEGIN SETUP
- var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
- new QWebChannel(baseUrl, function(channel) {
+ createWebChannel(function(channel) {
myFactory.create("testObj", function(obj) {
window[obj.objectName()] = obj;
obj.mySignal.connect(function(arg1, arg2) {
diff --git a/tests/qml/tst_bench.qml b/tests/qml/tst_bench.qml
index 405bc08..056dda9 100644
--- a/tests/qml/tst_bench.qml
+++ b/tests/qml/tst_bench.qml
@@ -101,8 +101,19 @@ WebChannelTest {
loadUrl("bench_init.html");
}
- function benchmark_init()
+ function benchmark_init_webview()
{
+ useWebViewTransport = true;
+ loadUrl("bench_init.html");
+ // init
+ awaitMessage();
+ // idle
+ awaitMessage();
+ }
+
+ function benchmark_init_websocket()
+ {
+ useWebViewTransport = false;
loadUrl("bench_init.html");
// init
awaitMessage();
diff --git a/tests/qml/tst_webchannel.qml b/tests/qml/tst_webchannel.qml
index b357358..160a1af 100644
--- a/tests/qml/tst_webchannel.qml
+++ b/tests/qml/tst_webchannel.qml
@@ -66,12 +66,4 @@ WebChannelTest {
webChannel.respond(msg.id, "barfoo");
compare(awaitRawMessage(), "received:barfoo");
}
-
- function test_ping()
- {
- loadUrl("respond.html");
- webChannel.ping();
- pongSpy.wait(500);
- compare(pongSpy.count, 1);
- }
}
diff --git a/tests/webchannel/tst_webchannel.cpp b/tests/webchannel/tst_webchannel.cpp
index 0d05913..bb08c21 100644
--- a/tests/webchannel/tst_webchannel.cpp
+++ b/tests/webchannel/tst_webchannel.cpp
@@ -44,11 +44,13 @@
#include <qwebchannel.h>
#include <qwebchannel_p.h>
#include <qmetaobjectpublisher_p.h>
+#include <qwebsockettransport.h>
#include <QtTest>
TestWebChannel::TestWebChannel(QObject *parent)
: QObject(parent)
+ , m_dummyTransport(new DummyTransport(this))
, m_lastInt(0)
, m_lastDouble(0)
{
@@ -74,19 +76,18 @@ void TestWebChannel::setVariant(const QVariant &v)
m_lastVariant = v;
}
-void TestWebChannel::testInitChannel()
+void TestWebChannel::testInitWebSocketTransport()
{
- QWebChannel channel;
-
- QSignalSpy initSpy(&channel, SIGNAL(initialized()));
- QSignalSpy baseUrlSpy(&channel, SIGNAL(baseUrlChanged(QString)));
+ QWebSocketTransport transport;
+ QSignalSpy initSpy(&transport, SIGNAL(initialized()));
+ QSignalSpy baseUrlSpy(&transport, SIGNAL(baseUrlChanged(QString)));
QVERIFY(initSpy.wait());
QCOMPARE(initSpy.size(), 1);
QCOMPARE(baseUrlSpy.size(), 1);
QCOMPARE(baseUrlSpy.first().size(), 1);
- QCOMPARE(channel.baseUrl(), baseUrlSpy.first().first().toString());
- QVERIFY(!channel.baseUrl().isEmpty());
+ QCOMPARE(transport.baseUrl(), baseUrlSpy.first().first().toString());
+ QVERIFY(!transport.baseUrl().isEmpty());
}
void TestWebChannel::testRegisterObjects()
@@ -233,6 +234,7 @@ void TestWebChannel::testInfoForObject()
void TestWebChannel::testInvokeMethodConversion()
{
QWebChannel channel;
+ channel.connectTo(m_dummyTransport);
QJsonArray args;
args.append(QJsonValue(1000));
@@ -271,8 +273,7 @@ static QHash<QString, QObject*> createObjects(QObject *parent)
void TestWebChannel::benchClassInfo()
{
QWebChannel channel;
- QSignalSpy initSpy(&channel, SIGNAL(initialized()));
- QVERIFY(initSpy.wait());
+ channel.connectTo(m_dummyTransport);
QObject parent;
const QHash<QString, QObject*> objects = createObjects(&parent);
@@ -287,8 +288,7 @@ void TestWebChannel::benchClassInfo()
void TestWebChannel::benchInitializeClients()
{
QWebChannel channel;
- QSignalSpy initSpy(&channel, SIGNAL(initialized()));
- QVERIFY(initSpy.wait());
+ channel.connectTo(m_dummyTransport);
QObject parent;
channel.registerObjects(createObjects(&parent));
@@ -306,8 +306,7 @@ void TestWebChannel::benchInitializeClients()
void TestWebChannel::benchPropertyUpdates()
{
QWebChannel channel;
- QSignalSpy initSpy(&channel, SIGNAL(initialized()));
- QVERIFY(initSpy.wait());
+ channel.connectTo(m_dummyTransport);
QObject parent;
const QHash<QString, QObject*> objects = createObjects(&parent);
@@ -333,8 +332,7 @@ void TestWebChannel::benchPropertyUpdates()
void TestWebChannel::benchRegisterObjects()
{
QWebChannel channel;
- QSignalSpy initSpy(&channel, SIGNAL(initialized()));
- QVERIFY(initSpy.wait());
+ channel.connectTo(m_dummyTransport);
QObject parent;
const QHash<QString, QObject*> objects = createObjects(&parent);
diff --git a/tests/webchannel/tst_webchannel.h b/tests/webchannel/tst_webchannel.h
index 7fd0bcc..26aa913 100644
--- a/tests/webchannel/tst_webchannel.h
+++ b/tests/webchannel/tst_webchannel.h
@@ -44,6 +44,25 @@
#include <QObject>
#include <QVariant>
+#include <qwebchanneltransportinterface.h>
+
+class DummyTransport : public QObject, public QWebChannelTransportInterface
+{
+ Q_OBJECT
+ Q_INTERFACES(QWebChannelTransportInterface)
+public:
+ explicit DummyTransport(QObject *parent)
+ : QObject(parent)
+ {}
+ ~DummyTransport() {};
+
+ void sendMessage(const QString &/*message*/) const Q_DECL_OVERRIDE
+ {}
+ void sendMessage(const QByteArray &/*message*/) const Q_DECL_OVERRIDE
+ {}
+ void setMessageHandler(QWebChannelMessageHandlerInterface * /*handler*/) Q_DECL_OVERRIDE
+ {}
+};
class TestObject : public QObject
{
@@ -197,7 +216,7 @@ public:
Q_INVOKABLE void setVariant(const QVariant &v);
private slots:
- void testInitChannel();
+ void testInitWebSocketTransport();
void testRegisterObjects();
void testInfoForObject();
void testInvokeMethodConversion();
@@ -208,6 +227,8 @@ private slots:
void benchRegisterObjects();
private:
+ DummyTransport *m_dummyTransport;
+
int m_lastInt;
double m_lastDouble;
QVariant m_lastVariant;