diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-02-05 15:31:07 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-21 13:44:55 +0100 |
commit | 3a85e592c050e73e61dc54d22e133bcf08d2f3c5 (patch) | |
tree | da7384dca0418ecfda90a39c65e96c8875b2afd9 /tests | |
parent | 4d948685996df0e5e6c9242c12276d4d584bc974 (diff) | |
download | qtwebchannel-3a85e592c050e73e61dc54d22e133bcf08d2f3c5.tar.gz |
Refactor QML test code and add a test for multiple websocket clients.
This will allow us to refactor the handling of multiple clients without
breaking the functionality.
Change-Id: I277eb5d7337d398fdf0694ef539d3b6ab7e5d332
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/qml/TestWebView.qml | 81 | ||||
-rw-r--r-- | tests/qml/WebChannelTest.qml | 67 | ||||
-rw-r--r-- | tests/qml/data/multiclient.html | 19 | ||||
-rw-r--r-- | tests/qml/tst_metaobjectpublisher.qml | 18 | ||||
-rw-r--r-- | tests/qml/tst_multiclient.qml | 99 |
5 files changed, 234 insertions, 50 deletions
diff --git a/tests/qml/TestWebView.qml b/tests/qml/TestWebView.qml new file mode 100644 index 0000000..3b12f38 --- /dev/null +++ b/tests/qml/TestWebView.qml @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtTest 1.0 + +import QtWebKit 3.0 +import QtWebKit.experimental 1.0 + +WebView { + id: view + property var lastLoadStatus + + experimental.preferences.developerExtrasEnabled: true + experimental.preferences.navigatorQtObjectEnabled: true + + onLoadingChanged: { + // NOTE: we cannot use spy.signalArguments nor save the loadRequest anywhere, as it gets + // deleted after the slots connected to the signal have finished... i.e. it's a weak pointer, + // not a shared pointer. As such, we have to copy out the interesting data we need later on here... + lastLoadStatus = loadRequest.status + } + + SignalSpy { + id: loadingSpy + target: view + signalName: "onLoadingChanged" + } + + function waitForLoaded() + { + do { + loadingSpy.wait(500); + } while (loading); + return lastLoadStatus == WebView.LoadSucceededStatus; + } + + function clear() + { + url = ""; + loadingSpy.clear() + } +} diff --git a/tests/qml/WebChannelTest.qml b/tests/qml/WebChannelTest.qml index b3121d3..59a0cab 100644 --- a/tests/qml/WebChannelTest.qml +++ b/tests/qml/WebChannelTest.qml @@ -43,8 +43,6 @@ import QtQuick 2.0 import QtTest 1.0 import QtWebChannel 1.0 -import QtWebKit 3.0 -import QtWebKit.experimental 1.0 TestCase { property var lastLoadStatus @@ -55,32 +53,15 @@ TestCase { WebViewTransport { id: webViewTransport - webViewExperimental: view.experimental + webViewExperimental: defaultView.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 - // deleted after the slots connected to the signal have finished... i.e. it's a weak pointer, - // not a shared pointer. As such, we have to copy out the interesting data we need later on here... - lastLoadStatus = loadRequest.status - } - - SignalSpy { - id: loadingSpy - target: view - signalName: "onLoadingChanged" - } + TestWebView { + id: defaultView } - property var view: view WebChannel { id: webChannel @@ -95,7 +76,14 @@ TestCase { property var rawMessageSpy: rawMessageSpy property var rawMessageIdx: 0; - function loadUrl(url) + function urlForFile(file) + { + verify(useWebViewTransport || webSocketTransport.baseUrl != "", "webSocketTransport.baseUrl is empty"); + return "data/" + file + (!useWebViewTransport ? "?webChannelBaseUrl=" + webSocketTransport.baseUrl : ""); + } + + // load file in the given view or use the global one by default + function loadUrl(file, view) { if (useWebViewTransport) { webChannel.disconnectFrom(webSocketTransport); @@ -104,19 +92,16 @@ TestCase { 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); - } while (view.loading); - compare(lastLoadStatus, WebView.LoadSucceededStatus); + if (!view) { + view = defaultView; + } + view.url = urlForFile(file); + view.waitForLoaded(); } function cleanup() { - view.url = ""; - loadingSpy.clear(); + defaultView.clear(); rawMessageSpy.clear(); rawMessageIdx = 0; } @@ -139,4 +124,22 @@ TestCase { } return JSON.parse(msg); } + + function awaitInit() + { + var msg = awaitMessage(); + verify(msg); + verify(msg.data); + verify(msg.data.type); + compare(msg.data.type, "Qt.init"); + } + + function awaitIdle() + { + var msg = awaitMessage(); + verify(msg); + verify(msg.data); + compare(msg.data.type, "Qt.idle"); + verify(webChannel.test_clientIsIdle()) + } } diff --git a/tests/qml/data/multiclient.html b/tests/qml/data/multiclient.html new file mode 100644 index 0000000..1573a1a --- /dev/null +++ b/tests/qml/data/multiclient.html @@ -0,0 +1,19 @@ +<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 + createWebChannel(function(channel) { + foo.ping.connect(function() { + foo.pong(function(value) { + channel.exec({pongAnswer: value}); + }); + }); + }); + //END SETUP + </script> + </head> + <body> + </body> +</html> diff --git a/tests/qml/tst_metaobjectpublisher.qml b/tests/qml/tst_metaobjectpublisher.qml index 4fd9d07..fed52d8 100644 --- a/tests/qml/tst_metaobjectpublisher.qml +++ b/tests/qml/tst_metaobjectpublisher.qml @@ -95,24 +95,6 @@ WebChannelTest { webChannel.registeredObjects = [myObj, myOtherObj, myFactory]; } - function awaitInit() - { - var msg = awaitMessage(); - verify(msg); - verify(msg.data); - verify(msg.data.type); - compare(msg.data.type, "Qt.init"); - } - - function awaitIdle() - { - var msg = awaitMessage(); - verify(msg); - verify(msg.data); - compare(msg.data.type, "Qt.idle"); - verify(webChannel.test_clientIsIdle()) - } - function awaitMessageSkipIdle() { var msg; diff --git a/tests/qml/tst_multiclient.qml b/tests/qml/tst_multiclient.qml new file mode 100644 index 0000000..519a63c --- /dev/null +++ b/tests/qml/tst_multiclient.qml @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +import QtQuick 2.0 + +import QtWebChannel 1.0 + +WebChannelTest { + name: "MultiClient" + id: test + + QtObject { + id: foo + property int bar: 0 + + signal ping() + + function pong() + { + return ++bar; + } + + WebChannel.id: "foo" + } + + TestWebView { + id: client1 + } + TestWebView { + id: client2 + } + + function initTestCase() + { + webChannel.registeredObjects = [foo]; + } + + function test_multiclient() + { + loadUrl("multiclient.html", client1); + loadUrl("multiclient.html", client2); + // init, connect & idle messages for two clients + for (var i = 0; i < 3 * 2; ++i) { + awaitMessage(); + } + + foo.ping(); + + // invoke of pong method + awaitMessage(); + awaitMessage(); + + var msg = awaitMessage(); + compare(msg.data.pongAnswer, 1); + msg = awaitMessage(); + compare(msg.data.pongAnswer, 2); + + awaitIdle(); + awaitIdle(); + } +} |