Commit 534acf70 authored by Tim Nguyen's avatar Tim Nguyen
Browse files

Bug 1482870 - Make current theme experiment initialize in new windows. r=kmag

Differential Revision: https://phabricator.services.mozilla.com/D25525

--HG--
extra : moz-landing-system : lando
parent 05d16c89
Loading
Loading
Loading
Loading
+79 −66
Original line number Diff line number Diff line
@@ -47,32 +47,46 @@ add_task(async function test_experiment_static_theme() {

  await extension.startup();

  const testExperimentApplied = rootEl => {
    if (AppConstants.MOZ_ALLOW_LEGACY_EXTENSIONS) {
    is(root.style.getPropertyValue("--some-color-property"), hexToCSS("#ff00ff"),
      is(rootEl.style.getPropertyValue("--some-color-property"), hexToCSS("#ff00ff"),
         "Color property should be parsed and set.");
    ok(root.style.getPropertyValue("--some-image-property").startsWith("url("),
      ok(rootEl.style.getPropertyValue("--some-image-property").startsWith("url("),
         "Image property should be parsed.");
    ok(root.style.getPropertyValue("--some-image-property").endsWith("background.jpg)"),
      ok(rootEl.style.getPropertyValue("--some-image-property").endsWith("background.jpg)"),
         "Image property should be set.");
    is(root.style.getPropertyValue("--some-random-property"), "no-repeat",
      is(rootEl.style.getPropertyValue("--some-random-property"), "no-repeat",
         "Generic Property should be set.");
    } else {
    is(root.style.getPropertyValue("--some-color-property"), "",
      is(rootEl.style.getPropertyValue("--some-color-property"), "",
         "Color property should be unset");
    is(root.style.getPropertyValue("--some-image-property"), "",
      is(rootEl.style.getPropertyValue("--some-image-property"), "",
         "Image property should be unset");
    is(root.style.getPropertyValue("--some-random-property"), "",
      is(rootEl.style.getPropertyValue("--some-random-property"), "",
         "Generic Property should be unset.");
    }
  };

  info("Testing that current window updated with the experiment applied");
  testExperimentApplied(root);

  info("Testing that new window initialized with the experiment applied");
  const newWindow = await BrowserTestUtils.openNewBrowserWindow();
  const newWindowRoot = newWindow.document.documentElement;
  testExperimentApplied(newWindowRoot);

  await extension.unload();

  is(root.style.getPropertyValue("--some-color-property"), "",
  info("Testing that both windows unapplied the experiment");
  for (const rootEl of [root, newWindowRoot]) {
    is(rootEl.style.getPropertyValue("--some-color-property"), "",
       "Color property should be unset");
  is(root.style.getPropertyValue("--some-image-property"), "",
    is(rootEl.style.getPropertyValue("--some-image-property"), "",
       "Image property should be unset");
  is(root.style.getPropertyValue("--some-random-property"), "",
    is(rootEl.style.getPropertyValue("--some-random-property"), "",
       "Generic Property should be unset.");
  }
  await BrowserTestUtils.closeWindow(newWindow);
});

add_task(async function test_experiment_dynamic_theme() {
@@ -131,63 +145,62 @@ add_task(async function test_experiment_dynamic_theme() {
  extension.sendMessage("update-theme");
  await extension.awaitMessage("theme-updated");

  const testExperimentApplied = rootEl => {
    if (AppConstants.MOZ_ALLOW_LEGACY_EXTENSIONS) {
    is(root.style.getPropertyValue("--some-color-property"), hexToCSS("#ff00ff"),
      is(rootEl.style.getPropertyValue("--some-color-property"), hexToCSS("#ff00ff"),
         "Color property should be parsed and set.");
    ok(root.style.getPropertyValue("--some-image-property").startsWith("url("),
      ok(rootEl.style.getPropertyValue("--some-image-property").startsWith("url("),
         "Image property should be parsed.");
    ok(root.style.getPropertyValue("--some-image-property").endsWith("background.jpg)"),
      ok(rootEl.style.getPropertyValue("--some-image-property").endsWith("background.jpg)"),
         "Image property should be set.");
    is(root.style.getPropertyValue("--some-random-property"), "no-repeat",
      is(rootEl.style.getPropertyValue("--some-random-property"), "no-repeat",
         "Generic Property should be set.");
    } else {
    is(root.style.getPropertyValue("--some-color-property"), "",
      is(rootEl.style.getPropertyValue("--some-color-property"), "",
         "Color property should be unset");
    is(root.style.getPropertyValue("--some-image-property"), "",
      is(rootEl.style.getPropertyValue("--some-image-property"), "",
         "Image property should be unset");
    is(root.style.getPropertyValue("--some-random-property"), "",
      is(rootEl.style.getPropertyValue("--some-random-property"), "",
         "Generic Property should be unset.");
    }
  };
  testExperimentApplied(root);

  const newWindow = await BrowserTestUtils.openNewBrowserWindow();
  const newWindowRoot = newWindow.document.documentElement;

  testExperimentApplied(newWindowRoot);

  extension.sendMessage("reset-theme");
  await extension.awaitMessage("theme-reset");

  is(root.style.getPropertyValue("--some-color-property"), "",
  for (const rootEl of [root, newWindowRoot]) {
    is(rootEl.style.getPropertyValue("--some-color-property"), "",
       "Color property should be unset");
  is(root.style.getPropertyValue("--some-image-property"), "",
    is(rootEl.style.getPropertyValue("--some-image-property"), "",
       "Image property should be unset");
  is(root.style.getPropertyValue("--some-random-property"), "",
    is(rootEl.style.getPropertyValue("--some-random-property"), "",
       "Generic Property should be unset.");
  }

  extension.sendMessage("update-theme");
  await extension.awaitMessage("theme-updated");

  if (AppConstants.MOZ_ALLOW_LEGACY_EXTENSIONS) {
    is(root.style.getPropertyValue("--some-color-property"), hexToCSS("#ff00ff"),
       "Color property should be parsed and set.");
    ok(root.style.getPropertyValue("--some-image-property").startsWith("url("),
       "Image property should be parsed.");
    ok(root.style.getPropertyValue("--some-image-property").endsWith("background.jpg)"),
       "Image property should be set.");
    is(root.style.getPropertyValue("--some-random-property"), "no-repeat",
       "Generic Property should be set.");
  } else {
    is(root.style.getPropertyValue("--some-color-property"), "",
       "Color property should be unset");
    is(root.style.getPropertyValue("--some-image-property"), "",
       "Image property should be unset");
    is(root.style.getPropertyValue("--some-random-property"), "",
       "Generic Property should be unset.");
  }
  testExperimentApplied(root);
  testExperimentApplied(newWindowRoot);

  await extension.unload();

  is(root.style.getPropertyValue("--some-color-property"), "",
  for (const rootEl of [root, newWindowRoot]) {
    is(rootEl.style.getPropertyValue("--some-color-property"), "",
       "Color property should be unset");
  is(root.style.getPropertyValue("--some-image-property"), "",
    is(rootEl.style.getPropertyValue("--some-image-property"), "",
       "Image property should be unset");
  is(root.style.getPropertyValue("--some-random-property"), "",
    is(rootEl.style.getPropertyValue("--some-random-property"), "",
       "Generic Property should be unset.");
  }

  await BrowserTestUtils.closeWindow(newWindow);
});

add_task(async function test_experiment_stylesheet() {
+3 −3
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ LightweightThemeConsumer.prototype = {
      return;
    }

    this._update(data, data.experiment);
    this._update(data);
  },

  handleEvent(aEvent) {
@@ -186,7 +186,7 @@ LightweightThemeConsumer.prototype = {
    return this.darkThemeMediaQuery && this.darkThemeMediaQuery.matches;
  },

  _update(themeData, experiment) {
  _update(themeData) {
    this._lastData = themeData;

    let theme = themeData.theme;
@@ -219,7 +219,7 @@ LightweightThemeConsumer.prototype = {
      _setImage(root, active, `--${icon}-icon`, value);
    }

    this._setExperiment(active, experiment, theme.experimental);
    this._setExperiment(active, themeData.experiment, theme.experimental);
    _setImage(root, active, "--lwt-header-image", theme.headerURL);
    _setImage(root, active, "--lwt-additional-images", theme.additionalBackgrounds);
    _setProperties(root, active, theme);