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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_null_interfaces.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +* License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Test bug 489872 to make sure passing nulls to nsNavHistory doesn't crash.
    1.10 + */
    1.11 +
    1.12 +let Cr = Components.results;
    1.13 +
    1.14 +/**
    1.15 + * Print some debug message to the console. All arguments will be printed,
    1.16 + * separated by spaces.
    1.17 + *
    1.18 + * @param [arg0, arg1, arg2, ...]
    1.19 + *        Any number of arguments to print out
    1.20 + * @usage _("Hello World") -> prints "Hello World"
    1.21 + * @usage _(1, 2, 3) -> prints "1 2 3"
    1.22 + */
    1.23 +let _ = function(some, debug, text, to) print(Array.slice(arguments).join(" "));
    1.24 +
    1.25 +_("Make an array of services to test, each specifying a class id, interface,",
    1.26 +  "and an array of function names that don't throw when passed nulls");
    1.27 +let testServices = [
    1.28 +  ["browser/nav-history-service;1", "nsINavHistoryService",
    1.29 +    ["queryStringToQueries", "removePagesByTimeframe", "removePagesFromHost",
    1.30 +     "removeVisitsByTimeframe"]],
    1.31 +  ["browser/nav-bookmarks-service;1","nsINavBookmarksService",
    1.32 +    ["createFolder"]],
    1.33 +  ["browser/livemark-service;2","mozIAsyncLivemarks", ["reloadLivemarks"]],
    1.34 +  ["browser/annotation-service;1","nsIAnnotationService", []],
    1.35 +  ["browser/favicon-service;1","nsIFaviconService", []],
    1.36 +  ["browser/tagging-service;1","nsITaggingService", []],
    1.37 +];
    1.38 +_(testServices.join("\n"));
    1.39 +
    1.40 +function run_test()
    1.41 +{
    1.42 +  testServices.forEach(function([cid, iface, nothrow]) {
    1.43 +    _("Running test with", cid, iface, nothrow);
    1.44 +    let s = Cc["@mozilla.org/" + cid].getService(Ci[iface]);
    1.45 +
    1.46 +    let okName = function(name) {
    1.47 +      _("Checking if function is okay to test:", name);
    1.48 +      let func = s[name];
    1.49 +
    1.50 +      let mesg = "";
    1.51 +      if (typeof func != "function")
    1.52 +        mesg = "Not a function!";
    1.53 +      else if (func.length == 0)
    1.54 +        mesg = "No args needed!";
    1.55 +      else if (name == "QueryInterface")
    1.56 +        mesg = "Ignore QI!";
    1.57 +
    1.58 +      if (mesg == "")
    1.59 +        return true;
    1.60 +
    1.61 +      _(mesg, "Skipping:", name);
    1.62 +      return false;
    1.63 +    }
    1.64 +
    1.65 +    _("Generating an array of functions to test service:", s);
    1.66 +    [i for (i in s) if (okName(i))].sort().forEach(function(n) {
    1.67 +      _();
    1.68 +      _("Testing " + iface + " function with null args:", n);
    1.69 +
    1.70 +      let func = s[n];
    1.71 +      let num = func.length;
    1.72 +      _("Generating array of nulls for #args:", num);
    1.73 +      let args = [];
    1.74 +      for (let i = num; --i >= 0; )
    1.75 +        args.push(null);
    1.76 +
    1.77 +      let tryAgain = true;
    1.78 +      while (tryAgain == true) {
    1.79 +        try {
    1.80 +          _("Calling with args:", JSON.stringify(args));
    1.81 +          func.apply(s, args);
    1.82 +  
    1.83 +          _("The function didn't throw! Is it one of the nothrow?", nothrow);
    1.84 +          do_check_neq(nothrow.indexOf(n), -1);
    1.85 +
    1.86 +          _("Must have been an expected nothrow, so no need to try again");
    1.87 +          tryAgain = false;
    1.88 +        }
    1.89 +        catch(ex if ex.result == Cr.NS_ERROR_ILLEGAL_VALUE) {
    1.90 +          _("Caught an expected exception:", ex.name);
    1.91 +
    1.92 +          _("Moving on to the next test..");
    1.93 +          tryAgain = false;
    1.94 +        }
    1.95 +        catch(ex if ex.result == Cr.NS_ERROR_XPC_NEED_OUT_OBJECT) {
    1.96 +          let pos = Number(ex.message.match(/object arg (\d+)/)[1]);
    1.97 +          _("Function call expects an out object at", pos);
    1.98 +          args[pos] = {};
    1.99 +        }
   1.100 +        catch(ex if ex.result == Cr.NS_ERROR_NOT_IMPLEMENTED) {
   1.101 +          _("Method not implemented exception:", ex.name);
   1.102 +
   1.103 +          _("Moving on to the next test..");
   1.104 +          tryAgain = false;
   1.105 +        }
   1.106 +        catch(ex) {
   1.107 +          _("Caught some unexpected exception.. dumping");
   1.108 +          _([[i, ex[i]] for (i in ex)].join("\n"));
   1.109 +          do_check_true(false);
   1.110 +        }
   1.111 +      }
   1.112 +    });
   1.113 +  });
   1.114 +}

mercurial