gecko/dom/audiochannel/tests/test_audioChannelChange.html

210 lines
7.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test audio-channel-changed & visible-audio-channel-changed mozChromeEvent</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="content"></div>
<script type="application/javascript;version=1.7">
var expectedAudioTypes;
var expectedVisibleAudioTypes;
var expectedVisibleAudioType;
var index;
var visibleIndex;
var iframe1;
var normalAudio;
function playWithAudioType(audio, type) {
audio.mozAudioChannelType = type;
audio.src = "test.ogg";
audio.loop = true;
audio.play();
}
function fgBgTestListener(message) {
var type = message.type;
var channel = message.channel;
if (type == 'audio-channel-changed') {
is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
index++;
}
if (type == 'visible-audio-channel-changed') {
is(channel, expectedVisibleAudioType, channel + " is received and expected " + expectedVisibleAudioType);
}
// All audio types are playing now so ask to pause them.
// This call will stop audio from highest to telephony.
if ('cmd-pause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudio';
index++;
}
// According to there is a 1.5 second delay of releasing telephony,
// we need to wait for it then continue to pause others.
if ('cmd-secondPause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudioFollowing';
index++;
}
if (index == expectedAudioTypes.length) {
document.body.removeChild(iframe1);
script.removeMessageListener('chrome-event', fgBgTestListener);
normalAudio.pause();
SimpleTest.finish();
}
}
// Channel of visible-audio-channel-changed event should be always normal.
// Audios in background should not effect visible-audio-channel-changed.
function runFgBgTest() {
expectedAudioTypes = ["normal", "content", "notification",
"alarm", "telephony", "ringer", "publicnotification", "cmd-pause",
"ringer", "telephony", "alarm", "cmd-secondPause", "notification",
"content", "normal"];
expectedVisibleAudioType = "normal";
index = 0;
script.addMessageListener('chrome-event', fgBgTestListener);
// To play a audio with normal channel in the foreground.
normalAudio = new Audio();
playWithAudioType(normalAudio, 'normal');
iframe1.src = 'file_audio.html#bg';
document.body.appendChild(iframe1);
iframe1.setVisible(false);
}
function bgTestListener(message) {
var type = message.type;
var channel = message.channel;
if (type == 'audio-channel-changed') {
is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
index++;
}
if (type == 'visible-audio-channel-changed') {
is(channel, expectedVisibleAudioType, channel + " is received and expected " + expectedVisibleAudioType);
}
// All audio types are playing now so ask to pause them.
if ('cmd-pause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudio';
index++;
}
if ('cmd-secondPause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudioFollowing';
index++;
}
if (index == expectedAudioTypes.length) {
document.body.removeChild(iframe1);
script.removeMessageListener('chrome-event', bgTestListener);
runFgBgTest();
}
}
// 1. Channel of visible-audio-channel-changed event should be always none.
// 2. normal is not allowed to be played in the background.
function runBgTest() {
expectedAudioTypes = ["content", "notification",
"alarm", "telephony", "ringer", "publicnotification", "cmd-pause",
"ringer", "telephony", "alarm", "cmd-secondPause", "notification",
"content", "none"];
expectedVisibleAudioType = "none";
index = 0;
script.addMessageListener('chrome-event', bgTestListener);
iframe1.src = 'file_audio.html#bg';
document.body.appendChild(iframe1);
iframe1.setVisible(false);
}
function fgTestListener(message) {
var type = message.type;
var channel = message.channel;
if (type == 'audio-channel-changed') {
is(channel, expectedAudioTypes[index], channel + " is received and expected " + expectedAudioTypes[index]);
index++;
}
if (type == 'visible-audio-channel-changed') {
is(channel, expectedAudioTypes[visibleIndex], channel + " is received and expected " + expectedAudioTypes[visibleIndex]);
visibleIndex++;
}
// All audio types are playing now so ask to pause them.
if ('cmd-pause' == expectedAudioTypes[visibleIndex] &&
'cmd-pause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudio';
visibleIndex++;
index++;
}
if ('cmd-secondPause' == expectedAudioTypes[visibleIndex] &&
'cmd-secondPause' == expectedAudioTypes[index]) {
iframe1.src = 'file_audio.html#pauseAudioFollowing';
visibleIndex++;
index++;
}
if (index == expectedAudioTypes.length && visibleIndex == expectedAudioTypes.length) {
document.body.removeChild(iframe1);
script.removeMessageListener('chrome-event', fgTestListener);
runBgTest();
}
}
// The foreground audio will effect both of audio-channel-changed and
// visible-audio-channel-changed.
function runFgTest() {
expectedAudioTypes = ["normal", "content", "notification",
"alarm", "telephony", "ringer", "publicnotification",
"cmd-pause", "ringer", "telephony", "alarm",
"cmd-secondPause", "notification", "content",
"normal", "none"];
index = 0;
visibleIndex = 0;
script.addMessageListener('chrome-event', fgTestListener);
iframe1 = document.createElement('iframe');
iframe1.setAttribute('mozbrowser', true);
iframe1.src = 'file_audio.html#fg';
document.body.appendChild(iframe1);
}
var url = SimpleTest.getTestFileURL("AudioChannelChromeScript.js")
var script = SpecialPowers.loadChromeScript(url);
script.sendAsyncMessage("init-chrome-event", {
type: 'audio-channel-changed'
});
script.sendAsyncMessage("init-chrome-event", {
type: 'visible-audio-channel-changed'
});
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPermissions(
[{ "type": "browser", "allow": 1, "context": document },
{ "type": "embed-apps", "allow": 1, "context": document },
{ "type": "webapps-manage", "allow": 1, "context": document }], function() {
SpecialPowers.pushPrefEnv({"set": [["dom.ipc.browser_frames.oop_by_default", true],
["media.useAudioChannelService", true],
["dom.mozBrowserFramesEnabled", true]]}, runFgTest);
});
</script>
</body>
</html>