Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Side by Side Diff: chrome/browser/extensions/location_bar_controller_unittest.cc

Issue 527963003: Uncouple ActiveScriptController from LocationBarController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/extensions/active_script_controller.h"
10 #include "chrome/browser/extensions/extension_action.h" 11 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h" 12 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/location_bar_controller.h"
13 #include "chrome/browser/extensions/tab_helper.h" 15 #include "chrome/browser/extensions/tab_helper.h"
14 #include "chrome/browser/extensions/test_extension_system.h" 16 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/browser/sessions/session_tab_helper.h" 17 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
20 #include "components/crx_file/id_util.h"
18 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_builder.h" 22 #include "extensions/common/extension_builder.h"
23 #include "extensions/common/feature_switch.h"
20 #include "extensions/common/value_builder.h" 24 #include "extensions/common/value_builder.h"
21 25
22 #if defined(OS_CHROMEOS) 26 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" 27 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
24 #include "chrome/browser/chromeos/settings/cros_settings.h" 28 #include "chrome/browser/chromeos/settings/cros_settings.h"
25 #include "chrome/browser/chromeos/settings/device_settings_service.h" 29 #include "chrome/browser/chromeos/settings/device_settings_service.h"
26 #endif 30 #endif
27 31
28 namespace extensions { 32 namespace extensions {
29 namespace { 33 namespace {
30 34
31 class LocationBarControllerUnitTest : public ChromeRenderViewHostTestHarness { 35 class LocationBarControllerUnitTest : public ChromeRenderViewHostTestHarness {
32 protected: 36 protected:
33 virtual void SetUp() OVERRIDE { 37 virtual void SetUp() OVERRIDE {
38 active_script_override_.reset(new FeatureSwitch::ScopedOverride(
39 FeatureSwitch::scripts_require_action(), true));
40
34 ChromeRenderViewHostTestHarness::SetUp(); 41 ChromeRenderViewHostTestHarness::SetUp();
35 #if defined OS_CHROMEOS 42 #if defined OS_CHROMEOS
36 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); 43 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
37 #endif 44 #endif
38 TabHelper::CreateForWebContents(web_contents()); 45 TabHelper::CreateForWebContents(web_contents());
39 // Create an ExtensionService so the LocationBarController can find its 46 // Create an ExtensionService so the LocationBarController can find its
40 // extensions. 47 // extensions.
41 CommandLine command_line(CommandLine::NO_PROGRAM); 48 CommandLine command_line(CommandLine::NO_PROGRAM);
42 Profile* profile = 49 Profile* profile =
43 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 50 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
44 extension_service_ = static_cast<TestExtensionSystem*>( 51 extension_service_ = static_cast<TestExtensionSystem*>(
45 ExtensionSystem::Get(profile))->CreateExtensionService( 52 ExtensionSystem::Get(profile))->CreateExtensionService(
46 &command_line, base::FilePath(), false); 53 &command_line, base::FilePath(), false);
47 } 54 }
48 55
49 virtual void TearDown() OVERRIDE { 56 virtual void TearDown() OVERRIDE {
50 #if defined OS_CHROMEOS 57 #if defined OS_CHROMEOS
51 test_user_manager_.reset(); 58 test_user_manager_.reset();
52 #endif 59 #endif
53 ChromeRenderViewHostTestHarness::TearDown(); 60 ChromeRenderViewHostTestHarness::TearDown();
54 } 61 }
55 62
56 int tab_id() { 63 int tab_id() {
57 return SessionTabHelper::IdForTab(web_contents()); 64 return SessionTabHelper::IdForTab(web_contents());
58 } 65 }
59 66
67 const Extension* AddExtension(bool has_page_actions,
68 const std::string& name) {
69 DictionaryBuilder manifest;
70 manifest.Set("name", name)
71 .Set("version", "1.0.0")
72 .Set("manifest_version", 2)
73 .Set("permissions", ListBuilder().Append("tabs"));
74 if (has_page_actions) {
75 manifest.Set("page_action", DictionaryBuilder()
76 .Set("default_title", "Hello"));
77 }
78 scoped_refptr<const Extension> extension =
79 ExtensionBuilder().SetManifest(manifest.Pass())
80 .SetID(crx_file::id_util::GenerateId(name))
81 .Build();
82 extension_service_->AddExtension(extension.get());
83 return extension;
84 }
85
60 ExtensionService* extension_service_; 86 ExtensionService* extension_service_;
61 87
62 private: 88 private:
63 #if defined OS_CHROMEOS 89 #if defined OS_CHROMEOS
64 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 90 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
65 chromeos::ScopedTestCrosSettings test_cros_settings_; 91 chromeos::ScopedTestCrosSettings test_cros_settings_;
66 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; 92 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
67 #endif 93 #endif
94
95 // Since we also test that we show page actions for pending script requests,
96 // we need to enable that feature.
97 scoped_ptr<FeatureSwitch::ScopedOverride> active_script_override_;
68 }; 98 };
69 99
100 // Test that the location bar gets the proper current actions.
101 TEST_F(LocationBarControllerUnitTest, LocationBarDisplaysPageActions) {
102 // Load up two extensions, one with a page action and one without.
103 const Extension* page_action = AddExtension(true, "page_actions");
104 const Extension* no_action = AddExtension(false, "no_actions");
105
106 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents());
107 ASSERT_TRUE(tab_helper);
108 LocationBarController* controller = tab_helper->location_bar_controller();
109 ASSERT_TRUE(controller);
110
111 // There should only be one action - the action for the extension with a
112 // page action.
113 std::vector<ExtensionAction*> current_actions =
114 controller->GetCurrentActions();
115 ASSERT_EQ(1u, current_actions.size());
116 EXPECT_EQ(page_action->id(), current_actions[0]->extension_id());
117
118 // If we request a script injection, then the location bar controller should
119 // also show a page action for that extension.
120 ActiveScriptController* active_script_controller =
121 ActiveScriptController::GetForWebContents(web_contents());
122 ASSERT_TRUE(active_script_controller);
123 active_script_controller->RequestScriptInjectionForTesting(no_action,
124 base::Closure());
125 current_actions = controller->GetCurrentActions();
126 ASSERT_EQ(2u, current_actions.size());
127 // Check that each extension is present in the vector.
128 EXPECT_TRUE(current_actions[0]->extension_id() == no_action->id() ||
129 current_actions[1]->extension_id() == no_action->id());
130 EXPECT_TRUE(current_actions[0]->extension_id() == page_action->id() ||
131 current_actions[1]->extension_id() == page_action->id());
132
133 // If we request a script injection for an extension that already has a
134 // page action, only one action should be visible.
135 active_script_controller->RequestScriptInjectionForTesting(page_action,
136 base::Closure());
137 current_actions = controller->GetCurrentActions();
138 ASSERT_EQ(2u, current_actions.size());
139 EXPECT_TRUE(current_actions[0]->extension_id() == no_action->id() ||
140 current_actions[1]->extension_id() == no_action->id());
141 EXPECT_TRUE(current_actions[0]->extension_id() == page_action->id() ||
142 current_actions[1]->extension_id() == page_action->id());
143
144 // Navigating away means that only page actions are shown again.
145 NavigateAndCommit(GURL("http://google.com"));
146 current_actions = controller->GetCurrentActions();
147 ASSERT_EQ(1u, current_actions.size());
148 EXPECT_EQ(page_action->id(), current_actions[0]->extension_id());
149 }
150
70 // Test that navigating clears all state in a page action. 151 // Test that navigating clears all state in a page action.
71 TEST_F(LocationBarControllerUnitTest, NavigationClearsState) { 152 TEST_F(LocationBarControllerUnitTest, NavigationClearsState) {
72 scoped_refptr<const Extension> extension = 153 const Extension* extension = AddExtension(true, "page_actions");
73 ExtensionBuilder()
74 .SetManifest(DictionaryBuilder()
75 .Set("name", "Extension with page action")
76 .Set("version", "1.0.0")
77 .Set("manifest_version", 2)
78 .Set("permissions", ListBuilder()
79 .Append("tabs"))
80 .Set("page_action", DictionaryBuilder()
81 .Set("default_title", "Hello")))
82 .Build();
83 extension_service_->AddExtension(extension.get());
84 154
85 NavigateAndCommit(GURL("http://www.google.com")); 155 NavigateAndCommit(GURL("http://www.google.com"));
86 156
87 ExtensionAction& page_action = 157 ExtensionAction& page_action =
88 *ExtensionActionManager::Get(profile())->GetPageAction(*extension.get()); 158 *ExtensionActionManager::Get(profile())->GetPageAction(*extension);
89 page_action.SetTitle(tab_id(), "Goodbye"); 159 page_action.SetTitle(tab_id(), "Goodbye");
90 page_action.SetPopupUrl( 160 page_action.SetPopupUrl(tab_id(), extension->GetResourceURL("popup.html"));
91 tab_id(), extension->GetResourceURL("popup.html"));
92 161
93 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id())); 162 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
94 EXPECT_EQ(extension->GetResourceURL("popup.html"), 163 EXPECT_EQ(extension->GetResourceURL("popup.html"),
95 page_action.GetPopupUrl(tab_id())); 164 page_action.GetPopupUrl(tab_id()));
96 165
97 // Within-page navigation should keep the settings. 166 // Within-page navigation should keep the settings.
98 NavigateAndCommit(GURL("http://www.google.com/#hash")); 167 NavigateAndCommit(GURL("http://www.google.com/#hash"));
99 168
100 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id())); 169 EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
101 EXPECT_EQ(extension->GetResourceURL("popup.html"), 170 EXPECT_EQ(extension->GetResourceURL("popup.html"),
102 page_action.GetPopupUrl(tab_id())); 171 page_action.GetPopupUrl(tab_id()));
103 172
104 // Should discard the settings, and go back to the defaults. 173 // Should discard the settings, and go back to the defaults.
105 NavigateAndCommit(GURL("http://www.yahoo.com")); 174 NavigateAndCommit(GURL("http://www.yahoo.com"));
106 175
107 EXPECT_EQ("Hello", page_action.GetTitle(tab_id())); 176 EXPECT_EQ("Hello", page_action.GetTitle(tab_id()));
108 EXPECT_EQ(GURL(), page_action.GetPopupUrl(tab_id())); 177 EXPECT_EQ(GURL(), page_action.GetPopupUrl(tab_id()));
109 }; 178 }
110
111 // TODO(devlin): We should really have more tests for this.
112 // NavigationClearsState doesn't test at all that the LocationBarController
113 // actually *returns* the proper PageActions in GetCurrentActions. Do we do
114 // this elsewhere?
115 179
116 } // namespace 180 } // namespace
117 } // namespace extensions 181 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698