<!DOCTYPE html>
<html>
<body>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<style type="text/css">
    #status-log {
        margin: 10px 0;
        color: green;
        color: green;
    }
</style>
</head>
<body onload="run_test()">
    <h2>Description</h2>
    <p>This test if movementX/Y can provide the change in position of the pointer, as if movementX/Y = eNow.screenX/Y-ePrevious.screenX/Y</p>
    <hr/>

    <h2>Manual Test Steps:</h2>
    <p>
        <ol>
            <li>Click to start Test1.</li>
            <li>Move the mouse within the window, slow and fast, like a scribble.</li>
            <li>Click again to end test.</li>
        </ol>
    </p>
    <hr/>

    <div id="status-log">Waiting... Click to start loging.</div>
    <div class="data-log">
        <table>
            <tr><td></td><td>X</td><td>Y</td></tr>
            <tr><td>screen_init:</td><td id="screenX_init-log">X</td><td id="screenY_init-log">Y</td></tr>
            <tr><td>screen_last:</td><td id="screenX_last-log">X</td><td id="screenY_last-log">Y</td></tr>
            <tr><td>screen_delta:</td><td id="screenX_delta-log">X</td><td id="screenY_delta-log">Y</td></tr>
            <tr><td>movement_sum:</td><td id="movementX_sum-log">X</td><td id="movementY_sum-log">Y</td></tr>
            <tr><td>movement:</td><td id="movementX-log">X</td><td id="movementY-log">Y</td></tr>
        </table>
    </div>
    <hr/>

    <div id="log"></div>

    <script type="text/javascript" >
        var status_log = document.querySelector('#status-log'),
            movementX_log = document.querySelector('#movementX-log'),
            movementY_log = document.querySelector('#movementY-log'),
            movementX_sum_log = document.querySelector('#movementX_sum-log'),
            movementY_sum_log = document.querySelector('#movementY_sum-log'),
            screenX_init_log = document.querySelector('#screenX_init-log'),
            screenY_init_log = document.querySelector('#screenY_init-log'),
            screenX_last_log = document.querySelector('#screenX_last-log'),
            screenY_last_log = document.querySelector('#screenY_last-log');
            screenX_delta_log = document.querySelector('#screenX_delta-log'),
            screenY_delta_log = document.querySelector('#screenY_delta-log');

        var click_counter = 0;

        var screenX_init, screenY_init, movementX, movementY, movementX_sum, movementY_sum, screenX_last, screenY_last;

        var movementX_Y_inside_window_Test = async_test("Test that movementX/Y = eNow.screenX/Y-ePrevious.screenX/Y.");

        document.addEventListener("click", function (e) {
            click_counter++;

            switch(click_counter) {
                case 1:
                    status_log.innerHTML = "inside window: logging...";
                break;
                case 2:
                    status_log.innerHTML = "inside window: done";

                    movementX_Y_inside_window_Test.step(function() {
                        assert_equals(movementX_sum, screenX_last - screenX_init, "sum of movementX = screenX_last - screenX_init");
                        assert_equals(movementY_sum, screenY_last - screenY_init, "sum of movementY = screenY_last - screenY_init");
                    });
                    movementX_Y_inside_window_Test.done();
                break;
            }
        });

        document.addEventListener("mousemove", function (e) {
            movementX = e.movementX;
            movementY = e.movementY;

            if(click_counter === 1) {
                if(!screenX_init) {
                    screenX_init = screenX_last = e.screenX;
                    screenY_init = screenY_last = e.screenY;
                    movementX_sum = 0;
                    movementY_sum = 0;
                }
                else {
                    movementX_sum += movementX;
                    movementY_sum += movementY;

                    screenX_delta = e.screenX - screenX_last;
                    screenY_delta = e.screenY - screenY_last;

                    movementX_Y_inside_window_Test.step(function() {
                        assert_equals(movementX, screenX_delta, "movementX = screen_delta");
                        assert_equals(movementY, screenY_delta, "movementY = screen_delta");
                    });

                    screenX_last = e.screenX;
                    screenY_last = e.screenY;

                    updateData();
                }
            }
        });

        function updateData() {
            screenX_init_log.innerHTML = screenX_init;
            screenY_init_log.innerHTML = screenY_init;
            screenX_last_log.innerHTML = screenX_last;
            screenY_last_log.innerHTML = screenY_last;
            screenX_delta_log.innerHTML = screenX_delta;
            screenY_delta_log.innerHTML = screenY_delta;
            movementX_log.innerHTML = movementX;
            movementY_log.innerHTML = movementY;
            movementX_sum_log.innerHTML = movementX_sum;
            movementY_sum_log.innerHTML = movementY_sum;
        }

        function run_test() {
            x = Math.round(window.innerWidth / 2);
            y = Math.round(window.innerHeight / 2);
            var actions = new test_driver.Actions();
            actions.pointerMove(x, y)
              .pointerDown()
              .pointerUp();
            for (var i = 0; i < 10; i++) {
                // Alternatively move left/right and up/down.
                x += ((-1)**i) * i * 10;
                y -= ((-1)**i) * i * 10;
                actions.pointerMove(x, y);
            }
            actions.pointerMove(x, y)
              .pointerDown()
              .pointerUp()
              .send();
        }
        </script>
    </body>
</html>
