summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_chrome_ext_svg_context_fill.html
blob: 3b022be5ecee808e22fe4f959796a6cc5f17e330 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for permissions</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
  <style>
    img {
      -moz-context-properties: fill;
      fill: green;
    }

    img, div.ref {
      width: 100px;
      height: 100px;
    }

    div#green {
      background: green;
    }

    div#red {
      background: red;
    }
  </style>
  <h3>Testing on: <span id="test-params"></span></h3>
  <table>
   <thead>
     <tr>
       <th>webext image</th>
       <th>allowed ref</th>
       <th>disallowed ref</th>
    </tr>
   </thead>
   <tbody>
     <tr>
       <td>
         <img id="actual">
       </td>
       <td>
         <div id="green" class="ref"></div>
       </td>
       <td>
         <div id="red" class="ref"></div>
       </td>
     </tr>
   </tbody>
  </table>

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

const { TestUtils } = SpecialPowers.ChromeUtils.import(
  "resource://testing-common/TestUtils.jsm"
);

function screenshotPage(win, elementSelector) {
  const el = win.document.querySelector(elementSelector);
  return TestUtils.screenshotArea(el, win);
}

async function test_moz_extension_svg_context_fill({
  addonId,
  isPrivileged,
  expectAllowed,
}) {
  // Include current test params in the rendered html page (to be included in failure
  // screenshots).
  document.querySelector("#test-params").textContent = JSON.stringify({
    addonId,
    isPrivileged,
    expectAllowed,
  });

  let extDefinition = {
    manifest: {
      browser_specific_settings: { gecko: { id: addonId } },
    },
    background() {
      browser.test.sendMessage("svg-url", browser.runtime.getURL("context-fill-fallback-red.svg"));
    },
    files: {
      "context-fill-fallback-red.svg": `
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
             xmlns:xlink="http://www.w3.org/1999/xlink">
          <rect height="100%" width="100%" fill="context-fill red" />
        </svg>
      `,
    },
  }

  if (isPrivileged) {
    // isPrivileged is unused when useAddonManager is set (see ExtensionTestCommon.generate),
    // the internal permission being tested is only added when the extension has a startupReason
    // related to new installations and upgrades/downgrades and so the `startupReason` is set here
    // to be able to mock the startupReason expected when useAddonManager can't be used.
    extDefinition = {
      ...extDefinition,
      isPrivileged,
      startupReason: "ADDON_INSTALL",
    };
  } else {
    // useAddonManager temporary is instead used to explicitly test the other cases when the extension
    // is not expected to be privileged.
    extDefinition = {
      ...extDefinition,
      useAddonManager: "temporary",
    };
  }

  const extension = ExtensionTestUtils.loadExtension(extDefinition);

  await extension.startup();

  // Set the extension url on the img element part of the
  // comparison table defined in the html part of this test file.
  const svgURL = await extension.awaitMessage("svg-url");
  document.querySelector("#actual").src = svgURL;

  let screenshots;

  // Wait until the svg context fill has been applied
  // (unfortunately waiting for a document reflow does
  // not seem to be enough).
  const expectedColor = expectAllowed ? "green" : "red";
  await TestUtils.waitForCondition(
    async () => {
      const result = await screenshotPage(window, "#actual");
      const reference = await screenshotPage(window, `#${expectedColor}`);
      screenshots = {result, reference};
      return result == reference;
    },
    `Context-fill should be ${
      expectAllowed ? "allowed" : "disallowed"
    } (resulting in ${expectedColor}) on "${addonId}" extension`
  );

  // At least an assertion is required to prevent the test from
  // failing.
  is(
    screenshots.result,
    screenshots.reference,
    "svg context-fill test completed, result does match reference"
  );

  await extension.unload();
}

// This test file verify that the non-standard svg context-fill feature is allowed
// on extensions svg files coming from Mozilla-owned extensions.
//
// NOTE: line extension permission to use context fill is tested in test_recommendations.js

add_task(async function test_allowed_on_privileged_ext() {
  await test_moz_extension_svg_context_fill({
    addonId: "privileged-addon@mochi.test",
    isPrivileged: true,
    expectAllowed: true,
  });
});

add_task(async function test_disallowed_on_non_privileged_ext() {
  await test_moz_extension_svg_context_fill({
    addonId: "non-privileged-arbitrary-addon-id@mochi.test",
    isPrivileged: false,
    expectAllowed: false,
  });
});

add_task(async function test_allowed_on_privileged_ext_with_mozilla_id() {
  await test_moz_extension_svg_context_fill({
    addonId: "privileged-addon@mozilla.org",
    isPrivileged: true,
    expectAllowed: true,
  });

  await test_moz_extension_svg_context_fill({
    addonId: "privileged-addon@mozilla.com",
    isPrivileged: true,
    expectAllowed: true,
  });
});

add_task(async function test_allowed_on_non_privileged_ext_with_mozilla_id() {
  await test_moz_extension_svg_context_fill({
    addonId: "non-privileged-addon@mozilla.org",
    isPrivileged: false,
    expectAllowed: true,
  });

  await test_moz_extension_svg_context_fill({
    addonId: "non-privileged-addon@mozilla.com",
    isPrivileged: false,
    expectAllowed: true,
  });
});
</script>

</body>
</html>