diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2013-11-06 16:47:48 +0100 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2013-11-14 16:41:27 +0100 |
commit | bc06e88886ca33ce68c3ae3a72cf011257d1fadd (patch) | |
tree | 64716a51c4da5f73425f965475e5057a1c2345bc /tests | |
parent | 86eacb18b6868173b63b8f67feb481d6e291ad7a (diff) | |
download | qtwebchannel-bc06e88886ca33ce68c3ae3a72cf011257d1fadd.tar.gz |
Add unit test harness for QWebChannel and two initial tests.
This uncovered a bug in webchannel.js, which stringified strings leading
to duplicated quoting. This is also fixed now.
Furthermore, some QMake changes are required to make it possible to run
the tests without first installing QWebChannel.
Change-Id: If7e8f73a748f86f2d5c7d39000e90612367038af
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/qml/qml.cpp | 36 | ||||
-rw-r--r-- | tests/qml/qml.pro | 10 | ||||
-rw-r--r-- | tests/qml/receiveRaw.html | 15 | ||||
-rw-r--r-- | tests/qml/send.html | 17 | ||||
-rw-r--r-- | tests/qml/tst_webchannel.qml | 109 | ||||
-rw-r--r-- | tests/tests.pro | 4 |
6 files changed, 191 insertions, 0 deletions
diff --git a/tests/qml/qml.cpp b/tests/qml/qml.cpp new file mode 100644 index 0000000..242a2b6 --- /dev/null +++ b/tests/qml/qml.cpp @@ -0,0 +1,36 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 QWebChannel module on Qt labs. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtQuickTest/quicktest.h> + +QUICK_TEST_MAIN(qml) diff --git a/tests/qml/qml.pro b/tests/qml/qml.pro new file mode 100644 index 0000000..d687e63 --- /dev/null +++ b/tests/qml/qml.pro @@ -0,0 +1,10 @@ +QT += testlib + +TARGET = qml + +CONFIG += warn_on qmltestcase + +IMPORTPATH += $$OUT_PWD/../../src + +SOURCES += \ + qml.cpp diff --git a/tests/qml/receiveRaw.html b/tests/qml/receiveRaw.html new file mode 100644 index 0000000..cfe685e --- /dev/null +++ b/tests/qml/receiveRaw.html @@ -0,0 +1,15 @@ +<html> + <head> + <script type="text/javascript" src="qrc:///qwebchannel/webchannel.js"></script> + <script type="text/javascript"> + //BEGIN SETUP + var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + new QWebChannel(baseUrl, function(channel) { + channel.send("foobar"); + }); + //END SETUP + </script> + </head> + <body> + </body> +</html> diff --git a/tests/qml/send.html b/tests/qml/send.html new file mode 100644 index 0000000..f30e9c8 --- /dev/null +++ b/tests/qml/send.html @@ -0,0 +1,17 @@ +<html> + <head> + <script type="text/javascript" src="qrc:///qwebchannel/webchannel.js"></script> + <script type="text/javascript"> + //BEGIN SETUP + var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + new QWebChannel(baseUrl, function(channel) { + channel.subscribe("myMessage", function(payload) { + channel.send("myMessagePong:" + payload); + }); + }); + //END SETUP + </script> + </head> + <body> + </body> +</html> diff --git a/tests/qml/tst_webchannel.qml b/tests/qml/tst_webchannel.qml new file mode 100644 index 0000000..2336b39 --- /dev/null +++ b/tests/qml/tst_webchannel.qml @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 QWebChannel module on Qt labs. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtTest 1.0 + +import Qt.labs.WebChannel 1.0 +import QtWebKit 3.0 + +TestCase { + name: "WebChannel" + + // only run after the webchannel has finished initialization + when: webChannel.baseUrl != "" + + property var lastLoadStatus + + WebView { + id: view + + 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" + } + } + + WebChannel { + id: webChannel + } + + SignalSpy { + id: rawMessageSpy + target: webChannel + signalName: "onRawMessageReceived" + } + + function loadUrl(url) + { + verify(webChannel.baseUrl != "", "webChannel.baseUrl is empty"); + view.url = url + "?webChannelBaseUrl=" + webChannel.baseUrl; + // now wait for page to finish loading + do { + loadingSpy.wait(500); + } while (view.loading); + compare(lastLoadStatus, WebView.LoadSucceededStatus); + } + + function cleanup() + { + view.url = ""; + loadingSpy.clear(); + rawMessageSpy.clear(); + } + + //BEGIN TESTS + function test_receiveRawMessage() + { + loadUrl("receiveRaw.html"); + rawMessageSpy.wait(500); + compare(rawMessageSpy.signalArguments[0][0], "foobar"); + } + + function test_sendMessage() + { + loadUrl("send.html"); + webChannel.sendMessage("myMessage", "foobar"); + rawMessageSpy.wait(500); + compare(rawMessageSpy.signalArguments[0][0], "myMessagePong:foobar"); + } + //END TESTS +} diff --git a/tests/tests.pro b/tests/tests.pro new file mode 100644 index 0000000..3f166e8 --- /dev/null +++ b/tests/tests.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS = \ + qml |