Index: chrome/browser/extensions/active_script_controller_browsertest.cc |
diff --git a/chrome/browser/extensions/active_script_controller_browsertest.cc b/chrome/browser/extensions/active_script_controller_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2716e8bce0981bed865fdaca664a1ee449b8aad3 |
--- /dev/null |
+++ b/chrome/browser/extensions/active_script_controller_browsertest.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/active_script_controller.h" |
+#include "chrome/browser/extensions/extension_action.h" |
+#include "chrome/browser/extensions/extension_browsertest.h" |
+#include "chrome/browser/extensions/extension_test_message_listener.h" |
+#include "chrome/browser/extensions/location_bar_controller.h" |
+#include "chrome/browser/extensions/tab_helper.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/test/base/ui_test_utils.h" |
+#include "extensions/common/feature_switch.h" |
+#include "net/test/embedded_test_server/embedded_test_server.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+// Returns true if there is an ExtensionAction with the given |extension_id| in |
+// |actions|. |
+bool ActionsContainId(const std::vector<ExtensionAction*>& actions, |
+ const std::string& extension_id) { |
+ for (std::vector<ExtensionAction*>::const_iterator iter = actions.begin(); |
+ iter != actions.end(); |
+ ++iter) { |
+ if ((*iter)->extension_id() == extension_id) |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { |
+ public: |
+ ActiveScriptControllerBrowserTest() |
+ : feature_override_(FeatureSwitch::active_script_enforcement(), |
+ FeatureSwitch::OVERRIDE_ENABLED) {} |
+ private: |
+ FeatureSwitch::ScopedOverride feature_override_; |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, |
+ ActiveScriptsAreDisplayed) { |
+ base::FilePath active_script_path = |
+ test_data_dir_.AppendASCII("active_script"); |
+ |
+ // First, we load up three extensions: |
+ // - An extension with a content script that runs on all hosts, |
+ // - An extension that injects scripts into all hosts, |
+ // - An extension with a content script that runs on explicit hosts. |
+ const Extension* content_scripts_all_hosts = LoadExtension( |
+ active_script_path.AppendASCII("content_scripts_all_hosts")); |
+ ASSERT_TRUE(content_scripts_all_hosts); |
+ const Extension* inject_scripts_all_hosts = LoadExtension( |
+ active_script_path.AppendASCII("inject_scripts_all_hosts")); |
+ ASSERT_TRUE(inject_scripts_all_hosts); |
+ const Extension* content_scripts_explicit_hosts = LoadExtension( |
+ active_script_path.AppendASCII("content_scripts_explicit_hosts")); |
+ ASSERT_TRUE(content_scripts_explicit_hosts); |
+ |
+ // All of these extensions should inject a script (either through content |
+ // scripts or through chrome.tabs.executeScript()) that sends a message with |
+ // their names (for verification). |
+ // Be prepared to wait for each extension to inject the script. |
+ ExtensionTestMessageListener content_scripts_all_hosts_listener( |
+ "content_scripts_all_hosts", false /* won't reply */); |
+ ExtensionTestMessageListener inject_scripts_all_hosts_listener( |
+ "inject_scripts_all_hosts", false /* won't reply */); |
+ ExtensionTestMessageListener content_scripts_explicit_hosts_listener( |
+ "content_scripts_explicit_hosts", false /* won't reply */); |
+ |
+ // Navigate to an URL (which matches the explicit host specified in the |
+ // extension content_scripts_explicit_hosts). All three extensions should |
+ // inject the script. |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ ui_test_utils::NavigateToURL( |
+ browser(), |
+ embedded_test_server()->GetURL("/extensions/test_file.html")); |
+ |
+ // Wait for them all to complete. |
+ content_scripts_all_hosts_listener.WaitUntilSatisfied(); |
+ content_scripts_explicit_hosts_listener.WaitUntilSatisfied(); |
+ inject_scripts_all_hosts_listener.WaitUntilSatisfied(); |
+ |
+ // Retrieve the ActiveScriptController for that tab. |
+ content::WebContents* web_contents = |
+ browser()->tab_strip_model()->GetActiveWebContents(); |
+ ASSERT_TRUE(web_contents); |
+ TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); |
+ ASSERT_TRUE(tab_helper); |
+ ActiveScriptController* controller = |
+ tab_helper->location_bar_controller()->active_script_controller(); |
+ ASSERT_TRUE(controller); |
+ |
+ // Verify the shown ExtensionActions. Each of the extensions with "all hosts" |
+ // should be displayed, but the extension that specified explicit hosts |
+ // should not. |
+ std::vector<ExtensionAction*> actions = controller->GetCurrentActions(); |
+ EXPECT_EQ(2u, actions.size()); |
+ EXPECT_TRUE(ActionsContainId(actions, content_scripts_all_hosts->id())); |
+ EXPECT_TRUE(ActionsContainId(actions, inject_scripts_all_hosts->id())); |
+ EXPECT_FALSE(ActionsContainId(actions, content_scripts_explicit_hosts->id())); |
+} |
+ |
+} // namespace extensions |