Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / ManualTests / webaudio / suspend-resume.html
blob38f1b5ea00db9039d74571f1548b1a311ca9c8b7
1 <!doctype html>
2 <html>
3 <head>
4 <title>Test suspend/resume</title>
5 <style type="text/css">
6 header {
7 margin: 20px 0;
9 #results {
10 white-space: pre;
11 font-family: monospace;
13 </style>
14 </head>
16 <body>
17 <h1>Test suspend/resume</h1>
19 <p>Press "Start" button to start the test using a single oscillator.</p>
20 <p>Then press "Suspend" or "Resume" buttons as desired. "Suspend" should stop the audio.
21 "Resume" should resume the audio immediately.</p>
23 <p>Some tests to run:</p>
25 <ul>
26 <li>Suspend/Resume Test
27 <ol>
28 <li>Press "Start test"; audio should be heard.</li>
29 <li>Press "Suspend" multiple times. The first press should stop the audio. Each
30 subsequent press should do nothing, but the console should show that each press is
31 resolved successfully.
32 </li>
33 <li>Press "Resume". The first press should resume audio immediately. Each subsequent
34 press should do nothing, but the console should show that each press is resolved
35 successfully.
36 </li>
37 <li>Press "Stop test" to stop the test; audio should stop.</li>
38 </ol>
39 </li>
40 <li>Closed Test
41 <ol>
42 <li>Press "Start test"; audio should be heard.</li>
43 <li>Press "Stop test"; audio should stop</li>
44 <li>Press "Suspend" or "Resume". Each press should result in a rejected promise
45 immediately with an error that suspend or resume is invalid on a closed context.
46 </li>
47 </ol>
48 </li>
49 </ul>
51 <p>See <a href="crbug.com/476429">issue 476429</a></p>
53 <button onclick="startTest()">Start test</button>
54 <button onclick="stopTest()">Stop test</button>
55 <button onclick="suspend()">Suspend</button>
56 <button onclick="resume()">Resume</button>
58 <header>Results</header>
59 <div id="results"></div>
61 <script>
62 // This is loosely based on the test from isue 476429.
63 var context;
64 var osc;
66 var nextSuspendID = 0;
67 var nextResumeID = 0;
69 function startTest() {
70 try {
71 context = new AudioContext() || webkitAudioContext();
72 } catch (e) {
73 log("Neither AudioContext nor webkitAudioContext found");
74 return;
77 if (!context.suspend || !context.resume) {
78 log("AudioContext lacks suspend/resume support");
79 return;
82 osc = context.createOscillator();
83 osc.connect(context.destination);
84 osc.start();
87 function suspend() {
88 var suspendID = nextSuspendID++;
89 log("suspend request: " + suspendID);
90 context.suspend()
91 .then(function() {
92 log("suspend resolved: " + suspendID);
93 }, function(e) {
94 log("suspend rejected: " + suspendID + "; error: " + e);
95 });
98 function resume() {
99 var resumeID = nextResumeID++;
100 log("resume request: " + resumeID);
101 context.resume()
102 .then(function() {
103 log("resume resolved: " + resumeID);
104 }, function(e) {
105 log("resume rejected: " + resumeID + "; error: " + e);
109 function stopTest() {
110 if (context) {
111 osc.stop();
112 osc = null;
113 context.close()
114 .then(function() {
115 log("Context closed.");
116 }, function(e) {
117 log("Unable to close context: " + e);
122 function log(message) {
123 console.log(message);
124 var results = document.querySelector("#results");
125 results.textContent += message + "\n";
127 </script>
128 </body>
129 </html>