Bug 1446876 - Use waitForCondition to watch for managed state changes. r=prathiksha, a=test-only
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Wed, 20 Feb 2019 21:52:11 +0000
changeset 516066 f0f6ce3aa0f68e4e268dff78da5c9773437f6670
parent 516065 89ac71e3fa34388c0698a5af2e831a336a858052
child 516067 0f818b3e757e4d0d308876fd675d21c77b9d1c73
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersprathiksha, test-only
bugs1446876
milestone66.0
Bug 1446876 - Use waitForCondition to watch for managed state changes. r=prathiksha, a=test-only Differential Revision: https://phabricator.services.mozilla.com/D20555
browser/extensions/formautofill/test/mochitest/formautofill_common.js
browser/extensions/formautofill/test/mochitest/test_clear_form.html
browser/extensions/formautofill/test/mochitest/test_formautofill_preview_highlight.html
--- a/browser/extensions/formautofill/test/mochitest/formautofill_common.js
+++ b/browser/extensions/formautofill/test/mochitest/formautofill_common.js
@@ -78,28 +78,36 @@ function _getAdaptedProfile(profile) {
     adaptedProfile["street-address"] = FormAutofillUtils.toOneLineAddress(profile["street-address"]);
   }
 
   return adaptedProfile;
 }
 
 // We could not get ManuallyManagedState of element now, so directly check if
 // filter and text color style are applied.
