on xenial help sign in
Add requested tests.
commit1c08635b86fb4182560fa87665262e9e9877f336 authorRodney Dawes <rodney.dawes@canonical.com> dateFri 27 Jun 2014, 16:53 (12 years ago) parent4670c36
11 files changed+359−1
plugins/Ubuntu/Payments/Payments.qmltypes−1 tests/mocks/Ubuntu/CMakeLists.txt+1 tests/mocks/Ubuntu/Payments/CMakeLists.txt+10 tests/mocks/Ubuntu/Payments/MockPayments.cpp+87 tests/mocks/Ubuntu/Payments/MockPayments.h+60 tests/mocks/Ubuntu/Payments/Payments.qmltypes+42 tests/mocks/Ubuntu/Payments/plugin.cpp+28 tests/mocks/Ubuntu/Payments/plugin.h+31 tests/mocks/Ubuntu/Payments/qmldir+3 tests/qmltests/CMakeLists.txt+1 tests/qmltests/Dash/Previews/tst_PreviewPayments.qml+96
plugins/Ubuntu/Payments/Payments.qmltypes−1
@@ -32,7 +32,6 @@ Module {
3232 name: "formattedPriceChanged"
3333 Parameter { name: "formatted_price"; type: "string" }
3434 }
35− Signal { name: "purchaseCanceled" }
3635 Signal {
3736 name: "purchaseError"
3837 Parameter { name: "error"; type: "string" }
tests/mocks/Ubuntu/CMakeLists.txt+1
@@ -1,2 +1,3 @@
11add_subdirectory(DownloadDaemonListener)
2+add_subdirectory(Payments)
23add_subdirectory(Thumbnailer)
tests/mocks/Ubuntu/Payments/CMakeLists.txtnew file+10
@@ -0,0 +1,10 @@
1+set(FakeUbuntuPaymentsQml_SOURCES
2+ plugin.cpp
3+ MockPayments.cpp
4+ )
5+
6+add_library(FakeUbuntuPaymentsQml MODULE ${FakeUbuntuPaymentsQml_SOURCES})
7+
8+qt5_use_modules(FakeUbuntuPaymentsQml Qml Quick Core)
9+
10+add_unity8_mock(Ubuntu.Payments 0.1 Ubuntu/Payments TARGETS FakeUbuntuPaymentsQml)
tests/mocks/Ubuntu/Payments/MockPayments.cppnew file+87
@@ -0,0 +1,87 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QLocale>
18+
19+#include "MockPayments.h"
20+
21+
22+MockPayments::MockPayments(QObject *parent)
23+ : QObject(parent)
24+{
25+}
26+
27+QString MockPayments::currency() const
28+{
29+ return m_currency;
30+}
31+
32+double MockPayments::price() const
33+{
34+ return m_price;
35+}
36+
37+QString MockPayments::storeItemId() const
38+{
39+ return m_store_item_id;
40+}
41+
42+QString MockPayments::formattedPrice() const
43+{
44+ QLocale locale;
45+ return locale.toCurrencyString(m_price, m_currency);
46+}
47+
48+void MockPayments::setCurrency(const QString &currency)
49+{
50+ if(m_currency != currency) {
51+ m_currency = currency;
52+ Q_EMIT currencyChanged(currency);
53+ Q_EMIT formattedPriceChanged(formattedPrice());
54+ }
55+}
56+
57+void MockPayments::setPrice(const double price)
58+{
59+ if(m_price != price) {
60+ m_price = price;
61+ Q_EMIT priceChanged(price);
62+ Q_EMIT formattedPriceChanged(formattedPrice());
63+ }
64+}
65+
66+void MockPayments::setStoreItemId(const QString &store_item_id)
67+{
68+ if (m_store_item_id != store_item_id) {
69+ m_store_item_id = store_item_id;
70+ Q_EMIT storeItemIdChanged(m_store_item_id);
71+ }
72+
73+ if (m_store_item_id.isEmpty()) {
74+ return;
75+ }
76+}
77+
78+void MockPayments::start()
79+{
80+ if (!m_store_item_id.isEmpty()) {
81+ if (m_store_item_id == "com.example.invalid") {
82+ Q_EMIT purchaseError("Purchase failed.");
83+ } else {
84+ Q_EMIT purchaseCompleted();
85+ }
86+ }
87+}
tests/mocks/Ubuntu/Payments/MockPayments.hnew file+60
@@ -0,0 +1,60 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef MOCK_PAYMENTS_H
18+#define MOCK_PAYMENTS_H
19+
20+#include <QObject>
21+
22+
23+class MockPayments : public QObject
24+{
25+ Q_OBJECT
26+ Q_DISABLE_COPY(MockPayments)
27+ Q_PROPERTY(QString currency READ currency WRITE setCurrency NOTIFY currencyChanged)
28+ Q_PROPERTY(double price READ price WRITE setPrice NOTIFY priceChanged)
29+ Q_PROPERTY(QString storeItemId READ storeItemId WRITE setStoreItemId NOTIFY storeItemIdChanged)
30+ Q_PROPERTY(QString formattedPrice READ formattedPrice NOTIFY formattedPriceChanged)
31+
32+public:
33+ explicit MockPayments(QObject *parent = 0);
34+
35+ QString currency() const;
36+ double price() const;
37+ QString storeItemId() const;
38+ QString formattedPrice() const;
39+
40+ void setCurrency(const QString& currency);
41+ void setPrice(const double price);
42+ void setStoreItemId(const QString& store_item_id);
43+ Q_INVOKABLE void start();
44+
45+Q_SIGNALS:
46+ void currencyChanged(const QString& currency);
47+ void priceChanged(const double price);
48+ void storeItemIdChanged(const QString &store_item_id);
49+ void formattedPriceChanged(const QString &formatted_price);
50+
51+ void purchaseError(const QString &error);
52+ void purchaseCompleted();
53+
54+private:
55+ QString m_currency;
56+ double m_price;
57+ QString m_store_item_id;
58+};
59+
60+#endif // PAYMENTS_H
tests/mocks/Ubuntu/Payments/Payments.qmltypesnew file+42
@@ -0,0 +1,42 @@
1+import QtQuick.tooling 1.1
2+
3+// This file describes the plugin-supplied types contained in the library.
4+// It is used for QML tooling purposes only.
5+//
6+// This file was auto-generated by:
7+// 'qmlplugindump -notrelocatable Ubuntu.Payments 0.1 tests/mocks'
8+
9+Module {
10+ Component {
11+ name: "MockPayments"
12+ prototype: "QObject"
13+ exports: ["Ubuntu.Payments/Payments 0.1"]
14+ exportMetaObjectRevisions: [0]
15+ Property { name: "currency"; type: "string" }
16+ Property { name: "price"; type: "double" }
17+ Property { name: "storeItemId"; type: "string" }
18+ Property { name: "formattedPrice"; type: "string"; isReadonly: true }
19+ Signal {
20+ name: "currencyChanged"
21+ Parameter { name: "currency"; type: "string" }
22+ }
23+ Signal {
24+ name: "priceChanged"
25+ Parameter { name: "price"; type: "double" }
26+ }
27+ Signal {
28+ name: "storeItemIdChanged"
29+ Parameter { name: "store_item_id"; type: "string" }
30+ }
31+ Signal {
32+ name: "formattedPriceChanged"
33+ Parameter { name: "formatted_price"; type: "string" }
34+ }
35+ Signal {
36+ name: "purchaseError"
37+ Parameter { name: "error"; type: "string" }
38+ }
39+ Signal { name: "purchaseCompleted" }
40+ Method { name: "start" }
41+ }
42+}
tests/mocks/Ubuntu/Payments/plugin.cppnew file+28
@@ -0,0 +1,28 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QtQml>
18+
19+#include "plugin.h"
20+#include "MockPayments.h"
21+
22+
23+void BackendPlugin::registerTypes(const char *uri)
24+{
25+ Q_ASSERT(uri == QLatin1String("Ubuntu.Payments"));
26+
27+ qmlRegisterType<MockPayments>(uri, 0, 1, "Payments");
28+}
tests/mocks/Ubuntu/Payments/plugin.hnew file+31
@@ -0,0 +1,31 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef PAYMENTS_PLUGIN_H
18+#define PAYMENTS_PLUGIN_H
19+
20+#include <QQmlExtensionPlugin>
21+
22+class BackendPlugin : public QQmlExtensionPlugin
23+{
24+ Q_OBJECT
25+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
26+
27+public:
28+ void registerTypes(const char *uri);
29+};
30+
31+#endif // PAYMENTS_PLUGIN_H
tests/mocks/Ubuntu/Payments/qmldirnew file+3
@@ -0,0 +1,3 @@
1+module Ubuntu.Payments
2+plugin FakeUbuntuPaymentsQml
3+typeinfo Payments.qmltypes
tests/qmltests/CMakeLists.txt+1
@@ -46,6 +46,7 @@ add_qml_test(Dash/Previews PreviewActions)
4646add_qml_test(Dash/Previews PreviewAudioPlayback)
4747add_qml_test(Dash/Previews PreviewHeader)
4848add_qml_test(Dash/Previews PreviewImageGallery)
49+add_qml_test(Dash/Previews PreviewPayments)
4950add_qml_test(Dash/Previews PreviewProgress)
5051add_qml_test(Dash/Previews PreviewRatingDisplay)
5152add_qml_test(Dash/Previews PreviewRatingInput)
tests/qmltests/Dash/Previews/tst_PreviewPayments.qmlnew file+96
@@ -0,0 +1,96 @@
1+/*
2+ * Copyright 2014 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+import QtTest 1.0
19+import "../../../../qml/Dash/Previews"
20+import Unity.Test 0.1 as UT
21+import Ubuntu.Components 0.1
22+
23+Rectangle {
24+ id: root
25+ width: units.gu(60)
26+ height: units.gu(80)
27+
28+ property var jsonPurchase: {
29+ "source": { "price" : 0.99, "currency": "USD", "store_item_id": "com.example.package" }
30+ }
31+
32+ property var jsonPurchaseError: {
33+ "source": { "price" : 0.99, "currency": "USD", "store_item_id": "com.example.invalid" }
34+ }
35+
36+ SignalSpy {
37+ id: spy
38+ signalName: "triggered"
39+ }
40+
41+ PreviewPayments {
42+ id: previewPayments
43+ widgetId: "previewPayments"
44+ widgetData: jsonPurchase
45+ width: units.gu(30)
46+
47+ Rectangle {
48+ anchors.fill: parent
49+ color: "red"
50+ opacity: 0.1
51+ }
52+ }
53+
54+ UT.UnityTestCase {
55+ name: "PreviewPaymentsTest"
56+ when: windowShown
57+
58+ function cleanup()
59+ {
60+ spy.clear();
61+ }
62+
63+ function test_purchase_completed() {
64+ var button = findChild(root, "previewPayments");
65+ verify(button != null);
66+ spy.target = previewPayments;
67+
68+ // Exercise the purchaseCompleted signal here.
69+ previewPayments.widgetData = jsonPurchase;
70+ mouseClick(button, button.width / 2, button.height / 2);
71+
72+ compare(spy.count, 1);
73+ var args = spy.signalArguments[0];
74+ compare(args[0], "previewPayments");
75+ compare(args[1], "purchaseCompleted");
76+ compare(args[2], jsonPurchase["source"]);
77+ }
78+
79+ function test_purchase_error() {
80+ var button = findChild(root, "previewPayments");
81+ verify(button != null);
82+ spy.target = previewPayments;
83+
84+ // The mock Payments triggers an error when com.example.invalid is
85+ // passed to it as store_item_id. Exercise it here
86+ previewPayments.widgetData = jsonPurchaseError;
87+ mouseClick(button, button.width / 2, button.height / 2);
88+
89+ compare(spy.count, 1);
90+ var args = spy.signalArguments[0];
91+ compare(args[0], "previewPayments");
92+ compare(args[1], "purchaseError");
93+ compare(args[2], jsonPurchaseError["source"]);
94+ }
95+ }
96+}