test_chrome_only_media_queries.html (4360B)
1 <!doctype html> 2 <title>Test for parsing of non-content-exposed media-queries.</title> 3 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 4 <script src="chrome-only-media-queries.js"></script> 5 <style></style> 6 <script> 7 const SHEET = document.querySelector('style'); 8 9 let lazy = {}; 10 11 ChromeUtils.defineESModuleGetters(lazy, { 12 WindowsVersionInfo: 13 "resource://gre/modules/components-utils/WindowsVersionInfo.sys.mjs", 14 }); 15 16 17 SimpleTest.waitForExplicitFinish(); 18 19 function expect(q, shouldBeKnown) { 20 is(matchMedia(q).media, q, "Serialization should roundtrip"); 21 is(matchMedia(`${q} or (not ${q})`).matches, shouldBeKnown, `${q} should${shouldBeKnown ? "" : " not"} be known`); 22 } 23 24 function expectKnown(q) { 25 expect(q, true); 26 } 27 28 function expectUnkown(q) { 29 expect(q, false); 30 } 31 32 function testMatches(q, shouldMatch = true) { 33 is(matchMedia(q).matches, shouldMatch, `${q} should match`); 34 } 35 36 async function testMozPref(q, prefName, value, otherValue) { 37 expectKnown(q); 38 await SpecialPowers.pushPrefEnv({ 39 set:[[prefName, value]], 40 }); 41 testMatches(q, true); 42 let mediaList = matchMedia(q); 43 let change = new Promise(r => { 44 mediaList.addEventListener("change", r, { once: true }); 45 }); 46 await SpecialPowers.pushPrefEnv({ 47 set:[[prefName, otherValue]], 48 }); 49 testMatches(q, false); 50 // Should change dynamically successfully. 51 await change; 52 } 53 54 // Test a toggle that should always match for `1` or `0`. 55 function testToggle(toggle) { 56 expectKnown(`(${toggle})`); 57 expectKnown(`(${toggle}: 1)`); 58 expectKnown(`(${toggle}: 0)`); 59 60 expectUnkown(`(${toggle}: foo)`); 61 expectUnkown(`(${toggle}: true)`); 62 expectUnkown(`(${toggle}: false)`); 63 expectUnkown(`(${toggle}: -1)`); 64 expectUnkown(`(min-${toggle}: 0)`); 65 expectUnkown(`(max-${toggle}: 0)`); 66 expectUnkown(`(max-${toggle})`); 67 expectUnkown(`(min-${toggle})`); 68 69 let matches_1 = matchMedia(`(${toggle}: 1)`).matches; 70 let matches_0 = matchMedia(`(${toggle}: 0)`).matches; 71 isnot(matches_0, matches_1, `Should not match both true and false: ${toggle}`); 72 is(matches_0 || matches_1, true, `Should match at least one: ${toggle}`); 73 } 74 75 for (let toggle of CHROME_ONLY_TOGGLES) { 76 testToggle(toggle) 77 } 78 79 for (let query of CHROME_ONLY_QUERIES) { 80 expectKnown(query); 81 } 82 83 // These might be exposed to content by pref, we just want to make sure they're 84 // always exposed to chrome. 85 expectKnown("(prefers-contrast: more)") 86 expectKnown("(prefers-contrast: no-preference)") 87 expectKnown("(prefers-contrast: less)"); 88 expectKnown("(prefers-contrast)") 89 90 expectKnown("(forced-colors: none)"); 91 expectKnown("(forced-colors: active)"); 92 expectKnown("(forced-colors)"); 93 94 expectUnkown("(-moz-platform: )"); 95 96 (async function() { 97 await testMozPref('-moz-pref("foo.bar.bool", true)', "foo.bar.bool", true, false); 98 await testMozPref('-moz-pref("foo.bar.bool")', "foo.bar.bool", true, false); 99 await testMozPref('-moz-pref("foo.bar.str", "foo")', "foo.bar.str", "foo", "bar"); 100 await testMozPref('-moz-pref("foo.bar.str")', "foo.bar.str", "foo", ""); 101 await testMozPref('-moz-pref("foo.bar.int", 1)', "foo.bar.int", 1, 2); 102 await testMozPref('-moz-pref("foo.bar.int")', "foo.bar.int", 1, 0); 103 104 let supportsMica = matchMedia('(-moz-platform: windows)').matches && lazy.WindowsVersionInfo.get().buildNumber >= 22621; 105 info(`Mica supported: ${supportsMica}`); 106 for (let [query, pref] of [['(-moz-windows-mica)', 'widget.windows.mica'], ['(-moz-windows-mica-popups)', 'widget.windows.mica.popups']]) { 107 let prefIsBool = pref == 'widget.windows.mica'; 108 let prefValue = prefIsBool ? SpecialPowers.getBoolPref(pref, false) : SpecialPowers.getIntPref(pref, 0); 109 // Assuming we don't hit the "auto but disabled" case for popups. 110 let prefEnabled = !!prefValue; 111 let mediaList = matchMedia(query); 112 is(mediaList.matches, supportsMica && prefEnabled, `Mica support is as expected ${query}`); 113 let change = new Promise(r => { 114 mediaList.addEventListener("change", r, { once: true }); 115 }); 116 await SpecialPowers.pushPrefEnv({ 117 set: [[pref, prefIsBool ? !prefValue : (prefValue ? 0 : 1)]], 118 }); 119 if (supportsMica) { 120 await change; 121 } 122 is(mediaList.matches, supportsMica && !prefEnabled, `Mica query works dynamically if appropriate ${query}`); 123 await SpecialPowers.popPrefEnv(); 124 } 125 126 SimpleTest.finish(); 127 }()); 128 </script>