|
1 const MOCK_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{48068bc2-40ab-4904-8afd-4cdfb3a385f3}"); |
|
2 const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1"; |
|
3 |
|
4 const MOCK_SYSTEM_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{e86d888c-e41b-4b78-9104-2f2742a532de}"); |
|
5 const SYSTEM_ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/system-alerts-service;1"; |
|
6 |
|
7 var registrar = SpecialPowers.wrap(SpecialPowers.Components).manager. |
|
8 QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar); |
|
9 |
|
10 var mockAlertsService = { |
|
11 showAlertNotification: function(imageUrl, title, text, textClickable, |
|
12 cookie, alertListener, name, bidi, lang) { |
|
13 // probably should do this async.... |
|
14 SpecialPowers.wrap(alertListener).observe(null, "alertshow", cookie); |
|
15 |
|
16 if (SpecialPowers.getBoolPref("notification.prompt.testing.click_on_notification") == true) { |
|
17 SpecialPowers.wrap(alertListener).observe(null, "alertclickcallback", cookie); |
|
18 } |
|
19 |
|
20 SpecialPowers.wrap(alertListener).observe(null, "alertfinished", cookie); |
|
21 }, |
|
22 |
|
23 showAppNotification: function(imageUrl, title, text, alertListener, details) { |
|
24 this.showAlertNotification(imageUrl, title, text, details.textClickable, "", |
|
25 alertListener, details.name, details.dir, details.lang); |
|
26 }, |
|
27 |
|
28 QueryInterface: function(aIID) { |
|
29 if (SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) || |
|
30 SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService) || |
|
31 SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAppNotificationService)) { |
|
32 return this; |
|
33 } |
|
34 throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; |
|
35 }, |
|
36 |
|
37 createInstance: function(aOuter, aIID) { |
|
38 if (aOuter != null) { |
|
39 throw SpecialPowers.Components.results.NS_ERROR_NO_AGGREGATION; |
|
40 } |
|
41 return this.QueryInterface(aIID); |
|
42 } |
|
43 }; |
|
44 mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService); |
|
45 |
|
46 function setup_notifications(allowPrompt, forceClick, callback) { |
|
47 SpecialPowers.pushPrefEnv({'set': [["notification.prompt.testing", true], |
|
48 ["notification.prompt.testing.allow", allowPrompt], |
|
49 ["notification.prompt.testing.click_on_notification", forceClick]]}, |
|
50 callback); |
|
51 |
|
52 registrar.registerFactory(MOCK_SYSTEM_ALERTS_CID, "system alerts service", |
|
53 SYSTEM_ALERTS_SERVICE_CONTRACT_ID, |
|
54 mockAlertsService); |
|
55 |
|
56 registrar.registerFactory(MOCK_ALERTS_CID, "alerts service", |
|
57 ALERTS_SERVICE_CONTRACT_ID, |
|
58 mockAlertsService); |
|
59 } |
|
60 |
|
61 function reset_notifications() { |
|
62 registrar.unregisterFactory(MOCK_SYSTEM_ALERTS_CID, mockAlertsService); |
|
63 registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService); |
|
64 } |
|
65 |
|
66 function is_feature_enabled() { |
|
67 return navigator.mozNotification && SpecialPowers.getBoolPref("notification.feature.enabled"); |
|
68 } |
|
69 |