blob: 12149c03cfe15b660f427cd4c10ce23ee0d536d9 [file] [log] [blame] [edit]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test for Resource Load Statistics Pruning</title>
<script src="/js-test-resources/js-test.js"></script>
<script src="resources/util.js"></script>
</head>
<body>
<script>
description("Tests that statistics are pruned in the right order.");
jsTestIsAsync = true;
const olderTimestamp = Math.round((new Date()).getTime() / 1000);
const newerTimestamp = olderTimestamp + 10;
const newestTimestamp = newerTimestamp + 10;
const urlsToBePruned = [
{ url: "http://127.0.0.1:8000/temp", prevalent: false },
{ url: "http://127.0.0.2:8000/temp", prevalent: false },
{ url: "http://127.0.0.3:8000/temp", prevalent: true },
{ url: "http://127.0.0.4:8000/temp", prevalent: true },
{ url: "http://127.0.0.5:8000/temp", prevalent: false },
{ url: "http://127.0.0.6:8000/temp", prevalent: false },
{ url: "http://127.0.0.7:8000/temp", prevalent: true },
{ url: "http://127.0.0.8:8000/temp", prevalent: true }
];
function checkIfPrevalentAccordingToInitialExpectation(begin, end) {
var failed = false;
for (var i = begin; i < end; ++i) {
if (testRunner.isStatisticsPrevalentResource(urlsToBePruned[i].url) !== urlsToBePruned[i].prevalent) {
testFailed("checkIfPrevalentAccordingToInitialExpectation: Test iteration " + currentTest + " failed. " + urlsToBePruned[i].url + (urlsToBePruned[i].prevalent ? " wasn't " : " was ") + "prevalent");
failed = true;
}
}
if (failed)
setEnableFeature(false, finishJSTest);
}
function checkIfPrevalent(begin, end, expected) {
var failed = false;
for (var i = begin; i < end; ++i) {
if (testRunner.isStatisticsPrevalentResource(urlsToBePruned[i].url) !== expected) {
testFailed("checkIfPrevalent: Test iteration " + currentTest + " failed. " + urlsToBePruned[i].url + (expected ? " wasn't " : " was ") + "prevalent");
failed = true;
}
}
if (failed)
setEnableFeature(false, finishJSTest);
}
async function initializeStatisticsAndRunTests(step) {
switch (step) {
// Non-prevalent without user interaction to be pruned first.
case 1:
await testRunner.setStatisticsLastSeen(urlsToBePruned[0].url, olderTimestamp);
await initializeStatisticsAndRunTests(2);
break;
case 2:
await testRunner.setStatisticsLastSeen(urlsToBePruned[1].url, newerTimestamp);
await initializeStatisticsAndRunTests(3);
break;
// Prevalent without user interaction to be pruned second.
case 3:
await new Promise((resolve)=>{
testRunner.setStatisticsPrevalentResource(urlsToBePruned[2].url, true, resolve);
});
await initializeStatisticsAndRunTests(4);
break;
case 4:
await testRunner.setStatisticsLastSeen(urlsToBePruned[2].url, olderTimestamp);
await initializeStatisticsAndRunTests(5);
break;
case 5:
await new Promise((resolve)=>{
testRunner.setStatisticsPrevalentResource(urlsToBePruned[3].url, true, resolve);
});
await initializeStatisticsAndRunTests(6);
break;
case 6:
await testRunner.setStatisticsLastSeen(urlsToBePruned[3].url, newerTimestamp);
await initializeStatisticsAndRunTests(7);
break;
// Non-prevalent with user interaction to be pruned third.
case 7:
await new Promise((resolve)=>{
testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[4].url, true, resolve);
});
await initializeStatisticsAndRunTests(8);
break;
case 8:
await testRunner.setStatisticsLastSeen(urlsToBePruned[4].url, olderTimestamp);
await initializeStatisticsAndRunTests(9);
break;
case 9:
await new Promise((resolve)=>{
testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[5].url, true, resolve);
});
await initializeStatisticsAndRunTests(10);
break;
case 10:
await testRunner.setStatisticsLastSeen(urlsToBePruned[5].url, newerTimestamp);
await initializeStatisticsAndRunTests(11);
break;
// Prevalent with user interaction to be pruned last.
case 11:
await new Promise((resolve)=>{
testRunner.setStatisticsPrevalentResource(urlsToBePruned[6].url, true, resolve);
});
await initializeStatisticsAndRunTests(12);
break;
case 12:
await new Promise((resolve)=>{
testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[6].url, true, resolve);
});
await initializeStatisticsAndRunTests(13);
break;
case 13:
await testRunner.setStatisticsLastSeen(urlsToBePruned[6].url, olderTimestamp);
await initializeStatisticsAndRunTests(14);
break;
case 14:
await new Promise((resolve)=>{
testRunner.setStatisticsPrevalentResource(urlsToBePruned[7].url, true, resolve);
});
await initializeStatisticsAndRunTests(15);
break;
case 15:
await new Promise((resolve)=>{
testRunner.setStatisticsHasHadUserInteraction(urlsToBePruned[7].url, true, resolve);
});
await initializeStatisticsAndRunTests(16);
break;
case 16:
await testRunner.setStatisticsLastSeen(urlsToBePruned[7].url, newerTimestamp);
await initializeStatisticsAndRunTests(17);
break;
case 17:
checkIfPrevalentAccordingToInitialExpectation(0, urlsToBePruned.length);
runTest();
break;
}
}
var currentTest;
function checkStatisticsAfterPruning() {
// Pruned entries should not be prevalent.
checkIfPrevalent(0, currentTest, false);
// Non-pruned entries should keep their expected status.
checkIfPrevalentAccordingToInitialExpectation(currentTest, urlsToBePruned.length);
testPassed("Test iteration " + currentTest + " passed.");
if (currentTest < urlsToBePruned.length) {
++currentTest;
initializeStatisticsAndRunTests(1);
} else
setEnableFeature(false, finishJSTest);
}
let fillerUrl;
function runTest() {
fillerUrl = "http://127.0." + currentTest + ".1:8000/temp";
testRunner.setStatisticsPrevalentResource(fillerUrl, true, function() {
testRunner.setStatisticsHasHadUserInteraction(fillerUrl, true, async function () {
await testRunner.setStatisticsLastSeen(fillerUrl, newestTimestamp);
await testRunner.statisticsProcessStatisticsAndDataRecords();
checkStatisticsAfterPruning();
});
});
}
if (window.testRunner) {
setEnableFeature(true, function () {
testRunner.setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(false);
testRunner.setStatisticsMinimumTimeBetweenDataRecordsRemoval(0);
testRunner.setStatisticsMaxStatisticsEntries(urlsToBePruned.length);
testRunner.setStatisticsPruneEntriesDownTo(urlsToBePruned.length);
currentTest = 1;
initializeStatisticsAndRunTests(1);
});
}
</script>
</body>
</html>