michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Test bug 489872 to make sure passing nulls to nsNavHistory doesn't crash. michael@0: */ michael@0: michael@0: let Cr = Components.results; michael@0: michael@0: /** michael@0: * Print some debug message to the console. All arguments will be printed, michael@0: * separated by spaces. michael@0: * michael@0: * @param [arg0, arg1, arg2, ...] michael@0: * Any number of arguments to print out michael@0: * @usage _("Hello World") -> prints "Hello World" michael@0: * @usage _(1, 2, 3) -> prints "1 2 3" michael@0: */ michael@0: let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" ")); michael@0: michael@0: _("Make an array of services to test, each specifying a class id, interface,", michael@0: "and an array of function names that don't throw when passed nulls"); michael@0: let testServices = [ michael@0: ["browser/nav-history-service;1", "nsINavHistoryService", michael@0: ["queryStringToQueries", "removePagesByTimeframe", "removePagesFromHost", michael@0: "removeVisitsByTimeframe"]], michael@0: ["browser/nav-bookmarks-service;1","nsINavBookmarksService", michael@0: ["createFolder"]], michael@0: ["browser/livemark-service;2","mozIAsyncLivemarks", ["reloadLivemarks"]], michael@0: ["browser/annotation-service;1","nsIAnnotationService", []], michael@0: ["browser/favicon-service;1","nsIFaviconService", []], michael@0: ["browser/tagging-service;1","nsITaggingService", []], michael@0: ]; michael@0: _(testServices.join("\n")); michael@0: michael@0: function run_test() michael@0: { michael@0: testServices.forEach(function([cid, iface, nothrow]) { michael@0: _("Running test with", cid, iface, nothrow); michael@0: let s = Cc["@mozilla.org/" + cid].getService(Ci[iface]); michael@0: michael@0: let okName = function(name) { michael@0: _("Checking if function is okay to test:", name); michael@0: let func = s[name]; michael@0: michael@0: let mesg = ""; michael@0: if (typeof func != "function") michael@0: mesg = "Not a function!"; michael@0: else if (func.length == 0) michael@0: mesg = "No args needed!"; michael@0: else if (name == "QueryInterface") michael@0: mesg = "Ignore QI!"; michael@0: michael@0: if (mesg == "") michael@0: return true; michael@0: michael@0: _(mesg, "Skipping:", name); michael@0: return false; michael@0: } michael@0: michael@0: _("Generating an array of functions to test service:", s); michael@0: [i for (i in s) if (okName(i))].sort().forEach(function(n) { michael@0: _(); michael@0: _("Testing " + iface + " function with null args:", n); michael@0: michael@0: let func = s[n]; michael@0: let num = func.length; michael@0: _("Generating array of nulls for #args:", num); michael@0: let args = []; michael@0: for (let i = num; --i >= 0; ) michael@0: args.push(null); michael@0: michael@0: let tryAgain = true; michael@0: while (tryAgain == true) { michael@0: try { michael@0: _("Calling with args:", JSON.stringify(args)); michael@0: func.apply(s, args); michael@0: michael@0: _("The function didn't throw! Is it one of the nothrow?", nothrow); michael@0: do_check_neq(nothrow.indexOf(n), -1); michael@0: michael@0: _("Must have been an expected nothrow, so no need to try again"); michael@0: tryAgain = false; michael@0: } michael@0: catch(ex if ex.result == Cr.NS_ERROR_ILLEGAL_VALUE) { michael@0: _("Caught an expected exception:", ex.name); michael@0: michael@0: _("Moving on to the next test.."); michael@0: tryAgain = false; michael@0: } michael@0: catch(ex if ex.result == Cr.NS_ERROR_XPC_NEED_OUT_OBJECT) { michael@0: let pos = Number(ex.message.match(/object arg (\d+)/)[1]); michael@0: _("Function call expects an out object at", pos); michael@0: args[pos] = {}; michael@0: } michael@0: catch(ex if ex.result == Cr.NS_ERROR_NOT_IMPLEMENTED) { michael@0: _("Method not implemented exception:", ex.name); michael@0: michael@0: _("Moving on to the next test.."); michael@0: tryAgain = false; michael@0: } michael@0: catch(ex) { michael@0: _("Caught some unexpected exception.. dumping"); michael@0: _([[i, ex[i]] for (i in ex)].join("\n")); michael@0: do_check_true(false); michael@0: } michael@0: } michael@0: }); michael@0: }); michael@0: }