summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_runtime_getContexts.js
blob: efef355f74eb64797421e96e8ef0763ef3249209 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function test_runtime_getContexts() {
  function background() {
    browser.test.onMessage.addListener(async (msg, ...args) => {
      if (msg === "runtime.getContexts") {
        try {
          const filter = args[0];
          if (!filter) {
            // Expected to be rejected.
            await browser.runtime.getContexts();
          } else {
            // Expected to be resolved.
            const result = await browser.runtime.getContexts(filter);
            browser.test.sendMessage(`${msg}:result`, result);
          }
        } catch (err) {
          browser.test.log(`runtime.getContexts error: ${err}\n`);
          browser.test.sendMessage(`${msg}:error`, String(err));
        }
      }
    });
    browser.test.sendMessage("bgpage:loaded");
  }

  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
    },
    background,
    files: {
      "tab.html": `<!DOCTYPE html><html></html>`,
    },
  });

  await extension.startup();
  await extension.awaitMessage("bgpage:loaded");

  const documentOrigin = extension.extension.baseURI.spec.slice(0, -1);
  const tabDocumentUrl = extension.extension.baseURI.resolve("tab.html");
  const bgDocumentUrl = extension.extension.baseURI.resolve(
    "_generated_background_page.html"
  );

  let expectedBackground = {
    contextType: "BACKGROUND",
    documentOrigin,
    documentUrl: bgDocumentUrl,
    incognito: false,
    frameId: 0,
    tabId: -1,
    windowId: -1,
  };

  let expectedTab = {
    contextType: "TAB",
    documentOrigin,
    documentUrl: `${tabDocumentUrl}?fistOpenedTab=true`,
    incognito: false,
    frameId: 0,
    // tabId and windowId are expected to be -1 in xpcshell test
    // (these are also covered by browser_ext_getContexts.js).
    tabId: -1,
    windowId: -1,
  };

  const assertValidContextId = contextId => {
    Assert.equal(
      typeof contextId,
      "string",
      "contextId should be set to a string"
    );
    Assert.notEqual(
      contextId.length,
      0,
      "contextId should be set to a non-zero length string"
    );
  };
  const assertGetContextsResult = (
    actual,
    expected,
    msg,
    { assertContextId = false } = {}
  ) => {
    const actualCopy = assertContextId ? actual : actual.map(it => ({ ...it }));
    if (!assertContextId) {
      actualCopy.forEach(it => delete it.contextId);
    }
    Assert.deepEqual(actualCopy, expected, msg);
  };

  info(
    "Test runtime.getContexts rejects when called without any filter parameter"
  );
  extension.sendMessage("runtime.getContexts", undefined);
  let resError = await extension.awaitMessage("runtime.getContexts:error");
  Assert.equal(
    resError,
    "Error: Incorrect argument types for runtime.getContexts.",
    "Got the expected error message"
  );

  info(
    "Test runtime.getContext resolved when called with an empty filter parameter"
  );

  extension.sendMessage("runtime.getContexts", {});
  let res = await extension.awaitMessage("runtime.getContexts:result");

  assertGetContextsResult(
    res,
    [expectedBackground],
    "Got the expected properties for the background context"
  );

  let actualBackground = res[0];
  assertValidContextId(actualBackground.contextId);

  const page = await ExtensionTestUtils.loadContentPage(
    `${tabDocumentUrl}?fistOpenedTab=true`
  );

  res = await page.spawn([], () =>
    this.content.wrappedJSObject.browser.runtime.getContexts({})
  );

  const bgItem = res.find(it => it.contextType === "BACKGROUND");
  const tabItem = res.find(it => it.contextType === "TAB");

  assertValidContextId(tabItem.contextId);

  assertGetContextsResult(
    res,
    [expectedBackground, expectedTab],
    "Got expected properties for backgrond and tab contexts"
  );
  assertGetContextsResult(
    [bgItem],
    [actualBackground],
    "Expect the expected properties for the background context (included same contextId)",
    { assertContextId: true }
  );

  info("Test runtime.getContexts with a contextType filter");
  res = await page.spawn([], () =>
    this.content.wrappedJSObject.browser.runtime.getContexts({
      contextTypes: ["BACKGROUND"],
    })
  );
  assertGetContextsResult(
    res,
    [actualBackground],
    "Expect only the backgrund context to be included in the results",
    { assertContextId: true }
  );

  info("Test runtime.ContextType enum");
  const contextTypeEnum = await page.spawn([], () => {
    return this.content.wrappedJSObject.browser.runtime.ContextType;
  });

  const expectedTypesMap = ["BACKGROUND", "POPUP", "SIDE_PANEL", "TAB"].reduce(
    (acc, item) => {
      acc[item] = item;
      return acc;
    },
    {}
  );

  Assert.deepEqual(
    contextTypeEnum,
    expectedTypesMap,
    "Got the expected values in the ContextType enum"
  );

  await extension.unload();
});