summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_chrome_ext_hybrid_addons.html
blob: a74c551f0833d20affd08a21ff6cf0b383969033 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for hybrid addons: SDK or bootstrap.js + embedded WebExtension</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>

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

/**
 * This test contains additional tests that ensure that an SDK hybrid addon
 * which is using the new module loader can embed a webextension correctly:
 *
 * while the other tests related to the "Embedded WebExtension" are focused
 * on unit testing a specific component, these tests are testing that a complete
 * hybrid SDK addon works as expected.
 *
 * NOTE: this tests are also the only ones which tests an SDK hybrid addon that
 * uses the new module loader (the one actually used in production by real world
 * addons these days), while the Addon SDK "embedded-webextension" test addon
 * uses the old deprecated module loader (as all the other Addon SDK test addons).
 */

function generateClassicExtensionFiles({id, files}) {
  // The addon install.rdf file, as it would be generated by jpm from the addon
  // package.json metadata.
  files["install.rdf"] = `<?xml version="1.0" encoding="utf-8"?>
    <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:em="http://www.mozilla.org/2004/em-rdf#">
      <Description about="urn:mozilla:install-manifest">
        <em:id>${id}</em:id>
        <em:type>2</em:type>
        <em:bootstrap>true</em:bootstrap>
        <em:hasEmbeddedWebExtension>true</em:hasEmbeddedWebExtension>
        <em:unpack>false</em:unpack>
        <em:version>0.1.0</em:version>
        <em:name>Fake Hybrid Addon</em:name>
        <em:description>A fake hybrid addon</em:description>

        <!-- Firefox -->
        <em:targetApplication>
          <Description>
            <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
            <em:minVersion>51.0a1</em:minVersion>
            <em:maxVersion>*</em:maxVersion>
          </Description>
        </em:targetApplication>

        <!-- Fennec -->
        <em:targetApplication>
          <Description>
            <em:id>{aa3c5121-dab2-40e2-81ca-7ea25febc110}</em:id>
            <em:minVersion>51.0a1</em:minVersion>
            <em:maxVersion>*</em:maxVersion>
          </Description>
        </em:targetApplication>
      </Description>
    </RDF>`;

  // The addon package.json file.
  files["package.json"] = `{
    "id": "${id}",
    "name": "hybrid-addon",
    "version": "0.1.0",
    "description": "A fake hybrid addon",
    "main": "index.js",
    "engines": {
      "firefox": ">= 51.0a1",
      "fennec": ">= 51.0a1"
    },
    "license": "MPL-2.0",
    "hasEmbeddedWebExtension": true
  }`;

  // The bootstrap file that jpm bundle in any SDK addon built with it.
  files["bootstrap.js"] = `
    const { utils: Cu } = Components;
    const rootURI = __SCRIPT_URI_SPEC__.replace("bootstrap.js", "");
    const COMMONJS_URI = "resource://gre/modules/commonjs";
    const { require } = Cu.import(COMMONJS_URI + "/toolkit/require.js", {});
    const { Bootstrap } = require(COMMONJS_URI + "/sdk/addon/bootstrap.js");
    var { startup, shutdown, install, uninstall } = new Bootstrap(rootURI);
  `;

  return files;
}

add_task(function* test_sdk_hybrid_addon_with_jpm_module_loader() {
  function backgroundScript() {
    browser.runtime.sendMessage("background message", (reply) => {
      browser.test.assertEq("sdk received message: background message", reply,
                            "Got the expected reply from the SDK context");
      browser.test.notifyPass("sdk.webext-api.onmessage");
    });
  }

  async function sdkMainScript() {
    /* globals require */
    const webext = require("sdk/webextension");
    let {browser} = await webext.startup();
    browser.runtime.onMessage.addListener((msg, sender, sendReply) => {
      sendReply(`sdk received message: ${msg}`);
    });
  }

  let id = "fake@sdk.hybrid.addon";
  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    files: generateClassicExtensionFiles({
      id,
      files: {
        "index.js": sdkMainScript,
        "webextension/manifest.json": {
          name: "embedded webextension name",
          manifest_version: 2,
          version: "0.1.0",
          background: {
            scripts: ["bg.js"],
          },
        },
        "webextension/bg.js": backgroundScript,
      },
    }),
  }, id);

  extension.startup();

  yield extension.awaitFinish("sdk.webext-api.onmessage");

  yield extension.unload();
});
</script>

</body>
</html>