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

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

Issue 677533003: Changes BookmarkBarView to only create buttons as needed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: integrate feedback Created 6 years, 1 month 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 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/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h" 9 #include "base/values.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h" 12 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/browser/ui/app_list/app_list_util.h" 13 #include "chrome/browser/ui/app_list/app_list_util.h"
13 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view_test_helper.h" 14 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view_test_helper.h"
14 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/browser_with_test_window_test.h" 17 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/scoped_testing_local_state.h" 18 #include "chrome/test/base/scoped_testing_local_state.h"
18 #include "chrome/test/base/testing_browser_process.h" 19 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h" 20 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "components/bookmarks/browser/bookmark_model.h" 21 #include "components/bookmarks/browser/bookmark_model.h"
21 #include "components/bookmarks/test/bookmark_test_helpers.h" 22 #include "components/bookmarks/test/bookmark_test_helpers.h"
22 #include "components/search_engines/search_terms_data.h" 23 #include "components/search_engines/search_terms_data.h"
23 #include "components/search_engines/template_url_service.h" 24 #include "components/search_engines/template_url_service.h"
24 #include "components/search_engines/template_url_service_client.h" 25 #include "components/search_engines/template_url_service_client.h"
25 #include "ui/views/controls/button/label_button.h" 26 #include "ui/views/controls/button/label_button.h"
27 #include "ui/views/controls/button/menu_button.h"
26 28
27 class BookmarkBarViewTest : public BrowserWithTestWindowTest { 29 class BookmarkBarViewTest : public BrowserWithTestWindowTest {
28 public: 30 public:
29 BookmarkBarViewTest() {} 31 BookmarkBarViewTest() {}
30 32
31 void SetUp() override { 33 void SetUp() override {
32 BrowserWithTestWindowTest::SetUp(); 34 BrowserWithTestWindowTest::SetUp();
33 local_state_.reset( 35 local_state_.reset(
34 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); 36 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
35 } 37 }
36 38
37 void TearDown() override { 39 void TearDown() override {
38 test_helper_.reset(); 40 test_helper_.reset();
39 bookmark_bar_view_.reset(); 41 bookmark_bar_view_.reset();
40 local_state_.reset(); 42 local_state_.reset();
41 BrowserWithTestWindowTest::TearDown(); 43 BrowserWithTestWindowTest::TearDown();
42 } 44 }
43 45
44 protected: 46 protected:
45 TestingProfile* CreateProfile() override { 47 // Returns a string containing the label of each of the *visible* buttons on
46 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); 48 // the bookmark bar. Each label is separated by a space.
47 // TemplateURLService is normally NULL during testing. Instant extended 49 std::string GetStringForVisibleButtons() {
48 // needs this service so set a custom factory function. 50 std::string result;
49 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( 51 for (int i = 0; i < test_helper_->GetBookmarkButtonCount() &&
50 profile, &BookmarkBarViewTest::CreateTemplateURLService); 52 test_helper_->GetBookmarkButton(i)->visible();
51 return profile; 53 ++i) {
54 if (i != 0)
55 result += " ";
56 result +=
57 base::UTF16ToASCII(test_helper_->GetBookmarkButton(i)->GetText());
58 }
59 return result;
52 } 60 }
53 61
62 // Continues sizing the bookmark bar until it has |count| buttons that are
63 // visible.
64 // NOTE: if the model has more than |count| buttons this results in
65 // |count| + 1 buttons.
66 void SizeUntilButtonsVisible(int count) {
67 const int start_width = bookmark_bar_view_->width();
68 const int height = bookmark_bar_view_->GetPreferredSize().height();
69 for (int i = 0;
70 i < 100 && (test_helper_->GetBookmarkButtonCount() < count ||
71 !test_helper_->GetBookmarkButton(count - 1)->visible());
72 ++i) {
73 bookmark_bar_view_->SetBounds(0, 0, start_width + i * 10, height);
74 bookmark_bar_view_->Layout();
75 }
76 }
77
78 const BookmarkNode* GetBookmarkBarNode() {
79 return BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
80 }
81
82 void WaitForBookmarkModelToLoad() {
83 bookmarks::test::WaitForBookmarkModelToLoad(
84 BookmarkModelFactory::GetForProfile(profile()));
85 }
86
87 // Adds nodes to the bookmark bar node from |string|. See
88 // bookmarks::test::AddNodesFromModelString() for details on |string|.
89 void AddNodesToBookmarkBarFromModelString(const std::string& string) {
90 bookmarks::test::AddNodesFromModelString(
91 BookmarkModelFactory::GetForProfile(profile()),
92 GetBookmarkBarNode(),
93 string);
94 }
54 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll 95 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll
55 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if 96 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if
56 // need to create the BookmarkBarView after the model has populated. 97 // need to create the BookmarkBarView after the model has populated.
57 void CreateBookmarkBarView() { 98 void CreateBookmarkBarView() {
58 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr)); 99 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr));
59 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get())); 100 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get()));
60 } 101 }
61 102
62 // Creates the model, blocking until it loads, then creates the 103 // Creates the model, blocking until it loads, then creates the
63 // BookmarkBarView. 104 // BookmarkBarView.
64 void CreateBookmarkModelAndBookmarkBarView() { 105 void CreateBookmarkModelAndBookmarkBarView() {
65 profile()->CreateBookmarkModel(true); 106 profile()->CreateBookmarkModel(true);
66 bookmarks::test::WaitForBookmarkModelToLoad( 107 WaitForBookmarkModelToLoad();
67 BookmarkModelFactory::GetForProfile(profile()));
68 CreateBookmarkBarView(); 108 CreateBookmarkBarView();
69 } 109 }
70 110
111 // BrowserWithTestWindowTest:
112 TestingProfile* CreateProfile() override {
113 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
114 // TemplateURLService is normally NULL during testing. Instant extended
115 // needs this service so set a custom factory function.
116 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
117 profile, &BookmarkBarViewTest::CreateTemplateURLService);
118 return profile;
119 }
120
71 scoped_ptr<BookmarkBarViewTestHelper> test_helper_; 121 scoped_ptr<BookmarkBarViewTestHelper> test_helper_;
72 scoped_ptr<BookmarkBarView> bookmark_bar_view_; 122 scoped_ptr<BookmarkBarView> bookmark_bar_view_;
73 123
74 private: 124 private:
75 static KeyedService* CreateTemplateURLService( 125 static KeyedService* CreateTemplateURLService(
76 content::BrowserContext* profile) { 126 content::BrowserContext* profile) {
77 return new TemplateURLService( 127 return new TemplateURLService(
78 static_cast<Profile*>(profile)->GetPrefs(), 128 static_cast<Profile*>(profile)->GetPrefs(),
79 make_scoped_ptr(new SearchTermsData), NULL, 129 make_scoped_ptr(new SearchTermsData), NULL,
80 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure()); 130 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure());
(...skipping 21 matching lines...) Expand all
102 } else { 152 } else {
103 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); 153 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
104 } 154 }
105 155
106 // Make sure we can also properly transition from true to false. 156 // Make sure we can also properly transition from true to false.
107 browser()->profile()->GetPrefs()->SetBoolean( 157 browser()->profile()->GetPrefs()->SetBoolean(
108 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false); 158 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
109 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); 159 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
110 } 160 }
111 161
162 // Various assertions around visibilty of the overflow_button.
163 TEST_F(BookmarkBarViewTest, OverflowVisibility) {
164 profile()->CreateBookmarkModel(true);
165 CreateBookmarkBarView();
166 EXPECT_FALSE(test_helper_->overflow_button()->visible());
167
168 WaitForBookmarkModelToLoad();
169 AddNodesToBookmarkBarFromModelString("a b c d e f ");
170 EXPECT_TRUE(test_helper_->overflow_button()->visible());
171
172 SizeUntilButtonsVisible(1);
173 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
174 const int width_for_one = bookmark_bar_view_->bounds().width();
175 EXPECT_TRUE(test_helper_->overflow_button()->visible());
176
177 // Go really big, which should force all buttons to be added.
178 bookmark_bar_view_->SetBounds(
179 0, 0, 5000, bookmark_bar_view_->bounds().height());
180 bookmark_bar_view_->Layout();
181 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
182 EXPECT_FALSE(test_helper_->overflow_button()->visible());
183
184 bookmark_bar_view_->SetBounds(
185 0, 0, width_for_one, bookmark_bar_view_->bounds().height());
186 bookmark_bar_view_->Layout();
187 EXPECT_TRUE(test_helper_->overflow_button()->visible());
188 }
189
190 // Verifies buttons get added correctly when BookmarkBarView is created after
191 // the model and the model has nodes.
192 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAddedAfterModelHasNodes) {
193 profile()->CreateBookmarkModel(true);
194 WaitForBookmarkModelToLoad();
195 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile())->loaded());
196 AddNodesToBookmarkBarFromModelString("a b c d e f ");
197 CreateBookmarkBarView();
198 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
199
200 SizeUntilButtonsVisible(1);
201 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
202
203 // Go really big, which should force all buttons to be added.
204 bookmark_bar_view_->SetBounds(
205 0, 0, 5000, bookmark_bar_view_->bounds().height());
206 bookmark_bar_view_->Layout();
207 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
208 }
209
210 // Verifies buttons are added as the model and size change.
211 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAdded) {
212 CreateBookmarkModelAndBookmarkBarView();
213 EXPECT_TRUE(BookmarkModelFactory::GetForProfile(profile())->loaded());
214 AddNodesToBookmarkBarFromModelString("a b c d e f ");
215 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
216 SizeUntilButtonsVisible(1);
217 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
218
219 // Go really big, which should force all buttons to be added.
220 bookmark_bar_view_->SetBounds(
221 0, 0, 5000, bookmark_bar_view_->bounds().height());
222 bookmark_bar_view_->Layout();
223 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount());
224 }
225
226 TEST_F(BookmarkBarViewTest, AddNodesWhenBarAlreadySized) {
227 CreateBookmarkModelAndBookmarkBarView();
228 bookmark_bar_view_->SetBounds(
229 0, 0, 5000, bookmark_bar_view_->bounds().height());
230 AddNodesToBookmarkBarFromModelString("a b c d e f ");
231 bookmark_bar_view_->Layout();
232 EXPECT_EQ("a b c d e f", GetStringForVisibleButtons());
233 }
234
235 // Various assertions for removing nodes.
236 TEST_F(BookmarkBarViewTest, RemoveNode) {
237 CreateBookmarkModelAndBookmarkBarView();
238 const BookmarkNode* bookmark_bar_node =
239 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
240 AddNodesToBookmarkBarFromModelString("a b c d e f ");
241 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
242 SizeUntilButtonsVisible(1);
243 EXPECT_EQ(2, test_helper_->GetBookmarkButtonCount());
244
245 // Remove the 2nd node, should still only have 1 visible.
246 BookmarkModelFactory::GetForProfile(profile())->Remove(bookmark_bar_node, 1);
247 EXPECT_EQ("a", GetStringForVisibleButtons());
248
249 // Remove the first node, should force a new button (for the 'c' node).
250 BookmarkModelFactory::GetForProfile(profile())->Remove(bookmark_bar_node, 0);
251 ASSERT_EQ("c", GetStringForVisibleButtons());
252 }
253
254 // Assertions for moving a node on the bookmark bar.
255 TEST_F(BookmarkBarViewTest, MoveNode) {
256 CreateBookmarkModelAndBookmarkBarView();
257 const BookmarkNode* bookmark_bar_node =
258 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node();
259 AddNodesToBookmarkBarFromModelString("a b c d e f ");
260 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
261
262 // Move 'c' first resulting in 'c a b d e f'.
263 BookmarkModelFactory::GetForProfile(profile())
264 ->Move(bookmark_bar_node->GetChild(2), bookmark_bar_node, 0);
265 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
266
267 // Make enough room for 1 node.
268 SizeUntilButtonsVisible(1);
269 EXPECT_EQ("c", GetStringForVisibleButtons());
270
271 // Move 'f' first, resulting in 'f c a b d e'.
272 BookmarkModelFactory::GetForProfile(profile())
273 ->Move(bookmark_bar_node->GetChild(5), bookmark_bar_node, 0);
274 SizeUntilButtonsVisible(2);
275 EXPECT_EQ("f c", GetStringForVisibleButtons());
276
277 // Move 'f' to the end, resulting in 'c a b d e f'.
278 BookmarkModelFactory::GetForProfile(profile())
279 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 6);
280 SizeUntilButtonsVisible(2);
281 EXPECT_EQ("c a", GetStringForVisibleButtons());
282
283 // Move 'c' after 'a', resulting in 'a c b d e f'.
284 BookmarkModelFactory::GetForProfile(profile())
285 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 2);
286 SizeUntilButtonsVisible(2);
287 EXPECT_EQ("a c", GetStringForVisibleButtons());
288 }
289
290 // Assertions for changing the title of a node.
291 TEST_F(BookmarkBarViewTest, ChangeTitle) {
292 CreateBookmarkModelAndBookmarkBarView();
293 const BookmarkNode* bookmark_bar_node = GetBookmarkBarNode();
294 AddNodesToBookmarkBarFromModelString("a b c d e f ");
295 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
296
297 BookmarkModelFactory::GetForProfile(profile())
298 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
299 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount());
300
301 // Make enough room for 1 node.
302 SizeUntilButtonsVisible(1);
303 EXPECT_EQ("a1", GetStringForVisibleButtons());
304
305 BookmarkModelFactory::GetForProfile(profile())
306 ->SetTitle(bookmark_bar_node->GetChild(1), base::ASCIIToUTF16("b1"));
307 EXPECT_EQ("a1", GetStringForVisibleButtons());
308
309 BookmarkModelFactory::GetForProfile(profile())
310 ->SetTitle(bookmark_bar_node->GetChild(5), base::ASCIIToUTF16("f1"));
311 EXPECT_EQ("a1", GetStringForVisibleButtons());
312
313 BookmarkModelFactory::GetForProfile(profile())
314 ->SetTitle(bookmark_bar_node->GetChild(3), base::ASCIIToUTF16("d1"));
315
316 // Make the second button visible, changes the title of the first to something
317 // really long and make sure the second button hides.
318 SizeUntilButtonsVisible(2);
319 EXPECT_EQ("a1 b1", GetStringForVisibleButtons());
320 BookmarkModelFactory::GetForProfile(profile())
321 ->SetTitle(bookmark_bar_node->GetChild(0),
322 base::ASCIIToUTF16("a_really_long_title"));
323 EXPECT_LE(1, test_helper_->GetBookmarkButtonCount());
msw 2014/10/27 22:50:18 nit: EXPECT_EQ("a1", GetStringForVisibleButtons())
sky 2014/10/27 23:00:49 'a1' has changed to 'a_really_long_title', which i
msw 2014/10/27 23:06:00 Acknowledged.
324
325 // Change the title back and make sure the 2nd button is visible again. Don't
326 // use GetStringForVisibleButtons() here as more buttons may have been
msw 2014/10/27 22:50:19 nit: Is this still necessary? The visibility check
sky 2014/10/27 23:00:49 Not strictly, but it seemed nice and kept the rest
msw 2014/10/27 23:06:00 Acknowledged.
327 // created.
328 BookmarkModelFactory::GetForProfile(profile())
329 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1"));
330 ASSERT_LE(2, test_helper_->GetBookmarkButtonCount());
331 EXPECT_TRUE(test_helper_->GetBookmarkButton(0)->visible());
332 EXPECT_TRUE(test_helper_->GetBookmarkButton(1)->visible());
333
334 bookmark_bar_view_->SetBounds(
335 0, 0, 5000, bookmark_bar_view_->bounds().height());
336 bookmark_bar_view_->Layout();
337 EXPECT_EQ("a1 b1 c d1 e f1", GetStringForVisibleButtons());
338 }
339
112 #if !defined(OS_CHROMEOS) 340 #if !defined(OS_CHROMEOS)
113 // Verifies that the apps shortcut is shown or hidden following the policy 341 // Verifies that the apps shortcut is shown or hidden following the policy
114 // value. This policy (and the apps shortcut) isn't present on ChromeOS. 342 // value. This policy (and the apps shortcut) isn't present on ChromeOS.
115 TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) { 343 TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) {
116 CreateBookmarkModelAndBookmarkBarView(); 344 CreateBookmarkModelAndBookmarkBarView();
117 // By default, the pref is not managed and the apps shortcut is shown. 345 // By default, the pref is not managed and the apps shortcut is shown.
118 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService(); 346 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
119 EXPECT_FALSE(prefs->IsManagedPreference( 347 EXPECT_FALSE(prefs->IsManagedPreference(
120 bookmarks::prefs::kShowAppsShortcutInBookmarkBar)); 348 bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
121 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); 349 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
122 350
123 // Hide the apps shortcut by policy, via the managed pref. 351 // Hide the apps shortcut by policy, via the managed pref.
124 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, 352 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
125 new base::FundamentalValue(false)); 353 new base::FundamentalValue(false));
126 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); 354 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible());
127 355
128 // And try showing it via policy too. 356 // And try showing it via policy too.
129 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, 357 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
130 new base::FundamentalValue(true)); 358 new base::FundamentalValue(true));
131 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); 359 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible());
132 } 360 }
133 #endif 361 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698