Bug 1398991 - Mochitest for spec change on PaymentRequest.shippingOption. r=baku
authorEden Chuang <echuang@mozilla.com>
Fri, 15 Sep 2017 14:20:05 +0800
changeset 381376 26289fa4d71ac9a6fefde68643a17cd0517a81d9
parent 381375 080349a1a6214fb01f40c40daa3c961a285a22ca
child 381377 a2ed201095ea183c4657aae5d961d8c978b06a9d
push id32526
push userarchaeopteryx@coole-files.de
push dateSun, 17 Sep 2017 21:32:35 +0000
treeherdermozilla-central@ae39864562c6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1398991
milestone57.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 1398991 - Mochitest for spec change on PaymentRequest.shippingOption. r=baku This patch changes the test_construction.html with following modification. 1. Modify the test testWithDuplicateShippingOptionsParameters to expect a TypeError when constructing wiht duplicate shippingOption ids. 2. Add test testShippingOtpionAttribute for shippingOption setting checking with following conditions 1. No selected shippingOption and PaymentOptions.requestShipping is false 2. One selected shippingOption and PaymentOptions.requestShipping is false 3. One selected shippingOption and PaymentOptions.requestShipping is true 4. Multiple selected shippingOptions and PaymentOptions.requestShipping is true.
dom/payments/test/ConstructorChromeScript.js
dom/payments/test/test_constructor.html
--- a/dom/payments/test/ConstructorChromeScript.js
+++ b/dom/payments/test/ConstructorChromeScript.js
@@ -326,77 +326,16 @@ function checkNonBasicCardRequest(payReq
   if (paymentOptions.requestShipping) {
     emitTestFail("requestShipping option should be false.");
   }
   if (paymentOptions.shippingType != "shipping") {
     emitTestFail("shippingType option should be 'shipping'.")
   }
 }
 
-function checkDuplicateShippingOptionsRequest(payRequest) {
-  if (payRequest.paymentMethods.length != 1) {
-    emitTestFail("paymentMethods' length should be 1.");
-  }
-
-  const methodData = payRequest.paymentMethods.queryElementAt(0, Ci.nsIPaymentMethodData);
-  if (!methodData) {
-    emitTestFail("Fail to get payment methodData.");
-  }
-  let supportedMethod = methodData.supportedMethods;
-  if (supportedMethod != "basic-card") {
-    emitTestFail("supported method should be 'basic-card'.");
-  }
-  // checking the passed PaymentDetails parameter
-  const details = payRequest.paymentDetails;
-  if (details.id != "duplicate shipping options details" ) {
-    emitTestFail("details.id should be 'duplicate shipping options details'.");
-  }
-  if (details.totalItem.label != "Total") {
-    emitTestFail("total item's label should be 'Total'.");
-  }
-  if (details.totalItem.amount.currency != "USD") {
-    emitTestFail("total item's currency should be 'USD'.");
-  }
-  if (details.totalItem.amount.value != "1.00") {
-    emitTestFail("total item's value should be '1.00'.");
-  }
-
-  if (details.displayItems) {
-    emitTestFail("details.displayItems should be undefined.");
-  }
-  if (details.modifiers) {
-    emitTestFail("details.displayItems should be undefined.");
-  }
-  const shippingOptions = details.shippingOptions;
-  if (!shippingOptions) {
-    emitTestFail("details.shippingOptions should not be undefined.");
-  }
-  if (shippingOptions.length != 0) {
-    emitTestFail("shippingOptions' length should be 0.");
-  }
-
-  // checking the default generated PaymentOptions parameter
-  const paymentOptions = payRequest.paymentOptions;
-  if (paymentOptions.requestPayerName) {
-    emitTestFail("requestPayerName option should be false.");
-  }
-  if (paymentOptions.requestPayerEmail) {
-    emitTestFail("requestPayerEmail option should be false.");
-  }
-  if (paymentOptions.requestPayerPhone) {
-    emitTestFail("requestPayerPhone option should be false.");
-  }
-  if (paymentOptions.requestShipping) {
-    emitTestFail("requestShipping option should be false.");
-  }
-  if (paymentOptions.shippingType != "shipping") {
-    emitTestFail("shippingType option should be 'shipping'.")
-  }
-}
-
 function checkSimplestRequestHandler() {
   const paymentEnum = paymentSrv.enumerate();
   if (!paymentEnum.hasMoreElements()) {
     emitTestFail("PaymentRequestService should have at least one payment request.");
   }
   while (paymentEnum.hasMoreElements()) {
     let payRequest = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
     if (!payRequest) {
@@ -438,48 +377,29 @@ function checkNonBasicCardRequestHandler
       break;
     }
     checkNonBasicCardRequest(payRequest);
   }
   paymentSrv.cleanup();
   sendAsyncMessage("check-complete");
 }
 
