<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script src="/js-test-resources/ui-helper.js"></script>
<script src="resources/util.js"></script>
<script>
    description("Tests that user interaction is not reported repeatedly within a short period of time.");
    jsTestIsAsync = true;

    const topFrameOrigin = "http://127.0.0.1:8000";
    const subFrameOrigin = "http://localhost:8000";

    function activateElement(elementId, callback) {
        var element = document.getElementById(elementId);
        var centerX = element.offsetLeft + element.offsetWidth / 2;
        var centerY = element.offsetTop + element.offsetHeight / 2;
        UIHelper.activateAt(centerX, centerY).then(
            function() {
                callback();
            },
            function() {
                testFailed("Promise rejected.");
                setEnableFeature(false, finishJSTest);
            }
        );
    }

    function firstInteraction() {
        shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(topFrameOrigin)");
        shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(subFrameOrigin)");
        activateElement("testElement", secondInteraction);
    }

    function secondInteraction() {
        shouldBeTrue("testRunner.isStatisticsHasHadUserInteraction(topFrameOrigin)");
        shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(subFrameOrigin)");

        if (testRunner) {
            testRunner.setStatisticsHasHadUserInteraction(topFrameOrigin, false, function() {
                shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(topFrameOrigin)");

                activateElement("testElement", finishTest);
            });
        }
    }

    function finishTest() {
        shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(topFrameOrigin)");
        shouldBeFalse("testRunner.isStatisticsHasHadUserInteraction(subFrameOrigin)");

        setEnableFeature(false, finishJSTest);
    }

    onload = function() {
        setEnableFeature(true, function() {
            shouldBe("document.location.origin", "topFrameOrigin");

            firstInteraction();
        });
    };
</script>
<div id="testElement">This is the test element</div>
</body>
</html>
