Bug 1430979 - Wait for focus event in addition to FormReady event in formautofill tests. r=MattN
authorTooru Fujisawa <arai_a@mac.com>
Mon, 29 Jan 2018 19:15:11 +0900
changeset 401203 f7400dab78d50ab6735e28d81cca4c1f5a0eba77
parent 401202 47f092f7aa1206db4b54801320d489d7345bea7a
child 401204 ed6f573553e71ba4d7c0454a105e332c5cde2525
push id99311
push userarai_a@mac.com
push dateMon, 29 Jan 2018 10:19:29 +0000
treeherdermozilla-inbound@f7400dab78d5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1430979
milestone60.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 1430979 - Wait for focus event in addition to FormReady event in formautofill tests. r=MattN
browser/extensions/formautofill/test/browser/browser_editCreditCardDialog.js
browser/extensions/formautofill/test/browser/browser_manageAddressesDialog.js
browser/extensions/formautofill/test/browser/browser_manageCreditCardsDialog.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,93 @@
 /* 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);
-    win.addEventListener("load", () => {
+    waitForFocus(() => {
       win.addEventListener("unload", () => {
         ok(true, "Edit credit card dialog is closed");
         resolve();
       }, {once: true});
       win.document.querySelector("#cancel").click();
-    }, {once: true});
+    }, win);
   });
 });
 
 add_task(async function test_cancelEditCreditCardDialogWithESC() {
   await new Promise(resolve => {
     let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    win.addEventListener("load", () => {
+    waitForFocus(() => {
       win.addEventListener("unload", () => {
         ok(true, "Edit credit card dialog is closed with ESC key");
         resolve();
       }, {once: true});
       EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
-    }, {once: true});
+    }, win);
   });
 });
 
 add_task(async function test_saveCreditCard() {
   await new Promise(resolve => {
     let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL);
-    win.addEventListener("load", () => {
+    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);
-    }, {once: true});
+    }, 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);
-    win.addEventListener("load", () => {
+    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);
-    }, {once: true});
+    }, 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,56 +95,52 @@ 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");
-  await new Promise(resolve => {
-    let win = window.openDialog(EDIT_CREDIT_CARD_DIALOG_URL, null, null, creditCards[0]);
-    win.addEventListener("FormReady", () => {
-      win.addEventListener("unload", () => {
-        ok(true, "Edit credit card dialog is closed");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("VK_TAB", {}, win);
-      EventUtils.synthesizeKey("VK_RIGHT", {}, win);
-      EventUtils.synthesizeKey("test", {}, win);
-      win.document.querySelector("#save").click();
-    }, {once: true});
-  });
+  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;
+  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);
-    win.addEventListener("load", () => {
+    waitForFocus(() => {
       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);
       win.document.querySelector("#save").click();
 
       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);
-    }, {once: true});
+    }, win);
   });
   let creditCards = await getCreditCards();
 
   is(creditCards.length, 0, "Credit card storage is empty");
 });
--- a/browser/extensions/formautofill/test/browser/browser_manageAddressesDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_manageAddressesDialog.js
@@ -21,35 +21,31 @@ add_task(async function test_manageAddre
       is(btnAdd.disabled, false, "Add button enabled");
       is(btnRemove.disabled, true, "Remove button disabled");
       is(btnEdit.disabled, true, "Edit button disabled");
     });
   });
 });
 
 add_task(async function test_cancelManageAddressDialogWithESC() {
-  await new Promise(resolve => {
-    let win = window.openDialog(MANAGE_ADDRESSES_DIALOG_URL);
-    win.addEventListener("FormReady", () => {
-      win.addEventListener("unload", () => {
-        ok(true, "Manage addresses dialog is closed with ESC key");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
-    }, {once: true});
-  });
+  let win = window.openDialog(MANAGE_ADDRESSES_DIALOG_URL);
+  await waitForFocusAndFormReady(win);
+  let unloadPromise = BrowserTestUtils.waitForEvent(win, "unload");
+  EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
+  await unloadPromise;
+  ok(true, "Manage addresses dialog is closed with ESC key");
 });
 
 add_task(async function test_removingSingleAndMultipleAddresses() {
   await saveAddress(TEST_ADDRESS_1);
   await saveAddress(TEST_ADDRESS_2);
   await saveAddress(TEST_ADDRESS_3);
 
   let win = window.openDialog(MANAGE_ADDRESSES_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
   let btnRemove = win.document.querySelector(TEST_SELECTORS.btnRemove);
   let btnEdit = win.document.querySelector(TEST_SELECTORS.btnEdit);
 
   is(selRecords.length, 3, "Three addresses");
 
   EventUtils.synthesizeMouseAtCenter(selRecords.children[0], {}, win);
@@ -68,17 +64,17 @@ add_task(async function test_removingSin
   await BrowserTestUtils.waitForEvent(selRecords, "RecordsRemoved");
   is(selRecords.length, 0, "All addresses are removed");
 
   win.close();
 });
 
 add_task(async function test_addressesDialogWatchesStorageChanges() {
   let win = window.openDialog(MANAGE_ADDRESSES_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
 
   await saveAddress(TEST_ADDRESS_1);
   await BrowserTestUtils.waitForEvent(selRecords, "RecordsLoaded");
   is(selRecords.length, 1, "One address is shown");
 
   await removeAddresses([selRecords.options[0].value]);
--- a/browser/extensions/formautofill/test/browser/browser_manageCreditCardsDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_manageCreditCardsDialog.js
@@ -24,35 +24,31 @@ add_task(async function test_manageCredi
       is(btnShowHideCreditCards.disabled, true, "Show Credit Cards button disabled");
       is(btnAdd.disabled, false, "Add button enabled");
       is(btnEdit.disabled, true, "Edit button disabled");
     });
   });
 });
 
 add_task(async function test_cancelManageCreditCardsDialogWithESC() {
-  await new Promise(resolve => {
-    let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL);
-    win.addEventListener("FormReady", () => {
-      win.addEventListener("unload", () => {
-        ok(true, "Manage credit cards dialog is closed with ESC key");
-        resolve();
-      }, {once: true});
-      EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
-    }, {once: true});
-  });
+  let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL);
+  await waitForFocusAndFormReady(win);
+  let unloadPromise = BrowserTestUtils.waitForEvent(win, "unload");
+  EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
+  await unloadPromise;
+  ok(true, "Manage credit cards dialog is closed with ESC key");
 });
 
 add_task(async function test_removingSingleAndMultipleCreditCards() {
   await saveCreditCard(TEST_CREDIT_CARD_1);
   await saveCreditCard(TEST_CREDIT_CARD_2);
   await saveCreditCard(TEST_CREDIT_CARD_3);
 
   let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
   let btnRemove = win.document.querySelector(TEST_SELECTORS.btnRemove);
   let btnEdit = win.document.querySelector(TEST_SELECTORS.btnEdit);
 
   is(selRecords.length, 3, "Three credit cards");
   is(selRecords[0].text, "**** 6666", "Masked credit card 3");
   is(selRecords[1].text, "**** 4444, Timothy Berners-Lee", "Masked credit card 2");
@@ -74,17 +70,17 @@ add_task(async function test_removingSin
   await BrowserTestUtils.waitForEvent(selRecords, "RecordsRemoved");
   is(selRecords.length, 0, "All credit cards are removed");
 
   win.close();
 });
 
 add_task(async function test_creditCardsDialogWatchesStorageChanges() {
   let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
 
   await saveCreditCard(TEST_CREDIT_CARD_1);
   await BrowserTestUtils.waitForEvent(selRecords, "RecordsLoaded");
   is(selRecords.length, 1, "One credit card is shown");
 
   await removeCreditCards([selRecords.options[0].value]);
@@ -94,17 +90,17 @@ add_task(async function test_creditCards
 });
 
 add_task(async function test_showCreditCards() {
   await saveCreditCard(TEST_CREDIT_CARD_1);
   await saveCreditCard(TEST_CREDIT_CARD_2);
   await saveCreditCard(TEST_CREDIT_CARD_3);
 
   let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
   let btnShowHideCreditCards = win.document.querySelector(TEST_SELECTORS.btnShowHideCreditCards);
 
   is(btnShowHideCreditCards.disabled, false, "Show credit cards button enabled");
   is(btnShowHideCreditCards.textContent, "Show Credit Cards", "Label should be 'Show Credit Cards'");
 
   // Show credit card numbers
@@ -143,17 +139,17 @@ add_task(async function test_showCreditC
   win.close();
 });
 
 add_task(async function test_hasMasterPassword() {
   await saveCreditCard(TEST_CREDIT_CARD_1);
   LoginTestUtils.masterPassword.enable();
 
   let win = window.openDialog(MANAGE_CREDIT_CARDS_DIALOG_URL, null, DIALOG_SIZE);
-  await BrowserTestUtils.waitForEvent(win, "FormReady");
+  await waitForFocusAndFormReady(win);
 
   let selRecords = win.document.querySelector(TEST_SELECTORS.selRecords);
   let btnRemove = win.document.querySelector(TEST_SELECTORS.btnRemove);
   let btnShowHideCreditCards = win.document.querySelector(TEST_SELECTORS.btnShowHideCreditCards);
   let btnAdd = win.document.querySelector(TEST_SELECTORS.btnAdd);
   let btnEdit = win.document.querySelector(TEST_SELECTORS.btnEdit);
   let masterPasswordDialogShown = waitForMasterPasswordDialog();
 
--- a/browser/extensions/formautofill/test/browser/head.js
+++ b/browser/extensions/formautofill/test/browser/head.js
@@ -324,22 +324,24 @@ async function removeAllRecords() {
   }
 
   let creditCards = await getCreditCards();
   if (creditCards.length) {
     await removeCreditCards(creditCards.map(cc => cc.guid));
   }
 }
 
-function testDialog(url, testFn, arg) {
-  return new Promise(resolve => {
-    let win = window.openDialog(url, null, null, arg);
-    win.addEventListener("FormReady", () => {
-      win.addEventListener("unload", () => {
-        ok(true, "Dialog is closed");
-        resolve();
-      }, {once: true});
-      testFn(win);
-    }, {once: true});
-  });
+async function waitForFocusAndFormReady(win) {
+  return Promise.all([
+    new Promise(resolve => waitForFocus(resolve, win)),
+    BrowserTestUtils.waitForEvent(win, "FormReady"),
+  ]);
+}
+
+async function testDialog(url, testFn, arg) {
+  let win = window.openDialog(url, null, null, arg);
+  await waitForFocusAndFormReady(win);
+  let unloadPromise = BrowserTestUtils.waitForEvent(win, "unload");
+  testFn(win);
+  return unloadPromise;
 }
 
 registerCleanupFunction(removeAllRecords);