michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm"); michael@0: michael@0: // Test clearing plugin data by domain using ForgetAboutSite. michael@0: const testURL = "http://mochi.test:8888/browser/toolkit/forgetaboutsite/test/browser/browser_clearplugindata.html"; michael@0: michael@0: const pluginHostIface = Ci.nsIPluginHost; michael@0: var pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost); michael@0: pluginHost.QueryInterface(pluginHostIface); michael@0: michael@0: var pluginTag; michael@0: michael@0: function stored(needles) { michael@0: var something = pluginHost.siteHasData(this.pluginTag, null); michael@0: if (!needles) michael@0: return something; michael@0: michael@0: if (!something) michael@0: return false; michael@0: michael@0: for (var i = 0; i < needles.length; ++i) { michael@0: if (!pluginHost.siteHasData(this.pluginTag, needles[i])) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function setTestPluginEnabledState(newEnabledState, plugin) { michael@0: var oldEnabledState = plugin.enabledState; michael@0: plugin.enabledState = newEnabledState; michael@0: SimpleTest.registerCleanupFunction(function() { michael@0: plugin.enabledState = oldEnabledState; michael@0: }); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: var tags = pluginHost.getPluginTags(); michael@0: michael@0: // Find the test plugin michael@0: for (var i = 0; i < tags.length; i++) michael@0: { michael@0: if (tags[i].name == "Test Plug-in") michael@0: { michael@0: pluginTag = tags[i]; michael@0: } michael@0: } michael@0: if (!pluginTag) { michael@0: ok(false, "Test Plug-in not available, can't run test"); michael@0: finish(); michael@0: } michael@0: setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, pluginTag); michael@0: michael@0: executeSoon(do_test); michael@0: } michael@0: michael@0: function setFinishedCallback(callback) michael@0: { michael@0: let testPage = gBrowser.selectedBrowser.contentWindow.wrappedJSObject; michael@0: testPage.testFinishedCallback = function() { michael@0: setTimeout(function() { michael@0: info("got finished callback"); michael@0: callback(); michael@0: }, 0); michael@0: } michael@0: } michael@0: michael@0: function do_test() michael@0: { michael@0: // Load page to set data for the plugin. michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function () { michael@0: gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: setFinishedCallback(function() { michael@0: ok(stored(["192.168.1.1","foo.com","nonexistent.foo.com","bar.com","localhost"]), michael@0: "Data stored for sites"); michael@0: michael@0: // Clear data for "foo.com" and its subdomains. michael@0: ForgetAboutSite.removeDataFromDomain("foo.com"); michael@0: ok(stored(["bar.com","192.168.1.1","localhost"]), "Data stored for sites"); michael@0: ok(!stored(["foo.com"]), "Data cleared for foo.com"); michael@0: ok(!stored(["bar.foo.com"]), "Data cleared for subdomains of foo.com"); michael@0: michael@0: // Clear data for "bar.com" using a subdomain. michael@0: ForgetAboutSite.removeDataFromDomain("foo.bar.com"); michael@0: ok(!stored(["bar.com"]), "Data cleared for bar.com"); michael@0: michael@0: // Clear data for "192.168.1.1". michael@0: ForgetAboutSite.removeDataFromDomain("192.168.1.1"); michael@0: ok(!stored(["192.168.1.1"]), "Data cleared for 192.168.1.1"); michael@0: michael@0: // Clear data for "localhost". michael@0: ForgetAboutSite.removeDataFromDomain("localhost"); michael@0: ok(!stored(null), "All data cleared"); michael@0: michael@0: gBrowser.removeCurrentTab(); michael@0: michael@0: executeSoon(finish); michael@0: }); michael@0: }, true); michael@0: content.location = testURL; michael@0: } michael@0: