<!doctype html>
<meta charset=utf-8>
<title>mousemove event: preventDefault()</title>
<link rel="author" title="Mirko Brodesser" href="mailto:mbrodesser@mozilla.com">
<link rel="help" href="https://github.com/w3c/uievents/issues/278">
<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=resources/utils.js></script>

<body>
  <div id="a">div a</div>
  <div id="b">div b</div>
  <div id="c" draggable="true">div c</div>
</body>

<script>
  'use strict';

  let event_log = [];

  function logEvents(e) {
    event_log.push(e.type);
  }

  // Deliberately avoiding mouseup here because the last selectionchange
  // may be fired before or after the mouseup.
  ["mousedown", "mousemove", "selectionchange", "dragstart"].forEach(ename => {
    document.addEventListener(ename, logEvents);
  });
  document.addEventListener("mousemove", e => e.preventDefault());

  promise_test(async () => {
    event_log = [];

    const a = document.getElementById("a");
    const b = document.getElementById("b");

    let mouseup_promise = getEvent("mouseup", document);

    await new test_driver.Actions()
        .pointerMove(0, 0, {origin: a})
        .pointerDown()
        .addTick()
        .addTick()
        .pointerMove(0, 0, {origin: b})
        .addTick()
        .addTick()
        .pointerUp()
        .send();

    await mouseup_promise;

    const expected_events = ["mousemove", "mousedown", "selectionchange",
        "mousemove", "selectionchange"];

    assert_equals(event_log.toString(), expected_events.toString(),
        "received events");
  }, "selectionchange event firing when mousemove event is prevented");

  promise_test(async () => {
    event_log = [];

    const b = document.getElementById("b");
    const c = document.getElementById("c");

    let dragstart_promise = getEvent("dragstart", document);

    // A mouseup event is not expected.  This avoids timing out when the
    // dragstart event is missing.
    let mouseup_promise = getEvent("mouseup", document);

    await new test_driver.Actions()
        .pointerMove(0, 0, {origin: c})
        .pointerDown()
        .addTick()
        .addTick()
        .pointerMove(0, 0, {origin: b})
        .addTick()
        .addTick()
        .pointerUp()
        .send();

    await Promise.race([dragstart_promise, mouseup_promise]);

    const expected_events = ["mousemove", "mousedown", "mousemove", "dragstart"];

    assert_equals(event_log.toString(), expected_events.toString(),
        "received events");
  }, "dragstart event firing when mousemove event is prevented");
</script>
