summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_themes_warnings.js
blob: 025a4073ddb9cb9467eb690c2aad5bb58e21b601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"use strict";

const { AddonSettings } = ChromeUtils.importESModule(
  "resource://gre/modules/addons/AddonSettings.sys.mjs"
);

// This test checks that theme warnings are properly emitted.

function waitForConsole(task, message) {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async resolve => {
    SimpleTest.monitorConsole(resolve, [
      {
        message: new RegExp(message),
      },
    ]);
    await task();
    SimpleTest.endMonitorConsole();
  });
}

add_setup(async function () {
  SimpleTest.waitForExplicitFinish();
});

add_task(async function test_static_theme() {
  for (const property of ["colors", "images", "properties"]) {
    const extension = ExtensionTestUtils.loadExtension({
      manifest: {
        theme: {
          [property]: {
            such_property: "much_wow",
          },
        },
      },
    });
    await waitForConsole(
      extension.startup,
      `Unrecognized theme property found: ${property}.such_property`
    );
    await extension.unload();
  }
});

add_task(async function test_dynamic_theme() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["theme"],
    },
    background() {
      browser.test.onMessage.addListener((msg, details) => {
        if (msg === "update-theme") {
          browser.theme.update(details).then(() => {
            browser.test.sendMessage("theme-updated");
          });
        } else {
          browser.theme.reset().then(() => {
            browser.test.sendMessage("theme-reset");
          });
        }
      });
    },
  });

  await extension.startup();

  for (const property of ["colors", "images", "properties"]) {
    extension.sendMessage("update-theme", {
      [property]: {
        such_property: "much_wow",
      },
    });
    await waitForConsole(
      () => extension.awaitMessage("theme-updated"),
      `Unrecognized theme property found: ${property}.such_property`
    );
  }

  await extension.unload();
});

add_task(async function test_experiments_enabled() {
  info("Testing that experiments are handled correctly on nightly and deved");

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      theme: {
        properties: {
          such_property: "much_wow",
          unknown_property: "very_unknown",
        },
      },
      theme_experiment: {
        properties: {
          such_property: "--such-property",
        },
      },
    },
  });
  if (!AddonSettings.EXPERIMENTS_ENABLED) {
    await waitForConsole(
      extension.startup,
      "This extension is not allowed to run theme experiments"
    );
  } else {
    await waitForConsole(
      extension.startup,
      "Unrecognized theme property found: properties.unknown_property"
    );
  }
  await extension.unload();
});

add_task(async function test_experiments_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.experiments.enabled", false]],
  });

  info(
    "Testing that experiments are handled correctly when experiements pref is disabled"
  );

  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      theme: {
        properties: {
          such_property: "much_wow",
        },
      },
      theme_experiment: {
        properties: {
          such_property: "--such-property",
        },
      },
    },
  });
  await waitForConsole(
    extension.startup,
    "This extension is not allowed to run theme experiments"
  );
  await extension.unload();
  await SpecialPowers.popPrefEnv();
});