Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / audiobuffersource-playbackState.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <script src="resources/audio-testing.js"></script>
5 <script src="../resources/js-test.js"></script>
6 <script src="resources/compatibility.js"></script>
7 </head>
8
9 <body>
10 <div id="description"></div>
11 <div id="console"></div>
12
13 <script>
14 description("Test AudioContext activeSourceCount and AudioBufferSourceNode playbackState.");
15
16 // Create a few sources that start and end playing at various times.  After rendering, check that
17 // each source is in the correct state and that the number of active sources is correct.
18
19 var sampleRate = 44100;
20
21 // Render for this long.
22 var renderTime = 2;
23 var renderLength = timeToSampleFrame(renderTime, sampleRate);
24
25 var context;
26
27 // List of AudioBufferSourceNodes sources.
28 var sources = [];
29
30 // List of messages that will be printed out on success (or failure).  Messages are in the same
31 // order as the sources list above.
32 var sourceMessages = [];
33
34 // List of the expected playback state for each source.  In the same order a sources list above.
35 var sourceExpectedStates = [];
36
37 // Array mapping the playback state to a string.
38 var playbackStateName = ["UNSCHEDULED_STATE ",
39                          "SCHEDULED_STATE   ",
40                          "PLAYING_STATE     ",
41                          "FINISHED_STATE    "];
42
43 function checkResult(event)
44 {
45     var success = true;
46
47     // For each source, verify that the playback state matches our expected playback state.
48     for (var k = 0; k < sources.length; ++k) {
49         var prefix = playbackStateName[sourceExpectedStates[k]] + sourceMessages[k];
50         if (sources[k].playbackState == sourceExpectedStates[k]) {
51             testPassed(prefix);
52         } else {
53             testFailed(prefix + ": Actual = " + playbackStateName[sources[k].playbackState]);
54             success = false;
55         }
56     }
57
58     // Figure out how many active sources there should be from the expected states.
59     var playingState = sources[0].PLAYING_STATE
60
61     var expectedActiveCount = 0;
62     for (k = 0; k < sourceExpectedStates.length; ++k) {
63         if (sourceExpectedStates[k] == playingState) {
64             ++expectedActiveCount;
65         }
66     }
67       
68     if (context.activeSourceCount == expectedActiveCount) {
69         testPassed(context.activeSourceCount + " are currently playing as expected.");
70     } else {
71         testFailed(context.activeSourceCount + " are currently playing, but expected " + expectedActiveCount + ".");
72         success = false;
73     }
74
75     if (success) {
76         testPassed("activeSourceCount and playbackState tests succeeded.");
77     } else {
78         testFailed("activeSourceCount and playbackState tests did not succeed.");
79     }
80
81     finishJSTest();
82 }
83
84 // sourceLength - length of source in seconds
85 // noteFunction - function to turn on source appropriately
86 // expectedState - expected state of the source at the end of rendering
87 // message - message to be displayed if test passes
88 function createTest(sourceLength, noteFunction, expectedState, message)
89 {
90     var s = context.createBufferSource();
91     s.buffer = createImpulseBuffer(context, timeToSampleFrame(sourceLength, sampleRate));
92     s.connect(context.destination);
93     noteFunction(s);
94     sources.push(s);
95     sourceMessages.push(message);
96     sourceExpectedStates.push(expectedState);
97 }
98
99 function runTest()
100 {
101     if (window.testRunner) {
102         testRunner.dumpAsText();
103         testRunner.waitUntilDone();
104     }
105
106     window.jsTestIsAsync = true;
107
108     // Create offline audio context, rendering for renderTime seconds.
109     context = new OfflineAudioContext(2, timeToSampleFrame(renderTime, sampleRate), sampleRate);
110
111     // This is only used so we can access the playback state constants.
112     var bufferSource = context.createBufferSource();
113
114     // Dummy message so we know how long we're rendering so we can interpret the pass/fail messages
115     // correctly.
116     testPassed("Rendering time is " + renderTime + " seconds.");
117
118     // Test unscheduled state. Create 3 second source, but don't schedule it.
119
120     createTest(3,
121                function(s) { },
122                bufferSource.UNSCHEDULED_STATE,
123                "Source has been created");
124
125     // Test start.
126
127     createTest(3,
128                function(s) { s.start(renderTime + 1); },
129                bufferSource.SCHEDULED_STATE,
130                "3 second source scheduled to start at time " + (renderTime + 1));
131
132     createTest(2,
133                function(s) { s.start(1); },
134                bufferSource.PLAYING_STATE,
135                "2 second source starting at time 1");
136       
137     createTest(1.25,
138                function(s) { s.start(0); },
139                bufferSource.FINISHED_STATE,
140                "1.25 second source starting at time 0");
141
142     // Test start grain.
143
144     createTest(3,
145                function(s) { s.start(renderTime + 1, 0, 1); },
146                bufferSource.SCHEDULED_STATE,
147                "1 second grain scheduled to start at time " + (renderTime + 1));
148   
149     createTest(3,
150                function(s) { s.start(0.5, 0, 2); },
151                bufferSource.PLAYING_STATE,
152                "2 second grain starting at time 0.5");
153       
154     createTest(3,
155                function(s) { s.start(0.5, 0, 1); },
156                bufferSource.FINISHED_STATE,
157                "1 second grain starting at time 0.5");
158
159     // Test looping source
160
161     createTest(0.5,
162                function(s) { s.loop = true; s.start(renderTime + 1); },
163                bufferSource.SCHEDULED_STATE,
164                "a looping 0.5 second source scheduled at time " + (renderTime + 1));
165
166     createTest(0.5,
167                function(s) { s.loop = true; s.start(1.25); },
168                bufferSource.PLAYING_STATE,
169                "a looping 0.5 second source starting at time 1.25");
170
171     context.oncomplete = checkResult;
172     context.startRendering();
173 }
174       
175 runTest();
176 successfullyParsed = true;
177   
178 </script>
179
180 </body>
181 </html>