summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_use_counters.html
blob: 6e676b53441ccf4aeab7f6bc34d131dd7ab22a4b (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
<!doctype html>
<title>Test for Bug 1425700: CSS properties use-counters</title>
<link rel="author" title="Emilio Cobos Álvarez" href="mailto:emilio@crisal.io">
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<body>
<iframe id="iframe"></iframe>
<iframe id="second-iframe"></iframe>
<script>
const iframe = document.getElementById("iframe");

function iframe_reload(frame = iframe) {
  return new Promise(resolve => {
    frame.addEventListener("load", _ => resolve());
    frame.contentWindow.location.reload();
  });
}

function assert_recorded(win, recorded, properties, desc) {
  const utils = SpecialPowers.getDOMWindowUtils(win);
  isnot(properties.length, 0, "Sanity check");
  for (const prop of properties) {
    try {
      is(utils.isCssPropertyRecordedInUseCounter(prop), recorded,
         `${desc} - ${prop}`)
    } catch(ex) {
      ok(false, "Threw: " + prop);
    }
  }
}

// NOTE(emilio): This is no longer meaningful now we always record in the style
// system itself, which is what this tests. But we could conceivably change
// it so it doesn't hurt.
add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    "set": [
      ["layout.css.use-counters.enabled", true],
      ["layout.css.use-counters-unimplemented.enabled", true]
    ]
  });
});

// TODO(emilio): Make work (and test) inline style and maybe even CSSOM and
// such?
//
// Make sure that something on the lines of the following passes:
//
//   element.style.webkitTransform = "rotate(1deg)"
//   assert_recorded(true, ["-webkit-transform"]);
//   assert_recorded(false, ["transform"]);
//
const IMPLEMENTED_PROPERTIES = {
  description: "unimplemented properties",
  css: `
    * {
      grid-gap: 1px; /* shorthand alias */
      -webkit-background-size: 100px 100px; /* longhand alias */
      transform-origin: top left; /* longhand */
      background: green; /* shorthand */
    }
  `,
  recorded: [
    "grid-gap",
    "-webkit-background-size",
    "transform-origin",
    "background",
  ],
  // Should only record the aliases, not the non-aliased property.
  // Should only record shorthands, not the longhands it expands to.
  not_recorded: [
    "gap",
    "background-size",
    "-moz-transform-origin",
    "-webkit-transform-origin",
    "background-color",
  ],
};

const UNIMPLEMENTED_PROPERTIES = {
  description: "unimplemented properties",
  css: `
    * {
      grid-gap: 1px; /* shorthand alias */
      -webkit-background-size: 100px 100px; /* longhand alias */
      transform-origin: top left; /* longhand */
      background: green; /* shorthand */
      -webkit-font-smoothing: auto; /* counted unknown */
      zoom: 0.5; /* counted unknown */
    }
  `,
  recorded: [
    "grid-gap",
    "-webkit-background-size",
    "transform-origin",
    "background",
    "-webkit-font-smoothing",
    "zoom",
  ],
  not_recorded: [
    "size",
    "speak",
  ],
};

// Test on regular <style> elements.
add_task(async () => {
  for (let test of [IMPLEMENTED_PROPERTIES, UNIMPLEMENTED_PROPERTIES]) {
    await iframe_reload();

    const win = iframe.contentWindow;
    const style = document.createElement('style');
    style.textContent = test.css;

    iframe.contentDocument.body.appendChild(style);

    assert_recorded(win, true, test.recorded, `Test ${test.description} in <style> elements`);
    assert_recorded(win, false, test.not_recorded, `Test ${test.description} in <style> elements`);
  }
});

// Test on constructable stylesheets.
add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    "set": [
      ["layout.css.constructable-stylesheets.enabled", true]
    ]
  });

  for (let test of [IMPLEMENTED_PROPERTIES, UNIMPLEMENTED_PROPERTIES]) {
    for (let method of ["replace", "replaceSync"]) {
      await iframe_reload();
      const win = iframe.contentWindow;

      const sheet = new win.CSSStyleSheet();
      await sheet[method](test.css);

      assert_recorded(win, true, test.recorded, `Test ${test.description} in constructed sheet`);
      assert_recorded(win, false, test.not_recorded, `Test ${test.description} in constructed sheet`);
    }
  }
});

add_task(async () => {
  // Test for <link rel="stylesheet">. One iteration for the uncached version, one for the cached one.
  for (let test of [IMPLEMENTED_PROPERTIES, UNIMPLEMENTED_PROPERTIES]) {
    const uri = "data:text/css," + encodeURIComponent(test.css);
    for (let frame of [iframe, document.getElementById("second-iframe")]) {
      await iframe_reload(frame);
      const win = frame.contentWindow;
      const doc = frame.contentDocument;

      const link = doc.createElement("link");
      link.rel = "stylesheet";
      const linkLoaded = new Promise(resolve => {
        link.onload = resolve;
      });
      link.href = uri;
      doc.body.appendChild(link);
      await linkLoaded;
      assert_recorded(win, true, test.recorded, `Test ${test.description} in <link> ${frame.id}`);
      assert_recorded(win, false, test.not_recorded, `Test ${test.description} in <link> ${frame.id}`);
    }
  }
});

</script>
</body>