summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/test/browser/browser_ext_messaging.js
blob: 5d1b5cf31790db57e3962ee64911ba81e4fd1058 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-env webextensions */

"use strict";

const TEST_URL = "https://example.com/";

// These allowed rejections are copied from
// browser/components/extensions/test/browser/head.js.
const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(
  /Message manager disconnected/
);
PromiseTestUtils.allowMatchingRejectionsGlobally(
  /Receiving end does not exist/
);

const extension = ExtensionTestUtils.loadExtension({
  manifest: {
    content_scripts: [
      {
        matches: [TEST_URL],
        js: ["content-script.js"],
        run_at: "document_start",
      },
    ],
  },
  background() {
    let currentPort;

    browser.runtime.onConnect.addListener(port => {
      currentPort = port;
      port.onDisconnect.addListener(() =>
        browser.test.sendMessage("port-disconnected")
      );
      port.onMessage.addListener(msg =>
        browser.test.sendMessage("port-message-received", msg)
      );
      browser.test.sendMessage("port-connected");
    });

    browser.test.onMessage.addListener(async msg => {
      if (msg !== "test:port-message-send") {
        browser.test.fail(`Unexpected test message received: ${msg}`);
      }

      currentPort.postMessage("ping");
    });

    browser.test.sendMessage("background:ready");
  },
  files: {
    "content-script.js": function contentScript() {
      const port = browser.runtime.connect();
      port.onMessage.addListener(msg => port.postMessage(`${msg}-pong`));
    },
  },
});

add_task(async function setup_first_test() {
  await extension.startup();

  await extension.awaitMessage("background:ready");
});

addRDMTaskWithPreAndPost(
  TEST_URL,
  async function pre_task() {
    await extension.awaitMessage("port-connected");
  },
  async function test_port_kept_connected_on_switch_to_RDB() {
    extension.sendMessage("test:port-message-send");

    is(
      await extension.awaitMessage("port-message-received"),
      "ping-pong",
      "Got the expected message back from the content script"
    );
  },
  async function post_task() {
    extension.sendMessage("test:port-message-send");

    is(
      await extension.awaitMessage("port-message-received"),
      "ping-pong",
      "Got the expected message back from the content script"
    );
  }
);

add_task(async function cleanup_first_test() {
  await extension.awaitMessage("port-disconnected");

  await extension.unload();
});

addRDMTask(TEST_URL, async function test_tab_sender() {
  const extension2 = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],

      content_scripts: [
        {
          matches: [TEST_URL],
          js: ["content-script.js"],
          run_at: "document_start",
        },
      ],
    },

    async background() {
      const TEST_URL = "https://example.com/"; // eslint-disable-line no-shadow

      browser.test.log("Background script init");

      let extTab;
      const contentMessage = new Promise(resolve => {
        browser.test.log("Listen to content");
        const listener = async (msg, sender, respond) => {
          browser.test.assertEq(
            msg,
            "hello-from-content",
            "Background script got hello-from-content message"
          );

          const tabs = await browser.tabs.query({
            currentWindow: true,
            active: true,
          });
          browser.test.assertEq(
            tabs.length,
            1,
            "One tab is active in the current window"
          );
          extTab = tabs[0];
          browser.test.log(`Tab: id ${extTab.id}, url ${extTab.url}`);
          browser.test.assertEq(extTab.url, TEST_URL, "Tab has the test URL");

          browser.test.assertTrue(!!sender, "Message has a sender");
          browser.test.assertTrue(!!sender.tab, "Message has a sender.tab");
          browser.test.assertEq(
            sender.tab.id,
            extTab.id,
            "Sender's tab ID matches the RDM tab ID"
          );
          browser.test.assertEq(
            sender.tab.url,
            extTab.url,
            "Sender's tab URL matches the RDM tab URL"
          );

          browser.runtime.onMessage.removeListener(listener);
          resolve();
        };
        browser.runtime.onMessage.addListener(listener);
      });

      // Wait for "resume" message so we know the content script is also ready.
      await new Promise(resolve => {
        browser.test.onMessage.addListener(resolve);
        browser.test.sendMessage("background-script-ready");
      });

      await contentMessage;

      browser.test.log("Send message from background to content");
      const contentSender = await browser.tabs.sendMessage(
        extTab.id,
        "hello-from-background"
      );
      browser.test.assertEq(
        contentSender.id,
        browser.runtime.id,
        "The sender ID in content matches this extension"
      );

      browser.test.notifyPass("rdm-messaging");
    },

    files: {
      "content-script.js": async function () {
        browser.test.log("Content script init");

        browser.test.log("Listen to background");
        browser.runtime.onMessage.addListener((msg, sender, respond) => {
          browser.test.assertEq(
            msg,
            "hello-from-background",
            "Content script got hello-from-background message"
          );

          browser.test.assertTrue(!!sender, "Message has a sender");
          browser.test.assertTrue(!!sender.id, "Message has a sender.id");

          const { id } = sender;
          respond({ id });
        });

        // Wait for "resume" message so we know the background script is also ready.
        await new Promise(resolve => {
          browser.test.onMessage.addListener(resolve);
          browser.test.sendMessage("content-script-ready");
        });

        browser.test.log("Send message from content to background");
        browser.runtime.sendMessage("hello-from-content");
      },
    },
  });

  const contentScriptReady = extension2.awaitMessage("content-script-ready");
  const backgroundScriptReady = extension2.awaitMessage(
    "background-script-ready"
  );
  const finish = extension2.awaitFinish("rdm-messaging");

  await extension2.startup();

  // It appears the background script and content script can loaded in either order, so
  // we'll wait for the both to listen before proceeding.
  await backgroundScriptReady;
  await contentScriptReady;
  extension2.sendMessage("resume");

  await finish;
  await extension2.unload();
});