/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["test.wait300msAfterTabSwitch", true]],
  });
});

add_task(async function test_toTopLevelWidgetRect() {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html;charset=utf-8,test"
  );

  // Convert (12, 34) in a content document coordinates into this browser window
  // coordinates.
  const positionInBrowser = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    () => {
      return content.window.windowUtils.toTopLevelWidgetRect(12, 34, 0, 0);
    }
  );

  // Dispatch a mousedown event on the browser window coordinates position to
  // see whether it's fired on the correct position in the content document.
  const mouseDownPromise = BrowserTestUtils.waitForContentEvent(
    tab.linkedBrowser,
    "mousedown",
    false,
    event => {
      dump(`mousedown on (${event.clientX}, ${event.clientY})`);
      return event.clientX == 12 && event.clientY == 34;
    }
  );

  // A workaround for bug 1743857.
  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    await Promise.resolve();
  });

  EventUtils.synthesizeMouseAtPoint(
    positionInBrowser.x / window.devicePixelRatio,
    positionInBrowser.y / window.devicePixelRatio,
    { type: "mousedown", button: 1 }
  );
  await mouseDownPromise;

  Assert.ok(true, "windowUtils.toTopLevelWidgetRect() works as expected");

  BrowserTestUtils.removeTab(tab);
});
