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: // a place for miscellaneous social tests michael@0: michael@0: let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul"; michael@0: let blocklistURL = "http://example.com/browser/browser/base/content/test/social/blocklist.xml"; michael@0: michael@0: let manifest = { // normal provider michael@0: name: "provider ok", 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 manifest_bad = { // normal provider michael@0: name: "provider blocked", 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: }; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: // turn on logging for nsBlocklistService.js michael@0: Services.prefs.setBoolPref("extensions.logging.enabled", true); michael@0: registerCleanupFunction(function () { michael@0: Services.prefs.clearUserPref("extensions.logging.enabled"); michael@0: }); michael@0: michael@0: runSocialTests(tests, undefined, undefined, function () { michael@0: resetBlocklist(finish); //restore to original pref michael@0: }); michael@0: } michael@0: michael@0: var tests = { michael@0: testSimpleBlocklist: function(next) { michael@0: // this really just tests adding and clearing our blocklist for later tests michael@0: setAndUpdateBlocklist(blocklistURL, function() { michael@0: ok(Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocking 'blocked'"); michael@0: ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest)), "not blocking 'good'"); michael@0: resetBlocklist(function() { michael@0: ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocklist cleared"); michael@0: next(); michael@0: }); michael@0: }); michael@0: }, michael@0: testAddingNonBlockedProvider: function(next) { michael@0: function finishTest(isgood) { michael@0: ok(isgood, "adding non-blocked provider ok"); michael@0: Services.prefs.clearUserPref("social.manifest.good"); michael@0: resetBlocklist(next); michael@0: } michael@0: setManifestPref("social.manifest.good", manifest); michael@0: setAndUpdateBlocklist(blocklistURL, function() { michael@0: try { michael@0: SocialService.addProvider(manifest, function(provider) { michael@0: try { michael@0: SocialService.removeProvider(provider.origin, function() { michael@0: ok(true, "added and removed provider"); michael@0: finishTest(true); michael@0: }); michael@0: } catch(e) { michael@0: ok(false, "SocialService.removeProvider threw exception: " + e); michael@0: finishTest(false); michael@0: } michael@0: }); michael@0: } catch(e) { michael@0: ok(false, "SocialService.addProvider threw exception: " + e); michael@0: finishTest(false); michael@0: } michael@0: }); michael@0: }, michael@0: testAddingBlockedProvider: function(next) { michael@0: function finishTest(good) { michael@0: ok(good, "Unable to add blocklisted provider"); michael@0: Services.prefs.clearUserPref("social.manifest.blocked"); michael@0: resetBlocklist(next); michael@0: } michael@0: setManifestPref("social.manifest.blocked", manifest_bad); michael@0: setAndUpdateBlocklist(blocklistURL, function() { michael@0: try { michael@0: SocialService.addProvider(manifest_bad, function(provider) { michael@0: SocialService.removeProvider(provider.origin, function() { michael@0: ok(false, "SocialService.addProvider should throw blocklist exception"); michael@0: finishTest(false); michael@0: }); michael@0: }); michael@0: } catch(e) { michael@0: ok(true, "SocialService.addProvider should throw blocklist exception: " + e); michael@0: finishTest(true); michael@0: } michael@0: }); michael@0: }, michael@0: testInstallingBlockedProvider: function(next) { michael@0: function finishTest(good) { michael@0: ok(good, "Unable to add blocklisted provider"); michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: resetBlocklist(next); michael@0: } michael@0: let activationURL = manifest_bad.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: // whitelist to avoid the 3rd party install dialog, we only want to test michael@0: // the blocklist inside installProvider. michael@0: Services.prefs.setCharPref("social.whitelist", installFrom); michael@0: setAndUpdateBlocklist(blocklistURL, function() { michael@0: try { michael@0: // expecting an exception when attempting to install a hard blocked michael@0: // provider michael@0: Social.installProvider(doc, manifest_bad, function(addonManifest) { michael@0: gBrowser.removeTab(tab); michael@0: finishTest(false); michael@0: }); michael@0: } catch(e) { michael@0: gBrowser.removeTab(tab); michael@0: finishTest(true); michael@0: } michael@0: }); michael@0: }); michael@0: }, michael@0: testBlockingExistingProvider: function(next) { michael@0: let listener = { michael@0: _window: null, michael@0: onOpenWindow: function(aXULWindow) { michael@0: Services.wm.removeListener(this); michael@0: this._window = aXULWindow; michael@0: let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow); michael@0: michael@0: domwindow.addEventListener("load", function _load() { michael@0: domwindow.removeEventListener("load", _load, false); michael@0: michael@0: domwindow.addEventListener("unload", function _unload() { michael@0: domwindow.removeEventListener("unload", _unload, false); michael@0: info("blocklist window was closed"); michael@0: Services.wm.removeListener(listener); michael@0: next(); michael@0: }, false); michael@0: michael@0: is(domwindow.document.location.href, URI_EXTENSION_BLOCKLIST_DIALOG, "dialog opened and focused"); michael@0: // wait until after load to cancel so the dialog has initalized. we michael@0: // don't want to accept here since that restarts the browser. michael@0: executeSoon(() => { michael@0: let cancelButton = domwindow.document.documentElement.getButton("cancel"); michael@0: info("***** hit the cancel button\n"); michael@0: cancelButton.doCommand(); michael@0: }); michael@0: }, false); michael@0: }, michael@0: onCloseWindow: function(aXULWindow) { }, michael@0: onWindowTitleChange: function(aXULWindow, aNewTitle) { } michael@0: }; michael@0: michael@0: Services.wm.addListener(listener); michael@0: michael@0: setManifestPref("social.manifest.blocked", manifest_bad); michael@0: try { michael@0: SocialService.addProvider(manifest_bad, function(provider) { michael@0: // the act of blocking should cause a 'provider-disabled' notification michael@0: // from SocialService. michael@0: SocialService.registerProviderListener(function providerListener(topic, origin, providers) { michael@0: if (topic != "provider-disabled") michael@0: return; michael@0: SocialService.unregisterProviderListener(providerListener); michael@0: is(origin, provider.origin, "provider disabled"); michael@0: SocialService.getProvider(provider.origin, function(p) { michael@0: ok(p == null, "blocklisted provider disabled"); michael@0: Services.prefs.clearUserPref("social.manifest.blocked"); michael@0: resetBlocklist(); michael@0: }); michael@0: }); michael@0: // no callback - the act of updating should cause the listener above michael@0: // to fire. michael@0: setAndUpdateBlocklist(blocklistURL); michael@0: }); michael@0: } catch(e) { michael@0: ok(false, "unable to add provider " + e); michael@0: next(); michael@0: } michael@0: } michael@0: }