summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_test_changeContentInputValue.js
blob: d0016708b23e0521b0f112c4eb6d732b661074b4 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * Tests head.js#changeContentInputValue.
 */

"use strict";

// The origin for the test URIs.
const TEST_ORIGIN = "https://example.com";
const BASIC_FORM_PAGE_PATH = DIRECTORY_PATH + "form_basic.html";
const USERNAME_INPUT_SELECTOR = "#form-basic-username";

let testCases = [
  {
    name: "blank string should clear input value",
    originalValue: "start text",
    inputEvent: "",
    expectedKeypresses: ["Backspace"],
  },
  {
    name: "input value that adds to original string should only add the difference",
    originalValue: "start text",
    inputEvent: "start text!!!",
    expectedKeypresses: ["!", "!", "!"],
  },
  {
    name: "input value that is a subset of original string should only delete the difference",
    originalValue: "start text",
    inputEvent: "start",
    expectedKeypresses: ["Backspace"],
  },
  {
    name: "input value that is unrelated to the original string should replace it",
    originalValue: "start text",
    inputEvent: "wut?",
    expectedKeypresses: ["w", "u", "t", "?"],
  },
];

for (let testData of testCases) {
  let tmp = {
    async [testData.name]() {
      await testStringChange(testData);
    },
  };
  add_task(tmp[testData.name]);
}

async function testStringChange({
  name,
  originalValue,
  inputEvent,
  expectedKeypresses,
}) {
  info("Starting test " + name);
  await LoginTestUtils.clearData();

  await LoginTestUtils.addLogin({
    username: originalValue,
    password: "password",
  });

  let formProcessedPromise = listenForTestNotification("FormProcessed");
  let url = TEST_ORIGIN + BASIC_FORM_PAGE_PATH;
  info("Opening tab with url: " + url);

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url,
    },
    async function (browser) {
      info(`Opened tab with url: ${url}, waiting for focus`);
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      info("Waiting for form-processed message");
      await formProcessedPromise;
      await checkForm(browser, originalValue);
      info("form checked");

      await ContentTask.spawn(
        browser,
        { USERNAME_INPUT_SELECTOR, expectedKeypresses },
        async function ({ USERNAME_INPUT_SELECTOR, expectedKeypresses }) {
          let input = content.document.querySelector(USERNAME_INPUT_SELECTOR);

          let verifyKeyListener = event => {
            Assert.equal(
              expectedKeypresses[0],
              event.key,
              "Key press matches expected value"
            );
            expectedKeypresses.shift();

            if (!expectedKeypresses.length) {
              input.removeEventListner("keydown", verifyKeyListener);
              input.addEventListener("keydown", () => {
                throw new Error("Unexpected keypress encountered");
              });
            }
          };

          input.addEventListener("keydown", verifyKeyListener);
        }
      );

      changeContentInputValue(browser, USERNAME_INPUT_SELECTOR, inputEvent);
    }
  );
}

async function checkForm(browser, expectedUsername) {
  await ContentTask.spawn(
    browser,
    {
      expectedUsername,
      USERNAME_INPUT_SELECTOR,
    },
    async function contentCheckForm({
      expectedUsername,
      USERNAME_INPUT_SELECTOR,
    }) {
      let field = content.document.querySelector(USERNAME_INPUT_SELECTOR);
      Assert.equal(
        field.value,
        expectedUsername,
        `Username field has teh expected initial value '${expectedUsername}'`
      );
    }
  );
}