summaryrefslogtreecommitdiff
path: root/toolkit/forgetaboutsite/test/browser/browser_clearplugindata.js
blob: ca0d394c3fc1398e79cceb302199947d19c10dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");

// Test clearing plugin data by domain using ForgetAboutSite.
const testURL = "http://mochi.test:8888/browser/toolkit/forgetaboutsite/test/browser/browser_clearplugindata.html";

const pluginHostIface = Ci.nsIPluginHost;
var pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
pluginHost.QueryInterface(pluginHostIface);

var pluginTag;

function stored(needles) {
  var something = pluginHost.siteHasData(this.pluginTag, null);
  if (!needles)
    return something;

  if (!something)
    return false;

  for (var i = 0; i < needles.length; ++i) {
    if (!pluginHost.siteHasData(this.pluginTag, needles[i]))
      return false;
  }
  return true;
}

function setTestPluginEnabledState(newEnabledState, plugin) {
  var oldEnabledState = plugin.enabledState;
  plugin.enabledState = newEnabledState;
  SimpleTest.registerCleanupFunction(function() {
    plugin.enabledState = oldEnabledState;
  });
}

add_task(function* () {
  var tags = pluginHost.getPluginTags();

  // Find the test plugin
  for (var i = 0; i < tags.length; i++)
  {
    if (tags[i].name == "Test Plug-in")
    {
      pluginTag = tags[i];
    }
  }
  if (!pluginTag) {
    ok(false, "Test Plug-in not available, can't run test");
    return;
  }
  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, pluginTag);
  yield BrowserTestUtils.openNewForegroundTab(gBrowser, testURL);

  // Set data for the plugin after the page load.
  yield ContentTask.spawn(gBrowser.selectedBrowser, null, function*() {
    content.wrappedJSObject.testSteps();
  });

  ok(stored(["192.168.1.1", "foo.com", "nonexistent.foo.com", "bar.com", "localhost"]),
    "Data stored for sites");

  // Clear data for "foo.com" and its subdomains.
  yield ForgetAboutSite.removeDataFromDomain("foo.com");

  ok(stored(["bar.com", "192.168.1.1", "localhost"]), "Data stored for sites");
  ok(!stored(["foo.com"]), "Data cleared for foo.com");
  ok(!stored(["bar.foo.com"]), "Data cleared for subdomains of foo.com");

    // Clear data for "bar.com" using a subdomain.
  yield ForgetAboutSite.removeDataFromDomain("foo.bar.com");
  ok(!stored(["bar.com"]), "Data cleared for bar.com");

  // Clear data for "192.168.1.1".
  yield ForgetAboutSite.removeDataFromDomain("192.168.1.1");
  ok(!stored(["192.168.1.1"]), "Data cleared for 192.168.1.1");

  // Clear data for "localhost".
  yield ForgetAboutSite.removeDataFromDomain("localhost");
  ok(!stored(null), "All data cleared");

  gBrowser.removeCurrentTab();
});