Add completion handlers to TestRunner functions setStatisticsLastSeen(), setStatistic...
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / classify-as-prevalent-based-on-top-frame-unique-redirects-to.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 unique redirects to.");
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     const topFrameOrigin2 = "http://127.0.0.3:8000/temp";
16     const topFrameOrigin3 = "http://127.0.0.4:8000/temp";
17     const topFrameOrigin4 = "http://127.0.0.5:8000/temp";
18
19     function setEnableFeature(enable, completionHandler) {
20         internals.setResourceLoadStatisticsEnabled(enable);
21         testRunner.setCookieStoragePartitioningEnabled(enable);
22         if (!enable)
23             testRunner.statisticsResetToConsistentState(completionHandler);
24         else
25             completionHandler();
26     }
27
28     function completeTest() {
29         if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
30             testFailed("Host did not get classified as prevalent resource.");
31         if (testRunner.isStatisticsVeryPrevalentResource(statisticsUrl))
32             testFailed("Host got classified as very prevalent resource.");
33         else
34             testPassed("Host classified as prevalent resource.");
35
36         setEnableFeature(false, finishJSTest);
37     }
38
39     function runTest() {
40         testRunner.setStatisticsTopFrameUniqueRedirectTo(statisticsUrl, topFrameOrigin1);
41         testRunner.setStatisticsTopFrameUniqueRedirectTo(statisticsUrl, topFrameOrigin2);
42         testRunner.setStatisticsTopFrameUniqueRedirectTo(statisticsUrl, topFrameOrigin3);
43         testRunner.setStatisticsTopFrameUniqueRedirectTo(statisticsUrl, topFrameOrigin4);
44
45         testRunner.installStatisticsDidScanDataRecordsCallback(completeTest);
46
47         testRunner.statisticsProcessStatisticsAndDataRecords();
48     }
49
50     if (document.location.host === hostUnderTest && window.testRunner && window.internals) {
51         setEnableFeature(true, function() {
52             testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(true);
53
54             testRunner.setStatisticsPrevalentResource(statisticsUrl, false, function() {
55                 if (testRunner.isStatisticsPrevalentResource(statisticsUrl))
56                     testFailed("Host did not get set as non-prevalent resource.");
57                 runTest();
58             });
59         });
60     }
61 </script>
62 </body>
63 </html>