<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
<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>
<script src="scroll_support.js"></script>
<style>
    #targetDiv {
        width: 200px;
        height: 200px;
        overflow: scroll;
    }

    #innerDiv {
        width: 400px;
        height: 400px;
    }
</style>
</head>
<body style="margin:0" onload=runTest()>
    <div id="targetDiv">
        <div id="innerDiv">
        </div>
    </div>
</body>

<script>
var target_div = document.getElementById('targetDiv');

function runTest() {
  promise_test(async (t) => {
    // Skip the test on a Mac as they do not support touch screens.
    const isMac = navigator.platform.toUpperCase().indexOf('MAC')>=0;
    if (isMac)
      return;

    await resetTargetScrollState(t, target_div);
    await waitForCompositorReady();

    const timeout = 3000; // Because we have two pauses we need longer timeout
    const targetScrollendPromise = createScrollendPromiseForTarget(t, target_div, timeout);
    verifyNoScrollendOnDocument(t);

    let scrollend_count = 0;
    const scrollend_listener = () => {
      scrollend_count += 1;
    };
    target_div.addEventListener("scrollend", scrollend_listener);
    t.add_cleanup(() => {
      target_div.removeEventListener('scrollend', scrollend_listener);
    });

    // Perform a touch drag on target div and wait for target_div to get
    // a scrollend event.
    await new test_driver.Actions()
        .addPointer('TestPointer', 'touch')
        .pointerMove(0, 0, {origin: target_div}) // 0, 0 is center of element.
        .pointerDown()
        .addTick()
        .pointerMove(0, -40, {origin: target_div}) //  Drag up to move down.
        .addTick()
        .pause(200) //  Prevent inertial scroll.
        .pointerMove(0, -60, {origin: target_div})
        .addTick()
        .pause(200) //  Prevent inertial scroll.
        .pointerUp()
        .send();

    await targetScrollendPromise;

    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t, target_div);
    assert_equals(scrollend_count, 1);
  }, 'Tests that the target_div gets scrollend event when touch dragging.');

  promise_test(async (t) => {
    // Skip test on platforms that do not have a visible scrollbar (e.g.
    // overlay scrollbar).
    const scrollbar_width = target_div.offsetWidth - target_div.clientWidth;
    if (scrollbar_width == 0)
      return;

    await resetTargetScrollState(t, target_div);
    await waitForCompositorReady();

    const targetScrollendPromise = createScrollendPromiseForTarget(t, target_div);
    verifyNoScrollendOnDocument(t);

    const bounds = target_div.getBoundingClientRect();
    // Some versions of webdriver have been known to frown at non-int arguments
    // to pointerMove.
    const x = bounds.right - Math.round(scrollbar_width / 2);
    const y = bounds.bottom - 20;
    await new test_driver.Actions()
        .addPointer('TestPointer', 'mouse')
        .pointerMove(x, y, {origin: 'viewport'})
        .pointerDown()
        .addTick()
        .pointerUp()
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t, target_div);
  }, 'Tests that the target_div gets scrollend event when clicking ' +
     'scrollbar.');

  // Same issue as previous test.
  promise_test(async (t) => {
    // Skip test on platforms that do not have a visible scrollbar (e.g.
    // overlay scrollbar).
    const scrollbar_width = target_div.offsetWidth - target_div.clientWidth;
    if (scrollbar_width == 0)
      return;

    await resetTargetScrollState(t, target_div);
    await waitForCompositorReady();

    const targetScrollendPromise = createScrollendPromiseForTarget(t, target_div, 1000);
    verifyNoScrollendOnDocument(t);

    const bounds = target_div.getBoundingClientRect();
    // Some versions of webdriver have been known to frown at non-int arguments
    // to pointerMove.
    const x = bounds.right - Math.round(scrollbar_width / 2);
    const y = bounds.top + 30;
    const dy = 30;
    await new test_driver.Actions()
        .addPointer('TestPointer', 'mouse')
        .pointerMove(x, y, { origin: 'viewport' })
        .pointerDown()
        .pointerMove(x, y + dy, { origin: 'viewport' })
        .addTick()
        .pointerUp()
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t, target_div);
  }, 'Tests that the target_div gets scrollend event when dragging the ' +
      'scrollbar thumb.');

  promise_test(async (t) => {
    await resetTargetScrollState(t, target_div);
    await waitForCompositorReady();

    const targetScrollendPromise = createScrollendPromiseForTarget(t, target_div);
    verifyNoScrollendOnDocument(t);

    const x = 0;
    const y = 0;
    const dx = 0;
    const dy = 40;
    const duration_ms = 10;
    await new test_driver.Actions()
        .scroll(x, y, dx, dy, { origin: target_div }, duration_ms)
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t, target_div);
  }, 'Tests that the target_div gets scrollend event when mouse wheel ' +
     'scrolling.');

  promise_test(async (t) => {
    await resetTargetScrollState(t, target_div);
    await waitForCompositorReady();

    verifyNoScrollendOnDocument(t);
    const targetScrollendPromise = createScrollendPromiseForTarget(t, target_div);

    target_div.focus();
    window.test_driver.send_keys(target_div, '\ue015');

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t, target_div);
  }, 'Tests that the target_div gets scrollend event when sending DOWN key ' +
     'to the target.');
}

</script>
</html>
