gecko/dom/system/gonk/tests/header_helpers.js
Kyle Machulis 9631320c07 Bug 735017: Clean up namespaces and naming for gonk dom objects in gonk specific code - Patch 1: Change dom/system/b2g to dom/system/gonk; r=philikon
--HG--
rename : dom/system/b2g/AudioManager.cpp => dom/system/gonk/AudioManager.cpp
rename : dom/system/b2g/AudioManager.h => dom/system/gonk/AudioManager.h
rename : dom/system/b2g/GonkGPSGeolocationProvider.cpp => dom/system/gonk/GonkGPSGeolocationProvider.cpp
rename : dom/system/b2g/GonkGPSGeolocationProvider.h => dom/system/gonk/GonkGPSGeolocationProvider.h
rename : dom/system/b2g/Makefile.in => dom/system/gonk/Makefile.in
rename : dom/system/b2g/RadioInterfaceLayer.js => dom/system/gonk/RadioInterfaceLayer.js
rename : dom/system/b2g/RadioInterfaceLayer.manifest => dom/system/gonk/RadioInterfaceLayer.manifest
rename : dom/system/b2g/SystemWorkerManager.cpp => dom/system/gonk/SystemWorkerManager.cpp
rename : dom/system/b2g/SystemWorkerManager.h => dom/system/gonk/SystemWorkerManager.h
rename : dom/system/b2g/net_worker.js => dom/system/gonk/net_worker.js
rename : dom/system/b2g/nsIAudioManager.idl => dom/system/gonk/nsIAudioManager.idl
rename : dom/system/b2g/nsIRadioInterfaceLayer.idl => dom/system/gonk/nsIRadioInterfaceLayer.idl
rename : dom/system/b2g/nsIWorkerHolder.idl => dom/system/gonk/nsIWorkerHolder.idl
rename : dom/system/b2g/nsRadioInterfaceLayer.h => dom/system/gonk/nsRadioInterfaceLayer.h
rename : dom/system/b2g/ril_consts.js => dom/system/gonk/ril_consts.js
rename : dom/system/b2g/ril_worker.js => dom/system/gonk/ril_worker.js
rename : dom/system/b2g/systemlibs.js => dom/system/gonk/systemlibs.js
rename : dom/system/b2g/tests/header_helpers.js => dom/system/gonk/tests/header_helpers.js
rename : dom/system/b2g/tests/test_ril_worker_buf.js => dom/system/gonk/tests/test_ril_worker_buf.js
rename : dom/system/b2g/tests/test_ril_worker_sms.js => dom/system/gonk/tests/test_ril_worker_sms.js
rename : dom/system/b2g/tests/xpcshell.ini => dom/system/gonk/tests/xpcshell.ini
2012-03-14 15:42:31 -07:00

149 lines
3.7 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
/**
* Start a new RIL worker.
*
* @param custom_ns
* Namespace with symbols to be injected into the new worker
* namespace.
*
* @return an object that represents the worker's namespace.
*
* @note that this does not start an actual worker thread. The worker
* is executed on the main thread, within a separate namespace object.
*/
function newWorker(custom_ns) {
let worker_ns = {
importScripts: function fakeImportScripts() {
Array.slice(arguments).forEach(function (script) {
subscriptLoader.loadSubScript("resource://gre/modules/" + script, this);
}, this);
},
postRILMessage: function fakePostRILMessage(message) {
},
postMessage: function fakepostMessage(message) {
},
// Define these variables inside the worker scope so ES5 strict mode
// doesn't flip out.
onmessage: undefined,
onerror: undefined,
DEBUG: true
};
// The 'self' variable in a worker points to the worker's own namespace.
worker_ns.self = worker_ns;
// Copy the custom definitions over.
for (let key in custom_ns) {
worker_ns[key] = custom_ns[key];
}
// Load the RIL worker itself.
worker_ns.importScripts("ril_worker.js");
return worker_ns;
}
/**
* Create a parcel suitable for postRILMessage().
*
* @param fakeParcelSize
* Value to be written to parcel size field for testing
* incorrect/incomplete parcel reading. Replaced with correct
* one determined length of data if negative.
* @param response
* Response code of the incoming parcel.
* @param request
* Request code of the incoming parcel.
* @param data
* Extra data to be appended.
*
* @return an Uint8Array carrying all parcel data.
*/
function newIncomingParcel(fakeParcelSize, response, request, data) {
const UINT32_SIZE = 4;
const PARCEL_SIZE_SIZE = 4;
let realParcelSize = data.length + 2 * UINT32_SIZE;
let buffer = new ArrayBuffer(realParcelSize + PARCEL_SIZE_SIZE);
let bytes = new Uint8Array(buffer);
let writeIndex = 0;
function writeUint8(value) {
bytes[writeIndex] = value;
++writeIndex;
}
function writeUint32(value) {
writeUint8(value & 0xff);
writeUint8((value >> 8) & 0xff);
writeUint8((value >> 16) & 0xff);
writeUint8((value >> 24) & 0xff);
}
function writeParcelSize(value) {
writeUint8((value >> 24) & 0xff);
writeUint8((value >> 16) & 0xff);
writeUint8((value >> 8) & 0xff);
writeUint8(value & 0xff);
}
if (fakeParcelSize < 0) {
fakeParcelSize = realParcelSize;
}
writeParcelSize(fakeParcelSize);
writeUint32(response);
writeUint32(request);
// write parcel data
for (let ii = 0; ii < data.length; ++ii) {
writeUint8(data[ii]);
}
return bytes;
}
/**
* Test whether specified function throws exception with expected
* result.
*
* @param func
* Function to be tested.
* @param result
* Expected result. <code>null</code> for no throws.
* @param stack
* Optional stack object to be printed. <code>null</code> for
* Components#stack#caller.
*/
function do_check_throws(func, result, stack)
{
if (!stack)
stack = Components.stack.caller;
try {
func();
} catch (exc) {
if (exc.result == result)
return;
do_throw("expected result " + result + ", caught " + exc, stack);
}
if (result) {
do_throw("expected result " + result + ", none thrown", stack);
}
}