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

Side by Side Diff: chrome/browser/ui/views/bookmarks/bookmark_bar_view_unittest.cc

Issue 1246833004: bookmarks: Remove a bunch of unnecessary BookmarkModelFactory::GetForProfile() calls. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 5 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" 5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h" 13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/app_list/app_list_util.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" 15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view_test_helper.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
18 #include "chrome/test/base/browser_with_test_window_test.h" 18 #include "chrome/test/base/browser_with_test_window_test.h"
19 #include "chrome/test/base/scoped_testing_local_state.h" 19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h" 20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h" 21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "components/bookmarks/browser/bookmark_model.h" 22 #include "components/bookmarks/browser/bookmark_model.h"
23 #include "components/bookmarks/test/bookmark_test_helpers.h" 23 #include "components/bookmarks/test/bookmark_test_helpers.h"
24 #include "components/search_engines/search_terms_data.h" 24 #include "components/search_engines/search_terms_data.h"
25 #include "components/search_engines/template_url_service.h" 25 #include "components/search_engines/template_url_service.h"
26 #include "components/search_engines/template_url_service_client.h" 26 #include "components/search_engines/template_url_service_client.h"
27 #include "ui/views/controls/button/label_button.h" 27 #include "ui/views/controls/button/label_button.h"
28 #include "ui/views/controls/button/menu_button.h" 28 #include "ui/views/controls/button/menu_button.h"
29 29
30 using bookmarks::BookmarkModel;
30 using bookmarks::BookmarkNode; 31 using bookmarks::BookmarkNode;
31 32
32 class BookmarkBarViewTest : public BrowserWithTestWindowTest { 33 class BookmarkBarViewTest : public BrowserWithTestWindowTest {
33 public: 34 public:
34 BookmarkBarViewTest() {} 35 BookmarkBarViewTest() {}
35 36
36 void SetUp() override { 37 void SetUp() override {
37 BrowserWithTestWindowTest::SetUp(); 38 BrowserWithTestWindowTest::SetUp();
38 local_state_.reset( 39 local_state_.reset(
39 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); 40 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 const int height = bookmark_bar_view_->GetPreferredSize().height(); 72 const int height = bookmark_bar_view_->GetPreferredSize().height();
72 for (int i = 0; 73 for (int i = 0;
73 i < 100 && (test_helper_->GetBookmarkButtonCount() < count || 74 i < 100 && (test_helper_->GetBookmarkButtonCount() < count ||
74 !test_helper_->GetBookmarkButton(count - 1)->visible()); 75 !test_helper_->GetBookmarkButton(count - 1)->visible());
75 ++i) { 76 ++i) {
76 bookmark_bar_view_->SetBounds(0, 0, start_width + i * 10, height); 77 bookmark_bar_view_->SetBounds(0, 0, start_width + i * 10, height);
77 bookmark_bar_view_->Layout(); 78 bookmark_bar_view_->Layout();
78 } 79 }
79 } 80 }
80 81
81 const BookmarkNode* GetBookmarkBarNode() {
82 return BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
83 }
84
85 void WaitForBookmarkModelToLoad() { 82 void WaitForBookmarkModelToLoad() {
86 bookmarks::test::WaitForBookmarkModelToLoad( 83 bookmarks::test::WaitForBookmarkModelToLoad(
87 BookmarkModelFactory::GetForProfile(profile())); 84 BookmarkModelFactory::GetForProfile(profile()));
88 } 85 }
89 86
90 // Adds nodes to the bookmark bar node from |string|. See 87 // Adds nodes to the bookmark bar node from |string|. See
91 // bookmarks::test::AddNodesFromModelString() for details on |string|. 88 // bookmarks::test::AddNodesFromModelString() for details on |string|.
92 void AddNodesToBookmarkBarFromModelString(const std::string& string) { 89 void AddNodesToBookmarkBarFromModelString(const std::string& string) {
93 bookmarks::test::AddNodesFromModelString( 90 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
94 BookmarkModelFactory::GetForProfile(profile()), 91 bookmarks::test::AddNodesFromModelString(model, model->bookmark_bar_node(),
95 GetBookmarkBarNode(), 92 string);
96 string);
97 } 93 }
98 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll 94 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll
99 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if 95 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if
100 // need to create the BookmarkBarView after the model has populated. 96 // need to create the BookmarkBarView after the model has populated.
101 void CreateBookmarkBarView() { 97 void CreateBookmarkBarView() {
102 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr)); 98 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr));
103 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get())); 99 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get()));
104 } 100 }
105 101
106 // Creates the model, blocking until it loads, then creates the 102 // Creates the model, blocking until it loads, then creates the
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 bookmark_bar_view_->SetBounds( 227 bookmark_bar_view_->SetBounds(
232 0, 0, 5000, bookmark_bar_view_->bounds().height()); 228 0, 0, 5000, bookmark_bar_view_->bounds().height());
233 AddNodesToBookmarkBarFromModelString("a b c d e f "); 229 AddNodesToBookmarkBarFromModelString("a b c d e f ");
234 bookmark_bar_view_->Layout(); 230 bookmark_bar_view_->Layout();
235 EXPECT_EQ("a b c d e f", GetStringForVisibleButtons()); 231 EXPECT_EQ("a b c d e f", GetStringForVisibleButtons());
236 } 232 }
237 233
238 // Various assertions for removing nodes. 234 // Various assertions for removing nodes.
239 TEST_F(BookmarkBarViewTest, RemoveNode) { 235 TEST_F(BookmarkBarViewTest, RemoveNode) {
240 CreateBookmarkModelAndBookmarkBarView(); 236 CreateBookmarkModelAndBookmarkBarView();
241 const BookmarkNode* bookmark_bar_node = 237 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
242 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node(); 238 const BookmarkNode* bookmark_bar_node = model->bookmark_bar_node();
243 AddNodesToBookmarkBarFromModelString("a b c d e f "); 239 AddNodesToBookmarkBarFromModelString("a b c d e f ");
244 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); 240 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
245 SizeUntilButtonsVisible(1); 241 SizeUntilButtonsVisible(1);
246 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount()); 242 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
247 243
248 // Remove the 2nd node, should still only have 1 visible. 244 // Remove the 2nd node, should still only have 1 visible.
249 BookmarkModelFactory::GetForProfile(profile()) 245 model->Remove(bookmark_bar_node->GetChild(1));
250 ->Remove(bookmark_bar_node->GetChild(1));
251 EXPECT_EQ("a", GetStringForVisibleButtons()); 246 EXPECT_EQ("a", GetStringForVisibleButtons());
252 247
253 // Remove the first node, should force a new button (for the 'c' node). 248 // Remove the first node, should force a new button (for the 'c' node).
254 BookmarkModelFactory::GetForProfile(profile()) 249 model->Remove(bookmark_bar_node->GetChild(0));
255 ->Remove(bookmark_bar_node->GetChild(0));
256 ASSERT_EQ("c", GetStringForVisibleButtons()); 250 ASSERT_EQ("c", GetStringForVisibleButtons());
257 } 251 }
258 252
259 // Assertions for moving a node on the bookmark bar. 253 // Assertions for moving a node on the bookmark bar.
260 TEST_F(BookmarkBarViewTest, MoveNode) { 254 TEST_F(BookmarkBarViewTest, MoveNode) {
261 CreateBookmarkModelAndBookmarkBarView(); 255 CreateBookmarkModelAndBookmarkBarView();
262 const BookmarkNode* bookmark_bar_node = 256 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
263 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node(); 257 const BookmarkNode* bookmark_bar_node = model->bookmark_bar_node();
264 AddNodesToBookmarkBarFromModelString("a b c d e f "); 258 AddNodesToBookmarkBarFromModelString("a b c d e f ");
265 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); 259 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
266 260
267 // Move 'c' first resulting in 'c a b d e f'. 261 // Move 'c' first resulting in 'c a b d e f'.
268 BookmarkModelFactory::GetForProfile(profile()) 262 model->Move(bookmark_bar_node->GetChild(2), bookmark_bar_node, 0);
269 ->Move(bookmark_bar_node->GetChild(2), bookmark_bar_node, 0);
270 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); 263 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
271 264
272 // Make enough room for 1 node. 265 // Make enough room for 1 node.
273 SizeUntilButtonsVisible(1); 266 SizeUntilButtonsVisible(1);
274 EXPECT_EQ("c", GetStringForVisibleButtons()); 267 EXPECT_EQ("c", GetStringForVisibleButtons());
275 268
276 // Move 'f' first, resulting in 'f c a b d e'. 269 // Move 'f' first, resulting in 'f c a b d e'.
277 BookmarkModelFactory::GetForProfile(profile()) 270 model->Move(bookmark_bar_node->GetChild(5), bookmark_bar_node, 0);
278 ->Move(bookmark_bar_node->GetChild(5), bookmark_bar_node, 0);
279 SizeUntilButtonsVisible(2); 271 SizeUntilButtonsVisible(2);
280 EXPECT_EQ("f c", GetStringForVisibleButtons()); 272 EXPECT_EQ("f c", GetStringForVisibleButtons());
281 273
282 // Move 'f' to the end, resulting in 'c a b d e f'. 274 // Move 'f' to the end, resulting in 'c a b d e f'.
283 BookmarkModelFactory::GetForProfile(profile()) 275 model->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 6);
284 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 6);
285 SizeUntilButtonsVisible(2); 276 SizeUntilButtonsVisible(2);
286 EXPECT_EQ("c a", GetStringForVisibleButtons()); 277 EXPECT_EQ("c a", GetStringForVisibleButtons());
287 278
288 // Move 'c' after 'a', resulting in 'a c b d e f'. 279 // Move 'c' after 'a', resulting in 'a c b d e f'.
289 BookmarkModelFactory::GetForProfile(profile()) 280 model->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 2);
290 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 2);
291 SizeUntilButtonsVisible(2); 281 SizeUntilButtonsVisible(2);
292 EXPECT_EQ("a c", GetStringForVisibleButtons()); 282 EXPECT_EQ("a c", GetStringForVisibleButtons());
293 } 283 }
294 284
295 // Assertions for changing the title of a node. 285 // Assertions for changing the title of a node.
296 TEST_F(BookmarkBarViewTest, ChangeTitle) { 286 TEST_F(BookmarkBarViewTest, ChangeTitle) {
297 CreateBookmarkModelAndBookmarkBarView(); 287 CreateBookmarkModelAndBookmarkBarView();
298 const BookmarkNode* bookmark_bar_node = GetBookmarkBarNode(); 288 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
289 const BookmarkNode* bookmark_bar_node = model->bookmark_bar_node();
299 AddNodesToBookmarkBarFromModelString("a b c d e f "); 290 AddNodesToBookmarkBarFromModelString("a b c d e f ");
300 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); 291 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
301 292
302 BookmarkModelFactory::GetForProfile(profile()) 293 model->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
303 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
304 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); 294 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
305 295
306 // Make enough room for 1 node. 296 // Make enough room for 1 node.
307 SizeUntilButtonsVisible(1); 297 SizeUntilButtonsVisible(1);
308 EXPECT_EQ("a1", GetStringForVisibleButtons()); 298 EXPECT_EQ("a1", GetStringForVisibleButtons());
309 299
310 BookmarkModelFactory::GetForProfile(profile()) 300 model->SetTitle(bookmark_bar_node->GetChild(1), base::ASCIIToUTF16("b1"));
311 ->SetTitle(bookmark_bar_node->GetChild(1), base::ASCIIToUTF16("b1"));
312 EXPECT_EQ("a1", GetStringForVisibleButtons()); 301 EXPECT_EQ("a1", GetStringForVisibleButtons());
313 302
314 BookmarkModelFactory::GetForProfile(profile()) 303 model->SetTitle(bookmark_bar_node->GetChild(5), base::ASCIIToUTF16("f1"));
315 ->SetTitle(bookmark_bar_node->GetChild(5), base::ASCIIToUTF16("f1"));
316 EXPECT_EQ("a1", GetStringForVisibleButtons()); 304 EXPECT_EQ("a1", GetStringForVisibleButtons());
317 305
318 BookmarkModelFactory::GetForProfile(profile()) 306 model->SetTitle(bookmark_bar_node->GetChild(3), base::ASCIIToUTF16("d1"));
319 ->SetTitle(bookmark_bar_node->GetChild(3), base::ASCIIToUTF16("d1"));
320 307
321 // Make the second button visible, changes the title of the first to something 308 // Make the second button visible, changes the title of the first to something
322 // really long and make sure the second button hides. 309 // really long and make sure the second button hides.
323 SizeUntilButtonsVisible(2); 310 SizeUntilButtonsVisible(2);
324 EXPECT_EQ("a1 b1", GetStringForVisibleButtons()); 311 EXPECT_EQ("a1 b1", GetStringForVisibleButtons());
325 BookmarkModelFactory::GetForProfile(profile()) 312 model->SetTitle(bookmark_bar_node->GetChild(0),
326 ->SetTitle(bookmark_bar_node->GetChild(0), 313 base::ASCIIToUTF16("a_really_long_title"));
327 base::ASCIIToUTF16("a_really_long_title"));
328 EXPECT_LE(1, test_helper_->GetBookmarkButtonCount()); 314 EXPECT_LE(1, test_helper_->GetBookmarkButtonCount());
329 315
330 // Change the title back and make sure the 2nd button is visible again. Don't 316 // 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 317 // use GetStringForVisibleButtons() here as more buttons may have been
332 // created. 318 // created.
333 BookmarkModelFactory::GetForProfile(profile()) 319 model->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
334 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
335 ASSERT_LE(2, test_helper_->GetBookmarkButtonCount()); 320 ASSERT_LE(2, test_helper_->GetBookmarkButtonCount());
336 EXPECT_TRUE(test_helper_->GetBookmarkButton(0)->visible()); 321 EXPECT_TRUE(test_helper_->GetBookmarkButton(0)->visible());
337 EXPECT_TRUE(test_helper_->GetBookmarkButton(1)->visible()); 322 EXPECT_TRUE(test_helper_->GetBookmarkButton(1)->visible());
338 323
339 bookmark_bar_view_->SetBounds( 324 bookmark_bar_view_->SetBounds(
340 0, 0, 5000, bookmark_bar_view_->bounds().height()); 325 0, 0, 5000, bookmark_bar_view_->bounds().height());
341 bookmark_bar_view_->Layout(); 326 bookmark_bar_view_->Layout();
342 EXPECT_EQ("a1 b1 c d1 e f1", GetStringForVisibleButtons()); 327 EXPECT_EQ("a1 b1 c d1 e f1", GetStringForVisibleButtons());
343 } 328 }
344 329
(...skipping 12 matching lines...) Expand all
357 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, 342 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
358 new base::FundamentalValue(false)); 343 new base::FundamentalValue(false));
359 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); 344 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
360 345
361 // And try showing it via policy too. 346 // And try showing it via policy too.
362 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, 347 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
363 new base::FundamentalValue(true)); 348 new base::FundamentalValue(true));
364 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); 349 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
365 } 350 }
366 #endif 351 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698