summaryrefslogtreecommitdiffstats
path: root/dom/events/test/browser_beforeinput_by_execCommand_in_contentscript.js
blob: 062aa73a4cdf3ab57ede3a2debabf4a8a452533b (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
"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
  );

  function runTest() {
    var editor = content.document.querySelector("[contenteditable]");
    editor.focus();
    content.document.getSelection().selectAllChildren(editor);
    var beforeinput;
    editor.addEventListener("beforeinput", aEvent => {
      beforeinput = aEvent;
    });
    editor.addEventListener("input", aEvent => {
      if (!beforeinput) {
        sendAsyncMessage("Test:BeforeInputInContentEditable", {
          succeeded: false,
          message: "No beforeinput event is fired",
        });
        return;
      }
      sendAsyncMessage("Test:BeforeInputInContentEditable", {
        succeeded:
          editor.innerHTML === '<a href="http://example.com/">abcdef</a>',
        message: `editor.innerHTML=${editor.innerHTML}`,
      });
    });
  }

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

    let received = 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();
  }
});