summaryrefslogtreecommitdiffstats
path: root/layout/style/test/chrome/test_chrome_only_media_queries.html
blob: 1a2fb098c093edb8faa7dc23575595a83bdbf98d (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
<!doctype html>
<title>Test for parsing of non-content-exposed media-queries.</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome-only-media-queries.js"></script>
<style></style>
<script>
const SHEET = document.querySelector('style');

SimpleTest.waitForExplicitFinish();

async function testWithPref() {
  await new Promise(r => {
    SpecialPowers.pushPrefEnv(
      {
        set: [
          ["layout.css.forced-colors.enabled", false],
        ],
      },
      r
    );
  });
  expectKnown("(forced-colors: none)");
  expectKnown("(forced-colors: active)");
  expectKnown("(forced-colors)");
  SimpleTest.finish();
}

function expect(q, shouldBeKnown) {
  is(matchMedia(q).media, q, "Serialization should roundtrip");
  is(matchMedia(`${q} or (not ${q})`).matches, shouldBeKnown, `Query should${shouldBeKnown ? "" : " not"} be known`);
}

function expectKnown(q) {
  expect(q, true);
}

function expectUnkown(q) {
  expect(q, false);
}

// 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: )");

testWithPref();
</script>