summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audiocontext-interface/audiocontext-sinkid-state-change.https.html
blob: c22f69c18da72bf88b06b29148072c1b84769bbf (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
<!DOCTYPE html>
<head>
<title>Test AudioContext.setSinkId() state change</title>
</head>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
"use strict";

const audioContext = new AudioContext();
let outputDeviceList = null;
let firstDeviceId = null;

// Setup: Get permission via getUserMedia() and a list of audio output devices.
promise_setup(async t => {
  await navigator.mediaDevices.getUserMedia({ audio: true });
  const deviceList = await navigator.mediaDevices.enumerateDevices();
  outputDeviceList =
      deviceList.filter(({kind}) => kind === 'audiooutput');
  assert_greater_than(outputDeviceList.length, 1,
                      'the system must have more than 1 device.');
  firstDeviceId = outputDeviceList[1].deviceId;
}, 'Get permission via getUserMedia() and a list of audio output devices.');

// Test the sink change when from a suspended context.
promise_test(async t => {
  let events = [];
  await audioContext.suspend();

  // Step 6. Set wasRunning to false if the [[rendering thread state]] on the
  // AudioContext is "suspended".
  assert_equals(audioContext.state, 'suspended');

  // Step 11.5. Fire an event named sinkchange at the associated AudioContext.
  audioContext.onsinkchange = t.step_func(() => {
    events.push('sinkchange');
    assert_equals(audioContext.sinkId, firstDeviceId);
  });

  await audioContext.setSinkId(firstDeviceId);
  assert_equals(events[0], 'sinkchange');
  t.done();
}, 'Calling setSinkId() on a suspended AudioContext should fire only sink ' +
   'change events.');

// Test the sink change when from a running context.
promise_test(async t => {
  let events = [];
  await audioContext.resume();

  // Step 9. If wasRunning is true:
  assert_equals(audioContext.state, 'running');

  // Step 9.2.1. Set the state attribute of the AudioContext to "suspended".
  // Fire an event named statechange at the associated AudioContext.
  audioContext.onstatechange = t.step_func(() => {
    events.push('statechange:suspended');
    assert_equals(audioContext.state, 'suspended');
  });

  // Step 11.5. Fire an event named sinkchange at the associated AudioContext.
  audioContext.onsinkchange = t.step_func(() => {
    events.push('sinkchange');
    assert_equals(audioContext.sinkId, firstDeviceId);
  });

  // Step 12.2. Set the state attribute of the AudioContext to "running".
  // Fire an event named statechange at the associated AudioContext.
  audioContext.onstatechange = t.step_func(() => {
    events.push('statechange:running');
    assert_equals(audioContext.state, 'running');
  });

  await audioContext.setSinkId(firstDeviceId);
  assert_equals(events.length, 3);
  assert_equals(events[0], 'statechange:suspended');
  assert_equals(events[1], 'sinkchange');
  assert_equals(events[2], 'statechange:running');
  t.done();
}, 'Calling setSinkId() on a running AudioContext should fire both state ' +
   'and sink change events.');
</script>
</html>