summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_css_registered_properties.js
blob: 1429b551672b0dce094837a27b3b19d8e8f97536 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the ResourceCommand API around CSS_REGISTERED_PROPERTIES.

const ResourceCommand = require("resource://devtools/shared/commands/resource/resource-command.js");

const IFRAME_URL = `https://example.org/document-builder.sjs?html=${encodeURIComponent(`
  <style>
    @property --css-a {
      syntax: "<color>";
      inherits: true;
      initial-value: gold;
    }
  </style>
  <script>
    CSS.registerProperty({
      name: "--js-a",
      syntax: "<length>",
      inherits: true,
      initialValue: "20px"
    });
  </script>
  <h1>iframe</h1>
`)}`;

const TEST_URL = `https://example.org/document-builder.sjs?html=
  <head>
    <style>
      @property --css-a {
        syntax: "*";
        inherits: false;
      }

      @property --css-b {
        syntax: "<color>";
        inherits: true;
        initial-value: tomato;
      }
    </style>
    <script>
      CSS.registerProperty({
        name: "--js-a",
        syntax: "*",
        inherits: false,
      });
      CSS.registerProperty({
        name: "--js-b",
        syntax: "<length>",
        inherits: true,
        initialValue: "10px"
      });
    </script>
  </head>
  <h1>CSS_REGISTERED_PROPERTIES</h1>
  <iframe src="${encodeURIComponent(IFRAME_URL)}"></iframe>`;

