<html>
    <head>
        <script src="../../../resources/js-test-pre.js"></script>
        <script src="../../../resources/ui-helper.js"></script>
        <script>
            var expectedScrollTop = 5;
            var expectedScrollLeft = 3;
            var pixelsPerWheelTick = 40;
            var event;
            var div;

            jsTestIsAsync = true;

            async function dispatchWheelEvent()
            {
                if (!window.eventSender) {
                    finishJSTest();
                    return;
                }

                var overflowElement = document.getElementById("overflow");
                if (overflowElement)
                    overflowElement.addEventListener("mousewheel", mousewheelHandler, false);

                eventSender.mouseMoveTo(100, 110);
                await UIHelper.statelessMouseWheelScrollAt(100, 110, -expectedScrollLeft, -expectedScrollTop);
                checkOffsets();
            }

            function checkOffsets()
            {
                div = document.getElementById("overflow");
                shouldBe("div.scrollTop", "expectedScrollTop * pixelsPerWheelTick");
                shouldBe("div.scrollLeft", "expectedScrollLeft * pixelsPerWheelTick");

                finishJSTest();
            }

            function mousewheelHandler(e)
            {
                event = e;
                shouldBe("event.wheelDeltaY", "expectedScrollTop * -120");
                shouldBe("event.wheelDeltaX", "expectedScrollLeft * -120");

                if (e.wheelDeltaY)
                    shouldBe("event.wheelDelta", "expectedScrollTop * -120");
                else
                    shouldBe("event.wheelDelta", "expectedScrollLeft * -120");
            }
            
            window.addEventListener('load', () => {
                setTimeout(dispatchWheelEvent, 0);
            }, false);
        </script>
    </head>
    <body style="margin:0">
        <div id="overflow" style="border:2px solid black;overflow:auto;height:200px;width:200px;">
            <div style="background-color:red;height:200px;width:400px;"></div>
            <div style="background-color:green;height:200px;width:400px;"></div>
            <div style="background-color:red;height:200px;width:400px;"></div>
        </div>
        <div id="console"></div>
        <script src="../../../resources/js-test-post.js"></script>
    </body>
</html>
