<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script src="resources/util.js"></script>
<script>
description("Tests that we grant User Interaction credit for handled keypresses.");
jsTestIsAsync = true;

const statisticsUrl = "http://127.0.0.1:8000/temp";

onload = function() {
    const testFrame = document.getElementById("testFrame");

    if (window.testRunner && window.internals) {
        setEnableFeature(true, function() {
            testRunner.setStatisticsPrevalentResource(statisticsUrl, true, function() {
                if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
                    testFailed("Host did not get set as prevalent resource.");

                testRunner.setStatisticsHasHadUserInteraction(statisticsUrl, false, function() {
                    if (testRunner.isStatisticsHasHadUserInteraction(statisticsUrl))
                        testFailed("Host did not get cleared of user interaction.");

                    testInput = document.getElementById("testInput");

                    testRunner.installStatisticsDidModifyDataRecordsCallback(function() {
                        shouldBeEqualToString("testInput.value", "a");

                        if (!testRunner.isStatisticsHasHadUserInteraction(statisticsUrl))
                            testFailed("Origin did not get user interaction credit.");
                        else
                            testPassed("Origin was granted user interaction.");

                        setTimeout(function() {
                            testFrame.src = "about:blank";
                            setTimeout(function() {
                                setEnableFeature(false, finishJSTest);
                            }, 0);
                        }, 0);
                    });
                    testRunner.setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(false);
                    testRunner.setStatisticsMinimumTimeBetweenDataRecordsRemoval(0);
                    testRunner.statisticsProcessStatisticsAndDataRecords();

                    debug("Simulate user typing letter 'a' into the field.");
                    testInput.focus();
                    if (window.eventSender)
                        eventSender.keyDown("a");
                });
            });
        });
    }
}
</script>
<iframe id="testFrame" src="resources/onclick.html"></iframe>
<input id="testInput" type="text">
</body>
</html>
