Bug 1433876 - Test form submission with autofill in country without sub_keys. r=johannh
MozReview-Commit-ID: En5MlYkgXR1
--- a/browser/extensions/formautofill/FormAutofillUtils.jsm
+++ b/browser/extensions/formautofill/FormAutofillUtils.jsm
@@ -114,17 +114,17 @@ let AddressDataLoader = {
* addressData: {
* "data/US": {"lang": ["en"], ...// Data defined in libaddressinput metadata
* "alternative_names": ... // Data defined in extension }
* "data/CA": {} // Other supported country metadata
* "data/TW": {} // Other supported country metadata
* "data/TW/台北市": {} // Other supported country level 1 metadata
* }
* @param {string} country
- * @param {string} level1
+ * @param {string?} level1
* @returns {object} Default locale metadata
*/
_loadData(country, level1 = null) {
// Load the addressData if needed
if (!this._dataLoaded.country) {
this._addressData = this._loadScripts(ADDRESS_METADATA_PATH).addressData;
this._dataLoaded.country = true;
}
@@ -139,20 +139,20 @@ let AddressDataLoader = {
this._dataLoaded.level1.add(country);
}
return this._parse(this._addressData[`data/${country}/${level1}`]);
},
/**
* Return the region metadata with default locale and other locales (if exists).
* @param {string} country
- * @param {string} level1
+ * @param {string?} level1
* @returns {object} Return default locale and other locales metadata.
*/
- getData(country, level1) {
+ getData(country, level1 = null) {
let defaultLocale = this._loadData(country, level1);
if (!defaultLocale) {
return null;
}
let countryData = this._parse(this._addressData[`data/${country}`]);
let locales = [];
// TODO: Should be able to support multi-locale level 1/ level 2 metadata query
--- a/browser/extensions/formautofill/test/mochitest/mochitest.ini
+++ b/browser/extensions/formautofill/test/mochitest/mochitest.ini
@@ -2,16 +2,17 @@
prefs =
extensions.formautofill.creditCards.available=true
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_level_1_submission.html]
[test_autofocus_form.html]
[test_basic_autocomplete_form.html]
[test_basic_creditcard_autocomplete_form.html]
scheme=https
[test_clear_form.html]
scheme=https
[test_creditcard_autocomplete_off.html]
scheme=https
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/mochitest/test_address_level_1_submission.html
@@ -0,0 +1,97 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Test autofill submission for a country without address-level1</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/EventUtils.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: Test autofill submission for a country without address-level1
+
+<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";
+
+const TEST_ADDRESSES = [{
+ organization: "Mozilla",
+ "street-address": "123 Sesame Street",
+ "address-level1": "AL",
+ country: "DE",
+ timesUsed: 1,
+}];
+
+add_task(async function test_DE_is_valid_testcase() {
+ let chromeScript = SpecialPowers.loadChromeScript(function test_country_data() {
+ ChromeUtils.import("resource://formautofill/FormAutofillUtils.jsm");
+ let data = AddressDataLoader.getData("DE");
+ /* global addMessageListener */
+ addMessageListener("CheckSubKeys", () => {
+ return !data.defaultLocale.sub_keys;
+ });
+ });
+
+ SimpleTest.registerCleanupFunction(() => {
+ chromeScript.destroy();
+ });
+
+ let result = chromeScript.sendSyncMessage("CheckSubKeys");
+ ok(result[0][0], "Check that there are no sub_keys for the test country");
+});
+
+add_task(async function test_form_will_submit_without_sub_keys() {
+ await SpecialPowers.pushPrefEnv({
+ set: [
+ // This needs to match the country in the previous test and must have no sub_keys.
+ ["browser.search.region", "DE"],
+ // We already verified the first time use case in browser test
+ ["extensions.formautofill.firstTimeUse", false],
+ ],
+ });
+
+ // Click a field to get the form handler created
+ await focusAndWaitForFieldsIdentified("input[autocomplete='organization']");
+
+ let loadPromise = new Promise(resolve => {
+ /* eslint-disable-next-line mozilla/balanced-listeners */
+ document.getElementById("submit_frame").addEventListener("load", resolve);
+ });
+
+ clickOnElement("input[type=submit]");
+
+ await onStorageChanged("add");
+ // Check if timesUsed is set correctly
+ let matching = await checkAddresses(TEST_ADDRESSES);
+ ok(matching, "Address saved as expected");
+
+ await loadPromise;
+ isnot(window.submit_frame.location.href, "about:blank", "Check form submitted");
+});
+
+</script>
+
+<div>
+ <!-- Submit to the frame so that the test doesn't get replaced. We don't return
+ -- false in onsubmit since we're testing the submission succeeds. -->
+ <iframe id="submit_frame" name="submit_frame"></iframe>
+ <form action="/" target="submit_frame" method="POST">
+ <p><label>organization: <input autocomplete="organization" value="Mozilla"></label></p>
+ <p><label>streetAddress: <input autocomplete="street-address" value="123 Sesame Street"></label></p>
+ <p><label>address-level1: <select autocomplete="address-level1">
+ <option selected>AL</option>
+ <option>AK</option>
+ </select></label></p>
+ <p><label>country: <input autocomplete="country" value="DE"></label></p>
+ <p><input type="submit"></p>
+ </form>
+
+</div>
+</body>
+</html>