ReturnCodeChild.sys.mjs (2067B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 function xpcWrap(obj, iface) { 6 let ifacePointer = Cc[ 7 "@mozilla.org/supports-interface-pointer;1" 8 ].createInstance(Ci.nsISupportsInterfacePointer); 9 10 ifacePointer.data = obj; 11 return ifacePointer.data.QueryInterface(iface); 12 } 13 14 export var ReturnCodeChild = { 15 QueryInterface: ChromeUtils.generateQI(["nsIXPCTestReturnCodeChild"]), 16 17 doIt(behaviour) { 18 switch (behaviour) { 19 case Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_THROW: 20 throw(new Error("a requested error")); 21 case Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_RETURN_SUCCESS: 22 return; 23 case Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_RETURN_RESULTCODE: 24 Components.returnCode = Cr.NS_ERROR_FAILURE; 25 return; 26 case Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_NEST_RESULTCODES: 27 // Use xpconnect to create another instance of *this* component and 28 // call that. This way we have crossed the xpconnect bridge twice. 29 30 // We set *our* return code early - this should be what is returned 31 // to our caller, even though our "inner" component will set it to 32 // a different value that we will see (but our caller should not) 33 Components.returnCode = Cr.NS_ERROR_UNEXPECTED; 34 // call the child asking it to do the .returnCode set. 35 let sub = xpcWrap(ReturnCodeChild, Ci.nsIXPCTestReturnCodeChild); 36 let childResult = Cr.NS_OK; 37 try { 38 sub.doIt(Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_RETURN_RESULTCODE); 39 } catch (ex) { 40 childResult = ex.result; 41 } 42 // write it to the console so the test can check it. 43 let consoleService = Cc["@mozilla.org/consoleservice;1"] 44 .getService(Ci.nsIConsoleService); 45 consoleService.logStringMessage("nested child returned " + childResult); 46 return; 47 } 48 } 49 };