Add completion handlers to TestRunner functions setStatisticsLastSeen(), setStatistic...
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / prune-statistics.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <meta charset="UTF-8">
5     <title>Test for Resource Load Statistics Pruning</title>
6     <script src="/js-test-resources/js-test.js"></script>
7 </head>
8 <body>
9 <script>
10     description("Tests that statistics are pruned in the right order.");
11     jsTestIsAsync = true;
12
13     const olderTimestamp = Math.round((new Date()).getTime() / 1000);
14     const newerTimestamp = olderTimestamp + 10;
15     const newestTimestamp = newerTimestamp + 10;
16
17     const urlsToBePruned = [
18         { url: "http://127.0.0.1:8000/temp", prevalent: false },
19         { url: "http://127.0.0.2:8000/temp", prevalent: false },
20         { url: "http://127.0.0.3:8000/temp", prevalent: true },
21         { url: "http://127.0.0.4:8000/temp", prevalent: true },
22         { url: "http://127.0.0.5:8000/temp", prevalent: false },
23         { url: "http://127.0.0.6:8000/temp", prevalent: false },
24         { url: "http://127.0.0.7:8000/temp", prevalent: true },
25         { url: "http://127.0.0.8:8000/temp", prevalent: true }
26     ];
27
28     function checkIfPrevalentAccordingToInitialExpectation(begin, end) {
29         var failed = false;
30         for (var i = begin; i < end; ++i) {
31             if (testRunner.isStatisticsPrevalentResource(urlsToBePruned[i].url) !== urlsToBePruned[i].prevalent) {
32                 testFailed("checkIfPrevalentAccordingToInitialExpectation: Test iteration " + currentTest + " failed. " + urlsToBePruned[i].url + (urlsToBePruned[i].prevalent ? " wasn't " : " was ") + "prevalent");
33                 failed = true;
34             }
35         }
36         if (failed) {
37             testRunner.statisticsResetToConsistentState(function() {
38                 finishJSTest();
39             });
40         }
41     }
42
43     function checkIfPrevalent(begin, end, expected) {
44         var failed = false;
45         for (var i = begin; i < end; ++i) {
46             if (testRunner.isStatisticsPrevalentResource(urlsToBePruned[i].url) !== expected) {
47                 testFailed("checkIfPrevalent: Test iteration " + currentTest + " failed. " + urlsToBePruned[i].url + (expected ? " wasn't " : " was ") + "prevalent");
48                 failed = true;
49             }
50         }
51         if (failed) {
52             testRunner.statisticsResetToConsistentState(function() {
53                 finishJSTest();
54             });
55         }
56     }
57
58     function initializeStatisticsAndRunTests(step) {
59         switch (step) {
60             // Non-prevalent without user interaction to be pruned first.
61             case 1:
62                 testRunner.setStatisticsLastSeen(urlsToBePruned[0].url, olderTimestamp, function() { initializeStatisticsAndRunTests(2); });
63                 break;
64             case 2:
65                 testRunner.setStatisticsLastSeen(urlsToBePruned[1].url, newerTimestamp, function() { initializeStatisticsAndRunTests(3); });
66                 break;
67             // Prevalent without user interaction to be pruned second.
68             case 3:
69                 testRunner.setStatisticsPrevalentResource(urlsToBePruned[2].url, true, function() { initializeStatisticsAndRunTests(4); });
70                 break;
71             case 4:
72                 testRunner.setStatisticsLastSeen(urlsToBePruned[2].url, olderTimestamp, function() { initializeStatisticsAndRunTests(5); });
73                 break;
74             case 5:
75                 testRunner.setStatisticsPrevalentResource(urlsToBePruned[3].url, true, function() { initializeStatisticsAndRunTests(6); });
76                 break;
77             case 6:
78                 testRunner.setStatisticsLastSeen(urlsToBePruned[3].url, newerTimestamp, function() { initializeStatisticsAndRunTests(7); });
79                 break;
80             // Non-prevalent with user interaction to be pruned third.
81             case 7:
82                 testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[4].url, true, function() { initializeStatisticsAndRunTests(8); });
83                 break;
84             case 8:
85                 testRunner.setStatisticsLastSeen(urlsToBePruned[4].url, olderTimestamp, function() { initializeStatisticsAndRunTests(9); });
86                 break;
87             case 9:
88                 testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[5].url, true, function() { initializeStatisticsAndRunTests(10); });
89                 break;
90             case 10:
91                 testRunner.setStatisticsLastSeen(urlsToBePruned[5].url, newerTimestamp, function() { initializeStatisticsAndRunTests(11); });
92                 break;
93             // Prevalent with user interaction to be pruned last.
94             case 11:
95                 testRunner.setStatisticsPrevalentResource(urlsToBePruned[6].url, true, function() { initializeStatisticsAndRunTests(12); });
96                 break;
97             case 12:
98                 testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[6].url, true, function() { initializeStatisticsAndRunTests(13); });
99                 break;
100             case 13:
101                 testRunner.setStatisticsLastSeen(urlsToBePruned[6].url, olderTimestamp, function() { initializeStatisticsAndRunTests(14); });
102                 break;
103             case 14:
104                 testRunner.setStatisticsPrevalentResource(urlsToBePruned[7].url, true, function() { initializeStatisticsAndRunTests(15); });
105                 break;
106             case 15:
107                 testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[7].url, true, function() { initializeStatisticsAndRunTests(16); });
108                 break;
109             case 16:
110                 testRunner.setStatisticsLastSeen(urlsToBePruned[7].url, newerTimestamp, function() { initializeStatisticsAndRunTests(17); });
111                 break;
112             case 17:
113                 checkIfPrevalentAccordingToInitialExpectation(0, urlsToBePruned.length);
114                 runTest();
115                 break;
116         }
117     }
118
119     var currentTest;
120     function checkStatisticsAfterPruning() {
121         // Pruned entries should not be prevalent.
122         checkIfPrevalent(0, currentTest, false);
123         // Non-pruned entries should keep their expected status.
124         checkIfPrevalentAccordingToInitialExpectation(currentTest, urlsToBePruned.length);
125         testPassed("Test iteration " + currentTest + " passed.");
126         if (currentTest < urlsToBePruned.length) {
127             ++currentTest;
128             testRunner.installStatisticsDidScanDataRecordsCallback(checkStatisticsAfterPruning);
129             initializeStatisticsAndRunTests(1);
130         } else {
131             testRunner.statisticsResetToConsistentState(function() {
132                 finishJSTest();
133             });
134         }
135     }
136
137     let fillerUrl;
138     function runTest() {
139         fillerUrl = "http://127.0." + currentTest + ".1:8000/temp";
140         testRunner.setStatisticsPrevalentResource(fillerUrl, true, function() {
141             testRunner.setStatisticsHasHadUserInteraction(fillerUrl, true, function() {
142                 testRunner.setStatisticsLastSeen(fillerUrl, newestTimestamp, function() {
143                     testRunner.statisticsProcessStatisticsAndDataRecords();
144                 });
145             });
146         });
147     }
148
149     if (window.testRunner) {
150         testRunner.setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(false);
151         testRunner.setStatisticsMinimumTimeBetweenDataRecordsRemoval(0);
152         testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(true);
153         testRunner.setStatisticsMaxStatisticsEntries(urlsToBePruned.length);
154         testRunner.setStatisticsPruneEntriesDownTo(urlsToBePruned.length);
155
156         currentTest = 1;
157         testRunner.installStatisticsDidScanDataRecordsCallback(checkStatisticsAfterPruning);
158         initializeStatisticsAndRunTests(1);
159     }
160 </script>
161 </body>
162 </html>