-function checkFieldHighlighted(elem, expectedValue) {
-  const computedStyle = window.getComputedStyle(elem);
-  const isHighlighteApplied = computedStyle.getPropertyValue("filter") !== "none";
+async function checkFieldHighlighted(elem, expectedValue) {
+  let isHighlightApplied;
+  await SimpleTest.promiseWaitForCondition(function checkHighlight() {
+    const computedStyle = window.getComputedStyle(elem);
+    isHighlightApplied = computedStyle.getPropertyValue("filter") !== "none";
+    return isHighlightApplied === expectedValue;
+  }, `Checking #${elem.id} highlight style`);
 
-  is(isHighlighteApplied, expectedValue, `Checking #${elem.id} highlight style`);
+  is(isHighlightApplied, expectedValue, `Checking #${elem.id} highlight style`);
 }
 
-function checkFieldPreview(elem, expectedValue) {
-  const computedStyle = window.getComputedStyle(elem);
-  const isTextColorApplied = computedStyle.getPropertyValue("color") !== defaultTextColor;
+async function checkFieldPreview(elem, expectedValue) {
+  is(SpecialPowers.wrap(elem).previewValue, expectedValue, `Checking #${elem.id} previewValue`);
+  let isTextColorApplied;
+  await SimpleTest.promiseWaitForCondition(function checkPreview() {
+    const computedStyle = window.getComputedStyle(elem);
+    isTextColorApplied = computedStyle.getPropertyValue("color") !== defaultTextColor;
+    return isTextColorApplied === !!expectedValue;
+  }, `Checking #${elem.id} preview style`);
 
-  is(SpecialPowers.wrap(elem).previewValue, expectedValue, `Checking #${elem.id} previewValue`);
   is(isTextColorApplied, !!expectedValue, `Checking #${elem.id} preview style`);
 }
 
 function checkFieldValue(elem, expectedValue) {
   if (typeof elem === "string") {
     elem = document.querySelector(elem);
   }
   is(elem.value, String(expectedValue), "Checking " + elem.id + " field");
--- a/browser/extensions/formautofill/test/mochitest/test_clear_form.html
+++ b/browser/extensions/formautofill/test/mochitest/test_clear_form.html
@@ -48,24 +48,24 @@ add_task(async function setup_storage() 
   await addAddress(MOCK_ADDR_STORAGE[1]);
   await addAddress(MOCK_ADDR_STORAGE[2]);
 
   await addCreditCard(MOCK_CC_STORAGE[0]);
   await addCreditCard(MOCK_CC_STORAGE[1]);
 });
 
 
-function checkIsFormCleared(patch = {}) {
+async function checkIsFormCleared(patch = {}) {
   const form = document.getElementById("form1");
 
   for (const elem of form.elements) {
     const expectedValue = patch[elem.id] || "";
     checkFieldValue(elem, expectedValue);
-    checkFieldHighlighted(elem, false);
-    checkFieldPreview(elem, "");
+    await checkFieldHighlighted(elem, false);
+    await checkFieldPreview(elem, "");
   }
 }
 
 async function confirmClear(selector) {
   info("Await for clearing input");
   let promise = new Promise(resolve =>
     document.querySelector(selector).addEventListener("input", (event) => {
       ok(event instanceof InputEvent,
@@ -84,37 +84,37 @@ async function confirmClear(selector) {
 }
 
 add_task(async function simple_clear() {
   await triggerPopupAndHoverItem("#organization", 0);
   await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);
 
   await triggerPopupAndHoverItem("#tel", 0);
   await confirmClear("#tel");
-  checkIsFormCleared();
+  await checkIsFormCleared();
 });
 
 add_task(async function clear_adapted_record() {
   await triggerPopupAndHoverItem("#street-address", 0);
   await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);
 
   await triggerPopupAndHoverItem("#street-address", 0);
   await confirmClear("#street-address");
-  checkIsFormCleared();
+  await checkIsFormCleared();
 });
 
 add_task(async function clear_modified_form() {
   await triggerPopupAndHoverItem("#organization", 0);
   await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);
 
   await setInput("#tel", "+1111111111", true);
 
   await triggerPopupAndHoverItem("#street-address", 0);
   await confirmClear("#street-address");
-  checkIsFormCleared({tel: "+1111111111"});
+  await checkIsFormCleared({tel: "+1111111111"});
 });
 
 add_task(async function clear_distinct_section() {
   if (!(await canTestOSKeyStoreLogin())) {
     todo(false, "Cannot test OS key store login on official builds.");
     return;
   }
 
@@ -130,22 +130,22 @@ add_task(async function clear_distinct_s
   await confirmClear("#street-address");
 
   for (const [id, val] of Object.entries(MOCK_CC_STORAGE[0])) {
     const element = document.getElementById(id);
     if (!element) {
       return;
     }
     checkFieldValue(element, val);
-    checkFieldHighlighted(element, true);
+    await checkFieldHighlighted(element, true);
   }
 
   await triggerPopupAndHoverItem("#cc-name", 0);
   await confirmClear("#cc-name");
-  checkIsFormCleared();
+  await checkIsFormCleared();
 });
 
 </script>
 
 <p id="display"></p>
 
 <div id="content">
 
--- a/browser/extensions/formautofill/test/mochitest/test_formautofill_preview_highlight.html
+++ b/browser/extensions/formautofill/test/mochitest/test_formautofill_preview_highlight.html
@@ -25,66 +25,66 @@ const MOCK_STORAGE = [{
 }, {
   organization: "Mozilla",
   "street-address": "331 E. Evelyn Avenue",
 }, {
   organization: "Tel org",
   tel: "+12223334444",
 }];
 
-function checkFormFieldsStyle(profile, isPreviewing = true) {
+async function checkFormFieldsStyle(profile, isPreviewing = true) {
   const elems = document.querySelectorAll("input, select");
 
   for (const elem of elems) {
     const fillableValue = profile && profile[elem.id];
     const previewValue = isPreviewing && fillableValue || "";
 
-    checkFieldHighlighted(elem, !!fillableValue);
-    checkFieldPreview(elem, previewValue);
+    await checkFieldHighlighted(elem, !!fillableValue);
+    await checkFieldPreview(elem, previewValue);
   }
 }
 
 initPopupListener();
 
 add_task(async function setup_storage() {
   await addAddress(MOCK_STORAGE[0]);
   await addAddress(MOCK_STORAGE[1]);
   await addAddress(MOCK_STORAGE[2]);
 });
 
 add_task(async function check_preview() {
   const focusedInput = await setInput("#organization", "");
 
   synthesizeKey("KEY_ArrowDown");
   await expectPopup();
-  checkFormFieldsStyle(null);
+  await checkFormFieldsStyle(null);
 
   for (let i = 0; i < MOCK_STORAGE.length; i++) {
     synthesizeKey("KEY_ArrowDown");
     await notifySelectedIndex(i);
-    checkFormFieldsStyle(MOCK_STORAGE[i]);
+    await checkFormFieldsStyle(MOCK_STORAGE[i]);
   }
 
   // Navigate to the footer
   synthesizeKey("KEY_ArrowDown");
   await notifySelectedIndex(MOCK_STORAGE.length);
-  checkFormFieldsStyle(null);
+  await checkFormFieldsStyle(null);
 
   synthesizeKey("KEY_ArrowDown");
   await notifySelectedIndex(-1);
-  checkFormFieldsStyle(null);
+  await checkFormFieldsStyle(null);
 
   focusedInput.blur();
 });
 
 add_task(async function check_filled_highlight() {
   await triggerPopupAndHoverItem("#organization", 0);
   // filled 1st address
   await triggerAutofillAndCheckProfile(MOCK_STORAGE[0]);
-  checkFormFieldsStyle(MOCK_STORAGE[0], false);
+  await checkFormFieldsStyle(MOCK_STORAGE[0], false);
 });
 
 </script>
 
 <p id="display"></p>
 
 <div id="content">