1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/notification/desktop-notification/notification_common.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +const MOCK_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{48068bc2-40ab-4904-8afd-4cdfb3a385f3}"); 1.5 +const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1"; 1.6 + 1.7 +const MOCK_SYSTEM_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{e86d888c-e41b-4b78-9104-2f2742a532de}"); 1.8 +const SYSTEM_ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/system-alerts-service;1"; 1.9 + 1.10 +var registrar = SpecialPowers.wrap(SpecialPowers.Components).manager. 1.11 + QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar); 1.12 + 1.13 +var mockAlertsService = { 1.14 + showAlertNotification: function(imageUrl, title, text, textClickable, 1.15 + cookie, alertListener, name, bidi, lang) { 1.16 + // probably should do this async.... 1.17 + SpecialPowers.wrap(alertListener).observe(null, "alertshow", cookie); 1.18 + 1.19 + if (SpecialPowers.getBoolPref("notification.prompt.testing.click_on_notification") == true) { 1.20 + SpecialPowers.wrap(alertListener).observe(null, "alertclickcallback", cookie); 1.21 + } 1.22 + 1.23 + SpecialPowers.wrap(alertListener).observe(null, "alertfinished", cookie); 1.24 + }, 1.25 + 1.26 + showAppNotification: function(imageUrl, title, text, alertListener, details) { 1.27 + this.showAlertNotification(imageUrl, title, text, details.textClickable, "", 1.28 + alertListener, details.name, details.dir, details.lang); 1.29 + }, 1.30 + 1.31 + QueryInterface: function(aIID) { 1.32 + if (SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) || 1.33 + SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService) || 1.34 + SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAppNotificationService)) { 1.35 + return this; 1.36 + } 1.37 + throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; 1.38 + }, 1.39 + 1.40 + createInstance: function(aOuter, aIID) { 1.41 + if (aOuter != null) { 1.42 + throw SpecialPowers.Components.results.NS_ERROR_NO_AGGREGATION; 1.43 + } 1.44 + return this.QueryInterface(aIID); 1.45 + } 1.46 +}; 1.47 +mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService); 1.48 + 1.49 +function setup_notifications(allowPrompt, forceClick, callback) { 1.50 + SpecialPowers.pushPrefEnv({'set': [["notification.prompt.testing", true], 1.51 + ["notification.prompt.testing.allow", allowPrompt], 1.52 + ["notification.prompt.testing.click_on_notification", forceClick]]}, 1.53 + callback); 1.54 + 1.55 + registrar.registerFactory(MOCK_SYSTEM_ALERTS_CID, "system alerts service", 1.56 + SYSTEM_ALERTS_SERVICE_CONTRACT_ID, 1.57 + mockAlertsService); 1.58 + 1.59 + registrar.registerFactory(MOCK_ALERTS_CID, "alerts service", 1.60 + ALERTS_SERVICE_CONTRACT_ID, 1.61 + mockAlertsService); 1.62 +} 1.63 + 1.64 +function reset_notifications() { 1.65 + registrar.unregisterFactory(MOCK_SYSTEM_ALERTS_CID, mockAlertsService); 1.66 + registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService); 1.67 +} 1.68 + 1.69 +function is_feature_enabled() { 1.70 + return navigator.mozNotification && SpecialPowers.getBoolPref("notification.feature.enabled"); 1.71 +} 1.72 +