summaryrefslogtreecommitdiffstats
path: root/dom/media/tests/mochitest/test_ondevicechange.html
blob: 20894cff77c71d26320ae9ade5f32d7ef3c3e044 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1041393
-->
<head>
  <meta charset="utf-8">
  <title>onndevicechange tests</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1152383">ondevicechange tests</a>
<script type="application/javascript">

const RESPONSE_WAIT_TIME_MS = 10000;

function wait(time, message) {
  return new Promise(r => setTimeout(() => r(message), time));
}

function OnDeviceChangeEvent() {
  return new Promise(resolve => navigator.mediaDevices.ondevicechange = resolve);
}

function OnDeviceChangeEventReceived() {
  return Promise.race([
    OnDeviceChangeEvent(),
    wait(RESPONSE_WAIT_TIME_MS).then(() => Promise.reject("Timed out while waiting for devicechange event"))
  ]);
}

function OnDeviceChangeEventNotReceived() {
  return Promise.race([
    OnDeviceChangeEvent().then(() => Promise.reject("ondevicechange event is fired unexpectedly.")),
    wait(RESPONSE_WAIT_TIME_MS)
  ]);
}

var pushPrefs = (...p) => new Promise(r => SpecialPowers.pushPrefEnv({set: p}, r));

var videoTracks;

SimpleTest.requestCompleteLog();
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("Fake devicechange event is fired periodically, \
so we need to wait a while to make sure the event is fired or not as we expect.");

var videoTracks;

function wait(time, message) {
  return new Promise(r => setTimeout(() => r(message), time));
}

pushPrefs(["media.ondevicechange.fakeDeviceChangeEvent.enabled", true])
.then(() => pushPrefs(["media.navigator.permission.disabled", false]))
.then(() => pushPrefs(["media.ondevicechange.enabled", true]))
.then(() => info("assure devicechange event is NOT fired when gUM is NOT in use and permanent permission is NOT granted"))
.then(() => OnDeviceChangeEventNotReceived())
.then(() => ok(true, "devicechange event is NOT fired when gUM is NOT in use and permanent permission is NOT granted"))
.then(() => pushPrefs(['media.navigator.permission.disabled', true]))
.then(() => info("assure devicechange event is fired when gUM is NOT in use and permanent permission is granted"))
.then(() => OnDeviceChangeEventReceived())
.then(() => ok(true, "devicechange event is fired when gUM is NOT in use and permanent permission is granted"))
.then(() => navigator.mediaDevices.getUserMedia({video: true, fake: true}))
.then(st => {videoTracks = st.getVideoTracks();})
.then(() => info("assure devicechange event is fired when gUM is in use"))
.then(() => OnDeviceChangeEventReceived())
.then(() => ok(true, "devicechange event is fired when gUM is in use"))
.catch(e => ok(false, "Error: " + e))
.then(() => {
  if(videoTracks)
    videoTracks.forEach(track => track.stop());
})
.then(() => SimpleTest.finish());

</script>
</pre>
</body>
</html>