summaryrefslogtreecommitdiffstats
path: root/dom/events/test/browser_beforeinput_by_execCommand_in_contentscript.js
blob: d5e4ef1bbf2b5ea0d9308263c395a4bdcde8e82c (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";

async function installAndStartExtension() {
  function contentScript() {
    window.addEventListener("keydown", aEvent => {
      console.log("keydown event is fired");
      if (aEvent.defaultPrevented) {
        return;
      }
      let selection = window.getSelection();
      if (selection.isCollapsed) {
        return;
      }
      if (aEvent.ctrlKey && aEvent.key === "k") {
        document.execCommand("createLink", false, "http://example.com/");
        aEvent.preventDefault();
      }
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          js: ["content_script.js"],
          matches: ["<all_urls>"],
          run_at: "document_start",
        },
      ],
    },
    files: {
      "content_script.js": contentScript,
    },
  });

  await extension.startup();

  return extension;
}

add_task(async function () {
  const extension = await installAndStartExtension();
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/browser/dom/events/test/file_beforeinput_by_execCommand_in_contentscript.html",
    true
  );

  /**
   * Document.execCommand() shouldn't cause `beforeinput`, but it may be used
   * by addons for emulating user input and make the input undoable on builtin
   * editors.  Therefore, if and only if it's called by addons, `beforeinput`
   * should be fired.
   */
  function runTest() {
    const editor = content.document.querySelector("[contenteditable]");
    editor.focus();
    content.document.getSelection().selectAllChildren(editor);
    let beforeinput;
    editor.addEventListener("beforeinput", aEvent => {
      beforeinput = aEvent;
    });
    const description = 'Test execCommand("createLink")';
    editor.addEventListener("input", aEvent => {
      if (!beforeinput) {
        sendAsyncMessage("Test:BeforeInputInContentEditable", {
          succeeded: false,
          message: `${description}: No beforeinput event is fired`,
        });
        return;
      }
      sendAsyncMessage("Test:BeforeInputInContentEditable", {
        succeeded:
          editor.innerHTML === '<a href="http://example.com/">abcdef</a>',
        message: `${description}: editor.innerHTML=${editor.innerHTML}`,
      });
    });
  }

  try {
    tab.linkedBrowser.messageManager.loadFrameScript(
      "data:,(" + runTest.toString() + ")();",
      false
    );

    let testResult = new Promise(resolve => {
      let mm = tab.linkedBrowser.messageManager;
      mm.addMessageListener(
        "Test:BeforeInputInContentEditable",
        function onFinish(aMsg) {
          mm.removeMessageListener(
            "Test:BeforeInputInContentEditable",
            onFinish
          );
          is(aMsg.data.succeeded, true, aMsg.data.message);
          resolve();
        }
      );
    });
    info("Sending Ctrl+K...");
    await BrowserTestUtils.synthesizeKey(
      "k",
      { ctrlKey: true },
      tab.linkedBrowser
    );
    info("Waiting test result...");
    await testResult;
  } finally {
    BrowserTestUtils.removeTab(tab);
    await extension.unload();
  }
});

add_task(async function () {
  const extension = await installAndStartExtension();
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/browser/dom/events/test/file_beforeinput_by_execCommand_in_contentscript.html",
    true
  );

  /**
   * Document.execCommand() from addons should be treated as a user input.
   * Therefore, it should not block first nested Document.execCommand() call
   * in a "beforeinput" event listener in the web app.
   */
  function runTest() {
    const editor = content.document.querySelectorAll("[contenteditable]")[1];
    editor.focus();
    content.document.getSelection().selectAllChildren(editor);
    const beforeInputs = [];
    editor.parentNode.addEventListener(
      "beforeinput",
      aEvent => {
        beforeInputs.push(aEvent);
      },
      { capture: true }
    );
    const description =
      'Test web app calls execCommand("insertText") on "beforeinput"';
    editor.addEventListener("input", aEvent => {
      if (!beforeInputs.length) {
        sendAsyncMessage("Test:BeforeInputInContentEditable", {
          succeeded: false,
          message: `${description}: No beforeinput event is fired`,
        });
        return;
      }
      if (beforeInputs.length > 1) {
        sendAsyncMessage("Test:BeforeInputInContentEditable", {
          succeeded: false,
          message: `${description}: Too many beforeinput events are fired`,
        });
        return;
      }
      sendAsyncMessage("Test:BeforeInputInContentEditable", {
        succeeded: editor.innerHTML.replace("<br>", "") === "ABCDEF",
        message: `${description}: editor.innerHTML=${editor.innerHTML}`,
      });
    });
  }

  try {
    tab.linkedBrowser.messageManager.loadFrameScript(
      "data:,(" + runTest.toString() + ")();",
      false
    );

    let testResult = new Promise(resolve => {
      let mm = tab.linkedBrowser.messageManager;
      mm.addMessageListener(
        "Test:BeforeInputInContentEditable",
        function onFinish(aMsg) {
          mm.removeMessageListener(
            "Test:BeforeInputInContentEditable",
            onFinish
          );
          is(aMsg.data.succeeded, true, aMsg.data.message);
          resolve();
        }
      );
    });
    info("Sending Ctrl+K...");
    await BrowserTestUtils.synthesizeKey(
      "k",
      { ctrlKey: true },
      tab.linkedBrowser
    );
    info("Waiting test result...");
    await testResult;
  } finally {
    BrowserTestUtils.removeTab(tab);
    await extension.unload();
  }
});