-function checkDuplicateShippingOptionsRequestHandler() {
-  const paymentEnum = paymentSrv.enumerate();
-  if (!paymentEnum.hasMoreElements()) {
-    emitTestFail("PaymentRequestService should have at least one payment request.");
-  }
-  while (paymentEnum.hasMoreElements()) {
-    let payRequest = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
-    if (!payRequest) {
-      emitTestFail("Fail to get existing payment request.");
-      break;
-    }
-    checkDuplicateShippingOptionsRequest(payRequest);
-  }
-  paymentSrv.cleanup();
-  sendAsyncMessage("check-complete");
-}
-
 function checkMultipleRequestsHandler () {
   const paymentEnum = paymentSrv.enumerate();
   if (!paymentEnum.hasMoreElements()) {
     emitTestFail("PaymentRequestService should have at least one payment request.");
   }
   while (paymentEnum.hasMoreElements()) {
     let payRequest = paymentEnum.getNext().QueryInterface(Ci.nsIPaymentRequest);
     if (!payRequest) {
       emitTestFail("Fail to get existing payment request.");
       break;
     }
     if (payRequest.paymentDetails.id == "payment details") {
       checkComplexRequest(payRequest);
-    } else if (payRequest.paymentDetails.id == "duplicate shipping options details") {
-      checkDuplicateShippingOptionsRequest(payRequest);
     } else {
       checkSimplestRequest(payRequest);
     }
   }
   paymentSrv.cleanup();
   sendAsyncMessage("check-complete");
 }
 
@@ -500,17 +420,16 @@ function checkCrossOriginTopLevelPrincip
     }
   }
   paymentSrv.cleanup();
   sendAsyncMessage("check-complete");
 }
 
 addMessageListener("check-simplest-request", checkSimplestRequestHandler);
 addMessageListener("check-complex-request", checkComplexRequestHandler);
-addMessageListener("check-duplicate-shipping-options-request", checkDuplicateShippingOptionsRequestHandler);
 addMessageListener("check-multiple-requests", checkMultipleRequestsHandler);
 addMessageListener("check-nonbasiccard-request", checkNonBasicCardRequestHandler);
 addMessageListener("check-cross-origin-top-level-principal", checkCrossOriginTopLevelPrincipalHandler);
 
 addMessageListener("teardown", function() {
   paymentSrv.cleanup();
   sendAsyncMessage("teardown-complete");
 });
