Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 var MockServices = (function () {
2 "use strict";
4 const MOCK_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components)
5 .ID("{48068bc2-40ab-4904-8afd-4cdfb3a385f3}");
6 const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
8 const MOCK_SYSTEM_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components)
9 .ID("{e86d888c-e41b-4b78-9104-2f2742a532de}");
10 const SYSTEM_ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/system-alerts-service;1";
12 var registrar = SpecialPowers.wrap(SpecialPowers.Components).manager
13 .QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar);
15 var activeNotifications = Object.create(null);
17 var mockAlertsService = {
18 showAlertNotification: function(imageUrl, title, text, textClickable,
19 cookie, alertListener, name) {
20 var listener = SpecialPowers.wrap(alertListener);
21 activeNotifications[name] = {
22 listener: listener,
23 cookie: cookie
24 };
26 // fake async alert show event
27 setTimeout(function () {
28 listener.observe(null, "alertshow", cookie);
29 }, 100);
31 // ?? SpecialPowers.wrap(alertListener).observe(null, "alertclickcallback", cookie);
32 },
34 showAppNotification: function(imageUrl, title, text, textClickable,
35 manifestURL, alertListener, name) {
36 this.showAlertNotification(imageUrl, title, text, textClickable, "", alertListener, name);
37 },
39 closeAlert: function(name) {
40 var notification = activeNotifications[name];
41 if (notification) {
42 notification.listener.observe(null, "alertfinished", notification.cookie);
43 delete activeNotifications[name];
44 }
45 },
47 QueryInterface: function(aIID) {
48 if (SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) ||
49 SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService)) {
50 return this;
51 }
52 throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE;
53 },
55 createInstance: function(aOuter, aIID) {
56 if (aOuter != null) {
57 throw SpecialPowers.Components.results.NS_ERROR_NO_AGGREGATION;
58 }
59 return this.QueryInterface(aIID);
60 }
61 };
62 mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService);
64 // MockServices API
65 return {
66 register: function () {
67 registrar.registerFactory(MOCK_ALERTS_CID, "alerts service",
68 ALERTS_SERVICE_CONTRACT_ID,
69 mockAlertsService);
71 registrar.registerFactory(MOCK_SYSTEM_ALERTS_CID, "system alerts service",
72 SYSTEM_ALERTS_SERVICE_CONTRACT_ID,
73 mockAlertsService);
74 },
76 unregister: function () {
77 registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService);
78 registrar.unregisterFactory(MOCK_SYSTEM_ALERTS_CID, mockAlertsService);
79 },
80 };
81 })();