summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
blob: 12c4bb7ef84b4ee6f13b7f02e31a9249608613e0 (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
196
197
198
199
200
201
202
203
204
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const scriptPage = url =>
  `<html><head><meta charset="utf-8"><script src="${url}"></script></head><body>Test Popup</body></html>`;

add_task(async function test_execute_page_action_without_popup() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      commands: {
        _execute_page_action: {
          suggested_key: {
            default: "Alt+Shift+J",
          },
        },
        "send-keys-command": {
          suggested_key: {
            default: "Alt+Shift+3",
          },
        },
      },
      page_action: {},
    },

    background: function () {
      let isShown = false;

      browser.commands.onCommand.addListener(commandName => {
        if (commandName == "_execute_page_action") {
          browser.test.fail(
            `The onCommand listener should never fire for ${commandName}.`
          );
        } else if (commandName == "send-keys-command") {
          if (!isShown) {
            isShown = true;
            browser.tabs.query({ currentWindow: true, active: true }, tabs => {
              tabs.forEach(tab => {
                browser.pageAction.show(tab.id);
              });
              browser.test.sendMessage("send-keys");
            });
          }
        }
      });

      browser.pageAction.onClicked.addListener(() => {
        browser.test.assertTrue(
          isShown,
          "The onClicked event should fire if the page action is shown."
        );
        browser.test.notifyPass("page-action-without-popup");
      });

      browser.test.sendMessage("send-keys");
    },
  });

  extension.onMessage("send-keys", () => {
    EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
    EventUtils.synthesizeKey("3", { altKey: true, shiftKey: true });
  });

  await extension.startup();
  await extension.awaitFinish("page-action-without-popup");
  await extension.unload();
});

add_task(async function test_execute_page_action_with_popup() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "http://example.com/"
  );

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      commands: {
        _execute_page_action: {
          suggested_key: {
            default: "Alt+Shift+J",
          },
        },
        "send-keys-command": {
          suggested_key: {
            default: "Alt+Shift+3",
          },
        },
      },
      page_action: {
        default_popup: "popup.html",
        browser_style: true,
      },
    },

    files: {
      "popup.html": scriptPage("popup.js"),
      "popup.js": function () {
        browser.runtime.sendMessage("popup-opened");
      },
    },

    background: function () {
      let isShown = false;

      browser.commands.onCommand.addListener(message => {
        if (message == "_execute_page_action") {
          browser.test.fail(
            `The onCommand listener should never fire for ${message}.`
          );
        }

        if (message == "send-keys-command") {
          if (!isShown) {
            isShown = true;
            browser.tabs.query({ currentWindow: true, active: true }, tabs => {
              tabs.forEach(tab => {
                browser.pageAction.show(tab.id);
              });
              browser.test.sendMessage("send-keys");
            });
          }
        }
      });

      browser.pageAction.onClicked.addListener(() => {
        browser.test.fail(
          `The onClicked listener should never fire when the pageAction has a popup.`
        );
      });

      browser.runtime.onMessage.addListener(msg => {
        browser.test.assertEq(msg, "popup-opened", "expected popup opened");
        browser.test.assertTrue(
          isShown,
          "The onClicked event should fire if the page action is shown."
        );
        browser.test.notifyPass("page-action-with-popup");
      });

      browser.test.sendMessage("send-keys");
    },
  });

  extension.onMessage("send-keys", () => {
    EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
    EventUtils.synthesizeKey("3", { altKey: true, shiftKey: true });
  });

  await extension.startup();
  await extension.awaitFinish("page-action-with-popup");
  await extension.unload();
  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_execute_page_action_with_matching() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      commands: {
        _execute_page_action: {
          suggested_key: {
            default: "Alt+Shift+J",
          },
        },
      },
      page_action: {
        default_popup: "popup.html",
        show_matches: ["<all_urls>"],
        browser_style: true,
      },
    },

    files: {
      "popup.html": scriptPage("popup.js"),
      "popup.js": function () {
        window.addEventListener(
          "load",
          () => {
            browser.test.notifyPass("page-action-with-popup");
          },
          { once: true }
        );
      },
    },
  });

  await extension.startup();
  let tab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "http://example.com/"
  );
  EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
  info("Waiting for pageAction open.");
  await extension.awaitFinish("page-action-with-popup");

  // Bug 1447796 make sure the key command can close the page action
  let panel = document.getElementById(`${makeWidgetId(extension.id)}-panel`);
  let hidden = promisePopupHidden(panel);
  EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
  info("Waiting for pageAction close.");
  await hidden;

  await extension.unload();
  BrowserTestUtils.removeTab(tab);
});