webapprt/test/chrome/browser_mozpay.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webapprt/test/chrome/browser_mozpay.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +Cu.import("resource://gre/modules/Services.jsm");
     1.5 +let { PaymentManager } = Cu.import("resource://gre/modules/Payment.jsm", {});
     1.6 +Cu.import("resource://webapprt/modules/WebappRT.jsm");
     1.7 +
     1.8 +function test() {
     1.9 +  waitForExplicitFinish();
    1.10 +
    1.11 +  let curTest = 0;
    1.12 +
    1.13 +  let tests = [];
    1.14 +  tests.push({
    1.15 +    providerUri: "https://example.com:443/webapprtChrome/webapprt/test/chrome/mozpay-success.html?req=",
    1.16 +    message: "Success."
    1.17 +  });
    1.18 +  tests.push({
    1.19 +    providerUri: "https://example.com:443/webapprtChrome/webapprt/test/chrome/mozpay-failure.html?req=",
    1.20 +    message: "Chocolate rejected."
    1.21 +  });
    1.22 +
    1.23 +  let jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhdWQiOiAibW9j" +
    1.24 +            "a3BheXByb3ZpZGVyLnBocGZvZ2FwcC5jb20iLCAiaXNzIjogIkVudGVyI" +
    1.25 +            "HlvdSBhcHAga2V5IGhlcmUhIiwgInJlcXVlc3QiOiB7Im5hbWUiOiAiUG" +
    1.26 +            "llY2Ugb2YgQ2FrZSIsICJwcmljZSI6ICIxMC41MCIsICJwcmljZVRpZXI" +
    1.27 +            "iOiAxLCAicHJvZHVjdGRhdGEiOiAidHJhbnNhY3Rpb25faWQ9ODYiLCAi" +
    1.28 +            "Y3VycmVuY3lDb2RlIjogIlVTRCIsICJkZXNjcmlwdGlvbiI6ICJWaXJ0d" +
    1.29 +            "WFsIGNob2NvbGF0ZSBjYWtlIHRvIGZpbGwgeW91ciB2aXJ0dWFsIHR1bW" +
    1.30 +            "15In0sICJleHAiOiAxMzUyMjMyNzkyLCAiaWF0IjogMTM1MjIyOTE5Miw" +
    1.31 +            "gInR5cCI6ICJtb2NrL3BheW1lbnRzL2luYXBwL3YxIn0.QZxc62USCy4U" +
    1.32 +            "IyKIC1TKelVhNklvk-Ou1l_daKntaFI";
    1.33 +
    1.34 +  PaymentManager.registeredProviders = {};
    1.35 +  PaymentManager.registeredProviders["mock/payments/inapp/v1"] = {
    1.36 +    name: "mockprovider",
    1.37 +    description: "Mock Payment Provider",
    1.38 +    uri: tests[curTest].providerUri,
    1.39 +    requestMethod: "GET"
    1.40 +  };
    1.41 +
    1.42 +  let providerWindow;
    1.43 +
    1.44 +  let winObserver = function(win, topic) {
    1.45 +    if (topic == "domwindowopened") {
    1.46 +      win.addEventListener("load", function onLoadWindow() {
    1.47 +        win.removeEventListener("load", onLoadWindow, false);
    1.48 +
    1.49 +        if (win.document.getElementById("content").getAttribute("src") ==
    1.50 +            (tests[curTest].providerUri + jwt)) {
    1.51 +          ok(true, "Payment provider window shown.");
    1.52 +          providerWindow = win;
    1.53 +        }
    1.54 +      }, false);
    1.55 +    }
    1.56 +  }
    1.57 +
    1.58 +  Services.ww.registerNotification(winObserver);
    1.59 +
    1.60 +  let mutObserver = null;
    1.61 +
    1.62 +  function onLoad() {
    1.63 +    let msg = gAppBrowser.contentDocument.getElementById("msg");
    1.64 +    mutObserver = new MutationObserver(function(mutations) {
    1.65 +      is(msg.textContent, tests[curTest].message, "Got: " + tests[curTest].message);
    1.66 +
    1.67 +      if (!providerWindow) {
    1.68 +        ok(false, "Payment provider window shown.");
    1.69 +      } else {
    1.70 +        providerWindow.close();
    1.71 +        providerWindow = null;
    1.72 +      }
    1.73 +
    1.74 +      runNextTest();
    1.75 +    });
    1.76 +    mutObserver.observe(msg, { childList: true });
    1.77 +  }
    1.78 +
    1.79 +  loadWebapp("mozpay.webapp", undefined, onLoad);
    1.80 +
    1.81 +  function runNextTest() {
    1.82 +    providerWindow = null;
    1.83 +    if (mutObserver) {
    1.84 +      mutObserver.disconnect();
    1.85 +    }
    1.86 +
    1.87 +    curTest++;
    1.88 +
    1.89 +    if (curTest < tests.length) {
    1.90 +      PaymentManager.registeredProviders["mock/payments/inapp/v1"].uri = tests[curTest].providerUri;
    1.91 +
    1.92 +      gAppBrowser.addEventListener("load", function onLoadH() {
    1.93 +        gAppBrowser.removeEventListener("load", onLoadH, true);
    1.94 +        onLoad();
    1.95 +      }, true);
    1.96 +      gAppBrowser.reload();
    1.97 +    } else {
    1.98 +      finish();
    1.99 +    }
   1.100 +  }
   1.101 +
   1.102 +  registerCleanupFunction(function() {
   1.103 +    Services.ww.unregisterNotification(winObserver);
   1.104 +    mutObserver.disconnect();
   1.105 +  });
   1.106 +}

mercurial