Bug 990219 - Part 3: Add mochitest-plain for saving submitted form. r?MattN draft
authorsteveck-chung <schung@mozilla.com>
Wed, 10 May 2017 17:56:17 +0800
changeset 579400 79e64dc6a45bf05867007415406d2993d932b5a3
parent 579399 891ad801238ee0171cda57e6ed6e04ecfe5335c4
child 579401 aa8522840bb27b254fbfdb70dc750dd7874c96a8
push id59240
push userbmo:schung@mozilla.com
push dateWed, 17 May 2017 09:30:27 +0000
reviewersMattN
bugs990219
milestone55.0a1
Bug 990219 - Part 3: Add mochitest-plain for saving submitted form. r?MattN MozReview-Commit-ID: XJZWEBkCrx
browser/extensions/formautofill/test/mochitest/formautofill_common.js
browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js
browser/extensions/formautofill/test/mochitest/mochitest.ini
browser/extensions/formautofill/test/mochitest/test_address_submission.html
--- a/browser/extensions/formautofill/test/mochitest/formautofill_common.js
+++ b/browser/extensions/formautofill/test/mochitest/formautofill_common.js
@@ -1,22 +1,56 @@
 /* import-globals-from ../../../../../testing/mochitest/tests/SimpleTest/SimpleTest.js */
 /* import-globals-from ../../../../../toolkit/components/satchel/test/satchel_common.js */
 /* eslint-disable no-unused-vars */
 
 "use strict";
 
 let formFillChromeScript;
 
+const VALID_ADDRESS_FIELDS = [
+  "given-name",
+  "additional-name",
+  "family-name",
+  "organization",
+  "street-address",
+  "address-level2",
+  "address-level1",
+  "postal-code",
+  "country",
+  "tel",
+  "email",
+];
+
 function setInput(selector, value) {
   let input = document.querySelector("input" + selector);
   input.value = value;
   input.focus();
 }
 
+function clickOnElement(selector) {
+  let element = document.querySelector(selector);
+
+  if (!element) {
+    throw new Error("Can not find the element");
+  }
+
+  element.click();
+}
+
+function onAddressChanged() {
+  return new Promise(resolve => {
+    formFillChromeScript.addMessageListener("formautofill-storage-changed", function onChanged(data) {
+      formFillChromeScript.removeMessageListener("formautofill-storage-changed", onChanged);
+      is(data.data, "add", "Receive add storage changed event");
+      resolve();
+    });
+  });
+}
+
 function checkMenuEntries(expectedValues) {
   let actualValues = getMenuEntries();
 
   is(actualValues.length, expectedValues.length, " Checking length of expected menu");
   for (let i = 0; i < expectedValues.length; i++) {
     is(actualValues[i], expectedValues[i], " Checking menu entry #" + i);
   }
 }
@@ -49,16 +83,47 @@ function updateAddress(guid, address) {
     formFillChromeScript.addMessageListener("FormAutofillTest:AddressUpdated", function onUpdated(data) {
       formFillChromeScript.removeMessageListener("FormAutofillTest:AddressUpdated", onUpdated);
 
       resolve();
     });
   });
 }
 
