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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/social/test/browser/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     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 +let SocialService = Components.utils.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
     1.9 +
    1.10 +// A helper to run a suite of tests.
    1.11 +// The "test object" should be an object with function names as keys and a
    1.12 +// function as the value.  The functions will be called with a "cbnext" param
    1.13 +// which should be called when the test is complete.
    1.14 +// eg:
    1.15 +// test = {
    1.16 +//   foo: function(cbnext) {... cbnext();}
    1.17 +// }
    1.18 +function runTests(tests, cbPreTest, cbPostTest, cbFinish) {
    1.19 +  let testIter = Iterator(tests);
    1.20 +
    1.21 +  if (cbPreTest === undefined) {
    1.22 +    cbPreTest = function(cb) {cb()};
    1.23 +  }
    1.24 +  if (cbPostTest === undefined) {
    1.25 +    cbPostTest = function(cb) {cb()};
    1.26 +  }
    1.27 +
    1.28 +  function runNextTest() {
    1.29 +    let name, func;
    1.30 +    try {
    1.31 +      [name, func] = testIter.next();
    1.32 +    } catch (err if err instanceof StopIteration) {
    1.33 +      // out of items:
    1.34 +      (cbFinish || finish)();
    1.35 +      return;
    1.36 +    }
    1.37 +    // We run on a timeout as the frameworker also makes use of timeouts, so
    1.38 +    // this helps keep the debug messages sane.
    1.39 +    executeSoon(function() {
    1.40 +      function cleanupAndRunNextTest() {
    1.41 +        info("sub-test " + name + " complete");
    1.42 +        cbPostTest(runNextTest);
    1.43 +      }
    1.44 +      cbPreTest(function() {
    1.45 +        info("sub-test " + name + " starting");
    1.46 +        try {
    1.47 +          func.call(tests, cleanupAndRunNextTest);
    1.48 +        } catch (ex) {
    1.49 +          ok(false, "sub-test " + name + " failed: " + ex.toString() +"\n"+ex.stack);
    1.50 +          cleanupAndRunNextTest();
    1.51 +        }
    1.52 +      })
    1.53 +    });
    1.54 +  }
    1.55 +  runNextTest();
    1.56 +}
    1.57 +
    1.58 +// A mock notifications server.  Based on:
    1.59 +// dom/tests/mochitest/notification/notification_common.js
    1.60 +const FAKE_CID = Cc["@mozilla.org/uuid-generator;1"].
    1.61 +    getService(Ci.nsIUUIDGenerator).generateUUID();
    1.62 +
    1.63 +const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
    1.64 +const ALERTS_SERVICE_CID = Components.ID(Cc[ALERTS_SERVICE_CONTRACT_ID].number);
    1.65 +
    1.66 +function MockAlertsService() {}
    1.67 +
    1.68 +MockAlertsService.prototype = {
    1.69 +
    1.70 +    showAlertNotification: function(imageUrl, title, text, textClickable,
    1.71 +                                    cookie, alertListener, name) {
    1.72 +        let obData = JSON.stringify({
    1.73 +          imageUrl: imageUrl,
    1.74 +          title: title,
    1.75 +          text:text,
    1.76 +          textClickable: textClickable,
    1.77 +          cookie: cookie,
    1.78 +          name: name
    1.79 +        });
    1.80 +        Services.obs.notifyObservers(null, "social-test:notification-alert", obData);
    1.81 +
    1.82 +        if (textClickable) {
    1.83 +          // probably should do this async....
    1.84 +          alertListener.observe(null, "alertclickcallback", cookie);
    1.85 +        }
    1.86 +
    1.87 +        alertListener.observe(null, "alertfinished", cookie);
    1.88 +    },
    1.89 +
    1.90 +    QueryInterface: function(aIID) {
    1.91 +        if (aIID.equals(Ci.nsISupports) ||
    1.92 +            aIID.equals(Ci.nsIAlertsService))
    1.93 +            return this;
    1.94 +        throw Cr.NS_ERROR_NO_INTERFACE;
    1.95 +    }
    1.96 +};
    1.97 +
    1.98 +var factory = {
    1.99 +    createInstance: function(aOuter, aIID) {
   1.100 +        if (aOuter != null)
   1.101 +            throw Cr.NS_ERROR_NO_AGGREGATION;
   1.102 +        return new MockAlertsService().QueryInterface(aIID);
   1.103 +    }
   1.104 +};
   1.105 +
   1.106 +function replaceAlertsService() {
   1.107 +  Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   1.108 +            .registerFactory(FAKE_CID, "",
   1.109 +                             ALERTS_SERVICE_CONTRACT_ID,
   1.110 +                             factory)
   1.111 +}
   1.112 +
   1.113 +function restoreAlertsService() {
   1.114 +  Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   1.115 +            .registerFactory(ALERTS_SERVICE_CID, "",
   1.116 +                             ALERTS_SERVICE_CONTRACT_ID,
   1.117 +                             null);
   1.118 +}
   1.119 +// end of alerts service mock.

mercurial