ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / ui / location_bar / location_bar_browsertest.cc
blobf972f5014ed8aba27d54d4aed2511464349067ea
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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/test_extension_dir.h"
14 #include "chrome/browser/sessions/session_tab_helper.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/location_bar/location_bar.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/feature_switch.h"
22 #include "extensions/common/value_builder.h"
23 #include "extensions/test/extension_test_message_listener.h"
25 namespace {
27 const char kBackgroundScriptSource[] =
28 "chrome.pageAction.onClicked.addListener(function() {\n"
29 " chrome.test.sendMessage('clicked');\n"
30 "});\n"
31 "chrome.test.sendMessage('registered');\n";
32 const char kManifestSource[] =
33 "{"
34 " \"name\": \"%s\","
35 " \"version\": \"1.0\","
36 " \"manifest_version\": 2,"
37 " \"background\": { \"scripts\": [\"background.js\"] },"
38 " \"page_action\": { }"
39 "}";
41 } // namespace
43 class LocationBarBrowserTest : public ExtensionBrowserTest {
44 public:
45 LocationBarBrowserTest() {}
46 ~LocationBarBrowserTest() override {}
48 protected:
49 void SetUpCommandLine(base::CommandLine* command_line) override;
51 // Load an extension with a PageAction that sends a message when clicked.
52 const extensions::Extension* LoadPageActionExtension(
53 extensions::TestExtensionDir* dir);
55 private:
56 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_override_;
58 DISALLOW_COPY_AND_ASSIGN(LocationBarBrowserTest);
61 void LocationBarBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
62 // In order to let a vanilla extension override the bookmark star, we have to
63 // enable the switch.
64 enable_override_.reset(new extensions::FeatureSwitch::ScopedOverride(
65 extensions::FeatureSwitch::enable_override_bookmarks_ui(), true));
66 ExtensionBrowserTest::SetUpCommandLine(command_line);
69 const extensions::Extension* LocationBarBrowserTest::LoadPageActionExtension(
70 extensions::TestExtensionDir* dir) {
71 DCHECK(dir);
73 dir->WriteManifest(base::StringPrintf(kManifestSource, "page_action1"));
74 dir->WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundScriptSource);
76 ExtensionTestMessageListener registered_listener("registered", false);
77 const extensions::Extension* extension = LoadExtension(dir->unpacked_path());
78 registered_listener.WaitUntilSatisfied();
80 return extension;
83 // Test that page actions show up properly in the location bar. Since the
84 // page action logic is more fully tested as part of the extensions system, this
85 // only needs to check that they are displayed and clicking on them triggers
86 // the action.
87 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTest, PageActionUITest) {
88 LocationBarTesting* location_bar =
89 browser()->window()->GetLocationBar()->GetLocationBarForTesting();
91 // At the start, no page actions should exist.
92 EXPECT_EQ(0, location_bar->PageActionCount());
93 EXPECT_EQ(0, location_bar->PageActionVisibleCount());
95 // Load two extensions with page actions.
96 extensions::TestExtensionDir test_dir1;
97 const extensions::Extension* page_action1 =
98 LoadPageActionExtension(&test_dir1);
99 ASSERT_TRUE(page_action1);
101 extensions::TestExtensionDir test_dir2;
102 const extensions::Extension* page_action2 =
103 LoadPageActionExtension(&test_dir2);
104 ASSERT_TRUE(page_action2);
106 // Now there should be two page actions, but neither should be visible.
107 EXPECT_EQ(2, location_bar->PageActionCount());
108 EXPECT_EQ(0, location_bar->PageActionVisibleCount());
110 // Make the first page action visible.
111 ExtensionAction* action = extensions::ExtensionActionManager::Get(
112 profile())->GetPageAction(*page_action1);
113 content::WebContents* tab =
114 browser()->tab_strip_model()->GetActiveWebContents();
115 int tab_id = SessionTabHelper::IdForTab(tab);
116 action->SetIsVisible(tab_id, true);
117 extensions::ExtensionActionAPI::Get(profile())->NotifyChange(
118 action, tab, profile());
120 // Verify that only one action is visible and that it's the proper one.
121 EXPECT_EQ(2, location_bar->PageActionCount());
122 EXPECT_EQ(1, location_bar->PageActionVisibleCount());
123 EXPECT_EQ(action, location_bar->GetVisiblePageAction(0u));
125 // Trigger the visible page action, and ensure it executes.
126 ExtensionTestMessageListener clicked_listener("clicked", false);
127 clicked_listener.set_extension_id(page_action1->id());
128 location_bar->TestPageActionPressed(0u);
129 EXPECT_TRUE(clicked_listener.WaitUntilSatisfied());
132 // Test that installing an extension that overrides the bookmark star
133 // successfully hides the star.
134 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTest,
135 ExtensionCanOverrideBookmarkStar) {
136 LocationBarTesting* location_bar =
137 browser()->window()->GetLocationBar()->GetLocationBarForTesting();
138 // By default, we should show the star.
139 EXPECT_TRUE(location_bar->GetBookmarkStarVisibility());
141 // Create and install an extension that overrides the bookmark star.
142 extensions::DictionaryBuilder chrome_ui_overrides;
143 chrome_ui_overrides.Set(
144 "bookmarks_ui",
145 extensions::DictionaryBuilder().SetBoolean("remove_button", true));
146 scoped_refptr<const extensions::Extension> extension =
147 extensions::ExtensionBuilder().
148 SetManifest(extensions::DictionaryBuilder().
149 Set("name", "overrides star").
150 Set("manifest_version", 2).
151 Set("version", "0.1").
152 Set("description", "override the star").
153 Set("chrome_ui_overrides",
154 chrome_ui_overrides.Pass())).Build();
155 extension_service()->AddExtension(extension.get());
157 // The star should now be hidden.
158 EXPECT_FALSE(location_bar->GetBookmarkStarVisibility());
161 class LocationBarBrowserTestWithRedesign : public LocationBarBrowserTest {
162 public:
163 LocationBarBrowserTestWithRedesign() {}
164 ~LocationBarBrowserTestWithRedesign() override {}
166 private:
167 void SetUpCommandLine(base::CommandLine* command_line) override;
169 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_;
171 DISALLOW_COPY_AND_ASSIGN(LocationBarBrowserTestWithRedesign);
174 void LocationBarBrowserTestWithRedesign::SetUpCommandLine(
175 base::CommandLine* command_line) {
176 LocationBarBrowserTest::SetUpCommandLine(command_line);
177 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride(
178 extensions::FeatureSwitch::extension_action_redesign(), true));
181 // Test that page actions are not displayed in the location bar if the
182 // extension action redesign switch is enabled.
183 IN_PROC_BROWSER_TEST_F(LocationBarBrowserTestWithRedesign,
184 PageActionUITestWithRedesign) {
185 LocationBarTesting* location_bar =
186 browser()->window()->GetLocationBar()->GetLocationBarForTesting();
187 EXPECT_EQ(0, location_bar->PageActionCount());
188 EXPECT_EQ(0, location_bar->PageActionVisibleCount());
190 // Load an extension with a page action.
191 extensions::TestExtensionDir test_dir1;
192 const extensions::Extension* page_action1 =
193 LoadPageActionExtension(&test_dir1);
194 ASSERT_TRUE(page_action1);
196 // We should still have no page actions.
197 EXPECT_EQ(0, location_bar->PageActionCount());
198 EXPECT_EQ(0, location_bar->PageActionVisibleCount());
200 // Set the page action to be visible.
201 ExtensionAction* action = extensions::ExtensionActionManager::Get(
202 profile())->GetPageAction(*page_action1);
203 content::WebContents* tab =
204 browser()->tab_strip_model()->GetActiveWebContents();
205 int tab_id = SessionTabHelper::IdForTab(tab);
206 action->SetIsVisible(tab_id, true);
207 extensions::ExtensionActionAPI::Get(profile())->NotifyChange(
208 action, tab, profile());
210 // We should still have no page actions.
211 EXPECT_EQ(0, location_bar->PageActionCount());
212 EXPECT_EQ(0, location_bar->PageActionVisibleCount());