OLD | NEW |
---|---|
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 buttons on the |
46 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); | 48 // bookmark bar. Each label is separated by a space. |
47 // TemplateURLService is normally NULL during testing. Instant extended | 49 std::string GetStringForButtons() { |
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(); ++i) { |
msw
2014/10/27 20:40:25
Doesn't GetBookmarkButtonCount include invisible b
sky
2014/10/27 21:48:18
Indeed. I changed things around and made the names
| |
50 profile, &BookmarkBarViewTest::CreateTemplateURLService); | 52 if (i != 0) |
51 return profile; | 53 result += " "; |
54 result += | |
55 base::UTF16ToASCII(test_helper_->GetBookmarkButton(i)->GetText()); | |
56 } | |
57 return result; | |
52 } | 58 } |
53 | 59 |
60 // Continues sizing the bookmark bar until it has |count| buttons. | |
61 void SizeUntil(int count) { | |
62 const int start_width = bookmark_bar_view_->width(); | |
63 const int height = bookmark_bar_view_->GetPreferredSize().height(); | |
64 for (int i = 0; i < 100 && | |
msw
2014/10/27 20:40:25
nit: why not just do a while loop that increases b
sky
2014/10/27 21:48:18
If this gets to 100 something is likely very wrong
msw
2014/10/27 22:50:18
Acknowledged.
| |
65 test_helper_->GetBookmarkButtonCount() < count; ++i) { | |
66 bookmark_bar_view_->SetBounds(0, 0, start_width + i * 10, height); | |
67 bookmark_bar_view_->Layout(); | |
68 } | |
69 } | |
70 | |
71 const BookmarkNode* GetBookmarkBarNode() { | |
72 return BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node(); | |
73 } | |
74 | |
75 void WaitForBookmarkModelToLoad() { | |
76 bookmarks::test::WaitForBookmarkModelToLoad( | |
77 BookmarkModelFactory::GetForProfile(profile())); | |
78 } | |
79 | |
80 // Adds nodes to the bookmark bar node from |string|. See | |
81 // bookmarks::test::AddNodesFromModelString() for details on |string|. | |
82 void AddNodesToBookmarkBarFromModelString(const std::string& string) { | |
83 bookmarks::test::AddNodesFromModelString( | |
84 BookmarkModelFactory::GetForProfile(profile()), | |
85 GetBookmarkBarNode(), | |
86 string); | |
87 } | |
54 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll | 88 // Creates the BookmarkBarView and BookmarkBarViewTestHelper. Generally you'll |
55 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if | 89 // want to use CreateBookmarkModelAndBookmarkBarView(), but use this if |
56 // need to create the BookmarkBarView after the model has populated. | 90 // need to create the BookmarkBarView after the model has populated. |
57 void CreateBookmarkBarView() { | 91 void CreateBookmarkBarView() { |
58 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr)); | 92 bookmark_bar_view_.reset(new BookmarkBarView(browser(), nullptr)); |
59 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get())); | 93 test_helper_.reset(new BookmarkBarViewTestHelper(bookmark_bar_view_.get())); |
60 } | 94 } |
61 | 95 |
62 // Creates the model, blocking until it loads, then creates the | 96 // Creates the model, blocking until it loads, then creates the |
63 // BookmarkBarView. | 97 // BookmarkBarView. |
64 void CreateBookmarkModelAndBookmarkBarView() { | 98 void CreateBookmarkModelAndBookmarkBarView() { |
65 profile()->CreateBookmarkModel(true); | 99 profile()->CreateBookmarkModel(true); |
66 bookmarks::test::WaitForBookmarkModelToLoad( | 100 WaitForBookmarkModelToLoad(); |
67 BookmarkModelFactory::GetForProfile(profile())); | |
68 CreateBookmarkBarView(); | 101 CreateBookmarkBarView(); |
69 } | 102 } |
70 | 103 |
104 // BrowserWithTestWindowTest: | |
105 TestingProfile* CreateProfile() override { | |
106 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); | |
107 // TemplateURLService is normally NULL during testing. Instant extended | |
108 // needs this service so set a custom factory function. | |
109 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | |
110 profile, &BookmarkBarViewTest::CreateTemplateURLService); | |
111 return profile; | |
112 } | |
113 | |
71 scoped_ptr<BookmarkBarViewTestHelper> test_helper_; | 114 scoped_ptr<BookmarkBarViewTestHelper> test_helper_; |
72 scoped_ptr<BookmarkBarView> bookmark_bar_view_; | 115 scoped_ptr<BookmarkBarView> bookmark_bar_view_; |
73 | 116 |
74 private: | 117 private: |
75 static KeyedService* CreateTemplateURLService( | 118 static KeyedService* CreateTemplateURLService( |
76 content::BrowserContext* profile) { | 119 content::BrowserContext* profile) { |
77 return new TemplateURLService( | 120 return new TemplateURLService( |
78 static_cast<Profile*>(profile)->GetPrefs(), | 121 static_cast<Profile*>(profile)->GetPrefs(), |
79 make_scoped_ptr(new SearchTermsData), NULL, | 122 make_scoped_ptr(new SearchTermsData), NULL, |
80 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure()); | 123 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure()); |
(...skipping 21 matching lines...) Expand all Loading... | |
102 } else { | 145 } else { |
103 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); | 146 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); |
104 } | 147 } |
105 | 148 |
106 // Make sure we can also properly transition from true to false. | 149 // Make sure we can also properly transition from true to false. |
107 browser()->profile()->GetPrefs()->SetBoolean( | 150 browser()->profile()->GetPrefs()->SetBoolean( |
108 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false); | 151 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false); |
109 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); | 152 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); |
110 } | 153 } |
111 | 154 |
155 // Various assertions around visibilty of the overflow_button. | |
156 TEST_F(BookmarkBarViewTest, OverflowVisibility) { | |
157 profile()->CreateBookmarkModel(true); | |
158 CreateBookmarkBarView(); | |
159 EXPECT_FALSE(test_helper_->overflow_button()->visible()); | |
160 | |
161 WaitForBookmarkModelToLoad(); | |
162 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
163 EXPECT_TRUE(test_helper_->overflow_button()->visible()); | |
164 | |
165 SizeUntil(1); | |
166 EXPECT_EQ(1, test_helper_->GetBookmarkButtonCount()); | |
167 EXPECT_TRUE(test_helper_->overflow_button()->visible()); | |
168 | |
169 // Go really big, which should force all buttons to be added. | |
170 bookmark_bar_view_->SetBounds( | |
171 0, 0, 5000, bookmark_bar_view_->bounds().height()); | |
172 bookmark_bar_view_->Layout(); | |
173 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount()); | |
174 EXPECT_FALSE(test_helper_->overflow_button()->visible()); | |
msw
2014/10/27 20:40:25
nit: test shrinking the bookmark bar view here.
sky
2014/10/27 21:48:18
Done.
| |
175 } | |
176 | |
177 // Verifies buttons get added correctly when BookmarkBarView is created after | |
178 // the model and the model has nodes. | |
179 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAddedAfterModelHasNodes) { | |
180 profile()->CreateBookmarkModel(true); | |
181 WaitForBookmarkModelToLoad(); | |
182 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
183 CreateBookmarkBarView(); | |
184 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
185 | |
186 SizeUntil(1); | |
187 EXPECT_EQ(1, test_helper_->GetBookmarkButtonCount()); | |
188 | |
189 // Go really big, which should force all buttons to be added. | |
190 bookmark_bar_view_->SetBounds( | |
191 0, 0, 5000, bookmark_bar_view_->bounds().height()); | |
192 bookmark_bar_view_->Layout(); | |
193 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount()); | |
194 } | |
195 | |
196 // Verifies buttons are added as the model and size change. | |
msw
2014/10/27 20:40:25
So this test is just like above, but doesn't call
sky
2014/10/27 21:48:18
Done.
| |
197 TEST_F(BookmarkBarViewTest, ButtonsDynamicallyAdded) { | |
198 CreateBookmarkModelAndBookmarkBarView(); | |
199 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
200 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
201 SizeUntil(1); | |
202 EXPECT_EQ(1, test_helper_->GetBookmarkButtonCount()); | |
203 | |
204 // Go really big, which should force all buttons to be added. | |
205 bookmark_bar_view_->SetBounds( | |
206 0, 0, 5000, bookmark_bar_view_->bounds().height()); | |
207 bookmark_bar_view_->Layout(); | |
208 EXPECT_EQ(6, test_helper_->GetBookmarkButtonCount()); | |
209 } | |
210 | |
211 TEST_F(BookmarkBarViewTest, AddNodesWhenBarAlreadySized) { | |
212 CreateBookmarkModelAndBookmarkBarView(); | |
213 bookmark_bar_view_->SetBounds( | |
214 0, 0, 5000, bookmark_bar_view_->bounds().height()); | |
215 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
216 bookmark_bar_view_->Layout(); | |
217 EXPECT_EQ("a b c d e f", GetStringForButtons()); | |
218 } | |
219 | |
220 // Various assertions for removing nodes. | |
221 TEST_F(BookmarkBarViewTest, RemoveNode) { | |
222 CreateBookmarkModelAndBookmarkBarView(); | |
223 const BookmarkNode* bookmark_bar_node = | |
224 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node(); | |
225 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
226 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
msw
2014/10/27 20:40:25
nit: is it worthwhile for multiple tests to expect
sky
2014/10/27 21:48:18
I view it like a DCHECK. If this fails then the re
msw
2014/10/27 22:50:18
Acknowledged.
| |
227 SizeUntil(1); | |
228 EXPECT_EQ(1, test_helper_->GetBookmarkButtonCount()); | |
229 | |
230 // Remove the 2nd node, should still only have 1 visible. | |
231 BookmarkModelFactory::GetForProfile(profile())->Remove(bookmark_bar_node, 1); | |
232 EXPECT_EQ(1, test_helper_->GetBookmarkButtonCount()); | |
msw
2014/10/27 20:40:25
nit: EXPECT_EQ("a", GetStringForButtons()); instea
sky
2014/10/27 21:48:18
Done.
| |
233 | |
234 // Remove the first node, should force a new button (for the 'c' node). | |
235 BookmarkModelFactory::GetForProfile(profile())->Remove(bookmark_bar_node, 0); | |
236 ASSERT_EQ("c", GetStringForButtons()); | |
237 } | |
238 | |
239 // Assertions for moving a node on the bookmark bar. | |
240 TEST_F(BookmarkBarViewTest, MoveNode) { | |
241 CreateBookmarkModelAndBookmarkBarView(); | |
242 const BookmarkNode* bookmark_bar_node = | |
243 BookmarkModelFactory::GetForProfile(profile())->bookmark_bar_node(); | |
244 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
245 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
246 | |
247 // Move 'c' first resulting in 'c a b d e f'. | |
248 BookmarkModelFactory::GetForProfile(profile()) | |
249 ->Move(bookmark_bar_node->GetChild(2), bookmark_bar_node, 0); | |
250 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
251 | |
252 // Make enough room for 1 node. | |
253 SizeUntil(1); | |
254 EXPECT_EQ("c", GetStringForButtons()); | |
255 | |
256 // Move 'f' first, resulting in 'f c a b d e'. | |
257 BookmarkModelFactory::GetForProfile(profile()) | |
258 ->Move(bookmark_bar_node->GetChild(5), bookmark_bar_node, 0); | |
259 // Even though only one node fits, the second node isn't deleted. | |
260 SizeUntil(2); | |
261 EXPECT_EQ("f c", GetStringForButtons()); | |
262 | |
263 // Move 'f' to the end, resulting in 'c a b d e f'. | |
264 BookmarkModelFactory::GetForProfile(profile()) | |
265 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 6); | |
266 // Even though only one node fits, the second node isn't deleted. | |
msw
2014/10/27 20:40:25
nit: revise this comment to match the check.
sky
2014/10/27 21:48:18
Done.
| |
267 EXPECT_EQ("c", GetStringForButtons()); | |
268 | |
269 // Move 'c' after 'a', resulting in 'a c b d e f'. | |
270 BookmarkModelFactory::GetForProfile(profile()) | |
271 ->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 2); | |
272 // Even though only one node fits, the second node isn't deleted. | |
msw
2014/10/27 20:40:25
nit: revise this comment to match the check.
sky
2014/10/27 21:48:18
Done.
| |
273 EXPECT_EQ("a", GetStringForButtons()); | |
274 } | |
275 | |
276 // Assertions for changing the title of a node. | |
msw
2014/10/27 20:40:25
Should we test that increasing the name length cou
sky
2014/10/27 21:48:18
Done.
| |
277 TEST_F(BookmarkBarViewTest, ChangeTitle) { | |
278 CreateBookmarkModelAndBookmarkBarView(); | |
279 const BookmarkNode* bookmark_bar_node = GetBookmarkBarNode(); | |
280 AddNodesToBookmarkBarFromModelString("a b c d e f "); | |
281 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
282 | |
283 BookmarkModelFactory::GetForProfile(profile()) | |
284 ->SetTitle(bookmark_bar_node->GetChild(0), base::ASCIIToUTF16("a1")); | |
285 EXPECT_EQ(0, test_helper_->GetBookmarkButtonCount()); | |
286 | |
287 // Make enough room for 1 node. | |
288 SizeUntil(1); | |
289 EXPECT_EQ("a1", GetStringForButtons()); | |
290 | |
291 BookmarkModelFactory::GetForProfile(profile()) | |
292 ->SetTitle(bookmark_bar_node->GetChild(1), base::ASCIIToUTF16("b1")); | |
293 EXPECT_EQ("a1", GetStringForButtons()); | |
294 | |
295 BookmarkModelFactory::GetForProfile(profile()) | |
296 ->SetTitle(bookmark_bar_node->GetChild(5), base::ASCIIToUTF16("f1")); | |
297 EXPECT_EQ("a1", GetStringForButtons()); | |
298 | |
299 BookmarkModelFactory::GetForProfile(profile()) | |
300 ->SetTitle(bookmark_bar_node->GetChild(3), base::ASCIIToUTF16("d1")); | |
301 | |
302 bookmark_bar_view_->SetBounds( | |
303 0, 0, 5000, bookmark_bar_view_->bounds().height()); | |
304 bookmark_bar_view_->Layout(); | |
305 EXPECT_EQ("a1 b1 c d1 e f1", GetStringForButtons()); | |
306 } | |
307 | |
112 #if !defined(OS_CHROMEOS) | 308 #if !defined(OS_CHROMEOS) |
113 // Verifies that the apps shortcut is shown or hidden following the policy | 309 // 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. | 310 // value. This policy (and the apps shortcut) isn't present on ChromeOS. |
115 TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) { | 311 TEST_F(BookmarkBarViewTest, ManagedShowAppsShortcutInBookmarksBar) { |
116 CreateBookmarkModelAndBookmarkBarView(); | 312 CreateBookmarkModelAndBookmarkBarView(); |
117 // By default, the pref is not managed and the apps shortcut is shown. | 313 // By default, the pref is not managed and the apps shortcut is shown. |
118 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService(); | 314 TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService(); |
119 EXPECT_FALSE(prefs->IsManagedPreference( | 315 EXPECT_FALSE(prefs->IsManagedPreference( |
120 bookmarks::prefs::kShowAppsShortcutInBookmarkBar)); | 316 bookmarks::prefs::kShowAppsShortcutInBookmarkBar)); |
121 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); | 317 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); |
122 | 318 |
123 // Hide the apps shortcut by policy, via the managed pref. | 319 // Hide the apps shortcut by policy, via the managed pref. |
124 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, | 320 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, |
125 new base::FundamentalValue(false)); | 321 new base::FundamentalValue(false)); |
126 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); | 322 EXPECT_FALSE(test_helper_->apps_page_shortcut()->visible()); |
127 | 323 |
128 // And try showing it via policy too. | 324 // And try showing it via policy too. |
129 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, | 325 prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar, |
130 new base::FundamentalValue(true)); | 326 new base::FundamentalValue(true)); |
131 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); | 327 EXPECT_TRUE(test_helper_->apps_page_shortcut()->visible()); |
132 } | 328 } |
133 #endif | 329 #endif |
OLD | NEW |