Bug 1432952 - Use the testDialog helper in browser_editCreditCardDialog.js. r=jaws draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Tue, 10 Apr 2018 17:39:25 -0700
changeset 780012 e28eacb8c2c5d1e58c7e93ef71bd50226dfa08fd
parent 780011 3175d62bb3a46ca502f1d9aec1b305fb854e3fad
child 780013 dd24c823b5d195a1e1e44fe08ef71740da7c1702
child 780841 fd4a322c2ffe81937f362f0234d8a8d0fc45a154
child 780843 97ae3457c11e3a593bb1abe163a32e7008a086b1
push id105938
push usermozilla@noorenberghe.ca
push dateWed, 11 Apr 2018 02:32:24 +0000
reviewersjaws
bugs1432952
milestone61.0a1
Bug 1432952 - Use the testDialog helper in browser_editCreditCardDialog.js. r=jaws MozReview-Commit-ID: 9tquQ0C7D93
browser/extensions/formautofill/test/browser/browser_editCreditCardDialog.js
browser/extensions/formautofill/test/browser/head.js
--- a/browser/extensions/formautofill/test/browser/browser_editCreditCardDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_editCreditCardDialog.js
@@ -1,93 +1,65 @@
 /* eslint-disable mozilla/no-arbitrary-setTimeout */
 
 "use strict";
 
 add_task(async function test_cancelEditCreditCardDialog() {
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    waitForFocus(() => {
-      win.addEventListener("unload", () => {
-        ok(true, "Edit credit card dialog is closed");
-        resolve();
-      }, {once: true});
-      win.document.querySelector("#cancel").click();
-    }, win);
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    win.document.querySelector("#cancel").click();
   });
 });
 
 add_task(async function test_cancelEditCreditCardDialogWithESC() {
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    waitForFocus(() => {
-      win.addEventListener("unload", () => {
-        ok(true, "Edit credit card dialog is closed with ESC key");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
-    }, win);
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
   });
 });
 
 add_task(async function test_saveCreditCard() {
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    waitForFocus(() => {
-      win.addEventListener("unload", () => {
-        ok(true, "Edit credit card dialog is closed");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-number"], {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-name"], {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("0" + TEST_CREDIT_CARD_1["cc-exp-month"].toString(), {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-exp-year"].toString(), {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      info("saving credit card");
-      EventUtils.synthesizeKey("VK_RETURN", {}, win);
-    }, win);
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-number"], {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-name"], {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("0" + TEST_CREDIT_CARD_1["cc-exp-month"].toString(), {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_1["cc-exp-year"].toString(), {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    info("saving credit card");
+    EventUtils.synthesizeKey("VK_RETURN", {}, win);
   });
   let creditCards = await getCreditCards();
 
   is(creditCards.length, 1, "only one credit card is in storage");
   for (let [fieldName, fieldValue] of Object.entries(TEST_CREDIT_CARD_1)) {
     if (fieldName === "cc-number") {
       fieldValue = "*".repeat(fieldValue.length - 4) + fieldValue.substr(-4);
     }
     is(creditCards[0][fieldName], fieldValue, "check " + fieldName);
   }
   ok(creditCards[0]["cc-number-encrypted"], "cc-number-encrypted exists");
 });
 
 add_task(async function test_saveCreditCardWithMaxYear() {
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    waitForFocus(() => {
-      win.addEventListener("unload", () => {
-        ok(true, "Edit credit card dialog is closed");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-number"], {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-name"], {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-exp-month"].toString(), {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-exp-year"].toString(), {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      info("saving credit card");
-      EventUtils.synthesizeKey("VK_RETURN", {}, win);
-    }, win);
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-number"], {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-name"], {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-exp-month"].toString(), {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey(TEST_CREDIT_CARD_2["cc-exp-year"].toString(), {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    info("saving credit card");
+    EventUtils.synthesizeKey("VK_RETURN", {}, win);
   });
   let creditCards = await getCreditCards();
 
   is(creditCards.length, 2, "Two credit card is in storage");
   for (let [fieldName, fieldValue] of Object.entries(TEST_CREDIT_CARD_2)) {
     if (fieldName === "cc-number") {
       fieldValue = "*".repeat(fieldValue.length - 4) + fieldValue.substr(-4);
     }
@@ -95,52 +67,46 @@ add_task(async function test_saveCreditC
   }
   ok(creditCards[1]["cc-number-encrypted"], "cc-number-encrypted exists");
   await removeCreditCards([creditCards[1].guid]);
 });
 
 add_task(async function test_editCreditCard() {
   let creditCards = await getCreditCards();
   is(creditCards.length, 1, "only one credit card is in storage");
-  let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL, null, null, creditCards[0]);
-  await waitForFocusAndFormReady(win);
-  let unloadPromise = BrowserTestUtils.waitForEvent(win, "unload");
-  EventUtils.synthesizeKey("VK_TAB", {}, win);
-  EventUtils.synthesizeKey("VK_TAB", {}, win);
-  EventUtils.synthesizeKey("VK_RIGHT", {}, win);
-  EventUtils.synthesizeKey("test", {}, win);
-  win.document.querySelector("#save").click();
-  await unloadPromise;
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("VK_RIGHT", {}, win);
+    EventUtils.synthesizeKey("test", {}, win);
+    win.document.querySelector("#save").click();
+  }, creditCards[0]);
   ok(true, "Edit credit card dialog is closed");
   creditCards = await getCreditCards();
 
   is(creditCards.length, 1, "only one credit card is in storage");
   is(creditCards[0]["cc-name"], TEST_CREDIT_CARD_1["cc-name"] + "test", "cc name changed");
   await removeCreditCards([creditCards[0].guid]);
 
   creditCards = await getCreditCards();
   is(creditCards.length, 0, "Credit card storage is empty");
 });
 
 add_task(async function test_addInvalidCreditCard() {
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    waitForFocus(() => {
-      const unloadHandler = () => ok(false, "Edit credit card dialog shouldn't be closed");
-      win.addEventListener("unload", unloadHandler);
+  await testDialog(EDIT_CREDIT_CARD_DIALOG_URL, (win) => {
+    const unloadHandler = () => ok(false, "Edit credit card dialog shouldn't be closed");
+    win.addEventListener("unload", unloadHandler);
 
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("test", {}, win);
-      EventUtils.synthesizeMouseAtCenter(win.document.querySelector("#save"), {}, win);
+    EventUtils.synthesizeKey("VK_TAB", {}, win);
+    EventUtils.synthesizeKey("test", {}, win);
+    EventUtils.synthesizeMouseAtCenter(win.document.querySelector("#save"), {}, win);
 
-      is(win.document.querySelector("form").checkValidity(), false, "cc-number is invalid");
-      SimpleTest.requestFlakyTimeout("Ensure the window remains open after save attempt");
-      setTimeout(() => {
-        win.removeEventListener("unload", unloadHandler);
-        win.close();
-        resolve();
-      }, 500);
-    }, win);
+    is(win.document.querySelector("form").checkValidity(), false, "cc-number is invalid");
+    SimpleTest.requestFlakyTimeout("Ensure the window remains open after save attempt");
+    setTimeout(() => {
+      win.removeEventListener("unload", unloadHandler);
+      win.close();
+    }, 500);
   });
   let creditCards = await getCreditCards();
 
   is(creditCards.length, 0, "Credit card storage is empty");
 });
--- a/browser/extensions/formautofill/test/browser/head.js
+++ b/browser/extensions/formautofill/test/browser/head.js
@@ -333,17 +333,17 @@ async function removeAllRecords() {
 
 async function waitForFocusAndFormReady(win) {
   return Promise.all([
     new Promise(resolve => waitForFocus(resolve, win)),
     BrowserTestUtils.waitForEvent(win, "FormReady"),
   ]);
 }
 
-async function testDialog(url, testFn, arg) {
+async function testDialog(url, testFn, arg = undefined) {
   let win = window.openDialog(url, null, "width=600,height=600", arg);
   await waitForFocusAndFormReady(win);
   let unloadPromise = BrowserTestUtils.waitForEvent(win, "unload");
   await testFn(win);
   return unloadPromise;
 }
 
 registerCleanupFunction(removeAllRecords);