Source/WebCore:
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / do-not-switch-session-on-navigation-to-prevalent-without-interaction-database.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     <script src="resources/util.js"></script>
7 </head>
8 <body onload="runTest()">
9 <script>
10     description("Tests that the session is not switched upon top frame navigation to a prevalent resource without user interaction.");
11     jsTestIsAsync = true;
12
13     const prevalentOrigin = "http://127.0.0.1:8000";
14     const nonPrevalentOrigin = "http://localhost:8000";
15     const sessionCookieName = "sessionCookie";
16     const persistentCookieName = "persistentCookie";
17     const twoMinutesInSeconds = 120;
18
19     function setSessionCookie() {
20         document.cookie = sessionCookieName + "=1; path=/";
21     }
22
23     function setPersistentCookie() {
24         document.cookie = persistentCookieName + "=1; path=/; Max-Age=" + twoMinutesInSeconds + ";";
25     }
26
27     function checkCookies(shouldHaveSessionCookie, shouldHavePersistentCookie) {
28         let hasSessionCookie = (document.cookie + "").includes(sessionCookieName),
29             hasPersistentCookie = (document.cookie + "").includes(persistentCookieName);
30
31         if (shouldHaveSessionCookie && hasSessionCookie)
32             testPassed("Should have and has the session cookie.");
33         else if (shouldHaveSessionCookie && !hasSessionCookie) {
34             testFailed("Should have but doesn't have the session cookie.");
35             setEnableFeature(false, finishJSTest);
36         } else if (!shouldHaveSessionCookie && hasSessionCookie) {
37             testFailed("Shouldn't have but has the session cookie.");
38             setEnableFeature(false, finishJSTest);
39         } else
40             testPassed("Shouldn't have and doesn't have the session cookie.");
41
42
43         if (shouldHavePersistentCookie && hasPersistentCookie)
44             testPassed("Should have and has the persistent cookie.");
45         else if (shouldHavePersistentCookie && !hasPersistentCookie) {
46             testFailed("Should have but doesn't have the persistent cookie.");
47             setEnableFeature(false, finishJSTest);
48         } else if (!shouldHavePersistentCookie && hasPersistentCookie) {
49             testFailed("Shouldn't have but has the persistent cookie.");
50             setEnableFeature(false, finishJSTest);
51         } else
52             testPassed("Shouldn't have and doesn't have the persistent cookie.");
53     }
54
55     function runTest() {
56         switch (document.location.hash) {
57             case "#step1":
58                 setSessionCookie();
59                 setPersistentCookie();
60                 checkCookies(true, true);
61                 if (testRunner.hasStatisticsIsolatedSession(prevalentOrigin)) {
62                     testFailed("Origin has isolated session.");
63                     setEnableFeature(false, finishJSTest);
64                 } else
65                     testPassed("Origin has no isolated session.");
66                 document.location.href = nonPrevalentOrigin + "/resourceLoadStatistics/do-not-switch-session-on-navigation-to-prevalent-without-interaction.html#step2";
67                 break;
68             case "#step2":
69                 document.location.hash = "step3";
70                 if (document.location.origin !== nonPrevalentOrigin)
71                     testFailed("Step 2 is not on " + nonPrevalentOrigin + ".");
72                 testRunner.setStatisticsPrevalentResource(prevalentOrigin, true, function() {
73                     if (!testRunner.isStatisticsPrevalentResource(prevalentOrigin)) {
74                         testFailed(prevalentOrigin + " did not get set as prevalent resource.");
75                         setEnableFeature(false, finishJSTest);
76                     }
77                     testRunner.statisticsUpdateCookieBlocking(runTest);
78                 });
79                 break;
80             case "#step3":
81                 document.location.href = prevalentOrigin + "/resourceLoadStatistics/do-not-switch-session-on-navigation-to-prevalent-without-interaction.html#step4";
82                 break;
83             case "#step4":
84                 checkCookies(true, true);
85                 if (testRunner.hasStatisticsIsolatedSession(prevalentOrigin))
86                     testFailed("Origin has isolated session.");
87                 else
88                     testPassed("Origin has no isolated session.");
89                 setEnableFeature(false, finishJSTest);
90                 break;
91             default:
92                 testFailed("Unknown hash.");
93                 setEnableFeature(false, finishJSTest);
94         }
95     }
96
97     if (document.location.hash === "") {
98         if (document.location.origin !== prevalentOrigin)
99             testFailed("Test is not starting out on " + prevalentOrigin + ".");
100             
101             testRunner.setUseITPDatabase(true);
102         setEnableFeature(true, function () {
103             if (testRunner.isStatisticsPrevalentResource(prevalentOrigin))
104                 testFailed(prevalentOrigin + " was classified as prevalent resource before the test starts.");
105             document.location.hash = "step1";
106         });
107     }
108 </script>
109 </body>
110 </html>