summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_userScripts_register.js
blob: fd57cd273681acd7e92f94ff5463f95b807bdccd (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
"use strict";

const { createAppInfo } = AddonTestUtils;

AddonTestUtils.init(this);

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "49");

const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));

const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;

add_task(async function test_userscripts_register_cookieStoreId() {
  async function background() {
    const matches = ["<all_urls>"];

    await browser.test.assertRejects(
      browser.userScripts.register({
        js: [{ code: "" }],
        matches,
        cookieStoreId: "not_a_valid_cookieStoreId",
      }),
      /Invalid cookieStoreId/,
      "userScript.register with an invalid cookieStoreId"
    );

    await browser.test.assertRejects(
      browser.userScripts.register({
        js: [{ code: "" }],
        matches,
        cookieStoreId: "",
      }),
      /Invalid cookieStoreId/,
      "userScripts.register with an invalid cookieStoreId"
    );

    let cookieStoreIdJSArray = [
      {
        id: "firefox-container-1",
        code: `document.body.textContent += "1"`,
      },
      {
        id: ["firefox-container-2", "firefox-container-3"],
        code: `document.body.textContent += "2-3"`,
      },
      {
        id: "firefox-private",
        code: `document.body.textContent += "private"`,
      },
      {
        id: "firefox-default",
        code: `document.body.textContent += "default"`,
      },
    ];

    for (let { id, code } of cookieStoreIdJSArray) {
      await browser.userScripts.register({
        js: [{ code }],
        matches,
        runAt: "document_end",
        cookieStoreId: id,
      });
    }

    await browser.contentScripts.register({
      js: [
        {
          code: `browser.test.sendMessage("last-content-script");`,
        },
      ],
      matches,
      runAt: "document_end",
    });

    browser.test.sendMessage("background_ready");
  }

  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["<all_urls>"],
      user_scripts: {},
    },
    background,
    incognitoOverride: "spanning",
  });

  await extension.startup();
  await extension.awaitMessage("background_ready");

  registerCleanupFunction(() => extension.unload());

  let testCases = [
    {
      contentPageOptions: { userContextId: 0 },
      expectedTextContent: "default",
    },
    {
      contentPageOptions: { userContextId: 1 },
      expectedTextContent: "1",
    },
    {
      contentPageOptions: { userContextId: 2 },
      expectedTextContent: "2-3",
    },
    {
      contentPageOptions: { userContextId: 3 },
      expectedTextContent: "2-3",
    },
    {
      contentPageOptions: { userContextId: 4 },
      expectedTextContent: "",
    },
    {
      contentPageOptions: { privateBrowsing: true },
      expectedTextContent: "private",
    },
  ];

  for (let test of testCases) {
    let contentPage = await ExtensionTestUtils.loadContentPage(
      `${BASE_URL}/file_sample.html`,
      test.contentPageOptions
    );

    await extension.awaitMessage("last-content-script");

    let result = await contentPage.spawn([], () => {
      let textContent = content.document.body.textContent;
      // Omit the default content from file_sample.html.
      return textContent.replace("\n\nSample text\n\n\n\n", "");
    });

    await contentPage.close();

    equal(
      result,
      test.expectedTextContent,
      `Expected textContent on content page`
    );
  }
});