Move non-upstreamed Resource Timing tests out of web-platform-tests
[WebKit-https.git] / LayoutTests / http / wpt / resource-timing / rt-resources-per-worker.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Resource Timing - Entries per Worker</title>
6 <link rel="help" href="https://w3c.github.io/resource-timing/#processing-model">
7 <script src="/resources/testharness.js"></script>
8 <script src="/resources/testharnessreport.js"></script>
9 <script src="resources/rt-utilities.sub.js"></script>
10 </head>
11 <body>
12 <h1>Resource Timing: Entries per Worker</h1>
13 <div id="log"></div>
14 <script>
15 setup({explicit_done: true});
16
17 window.addEventListener("load", function() {
18     let worker1 = new Worker("resources/rt-worker-resources.js");
19     let worker2 = new Worker("resources/rt-worker-resources.js");
20
21     let worker1Entries = null;
22     let worker2Entries = null;
23
24     let promises = [];
25
26     window.performance.clearResourceTimings();
27     promises.push(window.loadResources(3));
28
29     wait(); // Ensure a DOMHighResTimeStamp change.
30
31     worker1.postMessage({n: 2});
32     promises.push(new Promise(function(resolve, reject) {
33         worker1.addEventListener("message", function(event) {
34             worker1Entries = event.data;
35             resolve();
36         });
37     }));
38
39     wait(); // Ensure a DOMHighResTimeStamp change.
40
41     worker2.postMessage({n: 15});
42     promises.push(new Promise(function(resolve, reject) {
43         worker2.addEventListener("message", function(event) {
44             worker2Entries = event.data;
45             resolve();
46         });
47     }));
48
49     Promise.all(promises).then(function() {
50         let supported = worker1Entries !== "error" && worker2Entries !== "error";
51
52         // Exclude the Worker script if it was in the list since it may have been added in the meantime.
53         let windowEntries = window.performance.getEntriesByType("resource");
54         windowEntries = windowEntries.filter((entry) => !/rt-worker-resources\.js$/.test(entry.name));
55
56         test(function() {
57             assert_true(supported);
58         }, "Workers have PerformanceObserver and PerformanceResourceTiming");
59
60         if (!supported) {
61             done();
62             return;
63         }
64
65         function checkContainsURL(url, list) {
66             for (let entry of list) {
67                 if (entry.name === url)
68                     return true;
69             }
70             return false;
71         }
72
73         function assertDisjointEntries(list, otherList1, otherList2) {
74             for (let entry of list) {
75                 assert_false(checkContainsURL(entry.name, otherList1), "a resource that should have been unique was not unique across contexts");
76                 assert_false(checkContainsURL(entry.name, otherList2), "a resource that should have been unique was not unique across contexts");
77             }
78         }
79
80         test(function(t) {
81             assert_equals(windowEntries.length, 3, "window should have 3 resource entries");
82             assertDisjointEntries(windowEntries, worker1Entries, worker2Entries);
83         }, "window resources differ from other contexts");
84
85         test(function(t) {
86             assert_equals(worker1Entries.length, 2, "worker1 should have 2 resource entries");
87             assertDisjointEntries(worker1Entries, windowEntries, worker2Entries);
88         }, "worker1 resources differ from other contexts");
89
90         test(function(t) {
91             assert_equals(worker2Entries.length, 15, "worker2 should have 15 resource entries");
92             assertDisjointEntries(worker2Entries, worker1Entries, windowEntries);
93         }, "worker2 resources differ from other contexts");
94
95         done();
96     });
97 });
98 </script>
99 </body>
100 </html>