Bug 1242874 - part5 : add test. draft
authorAlastor Wu <alwu@mozilla.com>
Thu, 21 Apr 2016 18:15:00 +0800
changeset 354584 9fe6e9ed0c46b8598ee85f50bde6d384cb01c7f4
parent 354583 f4da87dc7e3f59ff1f4466a0d54cd90ed3a89b3a
child 519049 f7d93db9286f196436b24155d5b319e2a9d26dd2
push id16130
push useralwu@mozilla.com
push dateThu, 21 Apr 2016 10:15:27 +0000
bugs1242874
milestone48.0a1
Bug 1242874 - part5 : add test. MozReview-Commit-ID: 6XE24lvUlYi
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_mediaPlayback_suspended.js
toolkit/content/tests/browser/browser_mediaPlayback_suspended_multipleAudio.js
toolkit/content/tests/browser/file_multipleAudio.html
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -26,24 +26,38 @@ support-files =
 skip-if = e10s # Bug ?????? - test directly manipulates content (gBrowser.contentDocument.getElementById("postForm").submit();)
 [browser_content_url_annotation.js]
 skip-if = !e10s || !crashreporter
 support-files =
   file_redirect.html
   file_redirect_to.html
 [browser_bug1170531.js]
 [browser_mediaPlayback.js]
+tags = audiochannel
 support-files =
   file_mediaPlayback.html
   file_mediaPlaybackFrame.html
 [browser_mediaPlayback_mute.js]
+tags = audiochannel
 support-files =
   file_mediaPlayback2.html
   file_mediaPlaybackFrame2.html
 skip-if = buildapp == 'mulet' || buildapp == 'b2g'
+[browser_mediaPlayback_suspended.js]
+tags = audiochannel
+support-files =
+  file_mediaPlayback2.html
+skip-if = buildapp == 'mulet' || buildapp == 'b2g'
+[browser_mediaPlayback_suspended_multipleAudio.js]
+tags = audiochannel
+support-files =
+  file_multipleAudio.html
+skip-if = buildapp == 'mulet' || buildapp == 'b2g'
 [browser_mute.js]
+tags = audiochannel
 [browser_mute2.js]