+function getAddresses() {
+  return new Promise(resolve => {
+    formFillChromeScript.sendAsyncMessage("FormAutofillTest:GetAddresses");
+    formFillChromeScript.addMessageListener("FormAutofillTest:Addresses", function onUpdated(data) {
+      formFillChromeScript.removeMessageListener("FormAutofillTest:Addresses", onUpdated);
+
+      resolve(data);
+    });
+  });
+}
+
+function areAddressesMatching(addressA, addressB) {
+  for (let field of VALID_ADDRESS_FIELDS) {
+    if (addressA[field] !== addressB[field]) {
+      return false;
+    }
+  }
+  return true;
+}
+
+function checkAddresses(expectedAddresses) {
+  return getAddresses().then((addresses => {
+    is(addresses.length, expectedAddresses.length, "Number of address are matching");
+    for (let address of addresses) {
+      for (let expectedAddress of expectedAddresses) {
+        ok(areAddressesMatching(address, expectedAddress), "2 Addresses' content are matching");
+      }
+    }
+  }));
+}
+
 function formAutoFillCommonSetup() {
   let chromeURL = SimpleTest.getTestFileURL("formautofill_parent_utils.js");
   formFillChromeScript = SpecialPowers.loadChromeScript(chromeURL);
   formFillChromeScript.addMessageListener("onpopupshown", ({results}) => {
     gLastAutoCompleteResults = results;
     if (gPopupShownListener) {
       gPopupShownListener({results});
     }
--- a/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js
+++ b/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js
@@ -53,11 +53,20 @@ addMessageListener("FormAutofillTest:Add
 addMessageListener("FormAutofillTest:RemoveAddress", (msg) => {
   ParentUtils.updateAddress("remove", "FormAutofill:RemoveAddress", msg, "FormAutofillTest:AddressRemoved");
 });
 
 addMessageListener("FormAutofillTest:UpdateAddress", (msg) => {
   ParentUtils.updateAddress("update", "FormAutofill:SaveAddress", msg, "FormAutofillTest:AddressUpdated");
 });
 
+addMessageListener("FormAutofillTest:GetAddresses", () => {
+  Services.cpmm.addMessageListener("FormAutofill:Addresses", function getResult(result) {
+    Services.cpmm.removeMessageListener("FormAutofill:Addresses", getResult);
+    sendAsyncMessage("FormAutofillTest:Addresses", result.data);
+  });
+
+  Services.cpmm.sendAsyncMessage("FormAutofill:GetAddresses", {});
+});
+
 addMessageListener("cleanup", () => {
   ParentUtils.cleanup();
 });
--- a/browser/extensions/formautofill/test/mochitest/mochitest.ini
+++ b/browser/extensions/formautofill/test/mochitest/mochitest.ini
@@ -1,9 +1,9 @@
 [DEFAULT]
 support-files =
   ../../../../../toolkit/components/satchel/test/satchel_common.js
   ../../../../../toolkit/components/satchel/test/parent_utils.js
   formautofill_common.js
   formautofill_parent_utils.js
 
+[test_address_submission.html]
 [test_basic_autocomplete_form.html]
-
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/mochitest/test_address_submission.html
@@ -0,0 +1,67 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test autofill submit</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="formautofill_common.js"></script>
+  <script type="text/javascript" src="satchel_common.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+Form autofill test: check if address is saved/updated correctly
+
+<script>
+/* import-globals-from ../../../../../testing/mochitest/tests/SimpleTest/SpawnTask.js */
+/* import-globals-from ../../../../../toolkit/components/satchel/test/satchel_common.js */
+/* import-globals-from formautofill_common.js */
+
+"use strict";
+
+let TEST_ADDRESSES = [{
+  organization: "Sesame Street",
+  "street-address": "123 Sesame Street.",
+  tel: "1-345-345-3456",
+}, {
+  organization: "Mozilla",
+  "street-address": "331 E. Evelyn Avenue",
+  tel: "1-650-903-0800",
+}];
+
+// Autofill the address from dropdown menu.
+add_task(async function check_storage_after_form_submitted() {
+  for (let key in TEST_ADDRESSES[0]) {
+    setInput("#" + key, TEST_ADDRESSES[0][key]);
+  }
+
+  setTimeout(() => {
+    clickOnElement("input[type=submit]");
+  }, 0);
+
+  let expectedAddresses = TEST_ADDRESSES.slice(0, 1);
+  await onAddressChanged();
+  await checkAddresses(expectedAddresses);
+});
+
+</script>
+
+<p id="display"></p>
+
+<div id="content">
+
+  <form id="form1" onsubmit="return false">
+    <p>This is a basic form for submitting test.</p>
+    <p><label>organization: <input id="organization" name="organization" autocomplete="organization" type="text"></label></p>
+    <p><label>streetAddress: <input id="street-address" name="street-address" autocomplete="street-address" type="text"></label></p>
+    <p><label>tel: <input id="tel" name="tel" autocomplete="tel" type="text"></label></p>
+    <p><label>country: <input id="country" name="country" autocomplete="country" type="text"></label></p>
+    <p><input type="submit"></p>
+    <p><button type="reset">Reset</button></p>
+  </form>
+
+</div>
+
+<pre id="test"></pre>
+</body>
+</html>