dom/media/tests/mochitest/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var Cc = SpecialPowers.Cc;
michael@0 6 var Ci = SpecialPowers.Ci;
michael@0 7 var Cr = SpecialPowers.Cr;
michael@0 8
michael@0 9 // Specifies whether we are using fake streams to run this automation
michael@0 10 var FAKE_ENABLED = true;
michael@0 11
michael@0 12
michael@0 13 /**
michael@0 14 * Create the necessary HTML elements for head and body as used by Mochitests
michael@0 15 *
michael@0 16 * @param {object} meta
michael@0 17 * Meta information of the test
michael@0 18 * @param {string} meta.title
michael@0 19 * Description of the test
michael@0 20 * @param {string} [meta.bug]
michael@0 21 * Bug the test was created for
michael@0 22 * @param {boolean} [meta.visible=false]
michael@0 23 * Visibility of the media elements
michael@0 24 */
michael@0 25 function createHTML(meta) {
michael@0 26 var test = document.getElementById('test');
michael@0 27
michael@0 28 // Create the head content
michael@0 29 var elem = document.createElement('meta');
michael@0 30 elem.setAttribute('charset', 'utf-8');
michael@0 31 document.head.appendChild(elem);
michael@0 32
michael@0 33 var title = document.createElement('title');
michael@0 34 title.textContent = meta.title;
michael@0 35 document.head.appendChild(title);
michael@0 36
michael@0 37 // Create the body content
michael@0 38 var anchor = document.createElement('a');
michael@0 39 anchor.setAttribute('target', '_blank');
michael@0 40
michael@0 41 if (meta.bug) {
michael@0 42 anchor.setAttribute('href', 'https://bugzilla.mozilla.org/show_bug.cgi?id=' + meta.bug);
michael@0 43 }
michael@0 44
michael@0 45 anchor.textContent = meta.title;
michael@0 46 document.body.insertBefore(anchor, test);
michael@0 47
michael@0 48 var display = document.createElement('p');
michael@0 49 display.setAttribute('id', 'display');
michael@0 50 document.body.insertBefore(display, test);
michael@0 51
michael@0 52 var content = document.createElement('div');
michael@0 53 content.setAttribute('id', 'content');
michael@0 54 content.style.display = meta.visible ? 'block' : "none";
michael@0 55 document.body.appendChild(content);
michael@0 56 }
michael@0 57
michael@0 58
michael@0 59 /**
michael@0 60 * Create the HTML element if it doesn't exist yet and attach
michael@0 61 * it to the content node.
michael@0 62 *
michael@0 63 * @param {string} type
michael@0 64 * Type of media element to create ('audio' or 'video')
michael@0 65 * @param {string} label
michael@0 66 * Description to use for the element
michael@0 67 * @return {HTMLMediaElement} The created HTML media element
michael@0 68 */
michael@0 69 function createMediaElement(type, label) {
michael@0 70 var id = label + '_' + type;
michael@0 71 var element = document.getElementById(id);
michael@0 72
michael@0 73 // Sanity check that we haven't created the element already
michael@0 74 if (element)
michael@0 75 return element;
michael@0 76
michael@0 77 element = document.createElement(type === 'audio' ? 'audio' : 'video');
michael@0 78 element.setAttribute('id', id);
michael@0 79 element.setAttribute('height', 100);
michael@0 80 element.setAttribute('width', 150);
michael@0 81 element.setAttribute('controls', 'controls');
michael@0 82 document.getElementById('content').appendChild(element);
michael@0 83
michael@0 84 return element;
michael@0 85 }
michael@0 86
michael@0 87
michael@0 88 /**
michael@0 89 * Wrapper function for mozGetUserMedia to allow a singular area of control
michael@0 90 * for determining whether we run this with fake devices or not.
michael@0 91 *
michael@0 92 * @param {Dictionary} constraints
michael@0 93 * The constraints for this mozGetUserMedia callback
michael@0 94 * @param {Function} onSuccess
michael@0 95 * The success callback if the stream is successfully retrieved
michael@0 96 * @param {Function} onError
michael@0 97 * The error callback if the stream fails to be retrieved
michael@0 98 */
michael@0 99 function getUserMedia(constraints, onSuccess, onError) {
michael@0 100 constraints["fake"] = FAKE_ENABLED;
michael@0 101
michael@0 102 info("Call getUserMedia for " + JSON.stringify(constraints));
michael@0 103 navigator.mozGetUserMedia(constraints, onSuccess, onError);
michael@0 104 }
michael@0 105
michael@0 106
michael@0 107 /**
michael@0 108 * Setup any Mochitest for WebRTC by enabling the preference for
michael@0 109 * peer connections. As by bug 797979 it will also enable mozGetUserMedia()
michael@0 110 * and disable the mozGetUserMedia() permission checking.
michael@0 111 *
michael@0 112 * @param {Function} aCallback
michael@0 113 * Test method to execute after initialization
michael@0 114 */
michael@0 115 function runTest(aCallback) {
michael@0 116 if (window.SimpleTest) {
michael@0 117 // Running as a Mochitest.
michael@0 118 SimpleTest.waitForExplicitFinish();
michael@0 119 SpecialPowers.pushPrefEnv({'set': [
michael@0 120 ['dom.messageChannel.enabled', true],
michael@0 121 ['media.peerconnection.enabled', true],
michael@0 122 ['media.peerconnection.identity.enabled', true],
michael@0 123 ['media.peerconnection.identity.timeout', 12000],
michael@0 124 ['media.navigator.permission.disabled', true]]
michael@0 125 }, function () {
michael@0 126 try {
michael@0 127 aCallback();
michael@0 128 }
michael@0 129 catch (err) {
michael@0 130 generateErrorCallback()(err);
michael@0 131 }
michael@0 132 });
michael@0 133 } else {
michael@0 134 // Steeplechase, let it call the callback.
michael@0 135 window.run_test = function(is_initiator) {
michael@0 136 var options = {is_local: is_initiator,
michael@0 137 is_remote: !is_initiator};
michael@0 138 aCallback(options);
michael@0 139 };
michael@0 140 // Also load the steeplechase test code.
michael@0 141 var s = document.createElement("script");
michael@0 142 s.src = "/test.js";
michael@0 143 document.head.appendChild(s);
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 /**
michael@0 148 * Checks that the media stream tracks have the expected amount of tracks
michael@0 149 * with the correct kind and id based on the type and constraints given.
michael@0 150 *
michael@0 151 * @param {Object} constraints specifies whether the stream should have
michael@0 152 * audio, video, or both
michael@0 153 * @param {String} type the type of media stream tracks being checked
michael@0 154 * @param {sequence<MediaStreamTrack>} mediaStreamTracks the media stream
michael@0 155 * tracks being checked
michael@0 156 */
michael@0 157 function checkMediaStreamTracksByType(constraints, type, mediaStreamTracks) {
michael@0 158 if(constraints[type]) {
michael@0 159 is(mediaStreamTracks.length, 1, 'One ' + type + ' track shall be present');
michael@0 160
michael@0 161 if(mediaStreamTracks.length) {
michael@0 162 is(mediaStreamTracks[0].kind, type, 'Track kind should be ' + type);
michael@0 163 ok(mediaStreamTracks[0].id, 'Track id should be defined');
michael@0 164 }
michael@0 165 } else {
michael@0 166 is(mediaStreamTracks.length, 0, 'No ' + type + ' tracks shall be present');
michael@0 167 }
michael@0 168 }
michael@0 169
michael@0 170 /**
michael@0 171 * Check that the given media stream contains the expected media stream
michael@0 172 * tracks given the associated audio & video constraints provided.
michael@0 173 *
michael@0 174 * @param {Object} constraints specifies whether the stream should have
michael@0 175 * audio, video, or both
michael@0 176 * @param {MediaStream} mediaStream the media stream being checked
michael@0 177 */
michael@0 178 function checkMediaStreamTracks(constraints, mediaStream) {
michael@0 179 checkMediaStreamTracksByType(constraints, 'audio',
michael@0 180 mediaStream.getAudioTracks());
michael@0 181 checkMediaStreamTracksByType(constraints, 'video',
michael@0 182 mediaStream.getVideoTracks());
michael@0 183 }
michael@0 184
michael@0 185 /**
michael@0 186 * Utility methods
michael@0 187 */
michael@0 188
michael@0 189 /**
michael@0 190 * Returns the contents of a blob as text
michael@0 191 *
michael@0 192 * @param {Blob} blob
michael@0 193 The blob to retrieve the contents from
michael@0 194 * @param {Function} onSuccess
michael@0 195 Callback with the blobs content as parameter
michael@0 196 */
michael@0 197 function getBlobContent(blob, onSuccess) {
michael@0 198 var reader = new FileReader();
michael@0 199
michael@0 200 // Listen for 'onloadend' which will always be called after a success or failure
michael@0 201 reader.onloadend = function (event) {
michael@0 202 onSuccess(event.target.result);
michael@0 203 };
michael@0 204
michael@0 205 reader.readAsText(blob);
michael@0 206 }
michael@0 207
michael@0 208 /**
michael@0 209 * Generates a callback function fired only under unexpected circumstances
michael@0 210 * while running the tests. The generated function kills off the test as well
michael@0 211 * gracefully.
michael@0 212 *
michael@0 213 * @param {String} [message]
michael@0 214 * An optional message to show if no object gets passed into the
michael@0 215 * generated callback method.
michael@0 216 */
michael@0 217 function generateErrorCallback(message) {
michael@0 218 var stack = new Error().stack.split("\n");
michael@0 219 stack.shift(); // Don't include this instantiation frame
michael@0 220
michael@0 221 /**
michael@0 222 * @param {object} aObj
michael@0 223 * The object fired back from the callback
michael@0 224 */
michael@0 225 return function (aObj) {
michael@0 226 if (aObj) {
michael@0 227 if (aObj.name && aObj.message) {
michael@0 228 ok(false, "Unexpected callback for '" + aObj.name +
michael@0 229 "' with message = '" + aObj.message + "' at " +
michael@0 230 JSON.stringify(stack));
michael@0 231 } else {
michael@0 232 ok(false, "Unexpected callback with = '" + aObj +
michael@0 233 "' at: " + JSON.stringify(stack));
michael@0 234 }
michael@0 235 } else {
michael@0 236 ok(false, "Unexpected callback with message = '" + message +
michael@0 237 "' at: " + JSON.stringify(stack));
michael@0 238 }
michael@0 239 SimpleTest.finish();
michael@0 240 }
michael@0 241 }
michael@0 242
michael@0 243 /**
michael@0 244 * Generates a callback function fired only for unexpected events happening.
michael@0 245 *
michael@0 246 * @param {String} description
michael@0 247 Description of the object for which the event has been fired
michael@0 248 * @param {String} eventName
michael@0 249 Name of the unexpected event
michael@0 250 */
michael@0 251 function unexpectedEventAndFinish(message, eventName) {
michael@0 252 var stack = new Error().stack.split("\n");
michael@0 253 stack.shift(); // Don't include this instantiation frame
michael@0 254
michael@0 255 return function () {
michael@0 256 ok(false, "Unexpected event '" + eventName + "' fired with message = '" +
michael@0 257 message + "' at: " + JSON.stringify(stack));
michael@0 258 SimpleTest.finish();
michael@0 259 }
michael@0 260 }

mercurial