summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_isInferredLoginForm.js
blob: 01bc3ff8169c57ea478d3ba18568ffc3410b8a62 (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
/**
 * Test for LoginHelper.isInferredLoginForm.
 */

"use strict";

const attributeTestData = [
  {
    testValues: ["", "form", "search", "signup", "sign-up", "sign/up"],
    expectation: false,
  },
  {
    testValues: [
      "Login",
      "Log in",
      "Log on",
      "Log-on",
      "Sign in",
      "Sigin",
      "Sign/in",
      "Sign-in",
      "Sign on",
      "Sign-on",
      "loginForm",
      "form-sign-in",
    ],
    expectation: true,
  },
];

const classNameTestData = [
  {
    testValues: [
      "",
      "inputTxt form-control",
      "user-input form-name",
      "text name mail",
      "form signup",
    ],
    expectation: false,
  },
  {
    testValues: ["login form"],
    expectation: true,
  },
];

const TESTCASES = [
  {
    description: "Test id attribute",
    update: (doc, v) => {
      doc.querySelector("form").setAttribute("id", v);
    },
    subtests: attributeTestData,
  },
  {
    description: "Test name attribute",
    update: (doc, v) => {
      doc.querySelector("form").setAttribute("name", v);
    },
    subtests: attributeTestData,
  },
  {
    description: "Test class attribute",
    update: (doc, v) => {
      doc.querySelector("form").setAttribute("class", v);
    },
    subtests: [...attributeTestData, ...classNameTestData],
  },
];

for (let testcase of TESTCASES) {
  info("Sanity checking the testcase: " + testcase.description);

  (function () {
    add_task(async function () {
      info("Starting testcase: " + testcase.description);

      for (let subtest of testcase.subtests) {
        const document = MockDocument.createTestDocument(
          "http://localhost:8080/test/",
          `<form id="id" name="name"></form>`
        );

        for (let value of subtest.testValues) {
          testcase.update(document, value);
          const ele = document.querySelector("form");
          const ret = LoginHelper.isInferredLoginForm(ele);
          Assert.strictEqual(
            ret,
            subtest.expectation,
            `${testcase.description}, isInferredLoginForm doesn't return correct result while setting the value to ${value}`
          );
        }
      }
    });
  })();
}