diff options
Diffstat (limited to 'b2g/chrome/content/test/mochitest')
9 files changed, 546 insertions, 0 deletions
diff --git a/b2g/chrome/content/test/mochitest/RecordingStatusChromeScript.js b/b2g/chrome/content/test/mochitest/RecordingStatusChromeScript.js new file mode 100644 index 000000000..1a5ed8274 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/RecordingStatusChromeScript.js @@ -0,0 +1,40 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components; +const { Services } = Cu.import('resource://gre/modules/Services.jsm'); +const { SystemAppProxy } = Cu.import('resource://gre/modules/SystemAppProxy.jsm'); + +var processId; + +function peekChildId(aSubject, aTopic, aData) { + Services.obs.removeObserver(peekChildId, 'recording-device-events'); + Services.obs.removeObserver(peekChildId, 'recording-device-ipc-events'); + let props = aSubject.QueryInterface(Ci.nsIPropertyBag2); + if (props.hasKey('childID')) { + processId = props.get('childID'); + } +} + +addMessageListener('init-chrome-event', function(message) { + // listen mozChromeEvent and forward to content process. + let type = message.type; + SystemAppProxy.addEventListener('mozChromeEvent', function(event) { + let details = event.detail; + if (details.type === type) { + sendAsyncMessage('chrome-event', details); + } + }, true); + + Services.obs.addObserver(peekChildId, 'recording-device-events', false); + Services.obs.addObserver(peekChildId, 'recording-device-ipc-events', false); +}); + +addMessageListener('fake-content-shutdown', function(message) { + let props = Cc["@mozilla.org/hash-property-bag;1"] + .createInstance(Ci.nsIWritablePropertyBag2); + if (processId) { + props.setPropertyAsUint64('childID', processId); + } + Services.obs.notifyObservers(props, 'recording-device-ipc-events', 'content-shutdown'); +}); diff --git a/b2g/chrome/content/test/mochitest/RecordingStatusHelper.js b/b2g/chrome/content/test/mochitest/RecordingStatusHelper.js new file mode 100644 index 000000000..5e3e6814e --- /dev/null +++ b/b2g/chrome/content/test/mochitest/RecordingStatusHelper.js @@ -0,0 +1,82 @@ +'use strict'; + +// resolve multiple promise in parallel +function expectAll(aValue) { + let deferred = new Promise(function(resolve, reject) { + let countdown = aValue.length; + let resolutionValues = new Array(countdown); + + for (let i = 0; i < aValue.length; i++) { + let index = i; + aValue[i].then(function(val) { + resolutionValues[index] = val; + if (--countdown === 0) { + resolve(resolutionValues); + } + }, reject); + } + }); + + return deferred; +} + +function TestInit() { + let url = SimpleTest.getTestFileURL("RecordingStatusChromeScript.js") + let script = SpecialPowers.loadChromeScript(url); + + let helper = { + finish: function () { + script.destroy(); + }, + fakeShutdown: function () { + script.sendAsyncMessage('fake-content-shutdown', {}); + } + }; + + script.addMessageListener('chrome-event', function (message) { + if (helper.hasOwnProperty('onEvent')) { + helper.onEvent(message); + } else { + ok(false, 'unexpected message: ' + JSON.stringify(message)); + } + }); + + script.sendAsyncMessage("init-chrome-event", { + type: 'recording-status' + }); + + return Promise.resolve(helper); +} + +function expectEvent(expected, eventHelper) { + return new Promise(function(resolve, reject) { + eventHelper.onEvent = function(message) { + delete eventHelper.onEvent; + ok(message, JSON.stringify(message)); + is(message.type, 'recording-status', 'event type: ' + message.type); + is(message.active, expected.active, 'recording active: ' + message.active); + is(message.isAudio, expected.isAudio, 'audio recording active: ' + message.isAudio); + is(message.isVideo, expected.isVideo, 'video recording active: ' + message.isVideo); + resolve(eventHelper); + }; + info('waiting for recording-status'); + }); +} + +function expectStream(params, callback) { + return new Promise(function(resolve, reject) { + var req = navigator.mozGetUserMedia( + params, + function(stream) { + ok(true, 'create media stream'); + callback(stream); + resolve(); + }, + function(err) { + ok(false, 'fail to create media stream'); + reject(err); + } + ); + info('waiting for gUM result'); + }); +} diff --git a/b2g/chrome/content/test/mochitest/file_getusermedia_iframe.html b/b2g/chrome/content/test/mochitest/file_getusermedia_iframe.html new file mode 100644 index 000000000..f2b18eab3 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/file_getusermedia_iframe.html @@ -0,0 +1,36 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Iframe for Recording Status</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript;version=1.7" src="RecordingStatusHelper.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<pre id="test"> +<script class="testbody" type="text/javascript;version=1.7"> + +var localStream; + +window.addEventListener('message', function(event) { + switch (event.data) { + case 'start': + let gumDeferred = expectStream({ audio: true, + fake: true + }, function(stream) { + localStream = stream; + event.source.postMessage('start-finished', window.location.origin); + }); + break; + case 'stop': + localStream.stop(); + localStream = null; + break; + } +}, false); + +</script> +</pre> +</body> +</html> diff --git a/b2g/chrome/content/test/mochitest/mochitest.ini b/b2g/chrome/content/test/mochitest/mochitest.ini new file mode 100644 index 000000000..d18a20401 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/mochitest.ini @@ -0,0 +1,11 @@ +[DEFAULT] +skip-if = ((buildapp == 'mulet' || buildapp == 'b2g') && toolkit != 'gonk') #require OOP support for mochitest-b2g-desktop, Bug 957554 +support-files = + RecordingStatusChromeScript.js + RecordingStatusHelper.js + file_getusermedia_iframe.html + +[test_recordingStatus_basic.html] +[test_recordingStatus_multiple_requests.html] +[test_recordingStatus_iframe.html] +[test_recordingStatus_kill_content_process.html] diff --git a/b2g/chrome/content/test/mochitest/moz.build b/b2g/chrome/content/test/mochitest/moz.build new file mode 100644 index 000000000..3b13ba431 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +MOCHITEST_MANIFESTS += ['mochitest.ini'] diff --git a/b2g/chrome/content/test/mochitest/test_recordingStatus_basic.html b/b2g/chrome/content/test/mochitest/test_recordingStatus_basic.html new file mode 100644 index 000000000..21f746d33 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/test_recordingStatus_basic.html @@ -0,0 +1,119 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Recording Status</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript;version=1.7" src="RecordingStatusHelper.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<pre id="test"> +<script class="testbody" type="text/javascript;version=1.7"> +'use strict'; + +SimpleTest.waitForExplicitFinish(); + +function test() { + let localStreams = []; + TestInit().then(function(eventHelper) { + /* step 1: create one audio stream + * expect: see one mozChromeEvent for audio recording start. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: false + }, eventHelper); + + let gumDeferred = expectStream({ audio: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 2: close the audio stream + * expect: see one mozChromeEvent for recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false, + }, eventHelper); + + localStreams.shift().stop(); + info('stop audio stream'); + return eventDeferred; + }).then(function(eventHelper) { + /* step 3: create one video stream + * expect: see one mozChromeEvent for video recording start + */ + let eventDeferred = expectEvent({ active: true, + isAudio: false, + isVideo: true + }, eventHelper); + + let gumDeferred = expectStream({ video: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 4: close the audio stream + * expect: see one mozChromeEvent for recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false, + }, eventHelper); + + localStreams.shift().stop(); + info('stop video stream'); + return eventDeferred; + }).then(function(eventHelper) { + /* step 3: create one audio/video stream + * expect: see one mozChromeEvent for audio/video recording start + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: true + }, eventHelper); + + let gumDeferred = expectStream({ audio: true, + video: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 4: close the audio stream + * expect: see one mozChromeEvent for recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false, + }, eventHelper); + + localStreams.shift().stop(); + info('stop audio/video stream'); + return eventDeferred; + }).then(function(eventHelper) { + eventHelper.finish(); + SimpleTest.finish(); + }); +} + +SpecialPowers.pushPrefEnv({ + "set": [ + ['media.navigator.permission.disabled', true] + ] +}, test); + +</script> +</pre> +</body> +</html> diff --git a/b2g/chrome/content/test/mochitest/test_recordingStatus_iframe.html b/b2g/chrome/content/test/mochitest/test_recordingStatus_iframe.html new file mode 100644 index 000000000..88c33c897 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/test_recordingStatus_iframe.html @@ -0,0 +1,71 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Recording Status in iframe</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript;version=1.7" src="RecordingStatusHelper.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<pre id="test"> +<iframe id="gum-iframe"></iframe> +<script class="testbody" type="text/javascript;version=1.7"> +SimpleTest.waitForExplicitFinish(); + +function test() { + TestInit().then(function(eventHelper) { + /* step 1: load iframe whilch creates audio stream + * expect: see one mozChromeEvent for audio recording start. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: false + }, eventHelper); + + let loadDeferred = new Promise(function(resolve, reject) { + let gumIframe = document.getElementById('gum-iframe'); + gumIframe.src = 'file_getusermedia_iframe.html'; + + window.addEventListener('message', function(event) { + if (event.data === 'start-finished') { + resolve(); + } + }, false); + + gumIframe.onload = function() { + info('start audio stream in iframe'); + gumIframe.contentWindow.postMessage('start', window.location.origin); + }; + }); + + return expectAll([eventDeferred, loadDeferred]); + }).then(function([eventHelper]) { + /* step 2: close the audio stream + * expect: see one mozChromeEvent for recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false + }, eventHelper); + + let win = document.getElementById('gum-iframe').contentWindow; + win.postMessage('stop', window.location.origin); + info('stop audio stream in iframe'); + return eventDeferred; + }).then(function(eventHelper) { + eventHelper.finish(); + SimpleTest.finish(); + }); +} + +SpecialPowers.pushPrefEnv({ + "set": [ + ['media.navigator.permission.disabled', true] + ] +}, test); + +</script> +</pre> +</body> +</html> diff --git a/b2g/chrome/content/test/mochitest/test_recordingStatus_kill_content_process.html b/b2g/chrome/content/test/mochitest/test_recordingStatus_kill_content_process.html new file mode 100644 index 000000000..239c2c2d5 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/test_recordingStatus_kill_content_process.html @@ -0,0 +1,72 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Recording Status after process shutdown</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript;version=1.7" src="RecordingStatusHelper.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<pre id="test"> +<script class="testbody" type="text/javascript;version=1.7"> +SimpleTest.waitForExplicitFinish(); + +function test() { + let localStreams = []; + TestInit().then(function(eventHelper) { + /* step 1: load iframe whilch creates audio stream + * expect: see one mozChromeEvent for audio recording start. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: false + }, eventHelper); + + let gumDeferred = expectStream({ audio: true, + fake: true + }, function(stream) { localStreams.push(stream); }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 2: create video stream + * expect: see one mozChromeEvent for audio recording start. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: true + }, eventHelper); + + let gumDeferred = expectStream({ video: true, + fake: true + }, function(stream) { localStreams.push(stream); }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 3: close the audio stream + * expect: see one mozChromeEvent for recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false + }, eventHelper); + + eventHelper.fakeShutdown(); + info('simulate content process been killed'); + return eventDeferred; + }).then(function(eventHelper) { + eventHelper.finish(); + SimpleTest.finish(); + }); +} + +SpecialPowers.pushPrefEnv({ + "set": [ + ['media.navigator.permission.disabled', true] + ] +}, test); + +</script> +</pre> +</body> +</html> diff --git a/b2g/chrome/content/test/mochitest/test_recordingStatus_multiple_requests.html b/b2g/chrome/content/test/mochitest/test_recordingStatus_multiple_requests.html new file mode 100644 index 000000000..7d31a94f8 --- /dev/null +++ b/b2g/chrome/content/test/mochitest/test_recordingStatus_multiple_requests.html @@ -0,0 +1,108 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Recording Status with multiple gUM requests</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript;version=1.7" src="RecordingStatusHelper.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<pre id="test"> +<script class="testbody" type="text/javascript;version=1.7"> +'use strict'; + +SimpleTest.waitForExplicitFinish(); + +function test() { + let localStreams = []; + TestInit().then(function(eventHelper) { + /* step 1: create one audio stream + * expect: see one mozChromeEvent for recording start. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: false + }, eventHelper); + + let gumDeferred = expectStream({ audio: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 2: create another audio stream + * expect: no mozChromeEvent after audio stream is created + */ + let gumDeferred = expectStream({ audio: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([Promise.resolve(eventHelper), gumDeferred]); + }).then(function([eventHelper]) { + /* step 3: create video stream + * expect: see one mozChromeEvent for recording start + */ + let eventDeferred = expectEvent({ active: true, + isAudio: true, + isVideo: true + }, eventHelper); + + let gumDeferred = expectStream({ video: true, + fake: true + }, function(stream) { + localStreams.push(stream); + }); + + return expectAll([eventDeferred, gumDeferred]); + }).then(function([eventHelper]) { + /* step 4: stop first audio stream + * expect: no mozChromeEvent after first audio stream is stopped + */ + localStreams.shift().stop(); + info('stop the first audio stream'); + return Promise.resolve(eventHelper); + }).then(function(eventHelper) { + /* step 5: stop the second audio stream + * expect: see one mozChromeEvent for audio recording stop. + */ + let eventDeferred = expectEvent({ active: true, + isAudio: false, + isVideo: true + }, eventHelper); + + localStreams.shift().stop(); + info('stop the second audio stream'); + return eventDeferred; + }).then(function(eventHelper) { + /* step 6: stop the video stream + * expect: see one mozChromeEvent for video recording stop. + */ + let eventDeferred = expectEvent({ active: false, + isAudio: false, + isVideo: false + }, eventHelper); + + localStreams.shift().stop(); + info('stop the video stream'); + return eventDeferred; + }).then(function(eventHelper) { + eventHelper.finish(); + SimpleTest.finish(); + }); +} + +SpecialPowers.pushPrefEnv({ + "set": [ + ['media.navigator.permission.disabled', true] + ] +}, test); + +</script> +</pre> +</body> +</html> |