Bug 1235535 - part2 : create muted-by-default test in another new file. r=baku. draft
authorAlastor Wu <alwu@mozilla.com>
Tue, 05 Jan 2016 19:26:01 +0800
changeset 318941 b40c212206aaeb06f1b970485eab15cdffe45999
parent 318940 f1c93a003216bf2f6934f44dc82ecdde5d7c0e6d
child 512518 2252e0ed2631dd5c2ded36d847b856d67d7354ab
push id8946
push useralwu@mozilla.com
push dateTue, 05 Jan 2016 11:26:16 +0000
reviewersbaku
bugs1235535
milestone46.0a1
Bug 1235535 - part2 : create muted-by-default test in another new file. r=baku.
dom/browser-element/mochitest/browserElement_AudioChannelMutedByDefault.js
dom/browser-element/mochitest/file_browserElement_AudioChannelMutedByDefault.html
dom/browser-element/mochitest/mochitest-oop.ini
dom/browser-element/mochitest/mochitest.ini
dom/browser-element/mochitest/test_browserElement_inproc_AudioChannelMutedByDefault.html
dom/browser-element/mochitest/test_browserElement_oop_AudioChannelMutedByDefault.html
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/browserElement_AudioChannelMutedByDefault.js
@@ -0,0 +1,105 @@
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+browserElementTestHelpers.setEnabledPref(true);
+browserElementTestHelpers.addPermission();
+
+var fileURL = 'http://example.org/tests/dom/browser-element/mochitest/file_browserElement_AudioChannelMutedByDefault.html';
+var testFrame;
+var ac;
+
+function alertListener(e) {
+  var message = e.detail.message
+  if (/^OK/.exec(message)) {
+    ok(true, "Message from file : " + message);
+  } else if (/^KO/.exec(message)) {
+    error(message);
+  } else if (/DONE/.exec(message)) {
+    ok(true, "Audio playback success!");
+    finish();
+  } else {
+    error("Undefined event.");
+  }
+}
+
+function assert(aVal, aMessage) {
+  return (!aVal) ? error(aMessage) : 0;
+}
+
+function error(aMessage) {
+  ok(false, "Error : " + aMessage);
+  finish();
+}
+
+function finish() {
+  testFrame.removeEventListener('mozbrowsershowmodalprompt', alertListener);
+  document.body.removeChild(testFrame);
+  SimpleTest.finish();
+}
+
+function setCommand(aArg) {
+  assert(!!ac, "Audio channel doesn't exist!");
+  info("# Command = " + aArg);
+  testFrame.src = fileURL + '#' + aArg;
+
+  switch (aArg) {
+    case 'play':
+      ac.onactivestatechanged = () => {
+        ac.onactivestatechanged = null;
+        ok(true, "activestatechanged event received.");
+
+        new Promise(function(r, rr) {
+          ac.getMuted().onsuccess = function(e) {
+            is(e.target.result, true, "Muted channel by default");
+            r();
+          }
+        }).then(function() {
+          ac.setMuted(false).onsuccess = function(e) {
+            ok(true, "Unmuted the channel.");
+          }
+        });
+      };
+      break;
+    default :
+      error("Undefined command!");
+  }
+}
+
+function runTests() {
+  setCommand('play');
+}
+
+function setupTestFrame() {
+  testFrame = document.createElement('iframe');
+  testFrame.setAttribute('mozbrowser', 'true');
+  testFrame.setAttribute('mozapp', 'http://example.org/manifest.webapp');
+  testFrame.src = fileURL;
+
+  function loadend() {
+    testFrame.removeEventListener('mozbrowserloadend', loadend);
+    ok("allowedAudioChannels" in testFrame, "allowedAudioChannels exist");
+    var channels = testFrame.allowedAudioChannels;
+    is(channels.length, 1, "1 audio channel by default");
+
+    ac = channels[0];
+    ok(ac instanceof BrowserElementAudioChannel, "Correct class");
+    ok("getMuted" in ac, "ac.getMuted exists");
+    ok("setMuted" in ac, "ac.setMuted exists");
+    ok("onactivestatechanged" in ac, "onactivestatechanged exists");
+
+    runTests();
+  }
+
+  info("Set EventListeners.");
+  testFrame.addEventListener('mozbrowsershowmodalprompt', alertListener);
+  testFrame.addEventListener('mozbrowserloadend', loadend);
+  document.body.appendChild(testFrame);
+}
+
+addEventListener('testready', function() {
+  SpecialPowers.pushPrefEnv({'set': [["b2g.system_manifest_url", "http://mochi.test:8888/manifest.webapp"],
+                                     ["dom.audiochannel.mutedByDefault", true]]},
+                            function() {
+    SimpleTest.executeSoon(setupTestFrame);
+  });
+});
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/file_browserElement_AudioChannelMutedByDefault.html
@@ -0,0 +1,65 @@
+<html>
+<body>
+<script>
+var audio = new Audio("audio.ogg");
+var context = new AudioContext();
+var node = context.createMediaElementSource(audio);
+var sp = context.createScriptProcessor(2048, 1);
+node.connect(sp);
+var expectedSamplesCount;
+var nonzeroSamplesCount = 0;
+var isStarted = false;
+
+function ok(aVal, aMsg) {
+  alert((!!aVal ? "OK" : "KO") + ", " + aMsg);
+}
+
+function finish() {
+  audio.onended = null;
+  audio.pause();
+  alert("DONE");
+}
+
+function processSamples(e) {
+  var buf = e.inputBuffer.getChannelData(0);
+  for (var i = 0; i < buf.length; ++i) {
+    if (buf[i] != 0) {
+      if (!isStarted) {
+        isStarted = true;
+        ok(true, "Start process audio sample.");
+      }
+      nonzeroSamplesCount++;
+    }
+  }
+
+  if (nonzeroSamplesCount >= expectedSamplesCount) {
+    finish();
+  }
+}
+
+audio.oncanplaythrough = function() {
+  var testDuration = audio.duration > 1.0 ? 1.0 : audio.duration * 0.5;
+  expectedSamplesCount = Math.floor(testDuration * context.sampleRate);
+  sp.onaudioprocess = processSamples;
+};
+
+function runCommands()
+{
+  switch(location.hash) {
+    case '#play':
+      ok(true, "Audio starts playing.")
+      audio.play();
+      audio.onended = () => {
+        audio.onended = null;
+        ok(false, "Audio shouldn't go ended in this test!")
+      };
+      break;
+    default :
+      ok(false, "Undefined command!");
+  }
+}
+
+window.addEventListener('hashchange', runCommands);
+</script>
+</body>
+</html>
--- a/dom/browser-element/mochitest/mochitest-oop.ini
+++ b/dom/browser-element/mochitest/mochitest-oop.ini
@@ -22,16 +22,17 @@ skip-if = toolkit=='gonk' || (toolkit ==
 [test_browserElement_oop_Alert.html]
 [test_browserElement_oop_AlertInFrame.html]
 [test_browserElement_oop_AllowEmbedAppsInNestedOOIframe.html]
 skip-if = toolkit=='gonk'
 [test_browserElement_oop_AppFramePermission.html]
 skip-if = (toolkit == 'gonk' && !debug)
 [test_browserElement_oop_AppWindowNamespace.html]
 skip-if = (toolkit == 'gonk' && !debug)
+[test_browserElement_oop_AudioChannelMutedByDefault.html]
 [test_browserElement_oop_Auth.html]
 skip-if = (toolkit == 'gonk' && !debug)
 [test_browserElement_oop_BackForward.html]
 [test_browserElement_oop_BadScreenshot.html]
 [test_browserElement_oop_BrowserWindowNamespace.html]
 skip-if = (toolkit == 'gonk' && !debug)
 [test_browserElement_oop_BrowserWindowResize.html]
 [test_browserElement_oop_Close.html]
--- a/dom/browser-element/mochitest/mochitest.ini
+++ b/dom/browser-element/mochitest/mochitest.ini
@@ -5,16 +5,17 @@ support-files =
   ../../../dom/media/test/short-video.ogv
   async.js
   browserElementTestHelpers.js
   browserElement_Alert.js
   browserElement_AlertInFrame.js
   browserElement_AllowEmbedAppsInNestedOOIframe.js
   browserElement_AppFramePermission.js
   browserElement_AppWindowNamespace.js
+  browserElement_AudioChannelMutedByDefault.js
   browserElement_AudioPlayback.js
   browserElement_Auth.js
   browserElement_BackForward.js
   browserElement_BadScreenshot.js
   browserElement_Viewmode.js
   browserElement_ThemeColor.js
   browserElement_BrowserWindowNamespace.js
   browserElement_BrowserWindowResize.js
@@ -85,16 +86,17 @@ support-files =
   browserElement_AudioChannel.js
   browserElement_AudioChannel_nested.js
   file_browserElement_AlertInFrame.html
   file_browserElement_AlertInFrame_Inner.html
   file_browserElement_AllowEmbedAppsInNestedOOIframe.html
   file_browserElement_AppFramePermission.html
   file_browserElement_AppWindowNamespace.html
   file_browserElement_AudioChannel_nested.html
+  file_browserElement_AudioChannelMutedByDefault.html
   file_browserElement_Viewmode.html
   file_browserElement_ThemeColor.html
   file_browserElement_BrowserWindowNamespace.html
   file_browserElement_CloseApp.html
   file_browserElement_CloseFromOpener.html
   file_browserElement_CookiesNotThirdParty.html
   file_browserElement_DisallowEmbedAppsInOOP.html
   file_browserElement_ExecuteScript.html
@@ -156,16 +158,18 @@ support-files =
 [test_browserElement_inproc_Viewmode.html]
 [test_browserElement_inproc_ThemeColor.html]
 skip-if = buildapp == 'b2g'
 [test_browserElement_inproc_AlertInFrame.html]
 [test_browserElement_inproc_AppFramePermission.html]
 skip-if = toolkit == 'android' || buildapp == 'b2g'
 [test_browserElement_inproc_AppWindowNamespace.html]
 skip-if = toolkit == 'android' || buildapp == 'b2g' # android(TIMED_OUT, bug 783509) androidx86(TIMED_OUT, bug 783509)
+[test_browserElement_inproc_AudioChannelMutedByDefault.html]
+skip-if = toolkit == 'android'
 [test_browserElement_inproc_AudioPlayback.html]
 [test_browserElement_inproc_Auth.html]
 skip-if = buildapp == 'b2g'
 [test_browserElement_inproc_BackForward.html]
 [test_browserElement_inproc_BadScreenshot.html]
 [test_browserElement_inproc_BrowserWindowNamespace.html]
 skip-if = buildapp == 'b2g'
 [test_browserElement_inproc_BrowserWindowResize.html]
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/test_browserElement_inproc_AudioChannelMutedByDefault.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1235535 - Audio Channel Muted-By-Default.</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="browserElementTestHelpers.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script type="application/javascript;version=1.7" src="browserElement_AudioChannelMutedByDefault.js">
+</script>
+</body>
+</html>
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/dom/browser-element/mochitest/test_browserElement_oop_AudioChannelMutedByDefault.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1235535 - Audio Channel Muted-By-Default.</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="browserElementTestHelpers.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<script type="application/javascript;version=1.7" src="browserElement_AudioChannelMutedByDefault.js">
+</script>
+</body>
+</html>
\ No newline at end of file