Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / automatic-pull-node.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
11 <div id="description"></div>
12 <div id="console"></div>
13
14 <script>
15 description("This test verifies that the AudioBasicInspectorNode based nodes work correctly.");
16
17 var sampleRate = 44100.0;
18 // We carefully arrange the renderLengthInFrames to be a multiple of the AudioNode rendering quantum (AudioNode::ProcessingSizeInFrames)
19 // so that AudioSourceNode will not feed tailing zeroes into the context and fail this test.
20 var renderLengthInFrames = 256;
21 var fftSize = 64;
22
23 var audioDataOne = 255; // Audio data 1.0 in Uint8 format will be 255.
24 var audioDataZero = 128; // Audio data 0 in Uint8 format will be 128.
25
26 var context;
27 var constantBuffer;
28 var bufferSource;
29 var analyser;
30
31 function constructCommonGraph() {
32     // Create offline audio context.
33     context = new webkitOfflineAudioContext(1, renderLengthInFrames, sampleRate);
34     constantBuffer = createConstantBuffer(context, renderLengthInFrames, 1);
35
36     bufferSource = context.createBufferSource();
37     bufferSource.buffer = constantBuffer;
38
39     analyser = context.createAnalyser();
40     analyser.fftSize = fftSize;
41
42     bufferSource.connect(analyser);
43 }
44
45 function test1Finished() {
46     var timeDomainData = new Uint8Array(fftSize);
47     analyser.getByteTimeDomainData(timeDomainData);
48
49     if (timeDomainData[0] >= audioDataOne)
50         testPassed("RealtimeAnalyserNode got pulled when connected from upstream node but not to downstream node.");
51     else
52         testFailed("RealtimeAnalyserNode failed to get pulled when connected from upstream node but not to downstream node.");
53
54     test2();
55 }
56
57 // To verify the realtimeAnalyser can pull data when there is an upstream node connected to it
58 // but it's not connected to a downstream node.
59 function test1() {
60     constructCommonGraph();
61
62     bufferSource.start(0);
63
64     context.oncomplete = test1Finished;
65     context.startRendering();
66 }
67
68 function test2Finished() {
69     var timeDomainData = new Uint8Array(fftSize);
70     analyser.getByteTimeDomainData(timeDomainData);
71
72     if (timeDomainData[0] >= audioDataOne)
73         testPassed("RealtimeAnalyserNode got pulled when connected from upstream node and to destination node.");
74     else
75         testFailed("RealtimeAnalyserNode failed to be pulled when connected by upstream node and to destination node.");
76
77     test3();
78 }
79
80 // To verify the realtimeAnalyser can process normally when there is an upstream node connected to it
81 // and it's also connected to a downstream node which ultimately connect to audio destination.
82 function test2() {
83     constructCommonGraph();
84
85     analyser.connect(context.destination);
86
87     bufferSource.start(0);
88
89     context.oncomplete = test2Finished;
90     context.startRendering();
91 }
92
93 function test3Finished() {
94     var timeDomainData = new Uint8Array(fftSize);
95     analyser.getByteTimeDomainData(timeDomainData);
96
97     // If realtimeAnalyser hasn't pulled any data, the data in buffer will be 0.
98     if (timeDomainData[0] == audioDataZero)
99         testPassed("RealtimeAnalyserNode didn't get pulled when it should not.");
100     else
101         testFailed("RealtimeAnalyserNode been pulled when it should not.");
102
103     finishJSTest();
104 }
105
106 // To verify the realtimeAnalyser will stop pulling if it is connected to a downstream node
107 // which is not ultimatly connected to any audio destination.
108 function test3() {
109     constructCommonGraph();
110
111     var gain = context.createGain();
112     analyser.connect(gain);
113
114     bufferSource.start(0);
115
116     context.oncomplete = test3Finished;
117     context.startRendering();
118 }
119
120 function runTest() {
121     if (window.testRunner) {
122         testRunner.dumpAsText();
123         testRunner.waitUntilDone();
124     }
125
126     window.jsTestIsAsync = true;
127
128     test1();
129 }
130
131 runTest();
132
133 </script>
134
135 </body>
136 </html>