Bug 1223261 - Add the audio playback test for the case muted-by-default. r=baku

--HG--
extra : transplant_source : %B7%1C%28c%40Zq%7DG%858%E9%DF%84g%82%00rJ/
This commit is contained in:
Alastor Wu 2015-11-12 18:46:15 +08:00
Родитель f49b90a02f
Коммит 8e37489c47
3 изменённых файлов: 101 добавлений и 3 удалений

Просмотреть файл

@ -10,6 +10,7 @@ browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();
function noaudio() {
info("Test : no-audio");
var iframe = document.createElement('iframe');
iframe.setAttribute('mozbrowser', 'true');
iframe.setAttribute('mozapp', 'http://example.org/manifest.webapp');
@ -143,6 +144,7 @@ function noaudio() {
}
function audio() {
info("Test : audio");
var iframe = document.createElement('iframe');
iframe.setAttribute('mozbrowser', 'true');
iframe.setAttribute('mozapp', 'http://example.org/manifest.webapp');
@ -179,7 +181,59 @@ function audio() {
document.body.appendChild(iframe);
}
var tests = [ noaudio, audio ];
function audioMutedByDefault() {
info("Test : audio-muted-by-default");
SpecialPowers.pushPrefEnv(
{'set': [["dom.audiochannel.mutedByDefault", true]]}, function () {
var iframe = document.createElement('iframe');
iframe.setAttribute('mozbrowser', 'true');
iframe.setAttribute('mozapp', 'http://example.org/manifest.webapp');
iframe.src = 'http://example.org/tests/dom/browser-element/mochitest/file_processingAudioSample.html';
function audio_loadend_MutedByDefault() {
ok("allowedAudioChannels" in iframe, "allowedAudioChannels exist");
var channels = iframe.allowedAudioChannels;
is(channels.length, 1, "1 audio channel by default");
var ac = channels[0];
ok(ac instanceof BrowserElementAudioChannel, "Correct class");
ok("getMuted" in ac, "ac.getMuted exists");
ok("setMuted" in ac, "ac.setMuted exists");
ac.onactivestatechanged = function() {
ok(true, "activestatechanged event received.");
ac.onactivestatechanged = null;
new Promise(function(r, rr) {
ac.getMuted().onsuccess = function(e) {
is(e.target.result, true, "Muted channel by default");
r();
}
})
.then(function() {
ac.setMuted(false).onsuccess = function(e) {
ok(true, "Unmuted the channel.");
}
})
}
var complete = false;
iframe.addEventListener("mozbrowsershowmodalprompt", function (e) {
is(e.detail.message, "playback-success", "Audio playback success!");
if (!complete) {
SpecialPowers.popPrefEnv(runTests);
complete = true;
}
});
}
iframe.addEventListener('mozbrowserloadend', audio_loadend_MutedByDefault);
document.body.appendChild(iframe);
});
}
var tests = [ noaudio, audio, audioMutedByDefault ];
function runTests() {
if (tests.length == 0) {
@ -191,8 +245,6 @@ function runTests() {
test();
}
addEventListener('load', function() {
SimpleTest.executeSoon(runTests);
});

Просмотреть файл

@ -0,0 +1,45 @@
<html>
<body>
<script>
var audio = new Audio("audio.ogg");
var context = new AudioContext();
var node = context.createMediaElementSource(audio);
var sp = context.createScriptProcessor(2048, 1);
node.connect(sp);
var expectedSamplesCount;
var nonzeroSamplesCount = 0;
var complete = false;
var iterationCount = 0;
function processSamples(e) {
if (complete) {
return;
}
// Start playing the audio until the AudioContext is connected and running.
if (iterationCount++ == 0) {
audio.play();
}
var buf = e.inputBuffer.getChannelData(0);
for (var i = 0; i < buf.length; ++i) {
if (buf[i] != 0) {
nonzeroSamplesCount++;
}
}
if (nonzeroSamplesCount >= expectedSamplesCount && !complete) {
alert("playback-success");
complete = true;
}
}
audio.oncanplaythrough = function() {
var testDuration = audio.duration > 2.0 ? 2.0 : audio.duration;
expectedSamplesCount = Math.floor(testDuration * context.sampleRate) ;
sp.onaudioprocess = processSamples;
};
</script>
</body>
</html>

Просмотреть файл

@ -140,6 +140,7 @@ support-files =
file_web_manifest.html
file_web_manifest.json
file_illegal_web_manifest.html
file_processingAudioSample.html
# Note: browserElementTestHelpers.js looks at the test's filename to determine
# whether the test should be OOP. "_oop_" signals OOP, "_inproc_" signals in