mobile/android/base/tests/testMozPay.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:660260286f99
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 let Cc = Components.classes;
7 let Ci = Components.interfaces;
8 let Cu = Components.utils;
9
10 Components.utils.import("resource://gre/modules/SharedPreferences.jsm");
11 Components.utils.import("resource://gre/modules/Promise.jsm");
12
13 let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
14 let deferred = 0;
15 let shouldPass = true;
16 let reqId = 0;
17 function getRequestId(increment) {
18 reqId += increment;
19 return "Request" + reqId;
20 }
21
22 let paymentSuccess = {
23 receiveMessage: function(aMsg) {
24 let msg = aMsg.json;
25 if (shouldPass) {
26 do_check_eq(msg.requestId, getRequestId(0));
27 } else {
28 do_throw("Test should not have passed");
29 }
30 deferred.resolve();
31 }
32 }
33
34 let paymentFailed = {
35 receiveMessage: function(aMsg) {
36 let msg = aMsg.json;
37 if (shouldPass) {
38 do_throw("Test should not have failed: " + msg.errorMsg);
39 } else {
40 do_check_eq(msg.requestId, getRequestId(0));
41 do_check_eq(msg.errorMsg, "FAILED CORRECTLY");
42 }
43 deferred.resolve();
44 }
45 }
46
47 add_task(function test_get_set() {
48 let ui = Cc["@mozilla.org/payment/ui-glue;1"].getService(Ci.nsIPaymentUIGlue);
49 deferred = Promise.defer();
50 let id = getRequestId(1);
51 ui.confirmPaymentRequest(id,
52 [{ wrappedJSObject: { type: "Fake Provider" } }],
53 function(aRequestId, type) {
54 do_check_eq(type, "Fake Provider");
55 deferred.resolve();
56 },
57 function(id, msg) {
58 do_throw("confirmPaymentRequest should not have failed");
59 deferred.resolve();
60 });
61 yield deferred.promise;
62 });
63
64 add_task(function test_default() {
65 ppmm.addMessageListener("Payment:Success", paymentSuccess);
66 ppmm.addMessageListener("Payment:Failed", paymentFailed);
67
68 let ui = Cc["@mozilla.org/payment/ui-glue;1"].getService(Ci.nsIPaymentUIGlue);
69 deferred = Promise.defer();
70 let id = getRequestId(1);
71 shouldPass = true;
72 ui.showPaymentFlow(id,
73 {
74 uri: "chrome://roboextender/content/paymentsUI.html",
75 jwt: "#pass"
76 },
77 function(id, msg) {
78 do_throw("confirmPaymentRequest should not have failed");
79 deferred.resolve();
80 });
81 yield deferred.promise;
82
83 deferred = Promise.defer();
84 let id = getRequestId(1);
85 shouldPass = false;
86 ui.showPaymentFlow(id,
87 {
88 uri: "chrome://roboextender/content/paymentsUI.html",
89 jwt: "#fail"
90 },
91 function(id, msg) {
92 do_throw("confirmPaymentRequest should not have failed");
93 deferred.resolve();
94 });
95
96 yield deferred.promise;
97
98 ppmm.removeMessageListener("Payment:Success", paymentSuccess);
99 ppmm.removeMessageListener("Payment:Failed", paymentFailed);
100 });
101
102 run_next_test();

mercurial