summaryrefslogtreecommitdiffstats
path: root/layout/base/tests/browser_stylesheet_change_events.js
blob: c86719705070d17344719732d62d5024f652464e (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
const gTestRoot = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  "http://127.0.0.1:8888/"
);

add_task(async function test() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: gTestRoot + "file_stylesheet_change_events.html" },
    async function (browser) {
      await SpecialPowers.spawn(
        browser,
        [gTestRoot],
        testApplicableStateChangeEvent
      );
    }
  );
});

// This function runs entirely in the content process. It doesn't have access
// any free variables in this file.
async function testApplicableStateChangeEvent(testRoot) {
  // We've seen the original stylesheet in the document.
  // Now add a stylesheet on the fly and make sure we see it.
  let doc = content.document;
  doc.styleSheetChangeEventsEnabled = true;

  const unexpectedContentEvent = event =>
    ok(false, "Received a " + event.type + " event on content");
  doc.addEventListener(
    "StyleSheetApplicableStateChanged",
    unexpectedContentEvent
  );
  doc.defaultView.addEventListener(
    "StyleSheetApplicableStateChanged",
    unexpectedContentEvent
  );
  doc.addEventListener("StyleSheetRemoved", unexpectedContentEvent);
  doc.defaultView.addEventListener("StyleSheetRemoved", unexpectedContentEvent);

  function shouldIgnoreEvent(e) {
    // accessiblecaret.css might be reported, interfering with the test
    // assertions, so let's ignore it
    return (
      e.stylesheet?.href === "resource://content-accessible/accessiblecaret.css"
    );
  }

  function waitForStyleApplicableStateChanged() {
    return ContentTaskUtils.waitForEvent(
      docShell.chromeEventHandler,
      "StyleSheetApplicableStateChanged",
      true,
      e => !shouldIgnoreEvent(e)
    );
  }

  function waitForStyleSheetRemovedEvent() {
    return ContentTaskUtils.waitForEvent(
      docShell.chromeEventHandler,
      "StyleSheetRemoved",
      true,
      e => !shouldIgnoreEvent(e)
    );
  }

  function checkApplicableStateChangeEvent(event, { applicable, stylesheet }) {
    is(
      event.type,
      "StyleSheetApplicableStateChanged",
      "event.type has expected value"
    );
    is(event.target, doc, "event targets correct document");
    is(event.stylesheet, stylesheet, "event.stylesheet has the expected value");
    is(event.applicable, applicable, "event.applicable has the expected value");
  }

  function checkStyleSheetRemovedEvent(event, { stylesheet }) {
    is(event.type, "StyleSheetRemoved", "event.type has expected value");
    is(event.target, doc, "event targets correct document");
    is(event.stylesheet, stylesheet, "event.stylesheet has the expected value");
  }

  // Updating the text content will actually create a new StyleSheet instance,
  // and so we should get one event for the new instance, and another one for
  // the removal of the "previous"one.
  function waitForTextContentChange() {
    return Promise.all([
      waitForStyleSheetRemovedEvent(),
      waitForStyleApplicableStateChanged(),
    ]);
  }

  let stateChanged, evt;

  {
    const gStyleSheet = "stylesheet_change_events.css";

    info("Add <link> and wait for applicable state change event");
    let linkEl = doc.createElement("link");
    linkEl.setAttribute("rel", "stylesheet");
    linkEl.setAttribute("type", "text/css");
    linkEl.setAttribute("href", testRoot + gStyleSheet);

    stateChanged = waitForStyleApplicableStateChanged();
    doc.body.appendChild(linkEl);
    evt = await stateChanged;

    ok(true, "received dynamic style sheet applicable state change event");
    checkApplicableStateChangeEvent(evt, {
      stylesheet: linkEl.sheet,
      applicable: true,
    });

    stateChanged = waitForStyleApplicableStateChanged();
    linkEl.sheet.disabled = true;
    evt = await stateChanged;

    ok(true, "received dynamic style sheet applicable state change event");
    checkApplicableStateChangeEvent(evt, {
      stylesheet: linkEl.sheet,
      applicable: false,
    });

    info("Remove stylesheet and wait for removed event");
    const removedStylesheet = linkEl.sheet;
    const onStyleSheetRemoved = waitForStyleSheetRemovedEvent();
    doc.body.removeChild(linkEl);
    const removedStyleSheetEvt = await onStyleSheetRemoved;

    ok(true, "received removed sheet event");
    checkStyleSheetRemovedEvent(removedStyleSheetEvt, {
      stylesheet: removedStylesheet,
    });
  }

  {
    info("Add <style> node and wait for applicable state changed event");
    let styleEl = doc.createElement("style");
    styleEl.textContent = `body { background: tomato; }`;

    stateChanged = waitForStyleApplicableStateChanged();
    doc.head.appendChild(styleEl);
    evt = await stateChanged;

    ok(true, "received dynamic style sheet applicable state change event");
    checkApplicableStateChangeEvent(evt, {
      stylesheet: styleEl.sheet,
      applicable: true,
    });

    info("Updating <style> text content");
    stateChanged = waitForTextContentChange();
    const inlineStyleSheetBeforeChange = styleEl.sheet;

    styleEl.textContent = `body { background: gold; }`;
    const [inlineRemovedEvt, inlineAddedEvt] = await stateChanged;

    ok(true, "received expected style sheet events");
    checkStyleSheetRemovedEvent(inlineRemovedEvt, {
      stylesheet: inlineStyleSheetBeforeChange,
    });
    checkApplicableStateChangeEvent(inlineAddedEvt, {
      stylesheet: styleEl.sheet,
      applicable: true,
    });

    info("Remove stylesheet and wait for removed event");
    const onStyleSheetRemoved = waitForStyleSheetRemovedEvent();

    const removedInlineStylesheet = styleEl.sheet;
    styleEl.remove();
    const removedStyleSheetEvt = await onStyleSheetRemoved;

    ok(true, "received removed style sheet event");
    checkStyleSheetRemovedEvent(removedStyleSheetEvt, {
      stylesheet: removedInlineStylesheet,
    });
  }

  {
    info(
      "Create a custom element and check we get an event for its stylesheet"
    );
    stateChanged = waitForStyleApplicableStateChanged();
    const el = doc.createElement("div");
    const shadowRoot = el.attachShadow({ mode: "open" });
    doc.body.appendChild(el);
    shadowRoot.innerHTML = `
        <span>custom</span>
        <style>
          span { color: salmon; }
        </style>`;
    evt = await stateChanged;

    ok(true, "received dynamic style sheet applicable state change event");
    const shadowStyleEl = shadowRoot.querySelector("style");
    checkApplicableStateChangeEvent(evt, {
      stylesheet: shadowStyleEl.sheet,
      applicable: true,
    });

    info("Updating <style> text content");
    stateChanged = waitForTextContentChange();
    const styleSheetBeforeChange = shadowStyleEl.sheet;
    shadowStyleEl.textContent = `span { color: cyan; }`;
    const [removedEvt, addedEvt] = await stateChanged;

    ok(true, "received expected style sheet events");
    checkStyleSheetRemovedEvent(removedEvt, {
      stylesheet: styleSheetBeforeChange,
    });
    checkApplicableStateChangeEvent(addedEvt, {
      stylesheet: shadowStyleEl.sheet,
      applicable: true,
    });

    info("Remove stylesheet and wait for removed event");
    const onStyleSheetRemoved = waitForStyleSheetRemovedEvent();
    const removedShadowStylesheet = shadowStyleEl.sheet;
    shadowStyleEl.remove();
    const removedStyleSheetEvt = await onStyleSheetRemoved;
    ok(true, "received removed style sheet event");
    checkStyleSheetRemovedEvent(removedStyleSheetEvt, {
      stylesheet: removedShadowStylesheet,
    });
  }
}