blob: 0166c695eb4aa29f2e7e55f627b4b82b91de475d [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test for Partitioned and Unpartitioned Cookie With User Interaction</title>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<script>
const partitionHost = "127.0.0.1:8000";
const thirdPartyOrigin = "http://localhost:8000";
const thirdPartyBaseUrl = thirdPartyOrigin + "/resourceLoadStatistics/resources";
const firstPartyCookieName = "firstPartyCookie";
const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
const thirdPartyCookieName = "thirdPartyCookie";
const subPathToSetThirdPartyCookie = "/set-cookie.php?name=" + thirdPartyCookieName + "&value=value";
const fragmentWithReturnUrl = "http://" + partitionHost + "/resourceLoadStatistics/partitioned-and-unpartitioned-cookie-with-partitioning-timeout.html";
const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + thirdPartyCookieName;
function setEnableFeature(enable) {
if (!enable)
testRunner.statisticsResetToConsistentState();
testRunner.setStatisticsNotifyPagesWhenDataRecordsWereScanned(enable);
internals.setResourceLoadStatisticsEnabled(enable);
testRunner.setCookieStoragePartitioningEnabled(enable);
}
function finishTest() {
setEnableFeature(false);
testRunner.notifyDone();
}
function openIframe(url, onLoadHandler) {
const element = document.createElement("iframe");
element.src = url;
if (onLoadHandler) {
element.onload = onLoadHandler;
}
document.body.appendChild(element);
}
function fireShouldPartitionCookiesHandlerAndContinue() {
testRunner.statisticsUpdateCookiePartitioning(runTest);
}
function setShortCookiePartitioningTimeOutPlusFireShouldPartitionCookiesHandlerAndContinue() {
testRunner.setStatisticsTimeToLiveCookiePartitionFree(1);
setTimeout(fireShouldPartitionCookiesHandlerAndContinue, 2000);
}
function setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue() {
testRunner.setStatisticsTimeToLiveCookiePartitionFree(86400);
testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true);
if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
testFailed("Third party did not get logged for user interaction.");
runTest();
}
function runTest() {
switch (document.location.hash) {
case "#step1":
// Set first-party cookie for http://localhost:8000
document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#step2";
break;
case "#step2":
// Check that the first-party cookie is not sent in partition 127.0.0.1:8000
document.location.hash = "step3";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.",
runTest);
break;
case "#step3":
// Set third-party cookie for http://localhost:8000 in partition 127.0.0.1:8000
document.location.hash = "step4";
openIframe(thirdPartyBaseUrl + subPathToSetThirdPartyCookie + "&message=Setting partitioned, third-party cookie.",
runTest);
break;
case "#step4":
// Check that only partitioned cookie get sent in partition 127.0.0.1:8000
document.location.hash = "step5";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third-party cookie.",
setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue);
break;
case "#step5":
// http://localhost:8000 now has user interaction, check that un-partitioned cookie is sent for third-party
document.location.hash = "step6";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After first user interaction, should only receive un-partitioned cookie.",
setShortCookiePartitioningTimeOutPlusFireShouldPartitionCookiesHandlerAndContinue);
break;
case "#step6":
// Cookie partitioning should be applied again
document.location.hash = "step7";
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should only receive partitioned, third-party cookie.",
setNormalCookiePartitioningTimeOutPlusUserInteractionAndContinue);
break;
case "#step7":
// http://localhost:8000 now has fresh user interaction, check that un-partitioned cookie is sent for third-party
openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After second user interaction, should only receive un-partitioned cookie.",
finishTest);
break;
}
}
if (document.location.hash === "" && window.testRunner && window.internals) {
setEnableFeature(true);
// Set localhost as prevalent and with non-recent user interaction to put it in the partitioning category.
testRunner.setStatisticsPrevalentResource("http://localhost", true);
if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
testFailed("Host did not get set as prevalent resource.");
testRunner.setStatisticsHasHadNonRecentUserInteraction("http://localhost");
testRunner.statisticsUpdateCookiePartitioning();
testRunner.waitUntilDone();
testRunner.dumpChildFramesAsText();
document.location.hash = "step1";
}
runTest();
</script>
</body>
</html>