<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            height: 2000px;
            width: 200%;
        }
    </style>
    <script src="../../../resources/js-test-pre.js"></script>
    <script src="../../../resources/ui-helper.js"></script>
    <script>
        var jsTestIsAsync = true;
        
        var initialScrollX;
        var initialScrollY;

        async function resetScrollPositions(x, y)
        {
            window.scrollTo(x, y);
            // Wait for scroll events to fire.
            await UIHelper.renderingUpdate();
        }
        
        async function testVerticalRubberband()
        {
            await resetScrollPositions(100, 0);
            initialScrollX = window.scrollX;
            
            let scrollListener = () => {
                if (window.scrollX != initialScrollX)
                    testFailed('Horizontal scroll position ' + window.scrollX + ' is not ' + initialScrollX);
            }

            window.addEventListener('scroll', scrollListener);

            // Scroll up with some sideways drift.
            const events = [
                {
                    type : "wheel",
                    viewX : 100,
                    viewY : 100,
                    deltaY : -20,
                    phase : "began"
                },
                {
                    type : "wheel",
                    deltaX : 10,
                    deltaY : -20,
                    phase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : 10,
                    deltaY : -20,
                    phase : "changed"
                },
                {
                    type : "wheel",
                    phase : "ended"
                },
                {
                    type : "wheel",
                    deltaX : 10,
                    deltaY : -20,
                    momentumPhase : "began"
                },
                {
                    type : "wheel",
                    deltaX : 10,
                    deltaY : -20,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : 20,
                    deltaY : -30,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : 10,
                    deltaY : -20,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    momentumPhase : "ended"
                }
            ];

            await UIHelper.mouseWheelSequence({ events });
            window.removeEventListener('scroll', scrollListener);
        }

        async function testHorizontalRubberband()
        {
            await resetScrollPositions(0, 100);
            initialScrollY = window.scrollY;

            let scrollListener = () => {
                if (window.scrollY != initialScrollY)
                    testFailed('Vertical scroll position ' + window.scrollY + ' is not ' + initialScrollY);
            }

            window.addEventListener('scroll', scrollListener);

            // Scroll right with some upwards drift.
            const events = [
                {
                    type : "wheel",
                    viewX : 100,
                    viewY : 100,
                    deltaX : -20,
                    phase : "began"
                },
                {
                    type : "wheel",
                    deltaX : -20,
                    deltaY : -10,
                    phase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : -20,
                    deltaY : -10,
                    phase : "changed"
                },
                {
                    type : "wheel",
                    phase : "ended"
                },
                {
                    type : "wheel",
                    deltaX : -20,
                    momentumPhase : "began"
                },
                {
                    type : "wheel",
                    deltaX : -20,
                    deltaY : 10,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : -30,
                    deltaY : 20,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    deltaX : -20,
                    deltaY : 10,
                    momentumPhase : "changed"
                },
                {
                    type : "wheel",
                    momentumPhase : "ended"
                }
            ];

            await UIHelper.mouseWheelSequence({ events });
            window.removeEventListener('scroll', scrollListener);
        }

        async function scrollTest()
        {
            debug('');
            debug('Tests axis locking in the momentum wheel gesture phase');

            await testVerticalRubberband();
            await testHorizontalRubberband();

            finishJSTest();
        }

        window.addEventListener('load', () => {
            setTimeout(scrollTest, 0);
        }, false);
    </script>
</head>
<body>
    <script src="../../../resources/js-test-post.js"></script>
</body>
</html>
