Bug 1303510 - 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 591557 5d70396cdb999caba6532d2bfcd236278c52e415
parent 591556 035be0582f32bdd0190a4d705bb89cc2442e9ca0
child 632542 ae9850dcbd93faaa3d141289969dd6ffed62e092
push id63085
push userbmo:schung@mozilla.com
push dateFri, 09 Jun 2017 06:50:48 +0000
reviewersMattN
bugs1303510
milestone55.0a1
Bug 1303510 - 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
@@ -7,58 +7,89 @@
 let formFillChromeScript;
 
 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();
+}
+
+async function onAddressChanged(type) {
+  return new Promise(resolve => {
+    formFillChromeScript.addMessageListener("formautofill-storage-changed", function onChanged(data) {
+      formFillChromeScript.removeMessageListener("formautofill-storage-changed", onChanged);
+      is(data.data, type, `Receive ${type} 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);
   }
 }
 
-function addAddress(address) {
+async function addAddress(address) {
   return new Promise(resolve => {
     formFillChromeScript.sendAsyncMessage("FormAutofillTest:AddAddress", {address});
     formFillChromeScript.addMessageListener("FormAutofillTest:AddressAdded", function onAdded(data) {
       formFillChromeScript.removeMessageListener("FormAutofillTest:AddressAdded", onAdded);
 
       resolve();
     });
   });
 }
 
-function removeAddress(guid) {
+async function removeAddress(guid) {
   return new Promise(resolve => {
     formFillChromeScript.sendAsyncMessage("FormAutofillTest:RemoveAddress", {guid});
     formFillChromeScript.addMessageListener("FormAutofillTest:AddressRemoved", function onDeleted(data) {
       formFillChromeScript.removeMessageListener("FormAutofillTest:AddressRemoved", onDeleted);
 
       resolve();
     });
   });
 }
 
-function updateAddress(guid, address) {
+async function updateAddress(guid, address) {
   return new Promise(resolve => {
     formFillChromeScript.sendAsyncMessage("FormAutofillTest:UpdateAddress", {address, guid});
     formFillChromeScript.addMessageListener("FormAutofillTest:AddressUpdated", function onUpdated(data) {
       formFillChromeScript.removeMessageListener("FormAutofillTest:AddressUpdated", onUpdated);
 
       resolve();
     });
   });
 }
 
+async function checkAddresses(expectedAddresses) {
+  return new Promise(resolve => {
+    formFillChromeScript.sendAsyncMessage("FormAutofillTest:CheckAddresses", {expectedAddresses});
+    formFillChromeScript.addMessageListener("FormAutofillTest:areAddressesMatching", function onChecked(data) {
+      formFillChromeScript.removeMessageListener("FormAutofillTest:areAddressesMatching", onChecked);
+
+      resolve(data);
+    });
+  });
+}
+
 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
@@ -1,16 +1,17 @@
 // assert is available to chrome scripts loaded via SpecialPowers.loadChromeScript.
 /* global assert */
 
 "use strict";
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
+let {profileStorage} = Cu.import("resource://formautofill/ProfileStorage.jsm", {});
 
 var ParentUtils = {
   cleanUpAddress() {
     Services.cpmm.addMessageListener("FormAutofill:Addresses", function getResult(result) {
       Services.cpmm.removeMessageListener("FormAutofill:Addresses", getResult);
 
       let addresses = result.data;
       Services.cpmm.sendAsyncMessage("FormAutofill:RemoveAddresses",
@@ -36,16 +37,51 @@ var ParentUtils = {
     assert.ok(topic === "formautofill-storage-changed");
     sendAsyncMessage("formautofill-storage-changed", {subject: null, topic, data});
   },
 
   cleanup() {
     Services.obs.removeObserver(this, "formautofill-storage-changed");
     this.cleanUpAddress();
   },
+
+  areAddressesMatching(addressA, addressB) {
+    for (let field of profileStorage.addresses.VALID_FIELDS) {
+      if (addressA[field] !== addressB[field]) {
+        return false;
+      }
+    }
+    return true;
+  },
+
+  checkAddresses({expectedAddresses}) {
+    Services.cpmm.addMessageListener("FormAutofill:Addresses", function getResult(result) {
+      Services.cpmm.removeMessageListener("FormAutofill:Addresses", getResult);
+      let addresses = result.data;
+      if (addresses.length !== expectedAddresses.length) {
+        sendAsyncMessage("FormAutofillTest:areAddressesMatching", false);
+        return;
+      }
+
+      for (let address of addresses) {
+        let matching = expectedAddresses.some((expectedAddress) => {
+          return ParentUtils.areAddressesMatching(address, expectedAddress);
+        });
+
+        if (!matching) {
+          sendAsyncMessage("FormAutofillTest:areAddressesMatching", false);
+          return;
+        }
+      }
+
+      sendAsyncMessage("FormAutofillTest:areAddressesMatching", true);
+    });
+
+    Services.cpmm.sendAsyncMessage("FormAutofill:GetAddresses", {searchString: ""});
+  },
 };
 
 ParentUtils.cleanUpAddress();
 Services.obs.addObserver(ParentUtils, "formautofill-storage-changed");
 
 addMessageListener("FormAutofillTest:AddAddress", (msg) => {
   ParentUtils.updateAddress("add", "FormAutofill:SaveAddress", msg, "FormAutofillTest:AddressAdded");
 });
@@ -53,11 +89,15 @@ 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:CheckAddresses", (msg) => {
+  ParentUtils.checkAddresses(msg);
+});
+
 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,61 @@
+<!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]);
+  }
+
+  clickOnElement("input[type=submit]");
+
+  let expectedAddresses = TEST_ADDRESSES.slice(0, 1);
+  await onAddressChanged("add");
+  let matching = await checkAddresses(expectedAddresses);
+  ok(matching, "Address saved as expected");
+});
+
+</script>
+
+<div>
+
+  <form 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>
+  </form>
+
+</div>
+</body>
+</html>