Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: os == 'android'
- Manifest: layout/style/test/chrome/chrome.toml
<!doctype html>
<title>Test for parsing of non-content-exposed media-queries.</title>
<script src="chrome-only-media-queries.js"></script>
<style></style>
<script>
const SHEET = document.querySelector('style');
let lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
WindowsVersionInfo:
"resource://gre/modules/components-utils/WindowsVersionInfo.sys.mjs",
});
SimpleTest.waitForExplicitFinish();
function expect(q, shouldBeKnown) {
is(matchMedia(q).media, q, "Serialization should roundtrip");
is(matchMedia(`${q} or (not ${q})`).matches, shouldBeKnown, `${q} should${shouldBeKnown ? "" : " not"} be known`);
}
function expectKnown(q) {
expect(q, true);
}
function expectUnkown(q) {
expect(q, false);
}
function testMatches(q, shouldMatch = true) {
is(matchMedia(q).matches, shouldMatch, `${q} should match`);
}
async function testMozPref(q, prefName, value, otherValue) {
expectKnown(q);
await SpecialPowers.pushPrefEnv({
set:[[prefName, value]],
});
testMatches(q, true);
let mediaList = matchMedia(q);
let change = new Promise(r => {
mediaList.addEventListener("change", r, { once: true });
});
await SpecialPowers.pushPrefEnv({
set:[[prefName, otherValue]],
});
testMatches(q, false);
// Should change dynamically successfully.
await change;
}
// Test a toggle that should always match for `1` or `0`.
function testToggle(toggle) {
expectKnown(`(${toggle})`);
expectKnown(`(${toggle}: 1)`);
expectKnown(`(${toggle}: 0)`);
expectUnkown(`(${toggle}: foo)`);
expectUnkown(`(${toggle}: true)`);
expectUnkown(`(${toggle}: false)`);
expectUnkown(`(${toggle}: -1)`);
expectUnkown(`(min-${toggle}: 0)`);
expectUnkown(`(max-${toggle}: 0)`);
expectUnkown(`(max-${toggle})`);
expectUnkown(`(min-${toggle})`);
let matches_1 = matchMedia(`(${toggle}: 1)`).matches;
let matches_0 = matchMedia(`(${toggle}: 0)`).matches;
isnot(matches_0, matches_1, `Should not match both true and false: ${toggle}`);
is(matches_0 || matches_1, true, `Should match at least one: ${toggle}`);
}
for (let toggle of CHROME_ONLY_TOGGLES) {
testToggle(toggle)
}
for (let query of CHROME_ONLY_QUERIES) {
expectKnown(query);
}
// These might be exposed to content by pref, we just want to make sure they're
// always exposed to chrome.
expectKnown("(prefers-contrast: more)")
expectKnown("(prefers-contrast: no-preference)")
expectKnown("(prefers-contrast: less)");
expectKnown("(prefers-contrast)")
expectKnown("(forced-colors: none)");
expectKnown("(forced-colors: active)");
expectKnown("(forced-colors)");
expectUnkown("(-moz-platform: )");
(async function() {
await testMozPref('-moz-pref("foo.bar.bool", true)', "foo.bar.bool", true, false);
await testMozPref('-moz-pref("foo.bar.bool")', "foo.bar.bool", true, false);
await testMozPref('-moz-pref("foo.bar.str", "foo")', "foo.bar.str", "foo", "bar");
await testMozPref('-moz-pref("foo.bar.str")', "foo.bar.str", "foo", "");
await testMozPref('-moz-pref("foo.bar.int", 1)', "foo.bar.int", 1, 2);
await testMozPref('-moz-pref("foo.bar.int")', "foo.bar.int", 1, 0);
let supportsMica = matchMedia('(-moz-platform: windows)').matches && lazy.WindowsVersionInfo.get().buildNumber >= 22621;
info(`Mica supported: ${supportsMica}`);
for (let [query, pref] of [['(-moz-windows-mica)', 'widget.windows.mica'], ['(-moz-windows-mica-popups)', 'widget.windows.mica.popups']]) {
let prefEnabled = SpecialPowers.getBoolPref("widget.windows.mica", false);
let mediaList = matchMedia(query);
is(mediaList.matches, supportsMica && prefEnabled, `Mica support is as expected ${query}`);
let change = new Promise(r => {
mediaList.addEventListener("change", r, { once: true });
});
await SpecialPowers.pushPrefEnv({
set:[[pref, !prefEnabled]],
});
if (supportsMica) {
await change;
}
is(mediaList.matches, supportsMica && !prefEnabled, `Mica query works dynamically if appropriate ${query}`);
await SpecialPowers.popPrefEnv();
}
SimpleTest.finish();
}());
</script>