summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_mq_prefers_reduced_motion_dynamic.html
blob: 570c0d395401b54c39273b7fdfdd2f50842fb3a2 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1486971
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1478519</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1486971">Mozilla Bug 1486971</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
<script>
'use strict';

async function waitForFrame() {
  return new Promise(resolve => {
    window.requestAnimationFrame(resolve);
  });
}

// Returns a Promise which will be resolved when the 'change' event is received
// for the given media query string.
async function promiseForChange(mediaQuery) {
  return new Promise(resolve => {
    window.matchMedia(mediaQuery).addEventListener('change', event => {
      resolve(event.matches);
    }, { once: true });
  });
}

add_task(async () => {
  const initiallyMatches =
      window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  const changePromise = initiallyMatches ? promiseForChange('(prefers-reduced-motion: reduce)') : null;

  await SpecialPowers.pushPrefEnv({ set: [['ui.prefersReducedMotion', 0]]});

  if (changePromise) {
    await changePromise;
  }

  ok(!window.matchMedia('(prefers-reduced-motion: reduce)').matches,
     'Does not matches prefers-reduced-motion: reduced) when the system sets ' +
     'prefers-reduced-motion false');
  ok(!window.matchMedia('(prefers-reduced-motion)').matches,
     'Does not matches (prefers-reduced-motion) when the system sets ' +
     'prefers-reduced-motion false');
  ok(window.matchMedia('(prefers-reduced-motion: no-preference)').matches,
     'Matches (prefers-reduced-motion: no-preference) when the system sets ' +
     'prefers-reduced-motion false');
});

add_task(async () => {
  const reduce = promiseForChange('(prefers-reduced-motion: reduce)');
  const booleanContext = promiseForChange('(prefers-reduced-motion)');
  const noPreference = promiseForChange('(prefers-reduced-motion: no-preference)');

  await SpecialPowers.pushPrefEnv({ set: [['ui.prefersReducedMotion', 1]]});

  const [ reduceResult, booleanContextResult, noPreferenceResult ] =
    await Promise.all([ reduce, booleanContext, noPreference ]);

  ok(reduceResult,
     'Matches (prefers-reduced-motion: reduced) when the system sets ' +
     'prefers-reduced-motion true');
  ok(booleanContextResult,
     'Matches (prefers-reduced-motion) when the system sets ' +
     'prefers-reduced-motion true');
  ok(!noPreferenceResult,
     'Does not matches (prefers-reduced-motion: no-preference) when the ' +
     'system sets prefers-reduced-motion true');

  await SpecialPowers.flushPrefEnv();
});
</script>
</body>
</html>