michael@0: michael@0: michael@0: let AddonManager = Cu.import("resource://gre/modules/AddonManager.jsm", {}).AddonManager; michael@0: let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: const ADDON_TYPE_SERVICE = "service"; michael@0: const ID_SUFFIX = "@services.mozilla.org"; michael@0: const STRING_TYPE_NAME = "type.%ID%.name"; michael@0: const XPINSTALL_URL = "chrome://mozapps/content/xpinstall/xpinstallConfirm.xul"; michael@0: michael@0: let manifest = { // builtin provider michael@0: name: "provider 1", michael@0: origin: "https://example.com", michael@0: sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" michael@0: }; michael@0: let manifest2 = { // used for testing install michael@0: name: "provider 2", michael@0: origin: "https://test1.example.com", michael@0: sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png", michael@0: version: 1 michael@0: }; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let prefname = getManifestPrefname(manifest); michael@0: setBuiltinManifestPref(prefname, manifest); michael@0: // ensure that manifest2 is NOT showing as builtin michael@0: is(SocialService.getOriginActivationType(manifest.origin), "builtin", "manifest is builtin"); michael@0: is(SocialService.getOriginActivationType(manifest2.origin), "foreign", "manifest2 is not builtin"); michael@0: michael@0: Services.prefs.setBoolPref("social.remote-install.enabled", true); michael@0: runSocialTests(tests, undefined, undefined, function () { michael@0: Services.prefs.clearUserPref("social.remote-install.enabled"); michael@0: // clear our builtin pref michael@0: ok(!Services.prefs.prefHasUserValue(prefname), "manifest is not in user-prefs"); michael@0: resetBuiltinManifestPref(prefname); michael@0: // just in case the tests failed, clear these here as well michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: Services.prefs.clearUserPref("social.directories"); michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: function installListener(next, aManifest) { michael@0: let expectEvent = "onInstalling"; michael@0: let prefname = getManifestPrefname(aManifest); michael@0: // wait for the actual removal to call next michael@0: SocialService.registerProviderListener(function providerListener(topic, origin, providers) { michael@0: if (topic == "provider-disabled") { michael@0: SocialService.unregisterProviderListener(providerListener); michael@0: is(origin, aManifest.origin, "provider disabled"); michael@0: executeSoon(next); michael@0: } michael@0: }); michael@0: michael@0: return { michael@0: onInstalling: function(addon) { michael@0: is(expectEvent, "onInstalling", "install started"); michael@0: is(addon.manifest.origin, aManifest.origin, "provider about to be installed"); michael@0: ok(!Services.prefs.prefHasUserValue(prefname), "manifest is not in user-prefs"); michael@0: expectEvent = "onInstalled"; michael@0: }, michael@0: onInstalled: function(addon) { michael@0: is(addon.manifest.origin, aManifest.origin, "provider installed"); michael@0: ok(addon.installDate.getTime() > 0, "addon has installDate"); michael@0: ok(addon.updateDate.getTime() > 0, "addon has updateDate"); michael@0: ok(Services.prefs.prefHasUserValue(prefname), "manifest is in user-prefs"); michael@0: expectEvent = "onUninstalling"; michael@0: }, michael@0: onUninstalling: function(addon) { michael@0: is(expectEvent, "onUninstalling", "uninstall started"); michael@0: is(addon.manifest.origin, aManifest.origin, "provider about to be uninstalled"); michael@0: ok(Services.prefs.prefHasUserValue(prefname), "manifest is in user-prefs"); michael@0: expectEvent = "onUninstalled"; michael@0: }, michael@0: onUninstalled: function(addon) { michael@0: is(expectEvent, "onUninstalled", "provider has been uninstalled"); michael@0: is(addon.manifest.origin, aManifest.origin, "provider uninstalled"); michael@0: ok(!Services.prefs.prefHasUserValue(prefname), "manifest is not in user-prefs"); michael@0: AddonManager.removeAddonListener(this); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: var tests = { michael@0: testAddonEnableToggle: function(next) { michael@0: let expectEvent; michael@0: let prefname = getManifestPrefname(manifest); michael@0: let listener = { michael@0: onEnabled: function(addon) { michael@0: is(expectEvent, "onEnabled", "provider onEnabled"); michael@0: ok(!addon.userDisabled, "provider enabled"); michael@0: executeSoon(function() { michael@0: expectEvent = "onDisabling"; michael@0: addon.userDisabled = true; michael@0: }); michael@0: }, michael@0: onEnabling: function(addon) { michael@0: is(expectEvent, "onEnabling", "provider onEnabling"); michael@0: expectEvent = "onEnabled"; michael@0: }, michael@0: onDisabled: function(addon) { michael@0: is(expectEvent, "onDisabled", "provider onDisabled"); michael@0: ok(addon.userDisabled, "provider disabled"); michael@0: AddonManager.removeAddonListener(listener); michael@0: // clear the provider user-level pref michael@0: Services.prefs.clearUserPref(prefname); michael@0: executeSoon(next); michael@0: }, michael@0: onDisabling: function(addon) { michael@0: is(expectEvent, "onDisabling", "provider onDisabling"); michael@0: expectEvent = "onDisabled"; michael@0: } michael@0: }; michael@0: AddonManager.addAddonListener(listener); michael@0: michael@0: // we're only testing enable disable, so we quickly set the user-level pref michael@0: // for this provider and test enable/disable toggling michael@0: setManifestPref(prefname, manifest); michael@0: ok(Services.prefs.prefHasUserValue(prefname), "manifest is in user-prefs"); michael@0: AddonManager.getAddonsByTypes([ADDON_TYPE_SERVICE], function(addons) { michael@0: for (let addon of addons) { michael@0: if (addon.userDisabled) { michael@0: expectEvent = "onEnabling"; michael@0: addon.userDisabled = false; michael@0: // only test with one addon michael@0: return; michael@0: } michael@0: } michael@0: ok(false, "no addons toggled"); michael@0: next(); michael@0: }); michael@0: }, michael@0: testProviderEnableToggle: function(next) { michael@0: // enable and disabel a provider from the SocialService interface, check michael@0: // that the addon manager is updated michael@0: michael@0: let expectEvent; michael@0: let prefname = getManifestPrefname(manifest); michael@0: michael@0: let listener = { michael@0: onEnabled: function(addon) { michael@0: is(expectEvent, "onEnabled", "provider onEnabled"); michael@0: is(addon.manifest.origin, manifest.origin, "provider enabled"); michael@0: ok(!addon.userDisabled, "provider !userDisabled"); michael@0: }, michael@0: onEnabling: function(addon) { michael@0: is(expectEvent, "onEnabling", "provider onEnabling"); michael@0: is(addon.manifest.origin, manifest.origin, "provider about to be enabled"); michael@0: expectEvent = "onEnabled"; michael@0: }, michael@0: onDisabled: function(addon) { michael@0: is(expectEvent, "onDisabled", "provider onDisabled"); michael@0: is(addon.manifest.origin, manifest.origin, "provider disabled"); michael@0: ok(addon.userDisabled, "provider userDisabled"); michael@0: }, michael@0: onDisabling: function(addon) { michael@0: is(expectEvent, "onDisabling", "provider onDisabling"); michael@0: is(addon.manifest.origin, manifest.origin, "provider about to be disabled"); michael@0: expectEvent = "onDisabled"; michael@0: } michael@0: }; michael@0: AddonManager.addAddonListener(listener); michael@0: michael@0: expectEvent = "onEnabling"; michael@0: setManifestPref(prefname, manifest); michael@0: SocialService.addBuiltinProvider(manifest.origin, function(provider) { michael@0: expectEvent = "onDisabling"; michael@0: SocialService.removeProvider(provider.origin, function() { michael@0: AddonManager.removeAddonListener(listener); michael@0: Services.prefs.clearUserPref(prefname); michael@0: next(); michael@0: }); michael@0: }); michael@0: }, michael@0: testForeignInstall: function(next) { michael@0: AddonManager.addAddonListener(installListener(next, manifest2)); michael@0: michael@0: // we expect the addon install dialog to appear, we need to accept the michael@0: // install from the dialog. michael@0: info("Waiting for install dialog"); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }) michael@0: michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: Services.prefs.setCharPref("social.whitelist", ""); michael@0: is(SocialService.getOriginActivationType(installFrom), "foreign", "testing foriegn install"); michael@0: Social.installProvider(doc, manifest2, function(addonManifest) { michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: Social.uninstallProvider(addonManifest.origin); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: testBuiltinInstallWithoutManifest: function(next) { michael@0: // send installProvider null for the manifest michael@0: AddonManager.addAddonListener(installListener(next, manifest)); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: let prefname = getManifestPrefname(manifest); michael@0: let activationURL = manifest.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: is(SocialService.getOriginActivationType(installFrom), "builtin", "testing builtin install"); michael@0: ok(!Services.prefs.prefHasUserValue(prefname), "manifest is not in user-prefs"); michael@0: Social.installProvider(doc, null, function(addonManifest) { michael@0: ok(Services.prefs.prefHasUserValue(prefname), "manifest is in user-prefs"); michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: Social.uninstallProvider(addonManifest.origin); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: testBuiltinInstall: function(next) { michael@0: // send installProvider a json object for the manifest michael@0: AddonManager.addAddonListener(installListener(next, manifest)); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: let prefname = getManifestPrefname(manifest); michael@0: let activationURL = manifest.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: is(SocialService.getOriginActivationType(installFrom), "builtin", "testing builtin install"); michael@0: ok(!Services.prefs.prefHasUserValue(prefname), "manifest is not in user-prefs"); michael@0: Social.installProvider(doc, manifest, function(addonManifest) { michael@0: ok(Services.prefs.prefHasUserValue(prefname), "manifest is in user-prefs"); michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: Social.uninstallProvider(addonManifest.origin); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: testWhitelistInstall: function(next) { michael@0: AddonManager.addAddonListener(installListener(next, manifest2)); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: Services.prefs.setCharPref("social.whitelist", installFrom); michael@0: is(SocialService.getOriginActivationType(installFrom), "whitelist", "testing whitelist install"); michael@0: Social.installProvider(doc, manifest2, function(addonManifest) { michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: Social.uninstallProvider(addonManifest.origin); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: testDirectoryInstall: function(next) { michael@0: AddonManager.addAddonListener(installListener(next, manifest2)); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: Services.prefs.setCharPref("social.directories", installFrom); michael@0: is(SocialService.getOriginActivationType(installFrom), "directory", "testing directory install"); michael@0: Social.installProvider(doc, manifest2, function(addonManifest) { michael@0: Services.prefs.clearUserPref("social.directories"); michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: Social.uninstallProvider(addonManifest.origin); michael@0: gBrowser.removeTab(tab); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: testUpgradeProviderFromWorker: function(next) { michael@0: // add the provider, change the pref, add it again. The provider at that michael@0: // point should be upgraded michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let installFrom = doc.nodePrincipal.origin; michael@0: Services.prefs.setCharPref("social.whitelist", installFrom); michael@0: Social.installProvider(doc, manifest2, function(addonManifest) { michael@0: SocialService.addBuiltinProvider(addonManifest.origin, function(provider) { michael@0: is(provider.manifest.version, 1, "manifest version is 1"); michael@0: michael@0: // watch for the provider-update and test the new version michael@0: SocialService.registerProviderListener(function providerListener(topic, origin, providers) { michael@0: if (topic != "provider-update") michael@0: return; michael@0: is(origin, addonManifest.origin, "provider updated") michael@0: SocialService.unregisterProviderListener(providerListener); michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: let provider = Social._getProviderFromOrigin(origin); michael@0: is(provider.manifest.version, 2, "manifest version is 2"); michael@0: Social.uninstallProvider(origin, function() { michael@0: gBrowser.removeTab(tab); michael@0: next(); michael@0: }); michael@0: }); michael@0: michael@0: let port = provider.getWorkerPort(); michael@0: port.postMessage({topic: "worker.update", data: true}); michael@0: michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: }