gecko-dev/dom/base/test/test_audioNotificationNavig...

84 строки
2.3 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test for audio controller in windows, when using the Web Audio API</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<iframe></iframe>
<script type="application/javascript">
/**
* This test is used to ensure that the `audio-playback` notification would be
* dispatched correctly when we start playing web audio from a iframe.
*/
var expectedNotifications = null;
const iframe = document.querySelector("iframe");
async function startTest() {
addObserver();
await startLoadingWebAudioInIframe();
await reloadIFrame();
cleanUpTestAndRemoveObserver();
}
const observer = {
observe(subject, topic, data) {
is(topic, "audio-playback", "audio-playback received");
const expected = expectedNotifications.shift();
is(data, expected, `"${data}" is the right notification`);
if (expectedNotifications.length == 0) {
this.resolve();
}
},
wait() {
return new Promise((resolve) => {
info(`Waiting for the notification "${expectedNotifications[0]}"`);
this.resolve = resolve;
});
}
};
const observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
.getService(SpecialPowers.Ci.nsIObserverService);
SimpleTest.waitForExplicitFinish();
onload = startTest;
/**
* The following are test helper functions.
*/
function addObserver() {
observerService.addObserver(observer, "audio-playback");
ok(true, "Observer set");
}
async function startLoadingWebAudioInIframe() {
info("Load iframe");
expectedNotifications = ["active"];
iframe.src = "file_webAudioAudible.html";
await observer.wait();
}
async function reloadIFrame() {
info("Reload iframe");
// As reloading frame would stop previous audio playing in frame, we would
// receive "inactive-pause" first.
expectedNotifications = ["inactive-pause", "active"];
iframe.src = "file_webAudioAudible.html";
await observer.wait();
}
async function cleanUpTestAndRemoveObserver() {
info("Cleaning up iframe");
expectedNotifications = ["inactive-pause"];
iframe.src = null;
await observer.wait();
observerService.removeObserver(observer, "audio-playback");
ok(true, "Observer removed");
SimpleTest.finish();
}
</script>
</body>
</html>