Move non-upstreamed Resource Timing tests out of web-platform-tests
[WebKit-https.git] / LayoutTests / http / wpt / resource-timing / rt-shared-resource-in-workers.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     let sharedResourceURL = uniqueImageURL("shared");
26
27     window.performance.clearResourceTimings();
28     promises.push(window.fetch(sharedResourceURL));
29
30     wait(); // Ensure a DOMHighResTimeStamp change.
31
32     worker1.postMessage({sharedResourceURL});
33     promises.push(new Promise(function(resolve, reject) {
34         worker1.addEventListener("message", function(event) {
35             worker1Entries = event.data;
36             resolve();
37         });
38     }));
39
40     worker2.postMessage({sharedResourceURL});
41     promises.push(new Promise(function(resolve, reject) {
42         worker2.addEventListener("message", function(event) {
43             worker2Entries = event.data;
44             resolve();
45         });
46     }));
47
48     Promise.all(promises).then(function() {
49         let supported = worker1Entries !== "error" && worker2Entries !== "error";
50         let windowEntries = window.performance.getEntriesByType("resource");
51
52         test(function() {
53             assert_true(supported);
54         }, "Workers have PerformanceObserver and PerformanceResourceTiming");
55
56         if (!supported) {
57             done();
58             return;
59         }
60
61         function checkContainsURL(url, list) {
62             for (let entry of list) {
63                 if (entry.name === url)
64                     return true;
65             }
66             return false;
67         }
68
69         function assertSharedEntryIsDifferent(sharedEntry, list) {
70             for (let entry of list) {
71                 if (entry.name === sharedResourceURL) {
72                     assert_not_equals(sharedEntry.startTime, entry.startTime, "shared entry should be different between contexts");
73                     return;
74                 }
75             }
76         }
77
78         test(function(t) {
79             assert_true(checkContainsURL(sharedResourceURL, windowEntries), "window should have an entry for the shared resource");
80             assert_equals(windowEntries.length, 1, "window should have 1 resource entry");
81             assertSharedEntryIsDifferent(windowEntries[0], worker1Entries);
82             assertSharedEntryIsDifferent(windowEntries[0], worker2Entries);
83         }, "window has shared entry");
84
85         test(function(t) {
86             assert_true(checkContainsURL(sharedResourceURL, worker1Entries), "worker1 should have an entry for the shared resource");
87             assert_equals(worker1Entries.length, 1, "worker1 should have 1 resource entry");
88         }, "worker1 has shared entry");
89
90         test(function(t) {
91             assert_true(checkContainsURL(sharedResourceURL, worker2Entries), "worker2 should have an entry for the shared resource");
92             assert_equals(worker2Entries.length, 1, "worker2 should have 1 resource entry");
93         }, "worker2 has shared entry");
94
95         done();
96     });
97 });
98 </script>
99 </body>
100 </html>