1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/system/gonk/tests/header_helpers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.10 + 1.11 + 1.12 +let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.13 + .getService(Ci.mozIJSSubScriptLoader); 1.14 + 1.15 +/** 1.16 + * Start a new RIL worker. 1.17 + * 1.18 + * @param custom_ns 1.19 + * Namespace with symbols to be injected into the new worker 1.20 + * namespace. 1.21 + * 1.22 + * @return an object that represents the worker's namespace. 1.23 + * 1.24 + * @note that this does not start an actual worker thread. The worker 1.25 + * is executed on the main thread, within a separate namespace object. 1.26 + */ 1.27 +function newWorker(custom_ns) { 1.28 + let worker_ns = { 1.29 + importScripts: function() { 1.30 + Array.slice(arguments).forEach(function(script) { 1.31 + if (!script.startsWith("resource:")) { 1.32 + script = "resource://gre/modules/" + script; 1.33 + } 1.34 + subscriptLoader.loadSubScript(script, this); 1.35 + }, this); 1.36 + }, 1.37 + 1.38 + postRILMessage: function(message) { 1.39 + }, 1.40 + 1.41 + postMessage: function(message) { 1.42 + }, 1.43 + 1.44 + // Define these variables inside the worker scope so ES5 strict mode 1.45 + // doesn't flip out. 1.46 + onmessage: undefined, 1.47 + onerror: undefined, 1.48 + 1.49 + DEBUG: true 1.50 + }; 1.51 + // The 'self' variable in a worker points to the worker's own namespace. 1.52 + worker_ns.self = worker_ns; 1.53 + 1.54 + // Copy the custom definitions over. 1.55 + for (let key in custom_ns) { 1.56 + worker_ns[key] = custom_ns[key]; 1.57 + } 1.58 + 1.59 + // fake require() for toolkit/components/workerloader/require.js 1.60 + let require = (function() { 1.61 + return function require(script) { 1.62 + worker_ns.module = {}; 1.63 + worker_ns.importScripts(script); 1.64 + return worker_ns; 1.65 + } 1.66 + })(); 1.67 + 1.68 + Object.freeze(require); 1.69 + Object.defineProperty(worker_ns, "require", { 1.70 + value: require, 1.71 + enumerable: true, 1.72 + configurable: false 1.73 + }); 1.74 + 1.75 + // Load the RIL worker itself. 1.76 + worker_ns.importScripts("ril_worker.js"); 1.77 + 1.78 + // Register at least one client. 1.79 + worker_ns.ContextPool.registerClient({ clientId: 0 }); 1.80 + 1.81 + return worker_ns; 1.82 +} 1.83 + 1.84 +/** 1.85 + * Create a parcel suitable for postRILMessage(). 1.86 + * 1.87 + * @param fakeParcelSize 1.88 + * Value to be written to parcel size field for testing 1.89 + * incorrect/incomplete parcel reading. Replaced with correct 1.90 + * one determined length of data if negative. 1.91 + * @param response 1.92 + * Response code of the incoming parcel. 1.93 + * @param request 1.94 + * Request code of the incoming parcel. 1.95 + * @param data 1.96 + * Extra data to be appended. 1.97 + * 1.98 + * @return an Uint8Array carrying all parcel data. 1.99 + */ 1.100 +function newIncomingParcel(fakeParcelSize, response, request, data) { 1.101 + const UINT32_SIZE = 4; 1.102 + const PARCEL_SIZE_SIZE = 4; 1.103 + 1.104 + let realParcelSize = data.length + 2 * UINT32_SIZE; 1.105 + let buffer = new ArrayBuffer(realParcelSize + PARCEL_SIZE_SIZE); 1.106 + let bytes = new Uint8Array(buffer); 1.107 + 1.108 + let writeIndex = 0; 1.109 + function writeUint8(value) { 1.110 + bytes[writeIndex] = value; 1.111 + ++writeIndex; 1.112 + } 1.113 + 1.114 + function writeInt32(value) { 1.115 + writeUint8(value & 0xff); 1.116 + writeUint8((value >> 8) & 0xff); 1.117 + writeUint8((value >> 16) & 0xff); 1.118 + writeUint8((value >> 24) & 0xff); 1.119 + } 1.120 + 1.121 + function writeParcelSize(value) { 1.122 + writeUint8((value >> 24) & 0xff); 1.123 + writeUint8((value >> 16) & 0xff); 1.124 + writeUint8((value >> 8) & 0xff); 1.125 + writeUint8(value & 0xff); 1.126 + } 1.127 + 1.128 + if (fakeParcelSize < 0) { 1.129 + fakeParcelSize = realParcelSize; 1.130 + } 1.131 + writeParcelSize(fakeParcelSize); 1.132 + 1.133 + writeInt32(response); 1.134 + writeInt32(request); 1.135 + 1.136 + // write parcel data 1.137 + for (let ii = 0; ii < data.length; ++ii) { 1.138 + writeUint8(data[ii]); 1.139 + } 1.140 + 1.141 + return bytes; 1.142 +} 1.143 + 1.144 +/** 1.145 + * 1.146 + */ 1.147 +let ril_ns; 1.148 +function newRadioInterface() { 1.149 + if (!ril_ns) { 1.150 + ril_ns = {}; 1.151 + subscriptLoader.loadSubScript("resource://gre/components/RadioInterfaceLayer.js", ril_ns); 1.152 + } 1.153 + 1.154 + return { 1.155 + __proto__: ril_ns.RadioInterface.prototype, 1.156 + }; 1.157 +} 1.158 + 1.159 +/** 1.160 + * Test whether specified function throws exception with expected 1.161 + * result. 1.162 + * 1.163 + * @param func 1.164 + * Function to be tested. 1.165 + * @param message 1.166 + * Message of expected exception. <code>null</code> for no throws. 1.167 + * @param stack 1.168 + * Optional stack object to be printed. <code>null</code> for 1.169 + * Components#stack#caller. 1.170 + */ 1.171 +function do_check_throws(func, message, stack) 1.172 +{ 1.173 + if (!stack) 1.174 + stack = Components.stack.caller; 1.175 + 1.176 + try { 1.177 + func(); 1.178 + } catch (exc) { 1.179 + if (exc.message === message) { 1.180 + return; 1.181 + } 1.182 + do_throw("expecting exception '" + message 1.183 + + "', caught '" + exc.message + "'", stack); 1.184 + } 1.185 + 1.186 + if (message) { 1.187 + do_throw("expecting exception '" + message + "', none thrown", stack); 1.188 + } 1.189 +} 1.190 +