Bug 1318383 - Add tests for hidden CTP plugin functionality. r=bsmedberg
authorMike Conley <mconley@mozilla.com>
Thu, 17 Nov 2016 12:01:58 -0500
changeset 325286 3ec83fde43f2e1ba1dc583df0c722033c63ab7b7
parent 325285 8103c612b79c2587ea4ca1b0a9f9f82db4b185b8
child 325303 deacac546efaedc8b7a5eef776bf9bba8398d401
child 325306 d412e21e3bba0a5de670cca8a4ef4ecd49b2a4b4
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersbsmedberg
bugs1318383
milestone53.0a1
Bug 1318383 - Add tests for hidden CTP plugin functionality. r=bsmedberg MozReview-Commit-ID: 5aNBGZFDnRC
browser/base/content/test/plugins/browser.ini
browser/base/content/test/plugins/browser_iterate_hidden_plugins.js
--- a/browser/base/content/test/plugins/browser.ini
+++ b/browser/base/content/test/plugins/browser.ini
@@ -57,16 +57,17 @@ skip-if = !crashreporter
 [browser_CTP_multi_allow.js]
 [browser_CTP_nonplugins.js]
 [browser_CTP_notificationBar.js]
 [browser_CTP_outsideScrollArea.js]
 [browser_CTP_remove_navigate.js]
 [browser_CTP_resize.js]
 [browser_CTP_zoom.js]
 [browser_blocking.js]
+[browser_iterate_hidden_plugins.js]
 [browser_plugins_added_dynamically.js]
 [browser_pluginnotification.js]
 [browser_plugin_reloading.js]
 [browser_blocklist_content.js]
 skip-if = !e10s
 [browser_globalplugin_crashinfobar.js]
 skip-if = !crashreporter
 [browser_pluginCrashCommentAndURL.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/plugins/browser_iterate_hidden_plugins.js
@@ -0,0 +1,125 @@
+"use strict";
+
+const TEST_PLUGIN_NAME = "Test Plug-in";
+const HIDDEN_CTP_PLUGIN_PREF = "plugins.navigator.hidden_ctp_plugin";
+
+/**
+ * If a plugin is click-to-play and named in HIDDEN_CTP_PLUGIN_PREF,
+ * then the plugin should be hidden in the navigator.plugins list by default
+ * when iterating. If, however, JS attempts to access the plugin via
+ * navigator.plugins[NAME] directly, we should show the "Hidden Plugin"
+ * notification bar.
+ */
+
+add_task(function* setup() {
+  // We'll make the Test Plugin click-to-play.
+  let originalPluginState = getTestPluginEnabledState();
+  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
+  registerCleanupFunction(() => {
+    setTestPluginEnabledState(originalPluginState);
+  });
+
+  // And then make the plugin hidden.
+  yield SpecialPowers.pushPrefEnv({
+    set: [[HIDDEN_CTP_PLUGIN_PREF, TEST_PLUGIN_NAME]],
+  })
+});
+
+/**
+ * Tests that if a plugin is click-to-play and in the
+ * HIDDEN_CTP_PLUGIN_PREF list, then it shouldn't be visible
+ * when iterating navigator.plugins.
+ */
+add_task(function* test_plugin_is_hidden_on_iteration() {
+  // The plugin should not be visible when we iterate
+  // navigator.plugins.
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com"
+  }, function*(browser) {
+    yield ContentTask.spawn(browser, TEST_PLUGIN_NAME, function*(pluginName) {
+      let plugins = Array.from(content.navigator.plugins);
+      Assert.ok(plugins.every(p => p.name != pluginName),
+                "Should not find Test Plugin");
+    });
+  });
+
+  // Now clear the HIDDEN_CTP_PLUGIN_PREF temporarily and
+  // make sure we can see the plugin again.
+  yield SpecialPowers.pushPrefEnv({
+    set: [[HIDDEN_CTP_PLUGIN_PREF, ""]],
+  });
+
+  // Note that I have to do this in a new tab since navigator
+  // caches navigator.plugins after an initial read.
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com"
+  }, function*(browser) {
+    yield ContentTask.spawn(browser, TEST_PLUGIN_NAME, function*(pluginName) {
+      let plugins = Array.from(content.navigator.plugins);
+      Assert.ok(plugins.some(p => p.name == pluginName),
+                "Should have found the Test Plugin");
+    });
+  });
+
+  yield SpecialPowers.popPrefEnv();
+});
+
+/**
+ * Tests that if a click-to-play plugin is hidden, that we show the
+ * Hidden Plugin notification bar when accessed directly by name
+ * via navigator.plugins.
+ */
+add_task(function* test_plugin_shows_hidden_notification_on_access() {
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com"
+  }, function*(browser) {
+    let notificationPromise = waitForNotificationBar("plugin-hidden", gBrowser.selectedBrowser);
+
+    yield ContentTask.spawn(browser, TEST_PLUGIN_NAME, function*(pluginName) {
+      let plugins = content.navigator.plugins;
+      // Just accessing the plugin should be enough to trigger the notification.
+      // We'll also do a sanity check and make sure that the HiddenPlugin event
+      // is firing (which is the event that triggers the notification bar).
+      let sawEvent = false;
+      addEventListener("HiddenPlugin", function onHiddenPlugin(e) {
+        sawEvent = true;
+        removeEventListener("HiddenPlugin", onHiddenPlugin, true);
+      }, true);
+      plugins[pluginName];
+      Assert.ok(sawEvent, "Should have seen the HiddenPlugin event.");
+    });
+
+    let notification = yield notificationPromise;
+    notification.close();
+  });
+
+  // Make sure that if the plugin wasn't hidden that touching it
+  // does _NOT_ show the notification bar.
+  yield SpecialPowers.pushPrefEnv({
+    set: [[HIDDEN_CTP_PLUGIN_PREF, ""]],
+  });
+
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com"
+  }, function*(browser) {
+    yield ContentTask.spawn(browser, TEST_PLUGIN_NAME, function*(pluginName) {
+      let plugins = content.navigator.plugins;
+      // Instead of waiting for a notification bar that should never come,
+      // we'll make sure that the HiddenPlugin event never fires in content
+      // (which is the event that triggers the notification bar).
+      let sawEvent = false;
+      addEventListener("HiddenPlugin", function onHiddenPlugin(e) {
+        sawEvent = true;
+        removeEventListener("HiddenPlugin", onHiddenPlugin, true);
+      }, true);
+      plugins[pluginName];
+      Assert.ok(!sawEvent, "Should not have seen the HiddenPlugin event.");
+    });
+  });
+
+  yield SpecialPowers.popPrefEnv();
+});