Bug 1443914 - Mochitest for force updating PaymentDetails. r=baku
authorEden Chuang <chenyu.chuang@gapp.nthu.edu.tw>
Thu, 29 Mar 2018 23:10:08 +0800
changeset 410746 1ce3dc676ee905929d1bce3146e19a028deeced3
parent 410745 200ba8d2989253c995cfa93c8ad3e3a43546bc13
child 410747 a8af22a4ed7521e18ead2b57ec128c2b949a222f
push id33736
push usershindli@mozilla.com
push dateFri, 30 Mar 2018 09:56:41 +0000
treeherdermozilla-central@b7fa9d95150e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1443914
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1443914 - Mochitest for force updating PaymentDetails. r=baku
dom/payments/test/Bug1443914ChromeScript.js
dom/payments/test/mochitest.ini
dom/payments/test/test_bug1443914.html
new file mode 100644
--- /dev/null
+++ b/dom/payments/test/Bug1443914ChromeScript.js
@@ -0,0 +1,80 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+const paymentSrv = Cc["@mozilla.org/dom/payments/payment-request-service;1"].getService(Ci.nsIPaymentRequestService);
+
+function emitTestFail(message) {
+  sendAsyncMessage("test-fail", message);
+}
+function emitTestPass(message) {
+  sendAsyncMessage("test-pass", message);
+}
+
+const shippingAddress = Cc["@mozilla.org/dom/payments/payment-address;1"].
+                           createInstance(Ci.nsIPaymentAddress);
+const addressLine = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
+const address = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+address.data = "Easton Ave";
+addressLine.appendElement(address);
+shippingAddress.init("USA",              // country
+                     addressLine,        // address line
+                     "CA",               // region
+                     "San Bruno",        // city
+                     "Test locality",    // dependent locality
+                     "94066",            // postal code
+                     "123456",           // sorting code
+                     "en",               // language code
+                     "Testing Org",      // organization
+                     "Bill A. Pacheco",  // recipient
+                     "+1-434-441-3879"); // phone
+
+const TestingUIService = {
+  showPayment: function(requestId) {
+    paymentSrv.changeShippingAddress(requestId, shippingAddress);
+  },
+  abortPayment: function(requestId) {
+  },
+  completePayment: function(requestId) {
+    let request = paymentSrv.getPaymentRequestById(requestId);
+    let completeResponse = Cc["@mozilla.org/dom/payments/payment-complete-action-response;1"].
+                           createInstance(Ci.nsIPaymentCompleteActionResponse);
+    completeResponse.init(requestId, Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED);
+    paymentSrv.respondPayment(completeResponse.QueryInterface(Ci.nsIPaymentActionResponse));
+  },
+  updatePayment: function(requestId) {
+    let request = paymentSrv.getPaymentRequestById(requestId);
+    if (request.shippingOptions != null) {
+      emitTestFail("request.shippingOptions should be null");
+    } else {
+      emitTestPass("request.shippingOptions should be null");
+    }
+    const showResponseData = Cc["@mozilla.org/dom/payments/general-response-data;1"].
+                                createInstance(Ci.nsIGeneralResponseData);
+    showResponseData.initData({});
+    const showResponse = Cc["@mozilla.org/dom/payments/payment-show-action-response;1"].
+                            createInstance(Ci.nsIPaymentShowActionResponse);
+    showResponse.init(requestId,
+                      Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
+                      "testing-payment-method",   // payment method
+                      showResponseData,           // payment method data
+                      "Bill A. Pacheco",          // payer name
+                      "",                         // payer email
+                      "");                        // payer phone
+    paymentSrv.respondPayment(showResponse.QueryInterface(Ci.nsIPaymentActionResponse));
+  },
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIService]),
+};
+
+addMessageListener("set-checking-shipping-options-ui-service", function() {
+  paymentSrv.setTestingUIService(TestingUIService.QueryInterface(Ci.nsIPaymentUIService));
+});
+
+addMessageListener("teardown", function() {
+  paymentSrv.cleanup();
+  paymentSrv.setTestingUIService(null);
+  sendAsyncMessage('teardown-complete');
+});
--- a/dom/payments/test/mochitest.ini
+++ b/dom/payments/test/mochitest.ini
@@ -1,28 +1,30 @@
 [DEFAULT]
 # skip-if !e10s will be removed once non-e10s is supported
 skip-if = !e10s
 scheme = https
 support-files =
   simple_payment_request.html
   echo_payment_request.html
   BasiccardChromeScript.js
