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

"use strict";

/* import-globals-from ../../mochitest/role.js */
loadScripts({ name: "role.js", dir: MOCHITESTS_DIR });

ChromeUtils.defineESModuleGetters(this, {
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
  UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.sys.mjs",
});

ChromeUtils.defineLazyGetter(this, "PlacesFrecencyRecalculator", () => {
  return Cc["@mozilla.org/places/frecency-recalculator;1"].getService(
    Ci.nsIObserver
  ).wrappedJSObject;
});

// Check that the URL bar manages accessibility
// selection notifications appropriately on startup (new window).
async function runTests() {
  // XXX: See bug 2016839
  let focused = waitForEvent(
    EVENT_FOCUS,
    event => event.accessible.role == ROLE_EDITCOMBOBOX
  );
  info("Creating new window");
  let newWin = await BrowserTestUtils.openNewBrowserWindow();
  await PlacesTestUtils.addVisits([
    {
      // eslint-disable-next-line sdl/no-insecure-url
      uri: "http://www.addons.mozilla.org/",
      transition: PlacesUtils.history.TRANSITION_TYPED,
    },
  ]);
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  registerCleanupFunction(async function () {
    await BrowserTestUtils.closeWindow(newWin);
    await PlacesUtils.history.clear();
  });
  info("Focusing window");
  newWin.focus();
  await focused;

  // XXX: See bug 2016839
  let caretMoved = waitForEvent(
    EVENT_TEXT_CARET_MOVED,
    event => event.accessible.role == ROLE_EDITCOMBOBOX
  );

  info("Autofilling after typing `a` in new window URL bar.");
  EventUtils.synthesizeKey("a", {}, newWin);
  await UrlbarTestUtils.promiseSearchComplete(newWin);
  Assert.equal(
    newWin.gURLBar.inputField.value,
    "addons.mozilla.org/",
    "autofilled value as expected"
  );

  info("Ensuring caret moved on text selection");
  await caretMoved;
}

addAccessibleTask(``, runTests);
