OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/active_script_controller.h" |
| 6 #include "chrome/browser/extensions/extension_action.h" |
| 7 #include "chrome/browser/extensions/extension_browsertest.h" |
| 8 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 9 #include "chrome/browser/extensions/location_bar_controller.h" |
| 10 #include "chrome/browser/extensions/tab_helper.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "extensions/common/feature_switch.h" |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Returns true if there is an ExtensionAction with the given |extension_id| in |
| 23 // |actions|. |
| 24 bool ActionsContainId(const std::vector<ExtensionAction*>& actions, |
| 25 const std::string& extension_id) { |
| 26 for (std::vector<ExtensionAction*>::const_iterator iter = actions.begin(); |
| 27 iter != actions.end(); |
| 28 ++iter) { |
| 29 if ((*iter)->extension_id() == extension_id) |
| 30 return true; |
| 31 } |
| 32 return false; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class ActiveScriptControllerBrowserTest : public ExtensionBrowserTest { |
| 38 public: |
| 39 ActiveScriptControllerBrowserTest() |
| 40 : feature_override_(FeatureSwitch::active_script_enforcement(), |
| 41 FeatureSwitch::OVERRIDE_ENABLED) {} |
| 42 private: |
| 43 FeatureSwitch::ScopedOverride feature_override_; |
| 44 }; |
| 45 |
| 46 IN_PROC_BROWSER_TEST_F(ActiveScriptControllerBrowserTest, |
| 47 ActiveScriptsAreDisplayed) { |
| 48 base::FilePath active_script_path = |
| 49 test_data_dir_.AppendASCII("active_script"); |
| 50 |
| 51 // First, we load up three extensions: |
| 52 // - An extension with a content script that runs on all hosts, |
| 53 // - An extension that injects scripts into all hosts, |
| 54 // - An extension with a content script that runs on explicit hosts. |
| 55 const Extension* content_scripts_all_hosts = LoadExtension( |
| 56 active_script_path.AppendASCII("content_scripts_all_hosts")); |
| 57 ASSERT_TRUE(content_scripts_all_hosts); |
| 58 const Extension* inject_scripts_all_hosts = LoadExtension( |
| 59 active_script_path.AppendASCII("inject_scripts_all_hosts")); |
| 60 ASSERT_TRUE(inject_scripts_all_hosts); |
| 61 const Extension* content_scripts_explicit_hosts = LoadExtension( |
| 62 active_script_path.AppendASCII("content_scripts_explicit_hosts")); |
| 63 ASSERT_TRUE(content_scripts_explicit_hosts); |
| 64 |
| 65 // All of these extensions should inject a script (either through content |
| 66 // scripts or through chrome.tabs.executeScript()) that sends a message with |
| 67 // their names (for verification). |
| 68 // Be prepared to wait for each extension to inject the script. |
| 69 ExtensionTestMessageListener content_scripts_all_hosts_listener( |
| 70 "content_scripts_all_hosts", false /* won't reply */); |
| 71 ExtensionTestMessageListener inject_scripts_all_hosts_listener( |
| 72 "inject_scripts_all_hosts", false /* won't reply */); |
| 73 ExtensionTestMessageListener content_scripts_explicit_hosts_listener( |
| 74 "content_scripts_explicit_hosts", false /* won't reply */); |
| 75 |
| 76 // Navigate to an URL (which matches the explicit host specified in the |
| 77 // extension content_scripts_explicit_hosts). All three extensions should |
| 78 // inject the script. |
| 79 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 80 ui_test_utils::NavigateToURL( |
| 81 browser(), |
| 82 embedded_test_server()->GetURL("/extensions/test_file.html")); |
| 83 |
| 84 // Wait for them all to complete. |
| 85 content_scripts_all_hosts_listener.WaitUntilSatisfied(); |
| 86 content_scripts_explicit_hosts_listener.WaitUntilSatisfied(); |
| 87 inject_scripts_all_hosts_listener.WaitUntilSatisfied(); |
| 88 |
| 89 // Retrieve the ActiveScriptController for that tab. |
| 90 content::WebContents* web_contents = |
| 91 browser()->tab_strip_model()->GetActiveWebContents(); |
| 92 ASSERT_TRUE(web_contents); |
| 93 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); |
| 94 ASSERT_TRUE(tab_helper); |
| 95 ActiveScriptController* controller = |
| 96 tab_helper->location_bar_controller()->active_script_controller(); |
| 97 ASSERT_TRUE(controller); |
| 98 |
| 99 // Verify the shown ExtensionActions. Each of the extensions with "all hosts" |
| 100 // should be displayed, but the extension that specified explicit hosts |
| 101 // should not. |
| 102 std::vector<ExtensionAction*> actions = controller->GetCurrentActions(); |
| 103 EXPECT_EQ(2u, actions.size()); |
| 104 EXPECT_TRUE(ActionsContainId(actions, content_scripts_all_hosts->id())); |
| 105 EXPECT_TRUE(ActionsContainId(actions, inject_scripts_all_hosts->id())); |
| 106 EXPECT_FALSE(ActionsContainId(actions, content_scripts_explicit_hosts->id())); |
| 107 } |
| 108 |
| 109 } // namespace extensions |
OLD | NEW |