Many resourceLoadStatistics tests and storageAccess tests using the ITP
[WebKit-https.git] / LayoutTests / http / tests / storageAccess / request-and-grant-access-then-navigate-cross-site-should-not-have-access-database.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <script src="/js-test-resources/js-test.js"></script>
5     <script src="/js-test-resources/ui-helper.js"></script>
6     <script src="/resourceLoadStatistics/resources/util.js"></script>
7     <script>
8         description("Tests that a cross-origin iframe from a prevalent domain that is granted storage access and then navigates itself cross-site does not have storage access.");
9         jsTestIsAsync = true;
10
11         window.addEventListener("message", receiveMessage, false);
12
13         function finishTest() {
14             setEnableFeature(false, finishJSTest);
15         }
16
17         const expectedPassMessages = 2;
18         var passMessagesReceived = 0;
19         function receiveMessage(event) {
20             if (event.origin === "http://localhost:8000") {
21                 if (event.data.indexOf("document.cookie") === 0) {
22                     testPassed(event.data);
23                 } else if (event.data.indexOf("PASS") !== -1) {
24                     testPassed(event.data.replace("PASS ", ""));
25                     passMessagesReceived++;
26                     if (passMessagesReceived >= expectedPassMessages)
27                         finishTest();
28                 } else {
29                     testFailed(event.data);
30                     finishTest();
31                 }
32             } else {
33                 testFailed("Received a message from an unexpected origin: " + event.origin);
34                 finishTest();
35             }
36         }
37
38         function activateElement(elementId) {
39             var element = document.getElementById(elementId);
40             var centerX = element.offsetLeft + element.offsetWidth / 2;
41             var centerY = element.offsetTop + element.offsetHeight / 2;
42             UIHelper.activateAt(centerX, centerY).then(
43                 function () {
44                     if (window.eventSender)
45                         eventSender.keyDown("escape");
46                     else {
47                         testFailed("No eventSender.");
48                         finishTest();
49                     }
50                 },
51                 function () {
52                     testFailed("Promise rejected.");
53                     finishTest();
54                 }
55             );
56         }
57
58         function runTest() {
59             activateElement("TheIframeThatRequestsStorageAccess");
60         }
61
62         const hostUnderTest = "localhost:8000";
63         const statisticsUrl = "http://" + hostUnderTest;
64         testRunner.setUseITPDatabase(true);
65         if (document.location.hash !== "#firstPartyCookieSet") {
66             setEnableFeature(true, function() {
67                 document.location.href = statisticsUrl + "/storageAccess/resources/set-cookie.php?name=firstPartyCookie&value=value#http://127.0.0.1:8000/storageAccess/request-and-grant-access-then-navigate-cross-site-should-not-have-access-database.html#firstPartyCookieSet";
68             });
69         } else {
70             testRunner.setStatisticsPrevalentResource(statisticsUrl, true, function() {
71                 if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
72                     testFailed("Host did not get set as prevalent resource.");
73                 testRunner.setStatisticsHasHadUserInteraction(statisticsUrl, true, function() {
74                     if (!testRunner.isStatisticsHasHadUserInteraction(statisticsUrl))
75                         testFailed("Host did not get logged for user interaction.");
76                     testRunner.statisticsUpdateCookieBlocking(function() {
77                         let iframeElement = document.createElement("iframe");
78                         iframeElement.setAttribute("sandbox", "allow-storage-access-by-user-activation allow-scripts allow-same-origin allow-modals");
79                         iframeElement.onload = runTest;
80                         iframeElement.id = "TheIframeThatRequestsStorageAccess";
81                         iframeElement.src = "http://localhost:8000/storageAccess/resources/self-navigating-frame-after-granted-access.html#userShouldGrantAccess,userShouldBeConsulted,policyShouldGrantAccess,crossSiteNavigation";
82                         document.body.appendChild(iframeElement);
83                     });
84                 });
85             });
86         }
87     </script>
88 </head>
89 <body>
90 </body>
91 </html>