summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/mochitest/test_address_level_1_submission.html
blob: 9d6ad1f7ea5a6e2baf5d88216471d0e89af2a0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autofill submission for a country without address-level1</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.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 ../../../../../toolkit/components/satchel/test/satchel_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() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.formautofill.addresses.capture.enabled", true],
      ["extensions.formautofill.addresses.supportedCountries", "US,CA,DE"],
      ["extensions.formautofill.creditCards.supportedCountries", "US,CA,DE"],
    ],
  });
  let chromeScript = SpecialPowers.loadChromeScript(function test_country_data() {
    /* eslint-env mozilla/chrome-script */
    const {AddressDataLoader} = ChromeUtils.importESModule("resource://gre/modules/shared/FormAutofillUtils.sys.mjs");
    let data = AddressDataLoader.getData("DE");
    addMessageListener("CheckSubKeys", () => {
      return !data.defaultLocale.sub_keys;
    });
  });

  SimpleTest.registerCleanupFunction(() => {
    chromeScript.destroy();
  });

  let result = await chromeScript.sendQuery("CheckSubKeys");
  ok(result, "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],
      ["extensions.formautofill.addresses.capture.enabled", true],
      ["extensions.formautofill.addresses.supportedCountries", "US,CA,DE"],
      ["extensions.formautofill.addresses.supported", "detect"]
    ],
  });
  // 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>