+tags = audiochannel
 skip-if = buildapp == 'mulet' || buildapp == 'b2g'
 [browser_quickfind_editable.js]
 [browser_saveImageURL.js]
 support-files =
   image.jpg
   image_page.html
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_mediaPlayback_suspended.js
@@ -0,0 +1,164 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_mediaPlayback2.html";
+
+var SuspendedType = {
+  NONE_SUSPENDED             : 0,
+  SUSPENDED_PAUSE            : 1,
+  SUSPENDED_BLOCK            : 2,
+  SUSPENDED_PAUSE_DISPOSABLE : 4
+};
+
+function wait_for_event(browser, event) {
+  return BrowserTestUtils.waitForEvent(browser, event, false, (event) => {
+    is(event.originalTarget, browser, "Event must be dispatched to correct browser.");
+    return true;
+  });
+}
+
+function check_audio_suspended(suspendedType) {
+  var list = content.document.getElementsByTagName('audio');
+  if (list.length != 1) {
+    ok(false, "There should be only one audio element in page!")
+  }
+
+  var audio = list[0];
+  is(audio.computedSuspended, suspendedType,
+     "The suspended state of MediaElement is correct.");
+}
+
+function check_audio_pause_state(expectedPauseState) {
+  var list = content.document.getElementsByTagName('audio');
+  if (list.length != 1) {
+    ok(false, "There should be only one audio element in page!")
+  }
+
+  var audio = list[0];
+  if (expectedPauseState) {
+    is(audio.paused, true, "Audio is paused correctly.");
+  } else {
+    is(audio.paused, false, "Audio is resumed correctly.");
+  }
+}
+
+function* suspended_pause(url, browser) {
+  info("### Start test for suspended-pause ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the suspended state of audio should be non-suspened -");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+
+  info("- pause playing audio -");
+  browser.pauseMedia(false /* non-disposable */);
+  yield ContentTask.spawn(browser, true /* expect for pause */,
+                                   check_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE,
+                                   check_audio_suspended);
+
+  info("- resume paused audio -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, false /* expect for playing */,
+                                   check_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+}
+
+function* suspended_pause_disposable(url, browser) {
+  info("### Start test for suspended-pause-disposable ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the suspended state of audio should be non-suspened -");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+
+  info("- pause playing audio -");
+  browser.pauseMedia(true /* disposable */);
+  yield ContentTask.spawn(browser, true /* expect for pause */,
+                                   check_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE_DISPOSABLE,
+                                   check_audio_suspended);
+
+  info("- resume paused audio -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, false /* expect for playing */,
+                                   check_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+}
+
+function* suspended_stop_disposable(url, browser) {
+  info("### Start test for suspended-stop-disposable ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the suspended state of audio should be non-suspened -");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+
+  info("- stop playing audio -");
+  browser.stopMedia();
+  yield wait_for_event(browser, "DOMAudioPlaybackStopped");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+}
+
+function* suspended_block(url, browser) {
+  info("### Start test for suspended-block ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- block playing audio -");
+  browser.blockMedia();
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_BLOCK,
+                                   check_audio_suspended);
+
+  info("- resume blocked audio -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_audio_suspended);
+}
+
+add_task(function* setup_test_preference() {
+  yield new Promise(resolve => {
+    SpecialPowers.pushPrefEnv({"set": [
+      ["media.useAudioChannelService.testing", true]
+    ]}, resolve);
+  });
+});
+
+add_task(function* test_suspended_pause() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_pause.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_pause_disposable() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_pause_disposable.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_stop_disposable() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_stop_disposable.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_block() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_block.bind(this, PAGE));
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_mediaPlayback_suspended_multipleAudio.js
@@ -0,0 +1,325 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_multipleAudio.html";
+
+var SuspendedType = {
+  NONE_SUSPENDED             : 0,
+  SUSPENDED_PAUSE            : 1,
+  SUSPENDED_BLOCK            : 2,
+  SUSPENDED_PAUSE_DISPOSABLE : 4
+};
+
+function wait_for_event(browser, event) {
+  return BrowserTestUtils.waitForEvent(browser, event, false, (event) => {
+    is(event.originalTarget, browser, "Event must be dispatched to correct browser.");
+    return true;
+  });
+}
+
+function check_all_audio_suspended(suspendedType) {
+  var autoPlay = content.document.getElementById('autoplay');
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!autoPlay || !nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(autoPlay.computedSuspended, suspendedType,
+     "The suspeded state of autoplay audio is correct.");
+  is(nonAutoPlay.computedSuspended, suspendedType,
+     "The suspeded state of non-autoplay audio is correct.");
+}
+
+function check_autoplay_audio_suspended(suspendedType) {
+  var autoPlay = content.document.getElementById('autoplay');
+  if (!autoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(autoPlay.computedSuspended, suspendedType,
+     "The suspeded state of autoplay audio is correct.");
+}
+
+function check_nonautoplay_audio_suspended(suspendedType) {
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(nonAutoPlay.computedSuspended, suspendedType,
+     "The suspeded state of non-autoplay audio is correct.");
+}
+
+function check_autoplay_audio_pause_state(expectedPauseState) {
+  var autoPlay = content.document.getElementById('autoplay');
+  if (!autoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  if (expectedPauseState) {
+    is(autoPlay.paused, true, "Audio is paused correctly.");
+  } else {
+    is(autoPlay.paused, false, "Audio is resumed correctly.");
+  }
+}
+
+function play_nonautoplay_audio_should_be_paused() {
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  nonAutoPlay.play();
+  return new Promise(resolve => {
+    nonAutoPlay.onpause = function () {
+      nonAutoPlay.onpause = null;
+      is(nonAutoPlay.ended, false, "Audio can't be playback.");
+      resolve();
+    }
+  });
+}
+
+function all_audio_onresume() {
+  var autoPlay = content.document.getElementById('autoplay');
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!autoPlay || !nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  return new Promise(resolve => {
+    var resumeCounter = 0;
+    function resume() {
+      if (++resumeCounter == 2) {
+        resolve();
+        autoPlay.onplay = nonAutoPlay.onplay = null;
+        ok(true, "Both audio are resumed at the same time.");
+      }
+    }
+    autoPlay.onplay = resume;
+    nonAutoPlay.onplay = resume;
+  });
+}
+
+function all_audio_onpause() {
+  var autoPlay = content.document.getElementById('autoplay');
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!autoPlay || !nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  return new Promise(resolve => {
+    var pauseCounter = 0;
+    function pause() {
+      if (++pauseCounter == 2) {
+        resolve();
+        autoPlay.onpause = nonAutoPlay.onpause = null;
+        ok(true, "Both audio are paused at the same time.");
+      }
+    }
+    autoPlay.onpause = pause;
+    nonAutoPlay.onpause = pause;
+  });
+}
+
+function play_nonautoplay_audio_should_play_until_ended() {
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  nonAutoPlay.play();
+  return new Promise(resolve => {
+    nonAutoPlay.onended = function () {
+      nonAutoPlay.onended = null;
+      ok(true, "Audio can be playback until ended.");
+      resolve();
+    }
+  });
+}
+
+function no_audio_resumed() {
+  var autoPlay = content.document.getElementById('autoplay');
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!autoPlay || !nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(autoPlay.paused && nonAutoPlay.paused, true, "No audio was resumed.");
+}
+
+function play_nonautoplay_audio_should_be_blocked(suspendedType) {
+  var nonAutoPlay = content.document.getElementById('nonautoplay');
+  if (!nonAutoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  nonAutoPlay.play();
+  return new Promise(resolve => {
+    nonAutoPlay.onplay = function () {
+      nonAutoPlay.onplay = null;
+      is(nonAutoPlay.computedSuspended, suspendedType,
+         "The suspeded state of non-autoplay audio is correct.");
+      resolve();
+    }
+  });
+}
+
+function* suspended_pause(url, browser) {
+  info("### Start test for suspended-pause ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the default suspended state of all audio should be non-suspened-");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- pause all audio in the page -");
+  browser.pauseMedia(false /* non-disposable */);
+  yield ContentTask.spawn(browser, true /* expect for pause */,
+                                   check_autoplay_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE,
+                                   check_autoplay_audio_suspended);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_nonautoplay_audio_suspended);
+
+  info("- no audio can be playback during suspended-paused -");
+  yield ContentTask.spawn(browser, null,
+                                   play_nonautoplay_audio_should_be_paused);
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE,
+                                   check_nonautoplay_audio_suspended);
+
+  info("- both audio should be resumed at the same time -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, null,
+                                   all_audio_onresume);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- both audio should be paused at the same time -");
+  browser.pauseMedia(false /* non-disposable */);
+  yield ContentTask.spawn(browser, null, all_audio_onpause);
+}
+
+function* suspended_pause_disposable(url, browser) {
+  info("### Start test for suspended-pause-disposable ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the default suspended state of all audio should be non-suspened -");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- only pause playing audio in the page -");
+  browser.pauseMedia(true /* non-disposable */);
+  yield ContentTask.spawn(browser, true /* expect for pause */,
+                                   check_autoplay_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_PAUSE_DISPOSABLE,
+                                   check_autoplay_audio_suspended);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_nonautoplay_audio_suspended);
+
+  info("- new playing audio should be playback correctly -");
+  yield ContentTask.spawn(browser, null,
+                                   play_nonautoplay_audio_should_play_until_ended);
+
+  info("- should only resume one audio -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, false /* expect for playing */,
+                                   check_autoplay_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+}
+
+function* suspended_stop_disposable(url, browser) {
+  info("### Start test for suspended-stop-disposable ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the default suspended state of all audio should be non-suspened -");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- only stop playing audio in the page -");
+  browser.stopMedia();
+  yield ContentTask.spawn(browser, true /* expect for pause */,
+                                   check_autoplay_audio_pause_state);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- new playing audio should be playback correctly -");
+  yield ContentTask.spawn(browser, null,
+                                   play_nonautoplay_audio_should_play_until_ended);
+
+  info("- no any audio can be resumed by page -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, null, no_audio_resumed);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+}
+
+function* suspended_block(url, browser) {
+  info("### Start test for suspended-block ###");
+  browser.loadURI(url);
+
+  info("- page should have playing audio -");
+  yield wait_for_event(browser, "DOMAudioPlaybackStarted");
+
+  info("- the default suspended state of all audio should be non-suspened-");
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+
+  info("- block autoplay audio -");
+  browser.blockMedia();
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_BLOCK,
+                                   check_autoplay_audio_suspended);
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_nonautoplay_audio_suspended);
+
+  info("- no audio can be playback during suspended-block -");
+  yield ContentTask.spawn(browser, SuspendedType.SUSPENDED_BLOCK,
+                                   play_nonautoplay_audio_should_be_blocked);
+
+  info("- both audio should be resumed at the same time -");
+  browser.resumeMedia();
+  yield ContentTask.spawn(browser, SuspendedType.NONE_SUSPENDED,
+                                   check_all_audio_suspended);
+}
+
+add_task(function* setup_test_preference() {
+  yield new Promise(resolve => {
+    SpecialPowers.pushPrefEnv({"set": [
+      ["media.useAudioChannelService.testing", true]
+    ]}, resolve);
+  });
+});
+
+add_task(function* test_suspended_pause() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_pause.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_pause_disposable() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_pause_disposable.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_stop_disposable() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_stop_disposable.bind(this, PAGE));
+});
+
+add_task(function* test_suspended_block() {
+  yield BrowserTestUtils.withNewTab({
+      gBrowser,
+      url: "about:blank"
+    }, suspended_block.bind(this, PAGE));
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_multipleAudio.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<body>
+<script type="text/javascript">
+</script>
+<audio id="autoplay" src="audio.ogg" autoplay></audio>
+<audio id="nonautoplay" src="audio.ogg"></audio>
+</body>