browser/base/content/test/social/browser_blocklist.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/social/browser_blocklist.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// a place for miscellaneous social tests
     1.9 +
    1.10 +let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
    1.11 +
    1.12 +const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
    1.13 +let blocklistURL = "http://example.com/browser/browser/base/content/test/social/blocklist.xml";
    1.14 +
    1.15 +let manifest = { // normal provider
    1.16 +  name: "provider ok",
    1.17 +  origin: "https://example.com",
    1.18 +  sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
    1.19 +  workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
    1.20 +  iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
    1.21 +};
    1.22 +let manifest_bad = { // normal provider
    1.23 +  name: "provider blocked",
    1.24 +  origin: "https://test1.example.com",
    1.25 +  sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html",
    1.26 +  workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js",
    1.27 +  iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png"
    1.28 +};
    1.29 +
    1.30 +function test() {
    1.31 +  waitForExplicitFinish();
    1.32 +  // turn on logging for nsBlocklistService.js
    1.33 +  Services.prefs.setBoolPref("extensions.logging.enabled", true);
    1.34 +  registerCleanupFunction(function () {
    1.35 +    Services.prefs.clearUserPref("extensions.logging.enabled");
    1.36 +  });
    1.37 +
    1.38 +  runSocialTests(tests, undefined, undefined, function () {
    1.39 +    resetBlocklist(finish); //restore to original pref
    1.40 +  });
    1.41 +}
    1.42 +
    1.43 +var tests = {
    1.44 +  testSimpleBlocklist: function(next) {
    1.45 +    // this really just tests adding and clearing our blocklist for later tests
    1.46 +    setAndUpdateBlocklist(blocklistURL, function() {
    1.47 +      ok(Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocking 'blocked'");
    1.48 +      ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest)), "not blocking 'good'");
    1.49 +      resetBlocklist(function() {
    1.50 +        ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocklist cleared");
    1.51 +        next();
    1.52 +      });
    1.53 +    });
    1.54 +  },
    1.55 +  testAddingNonBlockedProvider: function(next) {
    1.56 +    function finishTest(isgood) {
    1.57 +      ok(isgood, "adding non-blocked provider ok");
    1.58 +      Services.prefs.clearUserPref("social.manifest.good");
    1.59 +      resetBlocklist(next);
    1.60 +    }
    1.61 +    setManifestPref("social.manifest.good", manifest);
    1.62 +    setAndUpdateBlocklist(blocklistURL, function() {
    1.63 +      try {
    1.64 +        SocialService.addProvider(manifest, function(provider) {
    1.65 +          try {
    1.66 +            SocialService.removeProvider(provider.origin, function() {
    1.67 +              ok(true, "added and removed provider");
    1.68 +              finishTest(true);
    1.69 +            });
    1.70 +          } catch(e) {
    1.71 +            ok(false, "SocialService.removeProvider threw exception: " + e);
    1.72 +            finishTest(false);
    1.73 +          }
    1.74 +        });
    1.75 +      } catch(e) {
    1.76 +        ok(false, "SocialService.addProvider threw exception: " + e);
    1.77 +        finishTest(false);
    1.78 +      }
    1.79 +    });
    1.80 +  },
    1.81 +  testAddingBlockedProvider: function(next) {
    1.82 +    function finishTest(good) {
    1.83 +      ok(good, "Unable to add blocklisted provider");
    1.84 +      Services.prefs.clearUserPref("social.manifest.blocked");
    1.85 +      resetBlocklist(next);
    1.86 +    }
    1.87 +    setManifestPref("social.manifest.blocked", manifest_bad);
    1.88 +    setAndUpdateBlocklist(blocklistURL, function() {
    1.89 +      try {
    1.90 +        SocialService.addProvider(manifest_bad, function(provider) {
    1.91 +          SocialService.removeProvider(provider.origin, function() {
    1.92 +            ok(false, "SocialService.addProvider should throw blocklist exception");
    1.93 +            finishTest(false);
    1.94 +          });
    1.95 +        });
    1.96 +      } catch(e) {
    1.97 +        ok(true, "SocialService.addProvider should throw blocklist exception: " + e);
    1.98 +        finishTest(true);
    1.99 +      }
   1.100 +    });
   1.101 +  },
   1.102 +  testInstallingBlockedProvider: function(next) {
   1.103 +    function finishTest(good) {
   1.104 +      ok(good, "Unable to add blocklisted provider");
   1.105 +      Services.prefs.clearUserPref("social.whitelist");
   1.106 +      resetBlocklist(next);
   1.107 +    }
   1.108 +    let activationURL = manifest_bad.origin + "/browser/browser/base/content/test/social/social_activate.html"
   1.109 +    addTab(activationURL, function(tab) {
   1.110 +      let doc = tab.linkedBrowser.contentDocument;
   1.111 +      let installFrom = doc.nodePrincipal.origin;
   1.112 +      // whitelist to avoid the 3rd party install dialog, we only want to test
   1.113 +      // the blocklist inside installProvider.
   1.114 +      Services.prefs.setCharPref("social.whitelist", installFrom);
   1.115 +      setAndUpdateBlocklist(blocklistURL, function() {
   1.116 +        try {
   1.117 +          // expecting an exception when attempting to install a hard blocked
   1.118 +          // provider
   1.119 +          Social.installProvider(doc, manifest_bad, function(addonManifest) {
   1.120 +            gBrowser.removeTab(tab);
   1.121 +            finishTest(false);
   1.122 +          });
   1.123 +        } catch(e) {
   1.124 +          gBrowser.removeTab(tab);
   1.125 +          finishTest(true);
   1.126 +        }
   1.127 +      });
   1.128 +    });
   1.129 +  },
   1.130 +  testBlockingExistingProvider: function(next) {
   1.131 +    let listener = {
   1.132 +      _window: null,
   1.133 +      onOpenWindow: function(aXULWindow) {
   1.134 +        Services.wm.removeListener(this);
   1.135 +        this._window = aXULWindow;
   1.136 +        let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
   1.137 +                                  .getInterface(Ci.nsIDOMWindow);
   1.138 +
   1.139 +        domwindow.addEventListener("load", function _load() {
   1.140 +          domwindow.removeEventListener("load", _load, false);
   1.141 +
   1.142 +          domwindow.addEventListener("unload", function _unload() {
   1.143 +            domwindow.removeEventListener("unload", _unload, false);
   1.144 +            info("blocklist window was closed");
   1.145 +            Services.wm.removeListener(listener);
   1.146 +            next();
   1.147 +          }, false);
   1.148 +
   1.149 +          is(domwindow.document.location.href, URI_EXTENSION_BLOCKLIST_DIALOG, "dialog opened and focused");
   1.150 +          // wait until after load to cancel so the dialog has initalized. we
   1.151 +          // don't want to accept here since that restarts the browser.
   1.152 +          executeSoon(() => {
   1.153 +            let cancelButton = domwindow.document.documentElement.getButton("cancel");
   1.154 +            info("***** hit the cancel button\n");
   1.155 +            cancelButton.doCommand();
   1.156 +          });
   1.157 +        }, false);
   1.158 +      },
   1.159 +      onCloseWindow: function(aXULWindow) { },
   1.160 +      onWindowTitleChange: function(aXULWindow, aNewTitle) { }
   1.161 +    };
   1.162 +
   1.163 +    Services.wm.addListener(listener);
   1.164 +
   1.165 +    setManifestPref("social.manifest.blocked", manifest_bad);
   1.166 +    try {
   1.167 +      SocialService.addProvider(manifest_bad, function(provider) {
   1.168 +        // the act of blocking should cause a 'provider-disabled' notification
   1.169 +        // from SocialService.
   1.170 +        SocialService.registerProviderListener(function providerListener(topic, origin, providers) {
   1.171 +          if (topic != "provider-disabled")
   1.172 +            return;
   1.173 +          SocialService.unregisterProviderListener(providerListener);
   1.174 +          is(origin, provider.origin, "provider disabled");
   1.175 +          SocialService.getProvider(provider.origin, function(p) {
   1.176 +            ok(p == null, "blocklisted provider disabled");
   1.177 +            Services.prefs.clearUserPref("social.manifest.blocked");
   1.178 +            resetBlocklist();
   1.179 +          });
   1.180 +        });
   1.181 +        // no callback - the act of updating should cause the listener above
   1.182 +        // to fire.
   1.183 +        setAndUpdateBlocklist(blocklistURL);
   1.184 +      });
   1.185 +    } catch(e) {
   1.186 +      ok(false, "unable to add provider " + e);
   1.187 +      next();
   1.188 +    }
   1.189 +  }
   1.190 +}

mercurial