add_task(async function () {
  await pushPref("layout.css.properties-and-values.enabled", true);
  const tab = await addTab(TEST_URL);

  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab
  );

  // Wait for targets
  await targetCommand.startListening();
  const targets = [];
  const onAvailable = ({ targetFront }) => targets.push(targetFront);
  await targetCommand.watchTargets({
    types: [targetCommand.TYPES.FRAME],
    onAvailable,
  });
  await waitFor(() => targets.length === 2);
  const [topLevelTarget, iframeTarget] = targets.sort((a, b) =>
    a.isTopLevel ? -1 : 1
  );

  // Watching for new stylesheets shouldn't be
  const stylesheets = [];
  await resourceCommand.watchResources([resourceCommand.TYPES.STYLESHEET], {
    onAvailable: resources => stylesheets.push(...resources),
    ignoreExistingResources: true,
  });

  info("Check that we get existing registered properties");
  const availableResources = [];
  const updatedResources = [];
  const destroyedResources = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CSS_REGISTERED_PROPERTIES],
    {
      onAvailable: resources => availableResources.push(...resources),
      onUpdated: resources => updatedResources.push(...resources),
      onDestroyed: resources => destroyedResources.push(...resources),
    }
  );

  is(
    availableResources.length,
    6,
    "The 6 existing registered properties where retrieved"
  );

  // Sort resources so we get them alphabetically ordered by their name, with the ones for
  // the top level target displayed first.
  availableResources.sort((a, b) => {
    if (a.targetFront !== b.targetFront) {
      return a.targetFront.isTopLevel ? -1 : 1;
    }
    return a.name < b.name ? -1 : 1;
  });

  assertResource(availableResources[0], {
    name: "--css-a",
    syntax: "*",
    inherits: false,
    initialValue: null,
    fromJS: false,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[1], {
    name: "--css-b",
    syntax: "<color>",
    inherits: true,
    initialValue: "tomato",
    fromJS: false,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[2], {
    name: "--js-a",
    syntax: "*",
    inherits: false,
    initialValue: null,
    fromJS: true,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[3], {
    name: "--js-b",
    syntax: "<length>",
    inherits: true,
    initialValue: "10px",
    fromJS: true,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[4], {
    name: "--css-a",
    syntax: "<color>",
    inherits: true,
    initialValue: "gold",
    fromJS: false,
    targetFront: iframeTarget,
  });
  assertResource(availableResources[5], {
    name: "--js-a",
    syntax: "<length>",
    inherits: true,
    initialValue: "20px",
    fromJS: true,
    targetFront: iframeTarget,
  });

  info("Check that we didn't get notified about existing stylesheets");
  // wait a bit so we'd have the time to be notified about stylesheet resources
  await wait(500);
  is(
    stylesheets.length,
    0,
    "Watching for registered properties does not notify about existing stylesheets resources"
  );

  info("Check that we get properties from new stylesheets");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    const s = content.document.createElement("style");
    s.textContent = `
        @property --css-c {
          syntax: "<custom-ident>";
          inherits: true;
          initial-value: custom;
        }

        @property --css-d {
          syntax: "big | bigger";
          inherits: true;
          initial-value: big;
        }
    `;
    content.document.head.append(s);
  });

  info("Wait for registered properties to be available");
  await waitFor(() => availableResources.length === 8);
  ok(true, "Got notified about 2 new registered properties");
  assertResource(availableResources[6], {
    name: "--css-c",
    syntax: "<custom-ident>",
    inherits: true,
    initialValue: "custom",
    fromJS: false,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[7], {
    name: "--css-d",
    syntax: "big | bigger",
    inherits: true,
    initialValue: "big",
    fromJS: false,
    targetFront: topLevelTarget,
  });

  info("Wait to be notified about the new stylesheet");
  await waitFor(() => stylesheets.length === 1);
  ok(true, "we do get notified about stylesheets");

  info(
    "Check that we get notified about properties registered via CSS.registerProperty"
  );
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    content.CSS.registerProperty({
      name: "--js-c",
      syntax: "*",
      inherits: false,
      initialValue: 42,
    });
    content.CSS.registerProperty({
      name: "--js-d",
      syntax: "<color>#",
      inherits: true,
      initialValue: "blue,cyan",
    });
  });

  await waitFor(() => availableResources.length === 10);
  ok(true, "Got notified about 2 new registered properties");
  assertResource(availableResources[8], {
    name: "--js-c",
    syntax: "*",
    inherits: false,
    initialValue: "42",
    fromJS: true,
    targetFront: topLevelTarget,
  });
  assertResource(availableResources[9], {
    name: "--js-d",
    syntax: "<color>#",
    inherits: true,
    initialValue: "blue,cyan",
    fromJS: true,
    targetFront: topLevelTarget,
  });

  info(
    "Check that we get notified about properties registered via CSS.registerProperty in iframe"
  );
  const iframeBrowsingContext = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    () => content.document.querySelector("iframe").browsingContext
  );

  await SpecialPowers.spawn(iframeBrowsingContext, [], () => {
    content.CSS.registerProperty({
      name: "--js-iframe",
      syntax: "<color>#",
      inherits: true,
      initialValue: "red,salmon",
    });
  });

  await waitFor(() => availableResources.length === 11);
  ok(true, "Got notified about 2 new registered properties");
  assertResource(availableResources[10], {
    name: "--js-iframe",
    syntax: "<color>#",
    inherits: true,
    initialValue: "red,salmon",
    fromJS: true,
    targetFront: iframeTarget,
  });

  info(
    "Check that we get notified about destroyed properties when removing stylesheet"
  );
  // sanity check
  is(destroyedResources.length, 0, "No destroyed resources yet");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.document.querySelector("style").remove();
  });
  await waitFor(() => destroyedResources.length == 2);
  ok(true, "We got notified about destroyed resources");
  destroyedResources.sort((a, b) => a < b);
  is(
    destroyedResources[0].resourceType,
    ResourceCommand.TYPES.CSS_REGISTERED_PROPERTIES,
    "resource type is correct"
  );
  is(
    destroyedResources[0].resourceId,
    `${topLevelTarget.actorID}:css-registered-property:--css-a`,
    "expected css property was destroyed"
  );
  is(
    destroyedResources[1].resourceType,
    ResourceCommand.TYPES.CSS_REGISTERED_PROPERTIES,
    "resource type is correct"
  );
  is(
    destroyedResources[1].resourceId,
    `${topLevelTarget.actorID}:css-registered-property:--css-b`,
    "expected css property was destroyed"
  );

  info(
    "Check that we get notified about updated properties when modifying stylesheet"
  );
  is(updatedResources.length, 0, "No updated resources yet");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.document.querySelector("style").textContent = `
     /* not updated */
      @property --css-c {
        syntax: "<custom-ident>";
        inherits: true;
        initial-value: custom;
      }

      @property --css-d {
        syntax: "big | bigger";
        inherits: true;
        /* only change initial value (was big) */
        initial-value: bigger;
      }

      /* add a new property */
      @property --css-e {
        syntax: "<color>";
        inherits: false;
        initial-value: green;
      }
    `;
  });
  await waitFor(() => updatedResources.length === 1);
  ok(true, "One property was updated");
  assertResource(updatedResources[0].resource, {
    name: "--css-d",
    syntax: "big | bigger",
    inherits: true,
    initialValue: "bigger",
    fromJS: false,
    targetFront: topLevelTarget,
  });

  await waitFor(() => availableResources.length === 12);
  ok(true, "We got notified about the new property");
  assertResource(availableResources.at(-1), {
    name: "--css-e",
    syntax: "<color>",
    inherits: false,
    initialValue: "green",
    fromJS: false,
    targetFront: topLevelTarget,
  });

  await client.close();
});

async function assertResource(resource, expected) {
  is(
    resource.resourceType,
    ResourceCommand.TYPES.CSS_REGISTERED_PROPERTIES,
    "Resource type is correct"
  );
  is(resource.name, expected.name, "name is correct");
  is(resource.syntax, expected.syntax, "syntax is correct");
  is(resource.inherits, expected.inherits, "inherits is correct");
  is(resource.initialValue, expected.initialValue, "initialValue is correct");
  is(resource.fromJS, expected.fromJS, "fromJS is correct");
  is(
    resource.targetFront,
    expected.targetFront,
    "resource is associated with expected target"
  );
}