[Cleanup] Used scoped pointers in KeyedServiceFactory's SetTestingFactory functions.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view_unittest.cc
blob2ab778ea91d8db3c8a3fc13a5020cfa05dd16265
1 // Copyright 2013 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 "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/app_list/app_list_util.h"
15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view_test_helper.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/test/base/browser_with_test_window_test.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "components/bookmarks/browser/bookmark_model.h"
23 #include "components/bookmarks/test/bookmark_test_helpers.h"
24 #include "components/search_engines/search_terms_data.h"
25 #include "components/search_engines/template_url_service.h"
26 #include "components/search_engines/template_url_service_client.h"
27 #include "ui/views/controls/button/label_button.h"
28 #include "ui/views/controls/button/menu_button.h"
30 using bookmarks::BookmarkNode;
32 class BookmarkBarViewTest : public BrowserWithTestWindowTest {
33 public:
34 BookmarkBarViewTest() {}
36 void SetUp() override {
37 BrowserWithTestWindowTest::SetUp();
38 local_state_.reset(
39 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
42 void TearDown() override {
43 test_helper_.reset();
44 bookmark_bar_view_.reset();
45 local_state_.reset();
46 BrowserWithTestWindowTest::TearDown();
49 protected:
50 // Returns a string containing the label of each of the *visible* buttons on
51 // the bookmark bar. Each label is separated by a space.
52 std::string GetStringForVisibleButtons() {
53 std::string result;
54 for (int i = 0; i < test_helper_->GetBookmarkButtonCount() &&
55 test_helper_->GetBookmarkButton(i)->visible();
56 ++i) {
57 if (i != 0)
58 result += " ";
59 result +=
60 base::UTF16ToASCII(test_helper_->GetBookmarkButton(i)->GetText());
62 return result;
65 // Continues sizing the bookmark bar until it has |count| buttons that are
66 // visible.
67 // NOTE: if the model has more than |count| buttons this results in
68 // |count| + 1 buttons.
69 void SizeUntilButtonsVisible(int count) {
70 const int start_width = bookmark_bar_view_->width();
71 const int height = bookmark_bar_view_->GetPreferredSize().height();
72 for (int i = 0;
73 i < 100 && (test_helper_->GetBookmarkButtonCount() < count ||
74 !test_helper_->GetBookmarkButton(count - 1)->visible());
75 ++i) {
76 bookmark_bar_view_->SetBounds(0, 0, start_width + i * 10, height);
77 bookmark_bar_view_->Layout();
81 const BookmarkNode* GetBookmarkBarNode() {
82 return BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
85 void WaitForBookmarkModelToLoad() {
86 bookmarks::test::WaitForBookmarkModelToLoad(
87 BookmarkModelFactory::GetForProfile(profile()));
90 // Adds nodes to the bookmark bar node from |string|. See
91 // bookmarks::test::AddNodesFromModelString() for details on |string|.
92 void AddNodesToBookmarkBarFromModelString(const std::string& string) {
93 bookmarks::test::AddNodesFromModelString(
94 BookmarkModelFactory::GetForProfile(profile()),
95 GetBookmarkBarNode(),
96 string);
98 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll
99 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if
100 // need to create the BookmarkBarView after the model has populated.
101 void CreateBookmarkBarView() {
102 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr));
103 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get()));
106 // Creates the model, blocking until it loads, then creates the
107 // BookmarkBarView.
108 void CreateBookmarkModelAndBookmarkBarView() {
109 profile()->CreateBookmarkModel(true);
110 WaitForBookmarkModelToLoad();
111 CreateBookmarkBarView();
114 // BrowserWithTestWindowTest:
115 TestingProfile* CreateProfile() override {
116 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
117 // TemplateURLService is normally NULL during testing. Instant extended
118 // needs this service so set a custom factory function.
119 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
120 profile, &BookmarkBarViewTest::CreateTemplateURLService);
121 return profile;
124 scoped_ptr<BookmarkBarViewTestHelper> test_helper_;
125 scoped_ptr<BookmarkBarView> bookmark_bar_view_;
127 private:
128 static scoped_ptr<KeyedService> CreateTemplateURLService(
129 content::BrowserContext* profile) {
130 return make_scoped_ptr(new TemplateURLService(
131 static_cast<Profile*>(profile)->GetPrefs(),
132 make_scoped_ptr(new SearchTermsData), NULL,
133 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure()));
136 scoped_ptr<ScopedTestingLocalState> local_state_;
138 DISALLOW_COPY_AND_ASSIGN(BookmarkBarViewTest);
141 // Verify that in instant extended mode the visibility of the apps shortcut
142 // button properly follows the pref value.
143 TEST_F(BookmarkBarViewTest, AppsShortcutVisibility) {
144 CreateBookmarkModelAndBookmarkBarView();
145 browser()->profile()->GetPrefs()->SetBoolean(
146 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
147 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
149 // Try to make the Apps shortcut visible. Its visibility depends on whether
150 // the app launcher is enabled.
151 browser()->profile()->GetPrefs()->SetBoolean(
152 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, true);
153 if (IsAppLauncherEnabled()) {
154 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
155 } else {
156 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
159 // Make sure we can also properly transition from true to false.
160 browser()->profile()->GetPrefs()->SetBoolean(
161 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
162 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
165 // Various assertions around visibilty of the overflow_button.
166 TEST_F(BookmarkBarViewTest, OverflowVisibility) {
167 profile()->CreateBookmarkModel(true);
168 CreateBookmarkBarView();
169 EXPECT_FALSE(test_helper_->overflow_button()->visible());
171 WaitForBookmarkModelToLoad();
172 AddNodesToBookmarkBarFromModelString("a b c d e f ");
173 EXPECT_TRUE(test_helper_->overflow_button()->visible());
175 SizeUntilButtonsVisible(1);
176 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
177 const int width_for_one = bookmark_bar_view_->bounds().width();
178 EXPECT_TRUE(test_helper_->overflow_button()->visible());
180 // Go really big, which should force all buttons to be added.
181 bookmark_bar_view_->SetBounds(
182 0, 0, 5000, bookmark_bar_view_->bounds().height());
183 bookmark_bar_view_->Layout();
184 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
185 EXPECT_FALSE(test_helper_->overflow_button()->visible());
187 bookmark_bar_view_->SetBounds(
188 0, 0, width_for_one, bookmark_bar_view_->bounds().height());
189 bookmark_bar_view_->Layout();
190 EXPECT_TRUE(test_helper_->overflow_button()->visible());
193 // Verifies buttons get added correctly when BookmarkBarView is created after
194 // the model and the model has nodes.
195 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAddedAfterModelHasNodes) {
196 profile()->CreateBookmarkModel(true);
197 WaitForBookmarkModelToLoad();
198 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile())->loaded());
199 AddNodesToBookmarkBarFromModelString("a b c d e f ");
200 CreateBookmarkBarView();
201 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
203 SizeUntilButtonsVisible(1);
204 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
206 // Go really big, which should force all buttons to be added.
207 bookmark_bar_view_->SetBounds(
208 0, 0, 5000, bookmark_bar_view_->bounds().height());
209 bookmark_bar_view_->Layout();
210 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
213 // Verifies buttons are added as the model and size change.
214 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAdded) {
215 CreateBookmarkModelAndBookmarkBarView();
216 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile())->loaded());
217 AddNodesToBookmarkBarFromModelString("a b c d e f ");
218 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
219 SizeUntilButtonsVisible(1);
220 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
222 // Go really big, which should force all buttons to be added.
223 bookmark_bar_view_->SetBounds(
224 0, 0, 5000, bookmark_bar_view_->bounds().height());
225 bookmark_bar_view_->Layout();
226 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
229 TEST_F(BookmarkBarViewTest, AddNodesWhenBarAlreadySized) {
230 CreateBookmarkModelAndBookmarkBarView();
231 bookmark_bar_view_->SetBounds(
232 0, 0, 5000, bookmark_bar_view_->bounds().height());
233 AddNodesToBookmarkBarFromModelString("a b c d e f ");
234 bookmark_bar_view_->Layout();
235 EXPECT_EQ("a b c d e f", GetStringForVisibleButtons());
238 // Various assertions for removing nodes.
239 TEST_F(BookmarkBarViewTest, RemoveNode) {
240 CreateBookmarkModelAndBookmarkBarView();
241 const BookmarkNode* bookmark_bar_node =
242 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
243 AddNodesToBookmarkBarFromModelString("a b c d e f ");
244 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
245 SizeUntilButtonsVisible(1);
246 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
248 // Remove the 2nd node, should still only have 1 visible.
249 BookmarkModelFactory::GetForProfile(profile())
250 ->Remove(bookmark_bar_node->GetChild(1));
251 EXPECT_EQ("a", GetStringForVisibleButtons());
253 // Remove the first node, should force a new button (for the 'c' node).
254 BookmarkModelFactory::GetForProfile(profile())
255 ->Remove(bookmark_bar_node->GetChild(0));
256 ASSERT_EQ("c", GetStringForVisibleButtons());
259 // Assertions for moving a node on the bookmark bar.
260 TEST_F(BookmarkBarViewTest, MoveNode) {
261 CreateBookmarkModelAndBookmarkBarView();
262 const BookmarkNode* bookmark_bar_node =
263 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
264 AddNodesToBookmarkBarFromModelString("a b c d e f ");
265 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
267 // Move 'c' first resulting in 'c a b d e f'.
268 BookmarkModelFactory::GetForProfile(profile())
269 ->Move(bookmark_bar_node->GetChild(2), bookmark_bar_node, 0);
270 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
272 // Make enough room for 1 node.
273 SizeUntilButtonsVisible(1);
274 EXPECT_EQ("c", GetStringForVisibleButtons());
276 // Move 'f' first, resulting in 'f c a b d e'.
277 BookmarkModelFactory::GetForProfile(profile())
278 ->Move(bookmark_bar_node->GetChild(5), bookmark_bar_node, 0);
279 SizeUntilButtonsVisible(2);
280 EXPECT_EQ("f c", GetStringForVisibleButtons());
282 // Move 'f' to the end, resulting in 'c a b d e f'.
283 BookmarkModelFactory::GetForProfile(profile())
284 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 6);
285 SizeUntilButtonsVisible(2);
286 EXPECT_EQ("c a", GetStringForVisibleButtons());
288 // Move 'c' after 'a', resulting in 'a c b d e f'.
289 BookmarkModelFactory::GetForProfile(profile())
290 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 2);
291 SizeUntilButtonsVisible(2);
292 EXPECT_EQ("a c", GetStringForVisibleButtons());
295 // Assertions for changing the title of a node.
296 TEST_F(BookmarkBarViewTest, ChangeTitle) {
297 CreateBookmarkModelAndBookmarkBarView();
298 const BookmarkNode* bookmark_bar_node = GetBookmarkBarNode();
299 AddNodesToBookmarkBarFromModelString("a b c d e f ");
300 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
302 BookmarkModelFactory::GetForProfile(profile())
303 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
304 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
306 // Make enough room for 1 node.
307 SizeUntilButtonsVisible(1);
308 EXPECT_EQ("a1", GetStringForVisibleButtons());
310 BookmarkModelFactory::GetForProfile(profile())
311 ->SetTitle(bookmark_bar_node->GetChild(1), base::ASCIIToUTF16("b1"));
312 EXPECT_EQ("a1", GetStringForVisibleButtons());
314 BookmarkModelFactory::GetForProfile(profile())
315 ->SetTitle(bookmark_bar_node->GetChild(5), base::ASCIIToUTF16("f1"));
316 EXPECT_EQ("a1", GetStringForVisibleButtons());
318 BookmarkModelFactory::GetForProfile(profile())
319 ->SetTitle(bookmark_bar_node->GetChild(3), base::ASCIIToUTF16("d1"));
321 // Make the second button visible, changes the title of the first to something
322 // really long and make sure the second button hides.
323 SizeUntilButtonsVisible(2);
324 EXPECT_EQ("a1 b1", GetStringForVisibleButtons());
325 BookmarkModelFactory::GetForProfile(profile())
326 ->SetTitle(bookmark_bar_node->GetChild(0),
327 base::ASCIIToUTF16("a_really_long_title"));
328 EXPECT_LE(1, test_helper_->GetBookmarkButtonCount());
330 // Change the title back and make sure the 2nd button is visible again. Don't
331 // use GetStringForVisibleButtons() here as more buttons may have been
332 // created.
333 BookmarkModelFactory::GetForProfile(profile())
334 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
335 ASSERT_LE(2, test_helper_->GetBookmarkButtonCount());
336 EXPECT_TRUE(test_helper_->GetBookmarkButton(0)->visible());
337 EXPECT_TRUE(test_helper_->GetBookmarkButton(1)->visible());
339 bookmark_bar_view_->SetBounds(
340 0, 0, 5000, bookmark_bar_view_->bounds().height());
341 bookmark_bar_view_->Layout();
342 EXPECT_EQ("a1 b1 c d1 e f1", GetStringForVisibleButtons());
345 #if !defined(OS_CHROMEOS)
346 // Verifies that the apps shortcut is shown or hidden following the policy
347 // value. This policy (and the apps shortcut) isn't present on ChromeOS.
348 TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) {
349 CreateBookmarkModelAndBookmarkBarView();
350 // By default, the pref is not managed and the apps shortcut is shown.
351 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
352 EXPECT_FALSE(prefs->IsManagedPreference(
353 bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
354 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
356 // Hide the apps shortcut by policy, via the managed pref.
357 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
358 new base::FundamentalValue(false));
359 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
361 // And try showing it via policy too.
362 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
363 new base::FundamentalValue(true));
364 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
366 #endif