• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict"
2// https://w3c.github.io/hr-time/#time-origin
3
4async_test(function(test) {
5    // Cache global time before starting worker
6    const globalTimeOrigin = performance.timeOrigin;
7    const globalNowBeforeWorkerStart = performance.now();
8
9    // Start worker and retrieve time
10    const workerScript = "postMessage({timeOrigin: performance.timeOrigin, now: performance.now()})";
11    const blob = new Blob([workerScript]);
12    let worker = new Worker(URL.createObjectURL(blob));
13
14    worker.addEventListener("message", test.step_func_done(function(event) {
15        const workerTimeOrigin = event.data.timeOrigin;
16        const workerNow = event.data.now;
17
18        assert_not_equals(workerTimeOrigin, 0, "worker timeOrigin must not be 0");
19        assert_not_equals(performance.timeOrigin, 0, "Document timeOrigin must not be 0");
20
21        assert_equals(globalTimeOrigin, performance.timeOrigin, "timeOrigin should not be changed in same document mode");
22        assert_less_than(globalTimeOrigin, workerTimeOrigin, "Document timeOrigin must be earlier than worker timeOrigin");
23
24        // Document and worker's now() start from their respective timeOrigins.
25        const timeDiff = workerTimeOrigin - globalTimeOrigin; // convert worker's time to Document time.
26        assert_less_than(globalTimeOrigin + globalNowBeforeWorkerStart, globalTimeOrigin + timeDiff + workerNow, "Document old now is earlier than worker now.");
27
28        // Comparing timing between Document and worker threads could be delicate as it relies on the thread implementation and could be subject to race conditions.
29    }));
30}, 'timeOrigin and now() should be correctly ordered between window and worker');
31