<!DOCTYPE HTML>
<html>
<!--https://bugzilla.mozilla.org/show_bug.cgi?id=1672330-->
<head>
<title>Bug 1672330</title>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/paint_listener.js"></script>
<script src="/tests/gfx/layers/apz/test/mochitest/apz_test_utils.js"></script>
<script src="pointerlock_utils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
#target {
  width: 100px;
  height: 100px;
  background-color: green;
}
iframe {
  width: 400px;
  height: 300px;
  border: 1px solid blue;
}
</style>
</head>
<body>
<a target="_blank"href="https://bugzilla.mozilla.org/show_bug.cgi?id=1672330">Mozilla Bug 1672330</a>
<div id="target"></div>
<iframe src="https://example.com/tests/dom/tests/mochitest/pointerlock/iframe_differentDOM.html"></iframe>

<pre id="test">
<script type="text/javascript">
/**
 * Test for Bug 1672330
 */
SimpleTest.waitForExplicitFinish();

async function requestPointerLock(aElement, aExpectError = false) {
  let doc = aElement.ownerDocument;
  let waitForPointerLockEvent = function() {
    return new Promise((aResolve) => {
      let eventHandler = function(aEvent) {
        is(aEvent.type, aExpectError ? 'pointerlockerror' : 'pointerlockchange',
           `got ${aEvent.type}`);
        doc.removeEventListener('pointerlockchange', eventHandler);
        doc.removeEventListener('pointerlockerror', eventHandler);
        aResolve();
      };

      doc.addEventListener('pointerlockchange', eventHandler);
      doc.addEventListener('pointerlockerror', eventHandler);
    });
  };

  aElement.requestPointerLock();
  await waitForPointerLockEvent();
  is(doc.pointerLockElement, aExpectError ? null : aElement, "target pointer locked");
}

async function start() {
  await waitUntilApzStable();

  let target = document.getElementById("target");
  SpecialPowers.wrap(document).clearUserGestureActivation();
  // Pointer lock request should be rejected due to the lack of user gesture.
  await requestPointerLock(target, true);

  // Test mouse event should not be dispatched to document.
  document.addEventListener("mousemove", function(e) {
    ok(false, "Got unexpected mousemove");
  });

  info("test sending mouse event to iframe");
  let iframe = document.querySelector("iframe");
  synthesizeMouse(iframe, 10, 10, { type: "mousemove" });
  await new Promise(resolve => { SimpleTest.executeSoon(resolve); });

  info("test sending mouse event to another window");
  await new Promise((aResolve) => {
    let win = window.open("iframe_differentDOM.html");
    win.addEventListener("load", async function() {
      info("win onload");
      await waitUntilApzStable();
      synthesizeMouse(win.document.body, 10, 10, { type: "mousemove" }, win);
      win.close();
      aResolve();
    });
  });
  await new Promise(resolve => { SimpleTest.executeSoon(resolve); });

  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>
