Add completion handlers to TestRunner functions setStatisticsLastSeen(), setStatistic...
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / partitioned-and-unpartitioned-cookie-deletion.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("Test for partitioned and unpartitioned cookie deletion.");
10     jsTestIsAsync = true;
11
12     const partitionHost = "127.0.0.1:8000";
13     const thirdPartyOrigin = "http://localhost:8000";
14     const thirdPartyBaseUrl = thirdPartyOrigin + "/resourceLoadStatistics/resources";
15     const firstPartyCookieName = "firstPartyCookie";
16     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
17     const thirdPartyCookieName = "thirdPartyCookie";
18     const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=value";
19     const fragmentWithReturnUrl = "http://" + partitionHost + "/resourceLoadStatistics/partitioned-and-unpartitioned-cookie-deletion.html";
20     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + thirdPartyCookieName;
21
22     function setEnableFeature(enable, completionHandler) {
23         internals.setResourceLoadStatisticsEnabled(enable);
24         testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(enable);
25         testRunner.setCookieStoragePartitioningEnabled(enable);
26         if (!enable)
27             testRunner.statisticsResetToConsistentState(completionHandler);
28         else
29             completionHandler();
30     }
31
32     function finishTest() {
33         setEnableFeature(false, finishJSTest);
34     }
35
36     function openIframe(url, onLoadHandler) {
37         const element = document.createElement("iframe");
38         element.src = url;
39         if (onLoadHandler) {
40             element.onload = onLoadHandler;
41         }
42         document.body.appendChild(element);
43     }
44
45
46     function fireDataModificationHandlerAndContinue() {
47         testRunner.installStatisticsDidScanDataRecordsCallback(function() {
48             setTimeout(runTest, 500);
49         });
50         testRunner.statisticsProcessStatisticsAndDataRecords();
51     }
52
53     function setUserInteractionAndContinue() {
54         testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true, function() {
55             if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
56                 testFailed("Third party did not get logged for user interaction.");
57             runTest();
58         });
59     }
60
61     function runTest() {
62         switch (document.location.hash) {
63             case "#step1":
64                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
65                 break;
66             case "#step2":
67                 document.location.hash = "step3";
68                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.", runTest);
69                 break;
70             case "#step3":
71                 document.location.hash = "step4";
72                 openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting partitioned, third party cookie.", runTest);
73                 break;
74             case "#step4":
75                 document.location.hash = "step5";
76                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third party cookie.", fireDataModificationHandlerAndContinue);
77                 break;
78             case "#step5":
79                 document.location.hash = "step6";
80                 setTimeout(runTest, 10);
81                 break;
82             case "#step6":
83                 document.location.hash = "step7";
84                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After removal, should receive no cookies.", setUserInteractionAndContinue);
85                 break;
86             case "#step7":
87                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should receive no cookies.", finishTest);
88                 break;
89         }
90     }
91
92     if (document.location.host === partitionHost && document.location.hash === "" && window.testRunner && window.internals) {
93         setEnableFeature(true, function() {
94             testRunner.setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(false);
95             testRunner.setStatisticsMinimumTimeBetweenDataRecordsRemoval(0);
96
97             testRunner.dumpChildFramesAsText();
98             document.location.hash = "step1";
99
100             testRunner.setStatisticsPrevalentResource("http://localhost", true, function() {
101                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
102                     testFailed("Host did not get set as prevalent resource.");
103
104                 testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", true, runTest);
105             });
106         });
107     } else {
108         runTest();
109     }
110
111 </script>
112 </body>
113 </html>