<!DOCTYPE html>
<html>
<head>
    <style>
    * {
        box-sizing: border-box;
    }

    .container {
        width:100%;
        overflow:auto;
        height:auto;
    }

    .innercontainer {
        height:100%;
        width:50%;
    }
    </style>
    <script src="../../../resources/ui-helper.js"></script>
    <script src="../../../resources/js-test-pre.js"></script>
    <script>
        jsTestIsAsync = true;

        var iframeTarget;
        var innerIFrameTarget;
        var pageScrollPositionBefore;
        var iframeScrollPositionBefore;

        function checkForScroll()
        {
            // The iframe should not have scrolled at all.
            var pageScrollPositionAfter = document.scrollingElement.scrollTop;
            var iframeScrollPositionAfter = window.frames['target'].document.scrollingElement.scrollTop;
            var innerIFrameScrollPositionAfter = iframeTarget.contentWindow.frames['target'].document.scrollingElement.scrollTop;

            if (pageScrollPositionBefore != pageScrollPositionAfter)
                testFailed("Page received wheel events.");
            else
                testPassed("Page did not receive wheel events.");

            if (iframeScrollPositionBefore != iframeScrollPositionAfter)
                testFailed("iframe received wheel events.");
            else
                testPassed("iframe did not receive wheel events.");

            if (innerIFrameScrollPositionBefore != innerIFrameScrollPositionAfter)
                testPassed("iframe received wheel events.");
            else
                testFailed("iframe did not receive wheel events.");

            finishJSTest();
        }

        async function scrollTest()
        {
            pageScrollPositionBefore = document.scrollingElement.scrollTop;

            iframeTarget = document.getElementById('target');

            var iframeScrollingElement = window.frames['target'].document.scrollingElement;
            iframeScrollPositionBefore = iframeScrollingElement.scrollTop;

            innerIFrameTarget = iframeTarget.contentWindow.frames['target'].document.scrollingElement;
            innerIFrameScrollPositionBefore = innerIFrameTarget.scrollTop;

            // Scroll the #source until we reach the #target.
            var startPosX = Math.round(iframeTarget.offsetLeft) + 20;
            var startPosY = Math.round(iframeTarget.offsetTop) + 80;

            await UIHelper.startMonitoringWheelEvents();
            eventSender.mouseMoveTo(startPosX, startPosY);
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, -1, 'began', 'none');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, -1, 'changed', 'none');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, -1, 'changed', 'none');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, -1, 'none', 'begin');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, -1, 'none', 'continue');
            eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'none', 'end');

            await UIHelper.waitForScrollCompletion();
            checkForScroll();
        }

        function setupTopLevel()
        {
            if (!window.eventSender) {
                debug('This test requires eventSender');
                finishJSTest();
                return;
            }

            setTimeout(scrollTest, 0);
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="innercontainer">
            <div style="width:100%;">
                <div>Inner Frame:</div>
                <div style="height:92%;">
                    <iframe id="target" name="target" src="resources/testContent.html" onload="setupTopLevel();"></iframe>
                </div>
            </div>
        </div>
    </div>
    <div id="console"></div>
    <script>
    description("Tests that iframe doesn't pass wheel events to main frame when scrolling inside iframe");
    </script>
    <script src="../../../resources/js-test-post.js"></script>
</body>
</html>