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/. */
5 function getConsoleMessages() {
6 let consoleService = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
7 let messages = consoleService.getMessageArray().map((m) => m.toString());
8 // reset ready for the next call.
9 consoleService.reset();
14 // Load the component manifests.
15 registerXPCTestComponents();
17 test_simple("@mozilla.org/js/xpc/test/native/ESMReturnCodeParent;1");
18 test_nested("@mozilla.org/js/xpc/test/native/ESMReturnCodeParent;1");
21 function test_simple(contractID) {
22 let parent = Cc[contractID].createInstance(Ci.nsIXPCTestReturnCodeParent);
25 // flush existing messages before we start testing.
28 // Ask the C++ to call the JS object which will throw.
29 result = parent.callChild(Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_THROW);
30 Assert.equal(result, Cr.NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS,
31 "exception caused NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS");
33 let messages = getConsoleMessages();
34 Assert.equal(messages.length, 1, "got a console message from the exception");
35 Assert.ok(messages[0].includes("a requested error"), "got the message text");
37 // Ask the C++ to call the JS object which will return success.
38 result = parent.callChild(Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_RETURN_SUCCESS);
39 Assert.equal(result, Cr.NS_OK, "success is success");
41 Assert.deepEqual(getConsoleMessages(), [], "no messages reported on success.");
43 // And finally the point of this test!
44 // Ask the C++ to call the JS object which will use .returnCode
45 result = parent.callChild(Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_RETURN_RESULTCODE);
46 Assert.equal(result, Cr.NS_ERROR_FAILURE,
47 "NS_ERROR_FAILURE was seen as the error code.");
49 Assert.deepEqual(getConsoleMessages(), [], "no messages reported with .returnCode");
52 function test_nested(contractID) {
53 let parent = Cc[contractID].createInstance(Ci.nsIXPCTestReturnCodeParent);
56 // flush existing messages before we start testing.
59 // Ask the C++ to call the "outer" JS object, which will set .returnCode, but
60 // then create and call *another* component which itself sets the .returnCode
61 // to a different value. This checks the returnCode is correctly saved
62 // across call contexts.
63 result = parent.callChild(Ci.nsIXPCTestReturnCodeChild.CHILD_SHOULD_NEST_RESULTCODES);
64 Assert.equal(result, Cr.NS_ERROR_UNEXPECTED,
65 "NS_ERROR_UNEXPECTED was seen as the error code.");
66 // We expect one message, which is the child reporting what it got as the
67 // return code - which should be NS_ERROR_FAILURE
68 let expected = ["nested child returned " + Cr.NS_ERROR_FAILURE];
69 Assert.deepEqual(getConsoleMessages(), expected, "got the correct sub-error");