Files
Nicolas Chevobbe 3a3ae50758 Bug 1915872 - [devtools] Make getColor rely on CSS variables computed values. r=devtools-reviewers,ochameau.
The function used to parse the variables.css file and do some regex matching
on the light/dark theme rules to extract the declaration value of a variable.

This patch makes the function simpler by getting the computed value of the
CSS variable, which automatically adapt to the current theme since we do set
`color-scheme` value properly when setting a theme.

In order for this to work properly, the variables that might be used in this
function need to be registered so the engine know they are colors.

We take this as an opportunity to rename the function to `getCssVariableColor`,
which is more explicit, and also make it take the actual CSS variable name,
and not only the suffix part that comes after `--theme-` for better grep-ability.

The test in browser_theme.js is adapted to reflect those changes.

Differential Revision: https://phabricator.services.mozilla.com/D220718
2024-09-03 15:00:54 +00:00

189 lines
5.6 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Tests that theme utilities work
const {
getCssVariableColor,
getTheme,
setTheme,
} = require("resource://devtools/client/shared/theme.js");
const { PrefObserver } = require("resource://devtools/client/shared/prefs.js");
add_task(async function () {
const tab = await addTab("data:text/html;charset=utf-8,Test page");
const toolbox = await gDevTools.showToolboxForTab(tab);
testGetTheme();
testSetTheme();
await testGetCssVariableColor(toolbox.win);
await testColorExistence(toolbox.win);
});
function testGetTheme() {
const originalTheme = getTheme();
ok(originalTheme, "has some theme to start with.");
Services.prefs.setCharPref("devtools.theme", "light");
is(getTheme(), "light", "getTheme() correctly returns light theme");
Services.prefs.setCharPref("devtools.theme", "dark");
is(getTheme(), "dark", "getTheme() correctly returns dark theme");
Services.prefs.setCharPref("devtools.theme", "unknown");
is(getTheme(), "unknown", "getTheme() correctly returns an unknown theme");
Services.prefs.setCharPref("devtools.theme", originalTheme);
}
function testSetTheme() {
const originalTheme = getTheme();
// Put this in a variable rather than hardcoding it because the default
// changes between aurora and nightly
const otherTheme = originalTheme == "dark" ? "light" : "dark";
const prefObserver = new PrefObserver("devtools.");
prefObserver.once("devtools.theme", () => {
const newValue = Services.prefs.getCharPref("devtools.theme");
is(
newValue,
otherTheme,
"A preference event triggered by setTheme comes after the value is set."
);
});
setTheme(otherTheme);
is(
Services.prefs.getCharPref("devtools.theme"),
otherTheme,
"setTheme() correctly sets another theme."
);
setTheme(originalTheme);
is(
Services.prefs.getCharPref("devtools.theme"),
originalTheme,
"setTheme() correctly sets the original theme."
);
setTheme("unknown");
is(
Services.prefs.getCharPref("devtools.theme"),
"unknown",
"setTheme() correctly sets an unknown theme."
);
Services.prefs.setCharPref("devtools.theme", originalTheme);
prefObserver.destroy();
}
async function setDarkTheme(win) {
setTheme("dark");
await waitFor(() =>
InspectorUtils.isUsedColorSchemeDark(win.document.documentElement)
);
}
async function setLightTheme(win) {
setTheme("light");
await waitFor(
() => !InspectorUtils.isUsedColorSchemeDark(win.document.documentElement)
);
}
async function testGetCssVariableColor(toolboxWin) {
// getCssVariableColor return the computed color, which are in the rgb() format, so we
// need to computed them from the original hex values.
// --theme-highlight-blue: light-dark(var(--blue-55), #75bfff);
// --blue-55: #0074e8;
const LIGHT_RGB = InspectorUtils.colorToRGBA("#0074e8");
const DARK_RGB = InspectorUtils.colorToRGBA("#75bfff");
const BLUE_LIGHT = `rgb(${LIGHT_RGB.r}, ${LIGHT_RGB.g}, ${LIGHT_RGB.b})`;
const BLUE_DARK = `rgb(${DARK_RGB.r}, ${DARK_RGB.g}, ${DARK_RGB.b})`;
// Sanity check
ok(InspectorUtils.isValidCSSColor(BLUE_LIGHT), `BLUE_LIGHT is a valid color`);
ok(InspectorUtils.isValidCSSColor(BLUE_DARK), `BLUE_DARK is a valid color`);
const originalTheme = getTheme();
await setLightTheme(toolboxWin);
is(
getCssVariableColor("--theme-highlight-blue", toolboxWin),
BLUE_LIGHT,
"correctly gets color for enabled theme."
);
await setDarkTheme(toolboxWin);
is(
getCssVariableColor("--theme-highlight-blue", toolboxWin),
BLUE_DARK,
"correctly gets color for enabled theme."
);
setTheme("metal");
// wait until we're no longer in dark mode
await waitFor(
() =>
!InspectorUtils.isUsedColorSchemeDark(toolboxWin.document.documentElement)
);
is(
getCssVariableColor("--theme-highlight-blue", toolboxWin),
BLUE_LIGHT,
"correctly uses light for default theme if enabled theme not found"
);
is(
getCssVariableColor("--theme-somecomponents", toolboxWin),
null,
"if a type cannot be found, should return null."
);
setTheme(originalTheme);
}
async function testColorExistence(win) {
const vars = [
"--theme-body-background",
"--theme-sidebar-background",
"--theme-contrast-background",
"--theme-tab-toolbar-background",
"--theme-toolbar-background",
"--theme-selection-background",
"--theme-selection-color",
"--theme-selection-background-hover",
"--theme-splitter-color",
"--theme-comment",
"--theme-body-color",
"--theme-text-color-alt",
"--theme-text-color-inactive",
"--theme-text-color-strong",
"--theme-highlight-green",
"--theme-highlight-blue",
"--theme-highlight-bluegrey",
"--theme-highlight-purple",
"--theme-highlight-lightorange",
"--theme-highlight-orange",
"--theme-highlight-red",
"--theme-highlight-pink",
];
const originalTheme = getTheme();
await setLightTheme(win);
for (const variableName of vars) {
const color = getCssVariableColor(variableName, win);
ok(color, `${variableName} exists in light theme`);
ok(
InspectorUtils.isValidCSSColor(color),
`${variableName} is a valid color in light theme`
);
}
await setDarkTheme(win);
for (const variableName of vars) {
const color = getCssVariableColor(variableName, win);
ok(color, `${variableName} exists in dark theme`);
ok(
InspectorUtils.isValidCSSColor(color),
`${variableName} is a valid color in dark theme`
);
}
setTheme(originalTheme);
}