toolkit/components/social/test/xpcshell/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/social/test/xpcshell/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,226 @@
     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 file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
     1.9 +Cu.import("resource://gre/modules/Services.jsm");
    1.10 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.11 +
    1.12 +XPCOMUtils.defineLazyModuleGetter(this, "Promise",
    1.13 +  "resource://gre/modules/Promise.jsm");
    1.14 +XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
    1.15 +  "resource://gre/modules/PlacesUtils.jsm");
    1.16 +
    1.17 +const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest.");
    1.18 +const gProfD = do_get_profile();
    1.19 +
    1.20 +const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
    1.21 +const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
    1.22 +
    1.23 +function createAppInfo(id, name, version, platformVersion) {
    1.24 +  gAppInfo = {
    1.25 +    // nsIXULAppInfo
    1.26 +    vendor: "Mozilla",
    1.27 +    name: name,
    1.28 +    ID: id,
    1.29 +    version: version,
    1.30 +    appBuildID: "2007010101",
    1.31 +    platformVersion: platformVersion ? platformVersion : "1.0",
    1.32 +    platformBuildID: "2007010101",
    1.33 +
    1.34 +    // nsIXULRuntime
    1.35 +    inSafeMode: false,
    1.36 +    logConsoleErrors: true,
    1.37 +    OS: "XPCShell",
    1.38 +    XPCOMABI: "noarch-spidermonkey",
    1.39 +    invalidateCachesOnRestart: function invalidateCachesOnRestart() {
    1.40 +      // Do nothing
    1.41 +    },
    1.42 +
    1.43 +    // nsICrashReporter
    1.44 +    annotations: {},
    1.45 +
    1.46 +    annotateCrashReport: function(key, data) {
    1.47 +      this.annotations[key] = data;
    1.48 +    },
    1.49 +
    1.50 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo,
    1.51 +                                           Ci.nsIXULRuntime,
    1.52 +                                           Ci.nsICrashReporter,
    1.53 +                                           Ci.nsISupports])
    1.54 +  };
    1.55 +
    1.56 +  var XULAppInfoFactory = {
    1.57 +    createInstance: function (outer, iid) {
    1.58 +      if (outer != null)
    1.59 +        throw Components.results.NS_ERROR_NO_AGGREGATION;
    1.60 +      return gAppInfo.QueryInterface(iid);
    1.61 +    }
    1.62 +  };
    1.63 +  var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
    1.64 +  registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
    1.65 +                            XULAPPINFO_CONTRACTID, XULAppInfoFactory);
    1.66 +}
    1.67 +
    1.68 +function initApp() {
    1.69 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
    1.70 +  // prepare a blocklist file for the blocklist service
    1.71 +  var blocklistFile = gProfD.clone();
    1.72 +  blocklistFile.append("blocklist.xml");
    1.73 +  if (blocklistFile.exists())
    1.74 +    blocklistFile.remove(false);
    1.75 +  var source = do_get_file("blocklist.xml");
    1.76 +  source.copyTo(gProfD, "blocklist.xml");
    1.77 +  blocklistFile.lastModifiedTime = Date.now();
    1.78 +}
    1.79 +
    1.80 +function AsyncRunner() {
    1.81 +  do_test_pending();
    1.82 +  do_register_cleanup((function () this.destroy()).bind(this));
    1.83 +
    1.84 +  this._callbacks = {
    1.85 +    done: do_test_finished,
    1.86 +    error: function (err) {
    1.87 +      // xpcshell test functions like do_check_eq throw NS_ERROR_ABORT on
    1.88 +      // failure.  Ignore those so they aren't rethrown here.
    1.89 +      if (err !== Cr.NS_ERROR_ABORT) {
    1.90 +        if (err.stack) {
    1.91 +          err = err + " - See following stack:\n" + err.stack +
    1.92 +                      "\nUseless do_throw stack";
    1.93 +        }
    1.94 +        do_throw(err);
    1.95 +      }
    1.96 +    },
    1.97 +    consoleError: function (scriptErr) {
    1.98 +      // Try to ensure the error is related to the test.
    1.99 +      let filename = scriptErr.sourceName || scriptErr.toString() || "";
   1.100 +      if (filename.indexOf("/toolkit/components/social/") >= 0)
   1.101 +        do_throw(scriptErr);
   1.102 +    },
   1.103 +  };
   1.104 +  this._iteratorQueue = [];
   1.105 +
   1.106 +  // This catches errors reported to the console, e.g., via Cu.reportError, but
   1.107 +  // not on the runner's stack.
   1.108 +  Cc["@mozilla.org/consoleservice;1"].
   1.109 +    getService(Ci.nsIConsoleService).
   1.110 +    registerListener(this);
   1.111 +}
   1.112 +
   1.113 +AsyncRunner.prototype = {
   1.114 +
   1.115 +  appendIterator: function appendIterator(iter) {
   1.116 +    this._iteratorQueue.push(iter);
   1.117 +  },
   1.118 +
   1.119 +  next: function next(/* ... */) {
   1.120 +    if (!this._iteratorQueue.length) {
   1.121 +      this.destroy();
   1.122 +      this._callbacks.done();
   1.123 +      return;
   1.124 +    }
   1.125 +
   1.126 +    // send() discards all arguments after the first, so there's no choice here
   1.127 +    // but to send only one argument to the yielder.
   1.128 +    let args = [arguments.length <= 1 ? arguments[0] : Array.slice(arguments)];
   1.129 +    try {
   1.130 +      var val = this._iteratorQueue[0].send.apply(this._iteratorQueue[0], args);
   1.131 +    }
   1.132 +    catch (err if err instanceof StopIteration) {
   1.133 +      this._iteratorQueue.shift();
   1.134 +      this.next();
   1.135 +      return;
   1.136 +    }
   1.137 +    catch (err) {
   1.138 +      this._callbacks.error(err);
   1.139 +    }
   1.140 +
   1.141 +    // val is an iterator => prepend it to the queue and start on it
   1.142 +    // val is otherwise truthy => call next
   1.143 +    if (val) {
   1.144 +      if (typeof(val) != "boolean")
   1.145 +        this._iteratorQueue.unshift(val);
   1.146 +      this.next();
   1.147 +    }
   1.148 +  },
   1.149 +
   1.150 +  destroy: function destroy() {
   1.151 +    Cc["@mozilla.org/consoleservice;1"].
   1.152 +      getService(Ci.nsIConsoleService).
   1.153 +      unregisterListener(this);
   1.154 +    this.destroy = function alreadyDestroyed() {};
   1.155 +  },
   1.156 +
   1.157 +  observe: function observe(msg) {
   1.158 +    if (msg instanceof Ci.nsIScriptError &&
   1.159 +        !(msg.flags & Ci.nsIScriptError.warningFlag))
   1.160 +    {
   1.161 +      this._callbacks.consoleError(msg);
   1.162 +    }
   1.163 +  },
   1.164 +};
   1.165 +
   1.166 +
   1.167 +function promiseAddVisits(aPlaceInfo)
   1.168 +{
   1.169 +  let deferred = Promise.defer();
   1.170 +  let places = [];
   1.171 +  if (aPlaceInfo instanceof Ci.nsIURI) {
   1.172 +    places.push({ uri: aPlaceInfo });
   1.173 +  }
   1.174 +  else if (Array.isArray(aPlaceInfo)) {
   1.175 +    places = places.concat(aPlaceInfo);
   1.176 +  } else {
   1.177 +    places.push(aPlaceInfo)
   1.178 +  }
   1.179 +
   1.180 +  // Create mozIVisitInfo for each entry.
   1.181 +  let now = Date.now();
   1.182 +  for (let i = 0; i < places.length; i++) {
   1.183 +    if (!places[i].title) {
   1.184 +      places[i].title = "test visit for " + places[i].uri.spec;
   1.185 +    }
   1.186 +    places[i].visits = [{
   1.187 +      transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
   1.188 +                                                         : places[i].transition,
   1.189 +      visitDate: places[i].visitDate || (now++) * 1000,
   1.190 +      referrerURI: places[i].referrer
   1.191 +    }];
   1.192 +  }
   1.193 +
   1.194 +  PlacesUtils.asyncHistory.updatePlaces(
   1.195 +    places,
   1.196 +    {
   1.197 +      handleError: function handleError(aResultCode, aPlaceInfo) {
   1.198 +        let ex = new Components.Exception("Unexpected error in adding visits.",
   1.199 +                                          aResultCode);
   1.200 +        deferred.reject(ex);
   1.201 +      },
   1.202 +      handleResult: function () {},
   1.203 +      handleCompletion: function handleCompletion() {
   1.204 +        deferred.resolve();
   1.205 +      }
   1.206 +    }
   1.207 +  );
   1.208 +
   1.209 +  return deferred.promise;
   1.210 +}
   1.211 +
   1.212 +function promiseTopicObserved(aTopic)
   1.213 +{
   1.214 +  let deferred = Promise.defer();
   1.215 +
   1.216 +  Services.obs.addObserver(
   1.217 +    function PTO_observe(aSubject, aTopic, aData) {
   1.218 +      Services.obs.removeObserver(PTO_observe, aTopic);
   1.219 +      deferred.resolve([aSubject, aData]);
   1.220 +    }, aTopic, false);
   1.221 +
   1.222 +  return deferred.promise;
   1.223 +}
   1.224 +
   1.225 +function promiseClearHistory() {
   1.226 +  let promise = promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED);
   1.227 +  do_execute_soon(function() PlacesUtils.bhistory.removeAllPages());
   1.228 +  return promise;
   1.229 +}

mercurial