--- a/dom/payments/test/test_constructor.html
+++ b/dom/payments/test/test_constructor.html
@@ -194,34 +194,100 @@ https://bugzilla.mozilla.org/show_bug.cg
         resolve();
       });
       gScript.sendAsyncMessage("check-nonbasiccard-request");
     });
   }
 
   function testWithDuplicateShippingOptionsParameters() {
     return new Promise((resolve, reject) => {
-      const payRequest = new PaymentRequest(simplestMethods, duplicateShippingOptionsDetails);
-      ok(payRequest, "PaymentRequest should be created");
-      gScript.addMessageListener("check-complete", function checkCompleteHandler() {
-        gScript.removeMessageListener("check-complete", checkCompleteHandler);
+      try {
+        const payRequest = new PaymentRequest(simplestMethods,
+                                              duplicateShippingOptionsDetails,
+                                              {requestShipping: true});
+        ok(false, "Construction should fail with duplicate shippingOption Ids.");
+        resolve();
+      } catch (e) {
+        is(e.name, "TypeError", "Expected 'TypeError' with duplicate shippingOption Ids.");
         resolve();
-      });
-      gScript.sendAsyncMessage("check-duplicate-shipping-options-request");
+      }
+    });
+  }
+
+  function testShippingOtpionAttribute() {
+    return new Promise((resolve, reject) => {
+      const details = {
+        total: {
+          label: "Total",
+          amount: {
+            currency: "USD",
+            value: "1.00",
+          },
+        },
+        shippingOptions: [
+          {
+            id: "option1",
+            label: "option1",
+            amount: {
+              currency: "USD",
+              value: "1.00",
+            },
+            selected: false,
+          },
+          {
+            id: "option2",
+            label: "option2",
+            amount: {
+              currency: "USD",
+              value: "1.00",
+            },
+            selected: false,
+          },
+        ],
+      };
+      const payRequest1 = new PaymentRequest(simplestMethods,
+                                             details,
+                                             {requestShipping: false});
+      ok(payRequest1, "PaymentRequest should be created");
+      ok(!payRequest1.shippingOption,
+         "request.shippingOption should be null in default, when options.requestShipping is false");
+      details.shippingOptions[0].selected = true;
+      const payRequest2 = new PaymentRequest(simplestMethods,
+                                             details,
+                                             {requestShipping: false});
+      ok(payRequest2, "PaymentRequest should be created");
+      ok(!payRequest2.shippingOption,
+         "request.shippingOption should be null in default, when options.requestShipping is false");
+      const payRequest3 = new PaymentRequest(simplestMethods,
+                                             details,
+                                             {requestShipping: true});
+      ok(payRequest3, "PaymentRequest should be created");
+      ok(payRequest3.shippingOption,
+         "request.shippingOption should not be null when both shoppingOtpion.selected and options.requestOptions are true");
+      is(payRequest3.shippingOption, "option1",
+         "request.shippingOption should be 'option1'");
+      details.shippingOptions[1].selected = true;
+      const payRequest4 = new PaymentRequest(simplestMethods,
+                                             details,
+                                             {requestShipping: true});
+      ok(payRequest4, "PaymentRequest should be created");
+      ok(payRequest4.shippingOption,
+         "request.shippingOption should not be null when both shoppingOtpion.selected and options.requestOptions are true");
+      is(payRequest4.shippingOption, "option2",
+         "request.shippingOption should be 'option2' which is the last one selected.");
+      resolve();
     });
   }
 
   function testMultipleRequests() {
     return new Promise((resolve, reject) => {
       const payRequest1 = new PaymentRequest(complexMethods, complexDetails, complexOptions);
       const payRequest2 = new PaymentRequest(simplestMethods, simplestDetails);
-      const payRequest3 = new PaymentRequest(simplestMethods, duplicateShippingOptionsDetails);
       ok(payRequest1, "PaymentRequest with complex parameters should be created");
       ok(payRequest2, "PaymentRequest with simplest parameters should be created");
-      ok(payRequest3, "PaymentRequest with duplicate shipping options parameters should be created");
       gScript.addMessageListener("check-complete", function checkCompleteHandler() {
         gScript.removeMessageListener("check-complete", checkCompleteHandler);
         resolve();
       });
       gScript.sendAsyncMessage("check-multiple-requests");
     });
   }
 
@@ -257,16 +323,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 
   function runTests() {
     testWithSimplestParameters()
     .then(testWithComplexParameters)
     .then(testWithNonBasicCardMethods)
     .then(testWithDuplicateShippingOptionsParameters)
     .then(testMultipleRequests)
     .then(testCrossOriginTopLevelPrincipal)
+    .then(testShippingOtpionAttribute)
     .then(teardown)
     .catch( e => {
       ok(false, "Unexpected error: " + e.name);
       SimpleTest.finish();
     });
   }
 
   window.addEventListener('load', function() {
@@ -276,16 +343,10 @@ https://bugzilla.mozilla.org/show_bug.cg
       ]
     }, runTests);
   });
 
   </script>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1345361">Mozilla Bug 1345361</a>
-<p id="display"></p>
-<div id="content" style="display: none">
-
-</div>
-<pre id="test">
-</pre>
 </body>
 </html>