summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_external_messaging.html
blob: dfc1f9427ac4c32f874bafdf2c655caf474739f6 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension external messaging</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

function backgroundScript(id, otherId) {
  browser.runtime.onMessage.addListener((msg, sender) => {
    browser.test.fail(`Got unexpected message: ${uneval(msg)} ${uneval(sender)}`);
  });

  browser.runtime.onConnect.addListener(port => {
    browser.test.fail(`Got unexpected connection: ${uneval(port.sender)}`);
  });

  browser.runtime.onMessageExternal.addListener((msg, sender) => {
    browser.test.assertEq(otherId, sender.id, `${id}: Got expected external sender ID`);
    browser.test.assertEq(`helo-${id}`, msg, "Got expected message");

    browser.test.sendMessage("onMessage-done");

    return Promise.resolve(`ehlo-${otherId}`);
  });

  browser.runtime.onConnectExternal.addListener(port => {
    browser.test.assertEq(otherId, port.sender.id, `${id}: Got expected external connecter ID`);

    port.onMessage.addListener(msg => {
      browser.test.assertEq(`helo-${id}`, msg, "Got expected port message");

      port.postMessage(`ehlo-${otherId}`);

      browser.test.sendMessage("onConnect-done");
    });
  });

  browser.test.onMessage.addListener(msg => {
    if (msg === "go") {
      browser.runtime.sendMessage(otherId, `helo-${otherId}`).then(result => {
        browser.test.assertEq(`ehlo-${id}`, result, "Got expected reply");
        browser.test.sendMessage("sendMessage-done");
      });

      let port = browser.runtime.connect(otherId);
      port.postMessage(`helo-${otherId}`);

      port.onMessage.addListener(msg => {
        port.disconnect();

        browser.test.assertEq(msg, `ehlo-${id}`, "Got expected port reply");
        browser.test.sendMessage("connect-done");
      });
    }
  });
}

function makeExtension(id, otherId) {
  let args = `${JSON.stringify(id)}, ${JSON.stringify(otherId)}`;

  let extensionData = {
    background: `(${backgroundScript})(${args})`,
    manifest: {
      "applications": {"gecko": {id}},
    },
  };

  return ExtensionTestUtils.loadExtension(extensionData);
}

add_task(function* test_contentscript() {
  const ID1 = "foo-message@mochitest.mozilla.org";
  const ID2 = "bar-message@mochitest.mozilla.org";

  let extension1 = makeExtension(ID1, ID2);
  let extension2 = makeExtension(ID2, ID1);

  yield Promise.all([extension1.startup(), extension2.startup()]);

  extension1.sendMessage("go");
  extension2.sendMessage("go");

  yield Promise.all([
    extension1.awaitMessage("sendMessage-done"),
    extension2.awaitMessage("sendMessage-done"),

    extension1.awaitMessage("onMessage-done"),
    extension2.awaitMessage("onMessage-done"),

    extension1.awaitMessage("connect-done"),
    extension2.awaitMessage("connect-done"),

    extension1.awaitMessage("onConnect-done"),
    extension2.awaitMessage("onConnect-done"),
  ]);

  yield extension1.unload();
  yield extension2.unload();
});
</script>

</body>
</html>