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

// This tests the Privacy pane's Firefox QuickActions UI.

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  ActionsProviderQuickActions:
    "moz-src:///browser/components/urlbar/ActionsProviderQuickActions.sys.mjs",
  UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.sys.mjs",
});

add_setup(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.secondaryActions.featureGate", true]],
  });

  ActionsProviderQuickActions.addAction("testaction", {
    commands: ["testaction"],
    label: "quickactions-downloads2",
  });

  registerCleanupFunction(() => {
    ActionsProviderQuickActions.removeAction("testaction");
  });
});

add_task(async function test_show_prefs() {
  Services.prefs.setBoolPref(
    "browser.urlbar.scotchBonnet.enableOverride",
    false
  );
  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", false);

  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
  let doc = gBrowser.selectedBrowser.contentDocument;
  let quickActionsCheckbox = doc.getElementById("enableQuickActions");
  Assert.ok(
    !BrowserTestUtils.isVisible(quickActionsCheckbox),
    "Quick actions checkbox should be hidden when both prefs are false"
  );

  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", true);
  await quickActionsCheckbox.parentElement.updateComplete;
  Assert.ok(
    BrowserTestUtils.isVisible(quickActionsCheckbox),
    "Quick actions checkbox should be shown again"
  );

  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", false);
  Services.prefs.setBoolPref(
    "browser.urlbar.scotchBonnet.enableOverride",
    true
  );
  await quickActionsCheckbox.parentElement.updateComplete;
  Assert.ok(
    BrowserTestUtils.isVisible(quickActionsCheckbox),
    "Quick actions checkbox should still be shown"
  );

  Services.prefs.clearUserPref("browser.urlbar.scotchBonnet.enableOverride");
  Services.prefs.clearUserPref("browser.urlbar.quickactions.showPrefs");
  gBrowser.removeCurrentTab();
});

async function testActionIsShown(window, name) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "testact",
    waitForFocus: SimpleTest.waitForFocus,
  });
  try {
    await BrowserTestUtils.waitForMutationCondition(
      window.document,
      {},
      () =>
        !!window.document.querySelector(
          `.urlbarView-action-btn[data-action=${name}]`
        )
    );
    Assert.ok(true, `We found action "${name}"`);
    return true;
  } catch (e) {
    return false;
  }
}

add_task(async function test_prefs() {
  Services.prefs.setBoolPref("browser.urlbar.quickactions.showPrefs", true);
  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences#search"
  );

  Assert.ok(
    !(await testActionIsShown(window)),
    "Actions are not shown while pref disabled"
  );

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    let checkbox = content.document.getElementById("enableQuickActions");
    is(
      checkbox.checked,
      false,
      "Checkbox is not checked while feature is disabled"
    );
    checkbox.click();
  });

  Assert.ok(
    await testActionIsShown(window, "testaction"),
    "Actions are shown after user clicks checkbox"
  );

  Services.prefs.clearUserPref("browser.urlbar.quickactions.showPrefs");
  Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
  await BrowserTestUtils.removeTab(tab);
});
