"use strict";

const DEFAULT_TEST_DOC = `<form id="form">
  <input id="street-addr" autocomplete="street-address">
  <select id="address-level1" autocomplete="address-level1">
    <option value=""></option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AP">Armed Forces Pacific</option>

    <option value="ca">california</option>
    <option value="AR">US-Arkansas</option>
    <option value="US-CA">California</option>
    <option value="CA">California</option>
    <option value="US-AZ">US_Arizona</option>
    <option value="Ariz">Arizonac</option>
  </select>
  <input id="city" autocomplete="address-level2">
  <input id="country" autocomplete="country">
  <input id="email" autocomplete="email">
  <input id="tel" autocomplete="tel">
  <input id="cc-name" autocomplete="cc-name">
  <input id="cc-number" autocomplete="cc-number">
  <input id="cc-exp-month" autocomplete="cc-exp-month">
  <input id="cc-exp-year" autocomplete="cc-exp-year">
  <select id="cc-type">
    <option value="">Select</option>
    <option value="visa">Visa</option>
    <option value="mastercard">Master Card</option>
    <option value="amex">American Express</option>
  </select>
  <input id="submit" type="submit">
</form>`;

const TESTCASES = [
  {
    description: "Should not trigger credit card saving if number is empty",
    document: DEFAULT_TEST_DOC,
    targetElementId: "cc-name",
    formValue: {
      "#cc-name": "John Doe",
      "#cc-exp-month": 12,
      "#cc-exp-year": 2000,
    },
  },
  {
    description:
      "Should not trigger credit card saving if there is more than one cc-number field but less than four fields",
    document: `<form id="form">
                <input id="cc-type" autocomplete="cc-type">
                <input id="cc-name" autocomplete="cc-name">
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
                <input id="cc-exp-month" autocomplete="cc-exp-month">
                <input id="cc-exp-year" autocomplete="cc-exp-year">
                <input id="submit" type="submit">
              </form>
    `,
    targetElementId: "cc-name",
    formValue: {
      "#cc-name": "John Doe",
      "#cc-number1": "3714",
      "#cc-number2": "4963",
      "#cc-number3": "5398",
      "#cc-exp-month": 12,
      "#cc-exp-year": 2000,
      "#cc-type": "amex",
    },
  },
];

add_task(async function test_save_doorhanger_not_shown() {
  for (const TEST of TESTCASES) {
    info(`Test ${TEST.description}`);

    await BrowserTestUtils.withNewTab(EMPTY_URL, async function (browser) {
      await SpecialPowers.spawn(browser, [TEST.document], doc => {
        content.document.body.innerHTML = doc;
      });

      await SimpleTest.promiseFocus(browser);

      await focusUpdateSubmitForm(browser, {
        focusSelector: `#${TEST.targetElementId}`,
        newValues: TEST.formValue,
      });

      await ensureNoDoorhanger();
    });
  }
});
