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: let SocialService = Components.utils.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: // A helper to run a suite of tests. michael@0: // The "test object" should be an object with function names as keys and a michael@0: // function as the value. The functions will be called with a "cbnext" param michael@0: // which should be called when the test is complete. michael@0: // eg: michael@0: // test = { michael@0: // foo: function(cbnext) {... cbnext();} michael@0: // } michael@0: function runTests(tests, cbPreTest, cbPostTest, cbFinish) { michael@0: let testIter = Iterator(tests); michael@0: michael@0: if (cbPreTest === undefined) { michael@0: cbPreTest = function(cb) {cb()}; michael@0: } michael@0: if (cbPostTest === undefined) { michael@0: cbPostTest = function(cb) {cb()}; michael@0: } michael@0: michael@0: function runNextTest() { michael@0: let name, func; michael@0: try { michael@0: [name, func] = testIter.next(); michael@0: } catch (err if err instanceof StopIteration) { michael@0: // out of items: michael@0: (cbFinish || finish)(); michael@0: return; michael@0: } michael@0: // We run on a timeout as the frameworker also makes use of timeouts, so michael@0: // this helps keep the debug messages sane. michael@0: executeSoon(function() { michael@0: function cleanupAndRunNextTest() { michael@0: info("sub-test " + name + " complete"); michael@0: cbPostTest(runNextTest); michael@0: } michael@0: cbPreTest(function() { michael@0: info("sub-test " + name + " starting"); michael@0: try { michael@0: func.call(tests, cleanupAndRunNextTest); michael@0: } catch (ex) { michael@0: ok(false, "sub-test " + name + " failed: " + ex.toString() +"\n"+ex.stack); michael@0: cleanupAndRunNextTest(); michael@0: } michael@0: }) michael@0: }); michael@0: } michael@0: runNextTest(); michael@0: } michael@0: michael@0: // A mock notifications server. Based on: michael@0: // dom/tests/mochitest/notification/notification_common.js michael@0: const FAKE_CID = Cc["@mozilla.org/uuid-generator;1"]. michael@0: getService(Ci.nsIUUIDGenerator).generateUUID(); michael@0: michael@0: const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1"; michael@0: const ALERTS_SERVICE_CID = Components.ID(Cc[ALERTS_SERVICE_CONTRACT_ID].number); michael@0: michael@0: function MockAlertsService() {} michael@0: michael@0: MockAlertsService.prototype = { michael@0: michael@0: showAlertNotification: function(imageUrl, title, text, textClickable, michael@0: cookie, alertListener, name) { michael@0: let obData = JSON.stringify({ michael@0: imageUrl: imageUrl, michael@0: title: title, michael@0: text:text, michael@0: textClickable: textClickable, michael@0: cookie: cookie, michael@0: name: name michael@0: }); michael@0: Services.obs.notifyObservers(null, "social-test:notification-alert", obData); michael@0: michael@0: if (textClickable) { michael@0: // probably should do this async.... michael@0: alertListener.observe(null, "alertclickcallback", cookie); michael@0: } michael@0: michael@0: alertListener.observe(null, "alertfinished", cookie); michael@0: }, michael@0: michael@0: QueryInterface: function(aIID) { michael@0: if (aIID.equals(Ci.nsISupports) || michael@0: aIID.equals(Ci.nsIAlertsService)) michael@0: return this; michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: michael@0: var factory = { michael@0: createInstance: function(aOuter, aIID) { michael@0: if (aOuter != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return new MockAlertsService().QueryInterface(aIID); michael@0: } michael@0: }; michael@0: michael@0: function replaceAlertsService() { michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .registerFactory(FAKE_CID, "", michael@0: ALERTS_SERVICE_CONTRACT_ID, michael@0: factory) michael@0: } michael@0: michael@0: function restoreAlertsService() { michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .registerFactory(ALERTS_SERVICE_CID, "", michael@0: ALERTS_SERVICE_CONTRACT_ID, michael@0: null); michael@0: } michael@0: // end of alerts service mock.