<!DOCTYPE html>
<html>
<head>
    <style>
        #target {
            width: 200px;
            height: 200px;
            background-color: silver;
            margin: 20px;
        }
        
        #scroller {
            margin: 20px;
            width: 200px;
            height: 200px;
            border: 1px solid black;
            overflow: scroll;
        }
        
        .contents {
            height: 400%;
        }
    </style>
    <script src="../../../resources/js-test-pre.js"></script>
    <script src="../../../resources/ui-helper.js"></script>
    <script>

        jsTestIsAsync = true;

        let windowScrollEventCount = 0;
        let overflowScrollEventCount = 0;
        let expectedMinOverflowScrollEventCount = testRunner.isWebKit2 ? 2 : 1;

        async function testScroll()
        {
            shouldBe('windowScrollEventCount', '0');
            shouldBe('overflowScrollEventCount', '0');

            await UIHelper.ensurePresentationUpdate();
            await UIHelper.mouseWheelScrollAt(100, 100);

            shouldBe('windowScrollEventCount', '0');
            shouldBeTrue('overflowScrollEventCount >= expectedMinOverflowScrollEventCount');

            finishJSTest();
        }

        window.addEventListener('load', async () => {
            let target = document.getElementById('target');
            let scroller = document.getElementById('scroller');

            window.addEventListener('scroll', () => {
                ++windowScrollEventCount;
            }, false);

            scroller.addEventListener('scroll', () => {
                ++overflowScrollEventCount;
            }, false);

            target.addEventListener('wheel', (event) => {
                event.preventDefault();
                let newWheelEvent = new event.constructor(event.type, event);
                scroller.dispatchEvent(newWheelEvent);
            }, false);
            
            await testScroll();

        }, false);
    </script>
</head>
<body>
    <div id="target"></div>
    <div id="scroller">
        <div class="contents"></div>
    </div>
    <div id="console"></div>
    <script src="../../../resources/js-test-post.js"></script>
</body>
</html>
