toolkit/components/places/tests/unit/test_null_interfaces.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:f7b29761adf0
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 /**
6 * Test bug 489872 to make sure passing nulls to nsNavHistory doesn't crash.
7 */
8
9 let Cr = Components.results;
10
11 /**
12 * Print some debug message to the console. All arguments will be printed,
13 * separated by spaces.
14 *
15 * @param [arg0, arg1, arg2, ...]
16 * Any number of arguments to print out
17 * @usage _("Hello World") -> prints "Hello World"
18 * @usage _(1, 2, 3) -> prints "1 2 3"
19 */
20 let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" "));
21
22 _("Make an array of services to test, each specifying a class id, interface,",
23 "and an array of function names that don't throw when passed nulls");
24 let testServices = [
25 ["browser/nav-history-service;1", "nsINavHistoryService",
26 ["queryStringToQueries", "removePagesByTimeframe", "removePagesFromHost",
27 "removeVisitsByTimeframe"]],
28 ["browser/nav-bookmarks-service;1","nsINavBookmarksService",
29 ["createFolder"]],
30 ["browser/livemark-service;2","mozIAsyncLivemarks", ["reloadLivemarks"]],
31 ["browser/annotation-service;1","nsIAnnotationService", []],
32 ["browser/favicon-service;1","nsIFaviconService", []],
33 ["browser/tagging-service;1","nsITaggingService", []],
34 ];
35 _(testServices.join("\n"));
36
37 function run_test()
38 {
39 testServices.forEach(function([cid, iface, nothrow]) {
40 _("Running test with", cid, iface, nothrow);
41 let s = Cc["@mozilla.org/" + cid].getService(Ci[iface]);
42
43 let okName = function(name) {
44 _("Checking if function is okay to test:", name);
45 let func = s[name];
46
47 let mesg = "";
48 if (typeof func != "function")
49 mesg = "Not a function!";
50 else if (func.length == 0)
51 mesg = "No args needed!";
52 else if (name == "QueryInterface")
53 mesg = "Ignore QI!";
54
55 if (mesg == "")
56 return true;
57
58 _(mesg, "Skipping:", name);
59 return false;
60 }
61
62 _("Generating an array of functions to test service:", s);
63 [i for (i in s) if (okName(i))].sort().forEach(function(n) {
64 _();
65 _("Testing " + iface + " function with null args:", n);
66
67 let func = s[n];
68 let num = func.length;
69 _("Generating array of nulls for #args:", num);
70 let args = [];
71 for (let i = num; --i >= 0; )
72 args.push(null);
73
74 let tryAgain = true;
75 while (tryAgain == true) {
76 try {
77 _("Calling with args:", JSON.stringify(args));
78 func.apply(s, args);
79
80 _("The function didn't throw! Is it one of the nothrow?", nothrow);
81 do_check_neq(nothrow.indexOf(n), -1);
82
83 _("Must have been an expected nothrow, so no need to try again");
84 tryAgain = false;
85 }
86 catch(ex if ex.result == Cr.NS_ERROR_ILLEGAL_VALUE) {
87 _("Caught an expected exception:", ex.name);
88
89 _("Moving on to the next test..");
90 tryAgain = false;
91 }
92 catch(ex if ex.result == Cr.NS_ERROR_XPC_NEED_OUT_OBJECT) {
93 let pos = Number(ex.message.match(/object arg (\d+)/)[1]);
94 _("Function call expects an out object at", pos);
95 args[pos] = {};
96 }
97 catch(ex if ex.result == Cr.NS_ERROR_NOT_IMPLEMENTED) {
98 _("Method not implemented exception:", ex.name);
99
100 _("Moving on to the next test..");
101 tryAgain = false;
102 }
103 catch(ex) {
104 _("Caught some unexpected exception.. dumping");
105 _([[i, ex[i]] for (i in ex)].join("\n"));
106 do_check_true(false);
107 }
108 }
109 });
110 });
111 }

mercurial