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