summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_DOMInputPasswordAdded.js
blob: 11ca2ac1cde2972baa440769ee05b1310cacfe50 (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
const consts = {
  HTML_NS: "http://www.w3.org/1999/xhtml",

  INPUT_ID: "input1",
  FORM1_ID: "form1",
  FORM2_ID: "form2",
  CHANGE_INPUT_ID: "input2",
  BODY_INPUT_ID: "input3",
};

function task(contentConsts) {
  let resolve;
  let promise = new Promise(r => {
    resolve = r;
  });

  function unexpectedContentEvent(evt) {
    Assert.ok(false, "Received a " + evt.type + " event on content");
  }

  var gDoc = null;

  addEventListener("load", tabLoad, true);

  function tabLoad() {
    removeEventListener("load", tabLoad, true);
    gDoc = content.document;
    // These events shouldn't escape to content.
    gDoc.addEventListener("DOMInputPasswordAdded", unexpectedContentEvent);
    gDoc.defaultView.setTimeout(test_inputAddOutsideForm, 0);
  }

  function test_inputAddOutsideForm() {
    addEventListener(
      "DOMInputPasswordAdded",
      test_inputAddOutsideFormHandler,
      false
    );
    let input = gDoc.createElementNS(contentConsts.HTML_NS, "input");
    input.setAttribute("type", "password");
    input.setAttribute("id", contentConsts.BODY_INPUT_ID);
    input.setAttribute("data-test", "unique-attribute");
    gDoc.body.appendChild(input);
    info("Done appending the input element to the body");
  }

  function test_inputAddOutsideFormHandler(evt) {
    removeEventListener(evt.type, test_inputAddOutsideFormHandler, false);
    Assert.equal(
      evt.target.id,
      contentConsts.BODY_INPUT_ID,
      evt.type +
        " event targets correct input element (added password element outside form)"
    );
    gDoc.defaultView.setTimeout(test_inputChangesType, 0);
  }

  function test_inputChangesType() {
    addEventListener(
      "DOMInputPasswordAdded",
      test_inputChangesTypeHandler,
      false
    );
    let input = gDoc.getElementById(contentConsts.CHANGE_INPUT_ID);
    input.setAttribute("type", "password");
  }

  function test_inputChangesTypeHandler(evt) {
    removeEventListener(evt.type, test_inputChangesTypeHandler, false);
    Assert.equal(
      evt.target.id,
      contentConsts.CHANGE_INPUT_ID,
      evt.type + " event targets correct input element (changed type)"
    );
    gDoc.defaultView.setTimeout(completeTest, 0);
  }

  function completeTest() {
    Assert.ok(true, "Test completed");
    gDoc.removeEventListener("DOMInputPasswordAdded", unexpectedContentEvent);
    resolve();
  }

  return promise;
}

add_task(async function () {
  let tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser));
  let promise = ContentTask.spawn(tab.linkedBrowser, consts, task);
  BrowserTestUtils.loadURIString(
    tab.linkedBrowser,
    `data:text/html;charset=utf-8,
      <html><body>
        <input id="${consts.CHANGE_INPUT_ID}" />
      </body>
      </html>
    `
  );
  await promise;
  gBrowser.removeCurrentTab();
});