tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_getInfo.js (11781B)


      1 "use strict";
      2 
      3 var { FormAutofillHeuristics } = ChromeUtils.importESModule(
      4  "resource://gre/modules/shared/FormAutofillHeuristics.sys.mjs"
      5 );
      6 var { LabelUtils } = ChromeUtils.importESModule(
      7  "resource://gre/modules/shared/LabelUtils.sys.mjs"
      8 );
      9 var { FormAutofill } = ChromeUtils.importESModule(
     10  "resource://autofill/FormAutofill.sys.mjs"
     11 );
     12 
     13 const TESTCASES = [
     14  {
     15    description: "Input element in a label element",
     16    document: `<form>
     17                 <label> E-Mail
     18                   <input id="targetElement" type="text">
     19                 </label>
     20               </form>`,
     21    elementId: "targetElement",
     22    expectedReturnValue: ["email", {}],
     23  },
     24  {
     25    description:
     26      "A label element is out of the form contains the related input",
     27    document: `<label for="targetElement"> E-Mail</label>
     28               <form>
     29                 <input id="targetElement" type="text">
     30               </form>`,
     31    elementId: "targetElement",
     32    expectedReturnValue: ["email", {}],
     33  },
     34  {
     35    description: "A label element contains span element",
     36    document: `<label for="targetElement">FOO<span>E-Mail</span>BAR</label>
     37               <form>
     38                 <input id="targetElement" type="text">
     39               </form>`,
     40    elementId: "targetElement",
     41    expectedReturnValue: ["email", {}],
     42  },
     43  {
     44    description: "The signature in 'name' attr of an input",
     45    document: `<input id="targetElement" name="email" type="text">`,
     46    elementId: "targetElement",
     47    expectedReturnValue: ["email", {}],
     48  },
     49  {
     50    description: "The signature in 'id' attr of an input",
     51    document: `<input id="targetElement_email" name="tel" type="text">`,
     52    elementId: "targetElement_email",
     53    expectedReturnValue: ["email", {}],
     54  },
     55  {
     56    description: "Select element in a label element",
     57    document: `<form>
     58                 <label> State
     59                   <select id="targetElement"></select>
     60                 </label>
     61               </form>`,
     62    elementId: "targetElement",
     63    expectedReturnValue: ["address-level1", {}],
     64  },
     65  {
     66    description: "A select element without a form wrapped",
     67    document: `<label for="targetElement">State</label>
     68               <select id="targetElement"></select>`,
     69    elementId: "targetElement",
     70    expectedReturnValue: ["address-level1", {}],
     71  },
     72  {
     73    description: "address line input",
     74    document: `<label for="targetElement">street</label>
     75               <input id="targetElement" type="text">`,
     76    elementId: "targetElement",
     77    expectedReturnValue: ["street-address", {}],
     78  },
     79  {
     80    description: "CJK character - Traditional Chinese",
     81    document: `<label> 郵遞區號
     82                 <input id="targetElement" />
     83               </label>`,
     84    elementId: "targetElement",
     85    expectedReturnValue: ["postal-code", {}],
     86  },
     87  {
     88    description: "CJK character - Japanese",
     89    document: `<label> 郵便番号
     90                 <input id="targetElement" />
     91               </label>`,
     92    elementId: "targetElement",
     93    expectedReturnValue: ["postal-code", {}],
     94  },
     95  {
     96    description: "CJK character - Korean",
     97    document: `<label> 우편 번호
     98                 <input id="targetElement" />
     99               </label>`,
    100    elementId: "targetElement",
    101    expectedReturnValue: ["postal-code", {}],
    102  },
    103  {
    104    description: "",
    105    document: `<input id="targetElement" name="fullname">`,
    106    elementId: "targetElement",
    107    expectedReturnValue: ["name", {}],
    108  },
    109  {
    110    description: 'input element with "submit" type',
    111    document: `<input id="targetElement" type="submit" />`,
    112    elementId: "targetElement",
    113    expectedReturnValue: [[], {}],
    114  },
    115  {
    116    description: "The signature in 'name' attr of an email input",
    117    document: `<input id="targetElement" name="email" type="number">`,
    118    elementId: "targetElement",
    119    expectedReturnValue: ["email", {}],
    120  },
    121  {
    122    description: 'input element with "email" type',
    123    document: `<input id="targetElement" type="email" />`,
    124    elementId: "targetElement",
    125    expectedReturnValue: ["email", {}],
    126  },
    127  {
    128    description: "Exclude United State string",
    129    document: `<label>United State
    130                 <input id="targetElement" />
    131               </label>`,
    132    elementId: "targetElement",
    133    expectedReturnValue: [[], {}],
    134  },
    135  {
    136    description: '"County" field with "United State" string',
    137    document: `<label>United State County
    138                 <input id="targetElement" />
    139               </label>`,
    140    elementId: "targetElement",
    141    expectedReturnValue: ["address-level1", {}],
    142  },
    143  {
    144    description: '"city" field with double "United State" string',
    145    document: `<label>United State united sTATE city
    146                 <input id="targetElement" />
    147               </label>`,
    148    elementId: "targetElement",
    149    expectedReturnValue: ["address-level2", {}],
    150  },
    151  {
    152    description: "Verify credit card number",
    153    document: `<form>
    154                 <label for="targetElement"> Card Number</label>
    155                 <input id="targetElement" type="text">
    156               </form>`,
    157    elementId: "targetElement",
    158    expectedReturnValue: ["cc-number", { fathomConfidence: 1 }],
    159  },
    160  {
    161    description: "Identify credit card type field",
    162    document: `<form>
    163                 <label for="targetElement">Card Type</label>
    164                 <input id="targetElement" type="text">
    165               </form>`,
    166    elementId: "targetElement",
    167    expectedReturnValue: ["cc-type", {}],
    168  },
    169  {
    170    description: `Identify address field when contained in a form with autocomplete="off"`,
    171    document: `<form autocomplete="off">
    172                <input id="given-name">
    173               </form>`,
    174    elementId: "given-name",
    175    expectedReturnValue: ["given-name", {}],
    176  },
    177  {
    178    description: `Identify address field that has a placeholder but no label associated with it`,
    179    document: `<form>
    180                <input id="targetElement" placeholder="Name">
    181               </form>`,
    182    elementId: "targetElement",
    183    expectedReturnValue: ["name", {}],
    184  },
    185  {
    186    description: `Identify address field that has a placeholder, no associated label, and its autocomplete attribute is "off"`,
    187    document: `<form>
    188                <input id="targetElement" placeholder="Address" autocomplete="off">
    189               </form>`,
    190    elementId: "targetElement",
    191    expectedReturnValue: ["street-address", {}],
    192  },
    193  {
    194    description: `Identify address field that has a placeholder, no associated label, and the form's autocomplete attribute is "off"`,
    195    document: `<form autocomplete="off">
    196                <input id="targetElement" placeholder="Country">
    197               </form>`,
    198    elementId: "targetElement",
    199    expectedReturnValue: ["country", {}],
    200  },
    201 ];
    202 
    203 add_setup(async function () {
    204  Services.prefs.setStringPref(
    205    "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
    206    "1"
    207  );
    208 
    209  registerCleanupFunction(() => {
    210    Services.prefs.clearUserPref(
    211      "extensions.formautofill.creditCards.heuristics.fathom.testConfidence"
    212    );
    213  });
    214 });
    215 
    216 TESTCASES.forEach(testcase => {
    217  add_task(async function () {
    218    info("Starting testcase: " + testcase.description);
    219 
    220    let doc = MockDocument.createTestDocument(
    221      "http://localhost:8080/test/",
    222      testcase.document
    223    );
    224 
    225    let element = doc.getElementById(testcase.elementId);
    226    let value = FormAutofillHeuristics.inferFieldInfo(element);
    227 
    228    Assert.deepEqual(value, testcase.expectedReturnValue);
    229    LabelUtils.clearLabelMap();
    230  });
    231 });
    232 
    233 add_task(async function test_regexp_list() {
    234  info("Verify the fieldName support for select element.");
    235  let SUPPORT_LIST = {
    236    email: null, // email
    237    "tel-extension": null, // tel-extension
    238    phone: null, // tel
    239    organization: null, // organization
    240    "street-address": null, // street-address
    241    address1: null, // address-line1
    242    address2: null, // address-line2
    243    address3: null, // address-line3
    244    city: "address-level2",
    245    region: "address-level1",
    246    "postal-code": null, // postal-code
    247    country: "country",
    248    fullname: null, // name
    249    fname: null, // given-name
    250    mname: null, // additional-name
    251    lname: null, // family-name
    252    cardholder: null, // cc-name
    253    "cc-number": null, // cc-number
    254    addmonth: "cc-exp-month",
    255    addyear: "cc-exp-year",
    256  };
    257  for (let label of Object.keys(SUPPORT_LIST)) {
    258    let testcase = {
    259      description: `A select element supports ${label} or not`,
    260      document: `<select id="${label}"></select>`,
    261      elementId: label,
    262      expectedReturnValue: SUPPORT_LIST[label]
    263        ? [SUPPORT_LIST[label], {}]
    264        : [[], {}],
    265    };
    266    info(testcase.description);
    267    info(testcase.document);
    268    let doc = MockDocument.createTestDocument(
    269      "http://localhost:8080/test/",
    270      testcase.document
    271    );
    272 
    273    let element = doc.getElementById(testcase.elementId);
    274    let value = FormAutofillHeuristics.inferFieldInfo(element);
    275 
    276    Assert.deepEqual(value, testcase.expectedReturnValue, label);
    277  }
    278  LabelUtils.clearLabelMap();
    279 });
    280 
    281 add_task(async function test_autofill_creditCards_autocomplete_off_pref() {
    282  let document = `<form autocomplete="off">
    283                    <label for="targetElement"> Card Number</label>
    284                    <input id="targetElement" type="text">
    285                  </form>`;
    286  let expected = [[], {}];
    287  info(`Set pref so that credit card autofill respects autocomplete="off"`);
    288  Services.prefs.setBoolPref(
    289    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
    290    false
    291  );
    292  let doc = MockDocument.createTestDocument(
    293    "http://localhost:8080/test/",
    294    document
    295  );
    296  let element = doc.getElementById("targetElement");
    297  let value = FormAutofillHeuristics.inferFieldInfo(element);
    298 
    299  Assert.deepEqual(value, expected);
    300  document = `<form>
    301                <label for="targetElement"> Card Number</label>
    302                <input id="targetElement" type="text">
    303              </form>`;
    304  expected = ["cc-number", { fathomConfidence: 1 }];
    305  info(
    306    `Set pref so that credit card autofill does not respect autocomplete="off"`
    307  );
    308  Services.prefs.setBoolPref(
    309    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
    310    true
    311  );
    312  doc = MockDocument.createTestDocument(
    313    "http://localhost:8080/test/",
    314    document
    315  );
    316  element = doc.getElementById("targetElement");
    317  value = FormAutofillHeuristics.inferFieldInfo(element);
    318 
    319  Assert.deepEqual(value, expected);
    320  Services.prefs.clearUserPref(
    321    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF
    322  );
    323 });
    324 
    325 add_task(async function test_autofill_addresses_autocomplete_off_pref() {
    326  let document = `<form autocomplete="off">
    327                    <input id="given-name">
    328                  </form>`;
    329  let expected = [[], {}];
    330  info(`Set pref so that address autofill respects autocomplete="off"`);
    331  Services.prefs.setBoolPref(
    332    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,
    333    false
    334  );
    335  let doc = MockDocument.createTestDocument(
    336    "http://localhost:8080/test/",
    337    document
    338  );
    339  let element = doc.getElementById("given-name");
    340  let value = FormAutofillHeuristics.inferFieldInfo(element);
    341 
    342  Assert.deepEqual(value, expected);
    343  document = `<form>
    344                <input id="given-name">
    345              </form>`;
    346  expected = ["given-name", {}];
    347  info(`Set pref so that address autofill does not respect autocomplete="off"`);
    348  Services.prefs.setBoolPref(
    349    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,
    350    true
    351  );
    352  doc = MockDocument.createTestDocument(
    353    "http://localhost:8080/test/",
    354    document
    355  );
    356  element = doc.getElementById("given-name");
    357  value = FormAutofillHeuristics.inferFieldInfo(element);
    358 
    359  Assert.deepEqual(value, expected);
    360  Services.prefs.clearUserPref(
    361    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF
    362  );
    363 });