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

const { ExtensionAPI } = ExtensionCommon;

add_task(async function () {
  const schema = [
    {
      namespace: "manifest",
      types: [
        {
          $extend: "WebExtensionManifest",
          properties: {
            a_manifest_property: {
              type: "object",
              optional: true,
              properties: {
                nested: {
                  optional: true,
                  type: "any",
                },
              },
              additionalProperties: { $ref: "UnrecognizedProperty" },
            },
          },
        },
      ],
    },
    {
      namespace: "testManifestPermission",
      permissions: ["manifest:a_manifest_property"],
      functions: [
        {
          name: "testMethod",
          type: "function",
          async: true,
          parameters: [],
          permissions: ["manifest:a_manifest_property.nested"],
        },
      ],
    },
  ];

  class FakeAPI extends ExtensionAPI {
    getAPI(context) {
      return {
        testManifestPermission: {
          get testProperty() {
            return "value";
          },
          testMethod() {
            return Promise.resolve("value");
          },
        },
      };
    }
  }

  const modules = {
    testNamespace: {
      url: URL.createObjectURL(new Blob([FakeAPI.toString()])),
      schema: `data:,${JSON.stringify(schema)}`,
      scopes: ["addon_parent", "addon_child"],
      paths: [["testManifestPermission"]],
    },
  };

  Services.catMan.addCategoryEntry(
    "webextension-modules",
    "test-manifest-permission",
    `data:,${JSON.stringify(modules)}`,
    false,
    false
  );

  async function testExtension(extensionDef, assertFn) {
    let extension = ExtensionTestUtils.loadExtension(extensionDef);

    await extension.startup();
    await assertFn(extension);
    await extension.unload();
  }

  await testExtension(
    {
      manifest: {
        a_manifest_property: {},
      },
      background() {
        // Test hasPermission method implemented in ExtensionChild.jsm.
        browser.test.assertTrue(
          "testManifestPermission" in browser,
          "The API namespace is defined as expected"
        );
        browser.test.assertEq(
          undefined,
          browser.testManifestPermission &&
            browser.testManifestPermission.testMethod,
          "The property with nested manifest property permission should not be available "
        );
        browser.test.notifyPass("test-extension-manifest-without-nested-prop");
      },
    },
    async extension => {
      await extension.awaitFinish(
        "test-extension-manifest-without-nested-prop"
      );

      // Test hasPermission method implemented in Extension.jsm.
      equal(
        extension.extension.hasPermission("manifest:a_manifest_property"),
        true,
        "Got the expected Extension's hasPermission result on existing property"
      );
      equal(
        extension.extension.hasPermission(
          "manifest:a_manifest_property.nested"
        ),
        false,
        "Got the expected Extension's hasPermission result on existing subproperty"
      );
    }
  );

  await testExtension(
    {
      manifest: {
        a_manifest_property: {
          nested: {},
        },
      },
      background() {
        // Test hasPermission method implemented in ExtensionChild.jsm.
        browser.test.assertTrue(
          "testManifestPermission" in browser,
          "The API namespace is defined as expected"
        );
        browser.test.assertEq(
          "function",
          browser.testManifestPermission &&
            typeof browser.testManifestPermission.testMethod,
          "The property with nested manifest property permission should be available "
        );
        browser.test.notifyPass("test-extension-manifest-with-nested-prop");
      },
    },
    async extension => {
      await extension.awaitFinish("test-extension-manifest-with-nested-prop");

      // Test hasPermission method implemented in Extension.jsm.
      equal(
        extension.extension.hasPermission("manifest:a_manifest_property"),
        true,
        "Got the expected Extension's hasPermission result on existing property"
      );
      equal(
        extension.extension.hasPermission(
          "manifest:a_manifest_property.nested"
        ),
        true,
        "Got the expected Extension's hasPermission result on existing subproperty"
      );
      equal(
        extension.extension.hasPermission(
          "manifest:a_manifest_property.unexisting"
        ),
        false,
        "Got the expected Extension's hasPermission result on non existing subproperty"
      );
    }
  );
});