Bug 1552818 - part2 : refactor test 'test_audioNotificationNavigationWebAudio.html' by using async-await form. r=padenot
authoralwu <alwu@mozilla.com>
Tue, 02 Jul 2019 12:05:55 +0000
changeset 543831 e349eb0585f7835d6837532fef35019c73523cbf
parent 543830 e56260118399fa5a1d8b8274c804a996581292e2
child 543832 7c5b35e851d6d8a15ddb66991e8de06912be8bbe
push id2131
push userffxbld-merge
push dateMon, 26 Aug 2019 18:30:20 +0000
treeherdermozilla-release@b19ffb3ca153 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1552818
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1552818 - part2 : refactor test 'test_audioNotificationNavigationWebAudio.html' by using async-await form. r=padenot Using async and await form can increase the readability of the test. Differential Revision: https://phabricator.services.mozilla.com/D36535
dom/base/test/test_audioNotificationNavigationWebAudio.html
--- a/dom/base/test/test_audioNotificationNavigationWebAudio.html
+++ b/dom/base/test/test_audioNotificationNavigationWebAudio.html
@@ -1,79 +1,83 @@
 <!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>
-<pre id="test">
-</pre>
 <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");
 
-SimpleTest.waitForExplicitFinish();
+async function startTest() {
+  addObserver();
+  await startLoadingWebAudioInIframe();
+  await reloadIFrame();
+  cleanUpTestAndRemoveObserver();
+}
 
-var expectedNotification = null;
-var iframe = null;
-
-var observer = {
+const observer = {
   observe: function(subject, topic, data) {
     is(topic, "audio-playback", "audio-playback received");
-    var expected = expectedNotification.shift();
+    const expected = expectedNotifications.shift();
     is(data, expected, `"${data}" is the right notification`);
-    if (expectedNotification.length == 0) {
-      runTest();
+    if (expectedNotifications.length == 0) {
+      this.resolve();
     }
+  },
+  wait() {
+    return new Promise((resolve) => {
+      info(`Waiting for the notification "${expectedNotifications[0]}"`);
+      this.resolve = resolve;
+    });
   }
 };
 
-var observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
+const observerService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
                                    .getService(SpecialPowers.Ci.nsIObserverService);
-
-var tests = [
-  function() {
-    iframe = document.querySelector("iframe");
-    observerService.addObserver(observer, "audio-playback");
-    ok(true, "Observer set");
-    runTest();
-  },
-
-  function() {
-    info("Load iframe");
-    expectedNotification = ["active"];
-    iframe.src = "file_webAudioAudible.html";
-  },
+SimpleTest.waitForExplicitFinish();
+onload = startTest;
 
-  function() {
-    info("Reload iframe");
-    // As reloading frame would stop previous audio playing in frame, we would
-    // receive "inactive-pause" first.
-    expectedNotification = ["inactive-pause", "active"];
-    iframe.src = "file_webAudioAudible.html";
-  },
-  function() {
-    info("Cleaning up iframe");
-    expectedNotification = ["inactive-pause"];
-    iframe.src = null;
-  }
-];
+/**
+ * The following are test helper functions.
+ */
+function addObserver() {
+  observerService.addObserver(observer, "audio-playback");
+  ok(true, "Observer set");
+}
 
-function runTest() {
-  if (!tests.length) {
-    observerService.removeObserver(observer, "audio-playback");
-    ok(true, "Observer removed");
-    SimpleTest.finish();
-    return;
-  }
-
-  var test = tests.shift();
-  test();
+async function startLoadingWebAudioInIframe() {
+  info("Load iframe");
+  expectedNotifications = ["active"];
+  iframe.src = "file_webAudioAudible.html";
+  await observer.wait();
 }
 
-onload = runTest;
+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>
-