01b370823117dac843aa87329348e10af5185ab3
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / partitioned-and-unpartitioned-cookie-with-partitioning-timeout.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <title>Test for Partitioned and Unpartitioned Cookie With User Interaction</title>
6     <script src="../../resources/js-test-pre.js"></script>
7 </head>
8 <body>
9 <script>
10     const partitionHost = "127.0.0.1:8000";
11     const thirdPartyOrigin = "http://localhost:8000";
12     const thirdPartyBaseUrl = thirdPartyOrigin + "/resourceLoadStatistics/resources";
13     const firstPartyCookieName = "firstPartyCookie";
14     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
15     const thirdPartyCookieName = "thirdPartyCookie";
16     const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=value";
17     const fragmentWithReturnUrl = "http://" + partitionHost + "/resourceLoadStatistics/partitioned-and-unpartitioned-cookie-with-partitioning-timeout.html";
18     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + thirdPartyCookieName;
19
20     function setEnableFeature(enable) {
21         if (!enable)
22             testRunner.statisticsResetToConsistentState();
23         testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(enable);
24         internals.setResourceLoadStatisticsEnabled(enable);
25         testRunner.setCookieStoragePartitioningEnabled(enable);
26     }
27
28     function finishTest() {
29         setEnableFeature(false);
30         testRunner.notifyDone();
31     }
32
33     function openIframe(url, onLoadHandler) {
34         const element = document.createElement("iframe");
35         element.src = url;
36         if (onLoadHandler) {
37             element.onload = onLoadHandler;
38         }
39         document.body.appendChild(element);
40     }
41
42     function fireShouldPartitionCookiesHandlerAndContinue() {
43         testRunner.statisticsUpdateCookiePartitioning();
44         runTest();
45     }
46
47     function setShortCookiePartitioningTimeOutPlusFireShouldPartitionCookiesHandlerAndContinue() {
48         testRunner.setStatisticsTimeToLiveCookiePartitionFree(1);
49         setTimeout(fireShouldPartitionCookiesHandlerAndContinue, 2000);
50     }
51
52     function setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue() {
53         testRunner.setStatisticsTimeToLiveCookiePartitionFree(86400);
54         testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true);
55         if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
56             testFailed("Third party did not get logged for user interaction.");
57         runTest();
58     }
59
60     function runTest() {
61         switch (document.location.hash) {
62             case "#step1":
63                 // Set first-party cookie for http://localhost:8000
64                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
65                 break;
66             case "#step2":
67                 // Check that the first-party cookie is not sent in partition 127.0.0.1:8000
68                 document.location.hash = "step3";
69                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.",
70                     runTest);
71                 break;
72             case "#step3":
73                 // Set third-party cookie for http://localhost:8000 in partition 127.0.0.1:8000
74                 document.location.hash = "step4";
75                 openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting partitioned, third-party cookie.",
76                     runTest);
77                 break;
78             case "#step4":
79                 // Check that only partitioned cookie get sent in partition 127.0.0.1:8000
80                 document.location.hash = "step5";
81                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third-party cookie.",
82                     setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue);
83                 break;
84             case "#step5":
85                 // http://localhost:8000 now has user interaction, check that un-partitioned cookie is sent for third-party
86                 document.location.hash = "step6";
87                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After first user interaction, should only receive un-partitioned cookie.",
88                     setShortCookiePartitioningTimeOutPlusFireShouldPartitionCookiesHandlerAndContinue);
89                 break;
90             case "#step6":
91                 // Cookie partitioning should be applied again
92                 document.location.hash = "step7";
93                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third-party cookie.",
94                     setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue);
95                 break;
96             case "#step7":
97                 // http://localhost:8000 now has fresh user interaction, check that un-partitioned cookie is sent for third-party
98                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After second user interaction, should only receive un-partitioned cookie.",
99                     finishTest);
100                 break;
101         }
102     }
103
104     if (document.location.hash === "" && window.testRunner && window.internals) {
105         setEnableFeature(true);
106
107         // Set localhost as prevalent and with non-recent user interaction to put it in the partitioning category.
108         testRunner.setStatisticsPrevalentResource("http://localhost", true);
109         if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
110             testFailed("Host did not get set as prevalent resource.");
111         testRunner.setStatisticsHasHadNonRecentUserInteraction("http://localhost");
112
113         testRunner.statisticsUpdateCookiePartitioning();
114
115         testRunner.waitUntilDone();
116         testRunner.dumpChildFramesAsText();
117         document.location.hash = "step1";
118     }
119
120     runTest();
121 </script>
122 </body>
123 </html>