dom/audiochannel/tests/test_audioChannelChange.html

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:c0538d485d53
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test audio-channel-changed & visible-audio-channel-changed mozChromeEvent</title>
6 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8 </head>
9 <body>
10 <div id="content"></div>
11 <script type="application/javascript;version=1.7">
12 var expectedAudioTypes;
13 var expectedVisibleAudioTypes;
14 var expectedVisibleAudioType;
15 var index;
16 var visibleIndex;
17 var iframe1;
18 var normalAudio;
19
20 function playWithAudioType(audio, type) {
21 audio.mozAudioChannelType = type;
22 audio.src = "test.ogg";
23 audio.loop = true;
24
25 audio.play();
26 }
27
28 function fgBgTestListener(message) {
29 var type = message.type;
30 var channel = message.channel;
31
32 if (type == 'audio-channel-changed') {
33 is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
34 index++;
35 }
36
37 if (type == 'visible-audio-channel-changed') {
38 is(channel, expectedVisibleAudioType, channel + " is received and expected " + expectedVisibleAudioType);
39 }
40
41 // All audio types are playing now so ask to pause them.
42 // This call will stop audio from highest to telephony.
43 if ('cmd-pause' == expectedAudioTypes[index]) {
44 iframe1.src = 'file_audio.html#pauseAudio';
45 index++;
46 }
47
48 // According to there is a 1.5 second delay of releasing telephony,
49 // we need to wait for it then continue to pause others.
50 if ('cmd-secondPause' == expectedAudioTypes[index]) {
51 iframe1.src = 'file_audio.html#pauseAudioFollowing';
52 index++;
53 }
54
55 if (index == expectedAudioTypes.length) {
56 document.body.removeChild(iframe1);
57 script.removeMessageListener('chrome-event', fgBgTestListener);
58 normalAudio.pause();
59 SimpleTest.finish();
60 }
61 }
62
63 // Channel of visible-audio-channel-changed event should be always normal.
64 // Audios in background should not effect visible-audio-channel-changed.
65 function runFgBgTest() {
66 expectedAudioTypes = ["normal", "content", "notification",
67 "alarm", "telephony", "ringer", "publicnotification", "cmd-pause",
68 "ringer", "telephony", "alarm", "cmd-secondPause", "notification",
69 "content", "normal"];
70 expectedVisibleAudioType = "normal";
71 index = 0;
72
73 script.addMessageListener('chrome-event', fgBgTestListener);
74
75 // To play a audio with normal channel in the foreground.
76 normalAudio = new Audio();
77 playWithAudioType(normalAudio, 'normal');
78
79 iframe1.src = 'file_audio.html#bg';
80 document.body.appendChild(iframe1);
81 iframe1.setVisible(false);
82 }
83
84 function bgTestListener(message) {
85 var type = message.type;
86 var channel = message.channel;
87
88 if (type == 'audio-channel-changed') {
89 is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
90 index++;
91 }
92
93 if (type == 'visible-audio-channel-changed') {
94 is(channel, expectedVisibleAudioType, channel + " is received and expected " + expectedVisibleAudioType);
95 }
96
97 // All audio types are playing now so ask to pause them.
98 if ('cmd-pause' == expectedAudioTypes[index]) {
99 iframe1.src = 'file_audio.html#pauseAudio';
100 index++;
101 }
102
103 if ('cmd-secondPause' == expectedAudioTypes[index]) {
104 iframe1.src = 'file_audio.html#pauseAudioFollowing';
105 index++;
106 }
107
108 if (index == expectedAudioTypes.length) {
109 document.body.removeChild(iframe1);
110 script.removeMessageListener('chrome-event', bgTestListener);
111 runFgBgTest();
112 }
113 }
114
115 // 1. Channel of visible-audio-channel-changed event should be always none.
116 // 2. normal is not allowed to be played in the background.
117 function runBgTest() {
118 expectedAudioTypes = ["content", "notification",
119 "alarm", "telephony", "ringer", "publicnotification", "cmd-pause",
120 "ringer", "telephony", "alarm", "cmd-secondPause", "notification",
121 "content", "none"];
122 expectedVisibleAudioType = "none";
123 index = 0;
124
125 script.addMessageListener('chrome-event', bgTestListener);
126
127 iframe1.src = 'file_audio.html#bg';
128 document.body.appendChild(iframe1);
129 iframe1.setVisible(false);
130 }
131
132 function fgTestListener(message) {
133 var type = message.type;
134 var channel = message.channel;
135
136 if (type == 'audio-channel-changed') {
137 is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
138 index++;
139 }
140
141 if (type == 'visible-audio-channel-changed') {
142 is(channel, expectedAudioTypes[visibleIndex], channel + " is received and expected " + expectedAudioTypes[visibleIndex]);
143 visibleIndex++;
144 }
145
146 // All audio types are playing now so ask to pause them.
147 if ('cmd-pause' == expectedAudioTypes[visibleIndex] &&
148 'cmd-pause' == expectedAudioTypes[index]) {
149 iframe1.src = 'file_audio.html#pauseAudio';
150 visibleIndex++;
151 index++;
152 }
153
154 if ('cmd-secondPause' == expectedAudioTypes[visibleIndex] &&
155 'cmd-secondPause' == expectedAudioTypes[index]) {
156 iframe1.src = 'file_audio.html#pauseAudioFollowing';
157 visibleIndex++;
158 index++;
159 }
160
161 if (index == expectedAudioTypes.length && visibleIndex == expectedAudioTypes.length) {
162 document.body.removeChild(iframe1);
163 script.removeMessageListener('chrome-event', fgTestListener);
164 runBgTest();
165 }
166 }
167
168 // The foreground audio will effect both of audio-channel-changed and
169 // visible-audio-channel-changed.
170 function runFgTest() {
171 expectedAudioTypes = ["normal", "content", "notification",
172 "alarm", "telephony", "ringer", "publicnotification",
173 "cmd-pause", "ringer", "telephony", "alarm",
174 "cmd-secondPause", "notification", "content",
175 "normal", "none"];
176
177 index = 0;
178 visibleIndex = 0;
179
180 script.addMessageListener('chrome-event', fgTestListener);
181
182 iframe1 = document.createElement('iframe');
183 iframe1.setAttribute('mozbrowser', true);
184 iframe1.src = 'file_audio.html#fg';
185 document.body.appendChild(iframe1);
186 }
187
188 var url = SimpleTest.getTestFileURL("AudioChannelChromeScript.js")
189 var script = SpecialPowers.loadChromeScript(url);
190 script.sendAsyncMessage("init-chrome-event", {
191 type: 'audio-channel-changed'
192 });
193 script.sendAsyncMessage("init-chrome-event", {
194 type: 'visible-audio-channel-changed'
195 });
196
197 SimpleTest.waitForExplicitFinish();
198
199 SpecialPowers.pushPermissions(
200 [{ "type": "browser", "allow": 1, "context": document },
201 { "type": "embed-apps", "allow": 1, "context": document },
202 { "type": "webapps-manage", "allow": 1, "context": document }], function() {
203 SpecialPowers.pushPrefEnv({"set": [["dom.ipc.browser_frames.oop_by_default", true],
204 ["media.useAudioChannelService", true],
205 ["dom.mozBrowserFramesEnabled", true]]}, runFgTest);
206 });
207 </script>
208 </body>
209 </html>

mercurial