Add completion handlers to TestRunner functions setStatisticsLastSeen(), setStatistic...
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / classify-as-prevalent-based-on-top-frame-redirect-to-prevalent.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <script src="/js-test-resources/js-test.js"></script>
6 </head>
7 <body>
8 <script>
9     description("Tests for classification based on top frame redirect to other prevalent.");
10     jsTestIsAsync = true;
11
12     const hostUnderTest = "127.0.0.1:8000";
13     const statisticsUrl = "http://" + hostUnderTest + "/temp";
14     const topFrameOrigin1 = "http://127.0.0.2:8000/temp";
15
16     function setEnableFeature(enable, completionHandler) {
17         internals.setResourceLoadStatisticsEnabled(enable);
18         testRunner.setCookieStoragePartitioningEnabled(enable);
19         if (!enable)
20             testRunner.statisticsResetToConsistentState(completionHandler);
21         else
22             completionHandler();
23     }
24
25     function completeTest() {
26         if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
27             testFailed("Host did not get classified as prevalent resource.");
28         else if (testRunner.isStatisticsVeryPrevalentResource(statisticsUrl))
29             testFailed("Host got classified as very prevalent resource.");
30         else
31             testPassed("Host classified as prevalent resource.");
32
33         setEnableFeature(false, finishJSTest);
34     }
35
36     function runTest() {
37         testRunner.setStatisticsPrevalentResource(topFrameOrigin1, true, function() {
38             testRunner.setStatisticsTopFrameUniqueRedirectTo(statisticsUrl, topFrameOrigin1);
39
40             testRunner.installStatisticsDidScanDataRecordsCallback(completeTest);
41             testRunner.statisticsProcessStatisticsAndDataRecords();
42         });
43     }
44
45     if (document.location.host === hostUnderTest && window.testRunner && window.internals) {
46         setEnableFeature(true, function() {
47             testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(true);
48
49             testRunner.setStatisticsPrevalentResource(statisticsUrl, false, function() {
50                 if (testRunner.isStatisticsPrevalentResource(statisticsUrl))
51                     testFailed("Host did not get set as non-prevalent resource.");
52                 runTest();
53             });
54         });
55     }
56 </script>
57 </body>
58 </html>