Bug 1645668 - part2 : add test. r=bryce, a=RyanVM
authoralwu <alwu@mozilla.com>
Tue, 30 Jun 2020 19:55:01 +0000
changeset 601881 ecf6eea8a56f522e84c0022feb209e896b2cd0bf
parent 601880 f00bb12f9f8e08865feedf7bb6d26d6f52c6ebb0
child 601882 cfd2cb91e252d9508706ae5df22df702e70c213d
push id13337
push userryanvm@gmail.com
push dateWed, 01 Jul 2020 18:37:50 +0000
treeherdermozilla-beta@ecf6eea8a56f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbryce, RyanVM
bugs1645668
milestone79.0
Bug 1645668 - part2 : add test. r=bryce, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D81632
dom/base/test/mochitest.ini
dom/base/test/test_audioNotification_reloadMedia.html
--- a/dom/base/test/mochitest.ini
+++ b/dom/base/test/mochitest.ini
@@ -283,16 +283,18 @@ skip-if = (os == "win" && processor == "
 tags = audiochannel
 skip-if = (os == "win" && processor == "aarch64") # bug 1535775
 [test_audioNotificationNavigationWebAudio.html]
 tags = audiochannel
 skip-if = (os == "win" && processor == "aarch64") # bug 1535775
 [test_audioNotificationWithEarlyPlay.html]
 tags = audiochannel
 skip-if = (os == "win" && processor == "aarch64") # bug 1535775
+[test_audioNotification_reloadMedia.html]
+tags = audiochannel
 [test_base.xhtml]
 [test_bug1433073.html]
 skip-if = (os == "android" || headless) # See
 # https://bugzilla.mozilla.org/show_bug.cgi?id=1632196 and
 # https://bugzilla.mozilla.org/show_bug.cgi?id=1632438.
 [test_bug5141.html]
 [test_bug28293.html]
 [test_bug28293.xhtml]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_audioNotification_reloadMedia.html
@@ -0,0 +1,99 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Should receive 'active' notification when reloading a media</title>
+  <script src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<audio id="testAudio" loop controls>
+<script type="application/javascript">
+/**
+ * When playing media becomes audible, we should receive 'active' notification.
+ * In this case, we want to ensure that we can receive 'active' notification
+ * after reloading resource on a media element which was playing before.
+ */
+const observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
+                                     .getService(SpecialPowers.Ci.nsIObserverService);
+const audioPlaybackObserver = {
+  observe(subject, topic, data) {
+    is(topic, "audio-playback", "audio-playback received");
+    info(`received ${data}`);
+    if (data == this._expectedNotification) {
+      ok(true, `received ${this._expectedNotification}`);
+      this._resolve();
+    }
+  },
+  waitFor(expectedNotification) {
+    return new Promise((resolve) => {
+      info(`Waiting for the notification "${expectedNotification}"`);
+      this._resolve = resolve;
+      this._expectedNotification = expectedNotification;
+    });
+  }
+};
+
+SimpleTest.waitForExplicitFinish();
+
+async function startTest() {
+  info(`start media and should receive 'active' notification`);
+  await loadAndStartMediaPlayback();
+
+  info(`abort current media and should receive 'inactive' notification`);
+  await abortPlaybackByUnsetSrc();
+
+  info(`start media and should receive 'active' notification again`);
+  await loadAndStartMediaPlayback();
+
+  finishTest();
+}
+
+onload = addObserver;
+
+/**
+ * The following are helper functions.
+ */
+function addObserver() {
+  observerService.addObserver(audioPlaybackObserver, "audio-playback");
+  info("Observer set");
+  startTest();
+}
+
+function removeObserver() {
+  observerService.removeObserver(audioPlaybackObserver, "audio-playback");
+  info("Observer removed");
+}
+
+function waitUntilMediaBecomesActive() {
+  return audioPlaybackObserver.waitFor("active");
+}
+
+function waitUntilMediaBecomesInactive() {
+  return audioPlaybackObserver.waitFor("inactive-nonaudible");
+}
+
+async function loadAndStartMediaPlayback() {
+  const activePromise = waitUntilMediaBecomesActive();
+  const audio = document.getElementById("testAudio");
+  audio.src = "audio.ogg";
+  audio.play();
+  await activePromise;
+}
+
+async function abortPlaybackByUnsetSrc() {
+  const inactivePromise = waitUntilMediaBecomesInactive();
+  const audio = document.getElementById("testAudio");
+  audio.src = "";
+  audio.removeAttribute("src");
+  await inactivePromise;
+}
+
+function finishTest() {
+  removeObserver();
+  document.getElementById("testAudio").remove();
+  SimpleTest.finish();
+}
+
+</script>
+</body>
+</html>