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

"use strict";

add_task(async function testPrefRequired() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.preferences.experimental", false]],
  });

  await openPreferencesViaOpenPreferencesAPI("paneHome", { leaveOpen: true });
  let doc = gBrowser.contentDocument;

  let experimentalCategory = doc.getElementById("category-experimental");
  ok(experimentalCategory, "The category exists");
  ok(experimentalCategory.hidden, "The category is hidden");

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function testCanOpenWithPref() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.preferences.experimental", true]],
  });

  await openPreferencesViaOpenPreferencesAPI("paneHome", { leaveOpen: true });
  let doc = gBrowser.contentDocument;

  let experimentalCategory = doc.getElementById("category-experimental");
  ok(experimentalCategory, "The category exists");
  ok(!experimentalCategory.hidden, "The category is not hidden");

  let categoryHeader = await TestUtils.waitForCondition(
    () => doc.getElementById("firefoxExperimentalCategory"),
    "Waiting for experimental features category to get initialized"
  );
  ok(
    categoryHeader.hidden,
    "The category header should be hidden when Home is selected"
  );

  EventUtils.synthesizeMouseAtCenter(experimentalCategory, {}, doc.ownerGlobal);
  await TestUtils.waitForCondition(
    () => !categoryHeader.hidden,
    "Waiting until category is visible"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function testSearchFindsExperiments() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.preferences.experimental", true]],
  });

  await openPreferencesViaOpenPreferencesAPI("paneHome", { leaveOpen: true });
  let doc = gBrowser.contentDocument;

  let experimentalCategory = doc.getElementById("category-experimental");
  ok(experimentalCategory, "The category exists");
  ok(!experimentalCategory.hidden, "The category is not hidden");

  await TestUtils.waitForCondition(
    () => doc.querySelector("#pane-experimental-featureGates > .featureGate"),
    "Waiting for experimental features category to get initialized"
  );
  await evaluateSearchResults(
    "in development and evolving",
    ["pane-experimental-featureGates"],
    /* include experiments */ true
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function testExtraTemplate() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.preferences.experimental", true]],
  });

  // Pretend a feature has id of "featureGate" to reuse that template
  const server = new DefinitionServer();
  server.addDefinition({
    id: "testFeatureGateExtra",
    isPublicJexl: "true",
    preference: "test.feature",
  });
  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    `about:preferences?definitionsUrl=${encodeURIComponent(
      server.definitionsUrl
    )}#paneExperimental`
  );

  const doc = gBrowser.contentDocument;
  let extraContent = await TestUtils.waitForCondition(
    () => doc.getElementById("testFeatureGateExtraContent"),
    "wait for feature to get added to the DOM"
  );

  is(
    extraContent.textContent,
    "Test extra content",
    "extra template added extra content"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});
