webapprt/test/chrome/browser_mozpay.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:eb57ebbbd819
1 Cu.import("resource://gre/modules/Services.jsm");
2 let { PaymentManager } = Cu.import("resource://gre/modules/Payment.jsm", {});
3 Cu.import("resource://webapprt/modules/WebappRT.jsm");
4
5 function test() {
6 waitForExplicitFinish();
7
8 let curTest = 0;
9
10 let tests = [];
11 tests.push({
12 providerUri: "https://example.com:443/webapprtChrome/webapprt/test/chrome/mozpay-success.html?req=",
13 message: "Success."
14 });
15 tests.push({
16 providerUri: "https://example.com:443/webapprtChrome/webapprt/test/chrome/mozpay-failure.html?req=",
17 message: "Chocolate rejected."
18 });
19
20 let jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhdWQiOiAibW9j" +
21 "a3BheXByb3ZpZGVyLnBocGZvZ2FwcC5jb20iLCAiaXNzIjogIkVudGVyI" +
22 "HlvdSBhcHAga2V5IGhlcmUhIiwgInJlcXVlc3QiOiB7Im5hbWUiOiAiUG" +
23 "llY2Ugb2YgQ2FrZSIsICJwcmljZSI6ICIxMC41MCIsICJwcmljZVRpZXI" +
24 "iOiAxLCAicHJvZHVjdGRhdGEiOiAidHJhbnNhY3Rpb25faWQ9ODYiLCAi" +
25 "Y3VycmVuY3lDb2RlIjogIlVTRCIsICJkZXNjcmlwdGlvbiI6ICJWaXJ0d" +
26 "WFsIGNob2NvbGF0ZSBjYWtlIHRvIGZpbGwgeW91ciB2aXJ0dWFsIHR1bW" +
27 "15In0sICJleHAiOiAxMzUyMjMyNzkyLCAiaWF0IjogMTM1MjIyOTE5Miw" +
28 "gInR5cCI6ICJtb2NrL3BheW1lbnRzL2luYXBwL3YxIn0.QZxc62USCy4U" +
29 "IyKIC1TKelVhNklvk-Ou1l_daKntaFI";
30
31 PaymentManager.registeredProviders = {};
32 PaymentManager.registeredProviders["mock/payments/inapp/v1"] = {
33 name: "mockprovider",
34 description: "Mock Payment Provider",
35 uri: tests[curTest].providerUri,
36 requestMethod: "GET"
37 };
38
39 let providerWindow;
40
41 let winObserver = function(win, topic) {
42 if (topic == "domwindowopened") {
43 win.addEventListener("load", function onLoadWindow() {
44 win.removeEventListener("load", onLoadWindow, false);
45
46 if (win.document.getElementById("content").getAttribute("src") ==
47 (tests[curTest].providerUri + jwt)) {
48 ok(true, "Payment provider window shown.");
49 providerWindow = win;
50 }
51 }, false);
52 }
53 }
54
55 Services.ww.registerNotification(winObserver);
56
57 let mutObserver = null;
58
59 function onLoad() {
60 let msg = gAppBrowser.contentDocument.getElementById("msg");
61 mutObserver = new MutationObserver(function(mutations) {
62 is(msg.textContent, tests[curTest].message, "Got: " + tests[curTest].message);
63
64 if (!providerWindow) {
65 ok(false, "Payment provider window shown.");
66 } else {
67 providerWindow.close();
68 providerWindow = null;
69 }
70
71 runNextTest();
72 });
73 mutObserver.observe(msg, { childList: true });
74 }
75
76 loadWebapp("mozpay.webapp", undefined, onLoad);
77
78 function runNextTest() {
79 providerWindow = null;
80 if (mutObserver) {
81 mutObserver.disconnect();
82 }
83
84 curTest++;
85
86 if (curTest < tests.length) {
87 PaymentManager.registeredProviders["mock/payments/inapp/v1"].uri = tests[curTest].providerUri;
88
89 gAppBrowser.addEventListener("load", function onLoadH() {
90 gAppBrowser.removeEventListener("load", onLoadH, true);
91 onLoad();
92 }, true);
93 gAppBrowser.reload();
94 } else {
95 finish();
96 }
97 }
98
99 registerCleanupFunction(function() {
100 Services.ww.unregisterNotification(winObserver);
101 mutObserver.disconnect();
102 });
103 }

mercurial