File: head.js

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (262 lines) | stat: -rw-r--r-- 7,226 bytes parent folder | download
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
async function newFocusedWindow(trigger, isInitialBlank = false) {
  let winPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
  let delayedStartupPromise = BrowserTestUtils.waitForNewWindow();

  await trigger();

  let win = await winPromise;
  // New windows get focused after the first paint, see bug 1262946,
  // but this is racy for the initial about:blank
  if (!isInitialBlank) {
    await BrowserTestUtils.waitForContentEvent(
      win.gBrowser.selectedBrowser,
      "MozAfterPaint"
    );
  } else {
    await new Promise(res =>
      win.requestAnimationFrame(() => win.requestAnimationFrame(res))
    );
  }
  await delayedStartupPromise;
  return win;
}

const JS_CACHE_BASE_URL = "http://mochi.test:8888/browser/dom/base/test/";

function ev(event, file, hasElement = !!file) {
  return {
    event,
    url: file ? JS_CACHE_BASE_URL + file : undefined,
    hasElement,
  };
}

function unordered(list) {
  return {
    unordered: list,
  };
}

function optional_ev(...args) {
  const event = ev(...args);
  event.optional = true;
  return event;
}

async function jsCacheContentTask(test, item) {
  const defaultSkippedEvents = test.skippedEvents ?? [
    // The compilation is not target of this test.
    "compile:main thread",
    // This is triggered by 'invalidateMemory' below, but we don't have to
    // track it.
    "memorycache:invalidate",
    // The disk cache handling for the in-memory cache can happen multiple
    // times depending on the scheduling and speed
    // (e.g. debug vs opt, verify mode).
    "diskcache:noschedule",
  ];
  const nonSkippedEvents = test.nonSkippedEvents ?? [];
  const skippedEvents = defaultSkippedEvents.filter(
    ev => !nonSkippedEvents.includes(ev)
  );

  function match(param, event) {
    if (event.event !== param.event) {
      return false;
    }

    if (param.url && event.url !== param.url) {
      return false;
    }

    if (event.hasElement) {
      if (param.id !== "watchme") {
        return false;
      }
    } else {
      if (param.id) {
        return false;
      }
    }

    return true;
  }

  function consumeIfMatched(param, events) {
    while ("optional" in events[0]) {
      if (match(param, events[0])) {
        events.shift();
        return true;
      }
      dump("@@@ Skip optional event: " + events[0].event + "\n");
      events.shift();
    }

    if ("unordered" in events[0]) {
      const unordered = events[0].unordered;
      for (let i = 0; i < unordered.length; i++) {
        if (match(param, unordered[i])) {
          unordered.splice(i, 1);
          if (!unordered.length) {
            events.shift();
          }
          return true;
        }
      }

      return false;
    }

    if (match(param, events[0])) {
      events.shift();
      return true;
    }

    return false;
  }

  let result = true;

  const { promise, resolve, reject } = Promise.withResolvers();
  const observer = function (subject, topic, data) {
    const param = {};
    for (const line of data.split("\n")) {
      const m = line.match(/^([^:]+):(.*)/);
      param[m[1]] = m[2];
    }

    if (consumeIfMatched(param, item.events)) {
      dump("@@@ Got expected event: " + data + "\n");
      if (item.events.length === 0) {
        resolve();
      }
    } else if (skippedEvents.includes(param.event)) {
      dump("@@@ Ignoring: " + data + "\n");
    } else {
      dump("@@@ Got unexpected event: " + data + "\n");
      dump("@@@ Expected: " + JSON.stringify(item.events[0]) + "\n");
      result = false;
    }
  };
  Services.obs.addObserver(observer, "ScriptLoaderTest");

  const script = content.document.createElement("script");
  script.id = "watchme";
  if (test.module || item.module) {
    script.type = "module";
  }
  if (item.sri) {
    script.integrity = item.sri;
  }
  script.src = item.file;
  content.document.body.appendChild(script);

  await promise;

  Services.obs.removeObserver(observer, "ScriptLoaderTest");

  return result;
}

async function runJSCacheTests(tests) {
  await BrowserTestUtils.withNewTab(
    JS_CACHE_BASE_URL + "empty.html",
    async browser => {
      const tab = gBrowser.getTabForBrowser(browser);

      for (const test of tests) {
        ChromeUtils.clearResourceCache();
        Services.cache2.clear();

        if (test.useServiceWorker) {
          await SpecialPowers.spawn(browser, [], async () => {
            const registration = await content.navigator.serviceWorker.register(
              "file_js_cache_sw.js",
              { scope: "./" }
            );

            const sw = registration.installing || registration.active;

            await new Promise(resolve => {
              function onStateChange() {
                if (sw.state === "activated") {
                  sw.removeEventListener("statechange", onStateChange);
                  resolve();
                }
              }
              sw.addEventListener("statechange", onStateChange);
              onStateChange();
            });
          });
        }

        for (let i = 0; i < test.items.length; i++) {
          const item = test.items[i];
          info(`start: ${test.title} (item ${i})`);

          if (!test.skipReload) {
            // Make sure the test starts in clean document.
            await BrowserTestUtils.reloadTab(tab);
          }

          if (item.clearMemory) {
            info("clear memory cache");
            ChromeUtils.clearResourceCache();
          }
          if (item.invalidateMemory) {
            info("invalidate memory cache");
            ChromeUtils.invalidateResourceCache();
          }
          if (item.clearDisk) {
            info("clear disk cache");
            Services.cache2.clear();
          }
          if (item.memoryPressureLowMemory) {
            info("notifying memory pressure low-memory");
            await SpecialPowers.spawn(browser, [], () => {
              Services.obs.notifyObservers(
                null,
                "memory-pressure",
                "low-memory"
              );
            });
          }
          if (item.memoryPressureHeapMinimize) {
            info("notifying memory pressure heap-minimize");
            await SpecialPowers.spawn(browser, [], () => {
              Services.obs.notifyObservers(
                null,
                "memory-pressure",
                "heap-minimize"
              );
            });
          }
          if (item.memoryPressureStop) {
            info("notifying memory pressure stop");
            await SpecialPowers.spawn(browser, [], () => {
              Services.obs.notifyObservers(null, "memory-pressure-stop");
            });
          }
          const result = await SpecialPowers.spawn(
            browser,
            [test, item],
            jsCacheContentTask
          );
          ok(result, "Received expected events");
        }

        if (test.useServiceWorker) {
          await SpecialPowers.spawn(browser, [], async () => {
            const registration =
              await content.navigator.serviceWorker.getRegistration();
            registration.unregister();
          });
        }

        ok(true, "end: " + test.title);
      }
    }
  );

  ok(true, "Finished all tests");
}