Bug 1440530 - Move dialog setup code out to a helper function for the webpayments mochitest-browser tests. r?mattn draft
authorJared Wein <jwein@mozilla.com>
Thu, 22 Feb 2018 17:24:59 -0800
changeset 758802 564b804f848919583c4b368bf920b21ec58ada36
parent 758770 304f67528299cd9a5e63c124109be848691c3052
child 758871 a40663d466bb89c16a1b3e01fe967cd23f163035
push id100176
push userbmo:jaws@mozilla.com
push dateFri, 23 Feb 2018 01:25:18 +0000
reviewersmattn
bugs1440530
milestone60.0a1
Bug 1440530 - Move dialog setup code out to a helper function for the webpayments mochitest-browser tests. r?mattn MozReview-Commit-ID: 9rpN1lIjOCG
toolkit/components/payments/test/browser/browser_change_shipping.js
toolkit/components/payments/test/browser/browser_profile_storage.js
toolkit/components/payments/test/browser/browser_request_shipping.js
toolkit/components/payments/test/browser/browser_shippingaddresschange_error.js
toolkit/components/payments/test/browser/browser_show_dialog.js
toolkit/components/payments/test/browser/head.js
--- a/toolkit/components/payments/test/browser/browser_change_shipping.js
+++ b/toolkit/components/payments/test/browser/browser_change_shipping.js
@@ -49,35 +49,24 @@ add_task(async function test_show_comple
 
   profileStorage.creditCards.add(card);
   await onChanged;
 
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser,
-                            {
-                              methodData: [PTU.MethodData.basicCard],
-                              details: PTU.Details.twoShippingOptions,
-                              options: PTU.Options.requestShippingOption,
-                            },
-                            PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData: [PTU.MethodData.basicCard],
+        details: PTU.Details.twoShippingOptions,
+        options: PTU.Options.requestShippingOption,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     let shippingOptions =
       await spawnPaymentDialogTask(frame, PTU.DialogContentTasks.getShippingOptions);
     is(shippingOptions.selectedOptionCurrency, "USD", "Shipping options should be in USD");
     is(shippingOptions.optionCount, 2, "there should be two shipping options");
     is(shippingOptions.selectedOptionValue, "2", "default selected should be '2'");
 
     await spawnPaymentDialogTask(frame,
--- a/toolkit/components/payments/test/browser/browser_profile_storage.js
+++ b/toolkit/components/payments/test/browser/browser_profile_storage.js
@@ -33,30 +33,23 @@ add_task(async function test_initial_sta
     "cc-exp-year": 2028,
   });
   await onChanged;
 
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser, {methodData, details}, PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let win = await getPaymentWidget();
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
-    await dialogReadyPromise;
-    info("dialog ready");
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData,
+        details,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     await spawnPaymentDialogTask(frame, async function checkInitialStore({
       address1GUID,
       card1GUID,
     }) {
       info("checkInitialStore");
       let contentWin = Components.utils.waiveXrays(content);
       let {
--- a/toolkit/components/payments/test/browser/browser_request_shipping.js
+++ b/toolkit/components/payments/test/browser/browser_request_shipping.js
@@ -15,35 +15,24 @@ add_task(async function setup() {
   await onChanged;
 });
 
 add_task(async function test_request_shipping_present() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser,
-                            {
-                              methodData: [PTU.MethodData.basicCard],
-                              details: PTU.Details.twoShippingOptions,
-                              options: PTU.Options.requestShippingOption,
-                            },
-                            PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData: [PTU.MethodData.basicCard],
+        details: PTU.Details.twoShippingOptions,
+        options: PTU.Options.requestShippingOption,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     let isShippingOptionsVisible =
       await spawnPaymentDialogTask(frame,
                                    PTU.DialogContentTasks.isElementVisible,
                                    "shipping-option-picker");
     ok(isShippingOptionsVisible, "shipping-option-picker should be visible");
     let isShippingAddressVisible =
       await spawnPaymentDialogTask(frame, PTU.DialogContentTasks.isElementVisible,
@@ -55,34 +44,23 @@ add_task(async function test_request_shi
   });
 });
 
 add_task(async function test_request_shipping_not_present() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser,
-                            {
-                              methodData: [PTU.MethodData.basicCard],
-                              details: PTU.Details.twoShippingOptions,
-                            },
-                            PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData: [PTU.MethodData.basicCard],
+        details: PTU.Details.twoShippingOptions,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     let isShippingOptionsVisible =
       await spawnPaymentDialogTask(frame,
                                    PTU.DialogContentTasks.isElementVisible,
                                    "shipping-option-picker");
     ok(!isShippingOptionsVisible, "shipping-option-picker should not be visible");
     let isShippingAddressVisible =
       await spawnPaymentDialogTask(frame, PTU.DialogContentTasks.isElementVisible,
--- a/toolkit/components/payments/test/browser/browser_shippingaddresschange_error.js
+++ b/toolkit/components/payments/test/browser/browser_shippingaddresschange_error.js
@@ -2,35 +2,24 @@
 
 add_task(addSampleAddressesAndBasicCard);
 
 add_task(async function test_show_error_on_addresschange() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser,
-                            {
-                              methodData: [PTU.MethodData.basicCard],
-                              details: PTU.Details.twoShippingOptions,
-                              options: PTU.Options.requestShippingOption,
-                            },
-                            PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData: [PTU.MethodData.basicCard],
+        details: PTU.Details.twoShippingOptions,
+        options: PTU.Options.requestShippingOption,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     info("setting up the event handler for shippingoptionchange");
     let EXPECTED_ERROR_TEXT = "Cannot ship with option 1 on days that end with Y";
     await ContentTask.spawn(browser, {
       eventName: "shippingoptionchange",
       details: {
         error: EXPECTED_ERROR_TEXT,
         shippingOptions: [],
--- a/toolkit/components/payments/test/browser/browser_show_dialog.js
+++ b/toolkit/components/payments/test/browser/browser_show_dialog.js
@@ -3,50 +3,43 @@
 const methodData = [PTU.MethodData.basicCard];
 const details = PTU.Details.twoShippingOptions;
 
 add_task(async function test_show_abort_dialog() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser, {methodData, details}, PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let win = await getPaymentWidget();
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
+    let {win} =
+      await setupPaymentDialog(browser, {
+        methodData,
+        details,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     // abort the payment request
     ContentTask.spawn(browser, null, async () => content.rq.abort());
     await BrowserTestUtils.waitForCondition(() => win.closed, "dialog should be closed");
   });
 });
 
 add_task(async function test_show_manualAbort_dialog() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser, {methodData, details}, PTU.ContentTasks.createAndShowRequest);
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData,
+        details,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    // abort the payment request manually
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
     spawnPaymentDialogTask(frame, PTU.DialogContentTasks.manuallyClickCancel);
     await BrowserTestUtils.waitForCondition(() => win.closed, "dialog should be closed");
   });
 });
 
 add_task(async function test_show_completePayment() {
   let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
                                           (subject, data) => data == "add");
@@ -77,29 +70,24 @@ add_task(async function test_show_comple
 
   profileStorage.creditCards.add(card);
   await onChanged;
 
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser, {methodData, details}, PTU.ContentTasks.createAndShowRequest);
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData,
+        details,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
-    ok(frame, "Got payment frame");
     info("entering CSC");
     await spawnPaymentDialogTask(frame, PTU.DialogContentTasks.setSecurityCode, {
       securityCode: "999",
     });
     info("clicking pay");
     spawnPaymentDialogTask(frame, PTU.DialogContentTasks.completePayment);
 
     // Add a handler to complete the payment above.
@@ -135,28 +123,23 @@ add_task(async function test_show_comple
   });
 });
 
 add_task(async function test_show_completePayment() {
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: BLANK_PAGE_URL,
   }, async browser => {
-    let dialogReadyPromise = waitForWidgetReady();
-    // start by creating a PaymentRequest, and show it
-    await ContentTask.spawn(browser, {methodData, details}, PTU.ContentTasks.createAndShowRequest);
-
-    // get a reference to the UI dialog and the requestId
-    let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
-    ok(win, "Got payment widget");
-    let requestId = paymentUISrv.requestIdForWindow(win);
-    ok(requestId, "requestId should be defined");
-    is(win.closed, false, "dialog should not be closed");
-
-    let frame = await getPaymentFrame(win);
+    let {win, frame} =
+      await setupPaymentDialog(browser, {
+        methodData,
+        details,
+        taskFn: PTU.ContentTasks.createAndShowRequest,
+      }
+    );
 
     await ContentTask.spawn(browser, {
       eventName: "shippingoptionchange",
     }, PTU.ContentTasks.promisePaymentRequestEvent);
 
     info("changing shipping option to '1' from default selected option of '2'");
     spawnPaymentDialogTask(frame, PTU.DialogContentTasks.selectShippingOptionById, "1");
 
--- a/toolkit/components/payments/test/browser/head.js
+++ b/toolkit/components/payments/test/browser/head.js
@@ -144,16 +144,43 @@ async function addSampleAddressesAndBasi
   await onChanged;
 
   onChanged = TestUtils.topicObserved("formautofill-storage-changed",
                                       (subject, data) => data == "add");
   profileStorage.creditCards.add(PTU.BasicCards.JohnDoe);
   await onChanged;
 }
 
+async function setupPaymentDialog(browser, {methodData, details, options, taskFn}) {
+  let dialogReadyPromise = waitForWidgetReady();
+  // start by creating a PaymentRequest, and show it
+  await ContentTask.spawn(browser,
+                          {
+                            methodData,
+                            details,
+                            options,
+                          },
+                          taskFn);
+
+  // get a reference to the UI dialog and the requestId
+  let [win] = await Promise.all([getPaymentWidget(), dialogReadyPromise]);
+  ok(win, "Got payment widget");
+  let requestId = paymentUISrv.requestIdForWindow(win);
+  ok(requestId, "requestId should be defined");
+  is(win.closed, false, "dialog should not be closed");
+
+  let frame = await getPaymentFrame(win);
+  ok(frame, "Got payment frame");
+
+  await dialogReadyPromise;
+  info("dialog ready");
+
+  return {win, requestId, frame};
+}
+
 /**
  * Open a merchant tab with the given merchantTaskFn to create a PaymentRequest
  * and then open the associated PaymentRequest dialog in a new tab and run the
  * associated dialogTaskFn. The same taskArgs are passed to both functions.
  *
  * @param {Function} merchantTaskFn
  * @param {Function} dialogTaskFn
  * @param {Object} taskArgs