<!DOCTYPE HTML>
<html>
<!--https://bugzilla.mozilla.org/show_bug.cgi?id=1662587-->
<head>
<title>Bug 1662587</title>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="/tests/SimpleTest/SimpleTest.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=1662587">Mozilla Bug 1662587</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 1662587
 */
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);
    });
  };

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

async function exitPointerLock() {
  if (document.pointerLockElement) {
    document.exitPointerLock();
    await new Promise((aResolve) => {
      document.addEventListener("pointerlockchange", (aEvent) => {
        ok(true, `got ${aEvent.type}`);
        aResolve();
      }, { once: true });
    });
  }
  is(document.pointerLockElement, null, "pointer unlocked");
}

async function start() {
  let iframe = document.querySelector("iframe");
  let target = document.getElementById("target");
  await requestPointerLock(target);

  // Mouse event should be dispatched to locked element.
  synthesizeMouse(iframe, 10, 10, { type: "mousemove" });
  await new Promise((aResolve) => {
    document.addEventListener("mousemove", function(e) {
      info("Got mousemove");
      ok("movementX" in e, "has movementX");
      ok("movementY" in e, "has movementY");
      is(e.target, target, "event.target");
      aResolve();
    }, { once: true });
  });

  // Mouse wheel event should be dispatched to locked element.
  synthesizeWheel(iframe, 10, 10, { deltaY: 3.0, deltaMode: WheelEvent.DOM_DELTA_LINE });
  await new Promise((aResolve) => {
    document.addEventListener("wheel", function(e) {
      info("Got wheel");
      is(e.target, target, "event.target");
      aResolve();
    }, { once: true });
  });

  // Pointer lock requested in iframe should be rejected.
  let win = iframe.contentWindow;
  win.focus()
  await SpecialPowers.spawn(win, [], async () => {
    info("request pointer lock in xorigin iframe");
    content.document.querySelector("input").focus();
    SpecialPowers.wrap(content.document).notifyUserGestureActivation();
    content.document.body.requestPointerLock();
    await new Promise((aResolve) => {
      let handler = function(aEvent) {
        is(aEvent.type, 'pointerlockerror', `got ${aEvent.type}`);
        content.document.onpointerlockchange = null;
        content.document.onpointerlockerror = null;
        aResolve();
      };
      content.document.onpointerlockchange = handler;
      content.document.onpointerlockerror = handler;
    });
    content.document.exitPointerLock();
  });

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