on xenial help sign in
Check purchase state to determine if purchase was cancelled and hide progress.
89081afRodney Dawes
plugins/Ubuntu/Payments/Payments.cpp+10
@@ -29,6 +29,7 @@ static void observer(PayPackage* /* package */, const char* itemid, PayPackageIt
29 // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
29 // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
30
30
31 Payments *self = static_cast<Payments*>(user_data);
31 Payments *self = static_cast<Payments*>(user_data);
32+ static bool _purchasing = false;
32
33
33 // If the item ID is different, ignore it.
34 // If the item ID is different, ignore it.
34 if (itemid != self->storeItemId()) {
35 if (itemid != self->storeItemId()) {
@@ -41,6 +42,15 @@ static void observer(PayPackage* /* package */, const char* itemid, PayPackageIt
41 case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
42 case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
42 Q_EMIT self->purchaseCompleted();
43 Q_EMIT self->purchaseCompleted();
43 break;
44 break;
45+ case PAY_PACKAGE_ITEM_STATUS_PURCHASING:
46+ _purchasing = true;
47+ break;
48+ case PAY_PACKAGE_ITEM_STATUS_NOT_PURCHASED:
49+ if (_purchasing) {
50+ _purchasing = false;
51+ Q_EMIT self->purchaseCancelled();
52+ }
53+ break;
44 default:
54 default:
45 break;
55 break;
46 }
56 }
plugins/Ubuntu/Payments/Payments.h+1
@@ -53,6 +53,7 @@ Q_SIGNALS:
53
53
54 void purchaseError(const QString &error);
54 void purchaseError(const QString &error);
55 void purchaseCompleted();
55 void purchaseCompleted();
56+ void purchaseCancelled();
56
57
57private:
58private:
58 QString m_currency;
59 QString m_currency;
qml/Dash/Previews/PreviewPayments.qml+4
@@ -63,6 +63,10 @@ PreviewWidget {
63 paymentButton.opacity = 1;
63 paymentButton.opacity = 1;
64 root.triggered(widgetId, "purchaseError", source);
64 root.triggered(widgetId, "purchaseError", source);
65 }
65 }
66+ onPurchaseCancelled: {
67+ paymentButton.opacity = 1;
68+ root.triggered(widgetId, "purchaseCancelled", source);
69+ }
66 }
70 }
67 }
71 }
68
72
tests/mocks/Ubuntu/Payments/MockPayments.cpp+3−1
@@ -84,7 +84,9 @@ void MockPayments::start()
84
84
85void MockPayments::process()
85void MockPayments::process()
86{
86{
87− if (m_store_item_id == "com.example.invalid") {
87+ if (m_store_item_id == "com.example.cancel") {
88+ Q_EMIT purchaseCancelled();
89+ } else if (m_store_item_id == "com.example.invalid") {
88 Q_EMIT purchaseError("Purchase failed.");
90 Q_EMIT purchaseError("Purchase failed.");
89 } else {
91 } else {
90 Q_EMIT purchaseCompleted();
92 Q_EMIT purchaseCompleted();
tests/mocks/Ubuntu/Payments/MockPayments.h+1
@@ -51,6 +51,7 @@ Q_SIGNALS:
51
51
52 void purchaseError(const QString &error);
52 void purchaseError(const QString &error);
53 void purchaseCompleted();
53 void purchaseCompleted();
54+ void purchaseCancelled();
54
55
55private:
56private:
56 QString m_currency;
57 QString m_currency;
tests/qmltests/Dash/Previews/tst_PreviewPayments.qml+35
@@ -33,6 +33,10 @@ Rectangle {
33 "source": { "price" : 0.99, "currency": "USD", "store_item_id": "com.example.invalid" }
33 "source": { "price" : 0.99, "currency": "USD", "store_item_id": "com.example.invalid" }
34 }
34 }
35
35
36+ property var jsonPurchaseCancel: {
37+ "source": { "price" : 0.99, "currency": "USD", "store_item_id": "com.example.cancel" }
38+ }
39+
36 SignalSpy {
40 SignalSpy {
37 id: spy
41 id: spy
38 target: previewPayments
42 target: previewPayments
@@ -160,5 +164,36 @@ Rectangle {
160 compare(args[1], "purchaseError");
164 compare(args[1], "purchaseError");
161 compare(args[2], jsonPurchaseError["source"]);
165 compare(args[2], jsonPurchaseError["source"]);
162 }
166 }
167+
168+ function test_purchase_cancelled() {
169+ // The mock Payments triggers cancellation when com.example.cancel
170+ // is passed to it as store_item_id. Exercise it here
171+ previewPayments.widgetData = jsonPurchaseCancel;
172+
173+ var button = findChild(previewPayments, "paymentButton");
174+ var progress = findChild(previewPayments, "loadingBar");
175+ verify(button, "Button not found.");
176+ verify(progress, "Progress not found.");
177+
178+ mouseClick(button, button.width / 2, button.height / 2);
179+
180+ tryCompare(progress, "visible", true);
181+ tryCompare(progress, "opacity", 1);
182+ tryCompare(button, "visible", false);
183+ tryCompare(button, "opacity", 0);
184+
185+ paymentClient.process();
186+ spy.wait();
187+
188+ var args = spy.signalArguments[0];
189+ compare(args[0], "previewPayments");
190+ compare(args[1], "purchaseCancelled");
191+ compare(args[2], jsonPurchaseCancel["source"]);
192+
193+ tryCompare(progress, "visible", false);
194+ tryCompare(progress, "opacity", 0);
195+ tryCompare(button, "visible", true);
196+ tryCompare(button, "opacity", 1);
197+ }
163 }
198 }
164}
199}