+  Bug1443914ChromeScript.js
   ConstructorChromeScript.js
   CurrencyAmountValidationChromeScript.js
   GeneralChromeScript.js
   PMIValidationChromeScript.js
   ShowPaymentChromeScript.js
   RequestShippingChromeScript.js
 
 [test_abortPayment.html]
 run-if = nightly_build # Bug 1390018: Depends on the Nightly-only UI service
 [test_basiccard.html]
 [test_block_none10s.html]
 skip-if = e10s # Bug 1408250: Don't expose PaymentRequest Constructor in non-e10s
+[test_bug1443914.html]
 [test_canMakePayment.html]
 run-if = nightly_build # Bug 1390737: Depends on the Nightly-only UI service
 [test_constructor.html]
 [test_currency_amount_validation.html]
 [test_payment-request-in-iframe.html]
 [test_pmi_validation.html]
 [test_requestShipping.html]
 [test_showPayment.html]
new file mode 100644
--- /dev/null
+++ b/dom/payments/test/test_bug1443914.html
@@ -0,0 +1,140 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1443914
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1443914</title>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript">
+
+  "use strict";
+  SimpleTest.waitForExplicitFinish();
+
+  var gUrl = SimpleTest.getTestFileURL('Bug1443914ChromeScript.js');
+  var gScript = SpecialPowers.loadChromeScript(gUrl);
+
+  function testFailHandler(message) {
+    ok(false, message);
+  }
+  function testPassHandler(message) {
+    ok(true, message);
+  }
+  gScript.addMessageListener("test-fail", testFailHandler);
+  gScript.addMessageListener("test-pass", testPassHandler);
+
+  // testing data declation
+  const defaultMethods = [{
+    supportedMethods: "basic-card",
+    data: {
+      supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover',
+                          'diners', 'jcb', 'mir',
+      ],
+      supportedTypes: ['prepaid', 'debit', 'credit'],
+    },
+  }, {
+    supportedMethods: "testing-payment-method",
+  }];
+
+  const defaultDetails = {
+    id: "test payment",
+    total: {
+      label: "Total",
+      amount: {
+        currency: "USD",
+        value: "1.00"
+      }
+    },
+    shippingOptions: [
+      {
+        id: "NormalShipping",
+        label: "NormalShipping",
+        amount: {
+          currency: "USD",
+          value: "10.00"
+        },
+        selected: false,
+      },
+    ],
+  };
+
+  const defaultOptions = {
+    requestPayerName: true,
+    requestPayerEmail: false,
+    reqeustPayerPhone: false,
+    requestShipping: true,
+    shippingType: "shipping"
+  };
+
+  const updateDetails = {
+    total: {
+      label: "Total",
+      amount: {
+        currency: "USD",
+        value: "1.00"
+      }
+    },
+    error: "",
+  };
+  // testing function main body
+  function test_bug1443914() {
+    gScript.sendAsyncMessage("set-checking-shipping-options-ui-service");
+    return new Promise((resolve, reject) => {
+      const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
+      const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
+      request.addEventListener("shippingaddresschange", (event) => {
+        event.updateWith(updateDetails);
+      });
+      request.addEventListener("shippingoptionchange", (event) => {
+        event.updateWith(updateDetails);
+      });
+      request.show().then(response => {
+        response.complete("success").then(() => {
+	  resolve();
+	}).catch(error => {
+	  ok(false, "Unexpected error: " + error.name);
+	  resolve();
+	});
+      }).catch(error => {
+        ok(false, "Unexpected error: " + error.name);
+	resolve();
+      }).finally(handler.destruct);
+    });
+  }
+
+  function teardown() {
+    gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
+      gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
+      gScript.removeMessageListener("test-fail", testFailHandler);
+      gScript.removeMessageListener("test-pass", testPassHandler);
+      gScript.destroy();
+      SimpleTest.finish();
+    });
+    gScript.sendAsyncMessage("teardown");
+  }
+
+  function runTests() {
+    test_bug1443914()
+    .then(teardown)
+    .catch( e => {
+      ok(false, "Unexpected error: " + e.name);
+      SimpleTest.finish();
+    });
+  }
+
+  window.addEventListener('load', function() {
+    SpecialPowers.pushPrefEnv({
+      'set': [
+        ['dom.payments.request.enabled', true],
+      ]
+    }, runTests);
+  });
+
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1443914">Mozilla Bug 1443914</a>
+</body>
+</html>