Refactoring: Pull all fullscreen code out of Document and into its own helper class
[WebKit-https.git] / LayoutTests / webaudio / audiocontext-state-interrupted.html
1 <!DOCTYPE html>
2
3 <html>
4 <head>
5 <script src="../resources/js-test.js"></script>
6 <script type="text/javascript" src="resources/audio-testing.js"></script>
7 </head>
8
9 <body>
10 <div id="description"></div>
11 <div id="console"></div>
12
13 <script>
14 description('Basic tests for AudioNode API.');
15
16 var context = null;
17 var node = null;
18
19 function runTest() {
20     window.jsTestIsAsync = true;
21
22     context = new webkitAudioContext();
23
24     shouldBe('context.state', '"suspended"');
25     context.onstatechange = beganRunning;
26
27     node = context.createBufferSource();
28     evalAndLog('node.connect(context.destination)');
29
30 }
31
32 function beganRunning(e) {
33     debug('EVENT ' + event.type);
34     shouldBe('context.state', '"running"');
35
36     debug('');
37     debug('Test 1: resume() while interrupted is resolved after the interruption ends.');
38
39     context.onstatechange = firstInterruptionStarted;
40     if (window.internals)
41         evalAndLog('internals.beginMediaSessionInterruption("System")');
42
43 }
44
45 function firstInterruptionStarted(e) {
46     debug('EVENT ' + event.type);
47     shouldBe('context.state', '"interrupted"');
48     if (window.internals)
49         evalAndLog('internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")');
50
51     context.onstatechange = null;
52
53     debug('Calling context.resume()');
54     context.resume().then(firstResumeSucceeded);
55     debug('Delaying 100ms');
56     setTimeout(function() {
57         shouldBe('context.state', '"interrupted"');
58         if (window.internals)
59             evalAndLog('internals.endMediaSessionInterruption("MayResumePlaying")');
60     }, 100);
61 }
62
63 function firstResumeSucceeded() {
64     testPassed('context.resume() promise resolved');
65     shouldBe('context.state', '"running"');
66
67     debug('');
68     debug('Test 2: resume() while interrupted will cause interruption to end.')
69
70     context.onstatechange = secondInterruptionStarted;
71     if (window.internals)
72         evalAndLog('internals.beginMediaSessionInterruption("System")');
73 }
74
75 function secondInterruptionStarted(e) {
76     debug('EVENT ' + event.type);
77     shouldBe('context.state', '"interrupted"');
78     if (window.internals)
79         evalAndLog('internals.setMediaSessionRestrictions("WebAudio", "")');
80
81     context.onstatechange = null;
82
83     debug('Calling context.resume()');
84     context.resume().then(secondResumeSucceeded);
85 }
86
87 function secondResumeSucceeded() {
88     testPassed('context.resume() promise resolved');
89     shouldBe('context.state', '"running"');
90
91     debug('');
92     debug('Test 3: running AudioContexts will not resume after an interruption ends.')
93
94     context.onstatechange = thirdInterruptionStarted;
95     if (window.internals)
96         evalAndLog('internals.beginMediaSessionInterruption("System")');
97 }
98
99 function thirdInterruptionStarted() {
100     debug('EVENT ' + event.type);
101     shouldBe('context.state', '"interrupted"');
102
103     context.onstatechange = thirdInterruptionEnded;
104     if (window.internals)
105         evalAndLog('internals.endMediaSessionInterruption("")');
106 }
107
108 function thirdInterruptionEnded() {
109     debug('EVENT ' + event.type);
110     shouldBe('context.state', '"suspended"');
111
112     debug('');
113     debug('Test 4: resume() while interrupted will not resume playback after an interruption.')
114
115     if (window.internals)
116         evalAndLog('internals.setMediaSessionRestrictions("WebAudio", "InterruptedPlaybackNotPermitted")');
117
118     context.onstatechange = fourthInterruptionStarted;
119     if (window.internals)
120         evalAndLog('internals.beginMediaSessionInterruption("System")');
121 }
122
123 function fourthInterruptionStarted() {
124     debug('EVENT ' + event.type);
125     shouldBe('context.state', '"interrupted"');
126
127     context.onstatechange = fourthInterruptionEnded;
128
129     debug('Calling context.resume()');
130     context.resume();
131
132     if (window.internals)
133         evalAndLog('internals.endMediaSessionInterruption("")');
134 }
135
136 function fourthInterruptionEnded() {
137     debug('EVENT ' + event.type);
138     shouldBe('context.state', '"suspended"');
139     finishJSTest();
140 }
141
142 runTest();
143
144 </script>
145 </body>
146 </html>