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

Side by Side Diff: chrome/browser/cocoa/bookmark_bar_controller_unittest.mm

Issue 384105: Mac: Animate the bookmark bar showing/hiding. (Closed)
Patch Set: Comments added per rohitrao's review. Created 11 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 (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/scoped_nsobject.h" 8 #include "base/scoped_nsobject.h"
9 #include "base/sys_string_conversions.h" 9 #include "base/sys_string_conversions.h"
10 #import "chrome/browser/cocoa/bookmark_bar_constants.h" 10 #import "chrome/browser/cocoa/bookmark_bar_constants.h"
11 #import "chrome/browser/cocoa/bookmark_bar_controller.h" 11 #import "chrome/browser/cocoa/bookmark_bar_controller.h"
12 #import "chrome/browser/cocoa/bookmark_bar_view.h" 12 #import "chrome/browser/cocoa/bookmark_bar_view.h"
13 #include "chrome/browser/cocoa/browser_test_helper.h" 13 #include "chrome/browser/cocoa/browser_test_helper.h"
14 #import "chrome/browser/cocoa/cocoa_test_helper.h" 14 #import "chrome/browser/cocoa/cocoa_test_helper.h"
15 #import "chrome/browser/cocoa/toolbar_compressable.h"
16 #include "chrome/browser/cocoa/test_event_utils.h" 15 #include "chrome/browser/cocoa/test_event_utils.h"
17 #import "chrome/browser/cocoa/view_resizer_pong.h" 16 #import "chrome/browser/cocoa/view_resizer_pong.h"
18 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h" 19 #include "testing/platform_test.h"
21 20
22 // Just like a BookmarkBarController but openURL: is stubbed out. 21 // Just like a BookmarkBarController but openURL: is stubbed out.
23 @interface BookmarkBarControllerNoOpen : BookmarkBarController { 22 @interface BookmarkBarControllerNoOpen : BookmarkBarController {
24 @public 23 @public
25 std::vector<GURL> urls_; 24 std::vector<GURL> urls_;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 @implementation BookmarkBarControllerTogglePong 71 @implementation BookmarkBarControllerTogglePong
73 72
74 @synthesize toggles = toggles_; 73 @synthesize toggles = toggles_;
75 74
76 - (void)frameDidChange { 75 - (void)frameDidChange {
77 toggles_++; 76 toggles_++;
78 } 77 }
79 78
80 @end 79 @end
81 80
82 // A BookmarkBarController that always beleives that it's on the new tab page.
83 @interface AlwaysNewTabPageBookmarkBarController : BookmarkBarControllerNoOpen {
84 }
85 @end
86
87 @implementation AlwaysNewTabPageBookmarkBarController
88 -(BOOL)isNewTabPage {
89 return YES;
90 }
91 @end
92
93 @interface CompressablePong : NSObject<ToolbarCompressable> {
94 @private
95 BOOL compressed_;
96 }
97 @property (readonly) BOOL compressed;
98 @end
99
100 @implementation CompressablePong
101
102 @synthesize compressed = compressed_;
103
104 - (void)setShouldBeCompressed:(BOOL)compressed {
105 compressed_ = compressed;
106 }
107
108 @end
109
110 namespace { 81 namespace {
111 82
112 static const int kContentAreaHeight = 500; 83 static const int kContentAreaHeight = 500;
113 static const int kInfoBarViewHeight = 30; 84 static const int kInfoBarViewHeight = 30;
114 85
115 class BookmarkBarControllerTest : public PlatformTest { 86 class BookmarkBarControllerTest : public PlatformTest {
116 public: 87 public:
117 BookmarkBarControllerTest() { 88 BookmarkBarControllerTest() {
118 resizeDelegate_.reset([[ViewResizerPong alloc] init]); 89 resizeDelegate_.reset([[ViewResizerPong alloc] init]);
119 compressDelegate_.reset([[CompressablePong alloc] init]);
120 NSRect parent_frame = NSMakeRect(0, 0, 800, 50); 90 NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
121 parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]); 91 parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
122 [parent_view_ setHidden:YES]; 92 [parent_view_ setHidden:YES];
123 bar_.reset( 93 bar_.reset(
124 [[BookmarkBarControllerNoOpen alloc] 94 [[BookmarkBarControllerNoOpen alloc]
125 initWithBrowser:helper_.browser() 95 initWithBrowser:helper_.browser()
126 initialWidth:NSWidth(parent_frame) 96 initialWidth:NSWidth(parent_frame)
127 compressDelegate:compressDelegate_.get() 97 delegate:nil
128 resizeDelegate:resizeDelegate_.get()]); 98 resizeDelegate:resizeDelegate_.get()]);
129 99
130 InstallAndToggleBar(bar_.get()); 100 InstallAndToggleBar(bar_.get());
131 101
132 // Create a menu/item to act like a sender 102 // Create a menu/item to act like a sender
133 menu_.reset([[NSMenu alloc] initWithTitle:@"I_dont_care"]); 103 menu_.reset([[NSMenu alloc] initWithTitle:@"I_dont_care"]);
134 menu_item_.reset([[NSMenuItem alloc] 104 menu_item_.reset([[NSMenuItem alloc]
135 initWithTitle:@"still_dont_care" 105 initWithTitle:@"still_dont_care"
136 action:NULL 106 action:NULL
137 keyEquivalent:@""]); 107 keyEquivalent:@""]);
138 cell_.reset([[NSButtonCell alloc] init]); 108 cell_.reset([[NSButtonCell alloc] init]);
139 [menu_item_ setMenu:menu_.get()]; 109 [menu_item_ setMenu:menu_.get()];
140 [menu_ setDelegate:cell_.get()]; 110 [menu_ setDelegate:cell_.get()];
141 } 111 }
142 112
143 void InstallAndToggleBar(BookmarkBarController* bar) { 113 void InstallAndToggleBar(BookmarkBarController* bar) {
144 // Force loading of the nib. 114 // Force loading of the nib.
145 [bar view]; 115 [bar view];
146 // Awkwardness to look like we've been installed. 116 // Awkwardness to look like we've been installed.
147 [parent_view_ addSubview:[bar view]]; 117 [parent_view_ addSubview:[bar view]];
148 NSRect frame = [[[bar view] superview] frame]; 118 NSRect frame = [[[bar view] superview] frame];
149 frame.origin.y = 100; 119 frame.origin.y = 100;
150 [[[bar view] superview] setFrame:frame]; 120 [[[bar view] superview] setFrame:frame];
151 121
152 // make sure it's open so certain things aren't no-ops 122 // Make sure it's open so certain things aren't no-ops.
153 helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); 123 [bar updateAndShowNormalBar:YES
124 showDetachedBar:NO
125 withAnimation:NO];
126 }
127
128 // Update the state of the bookmark bar.
129 void UpdateBookmarkBar() {
130 [bar_ updateAndShowNormalBar:[bar_ isShownAsToolbar]
131 showDetachedBar:[bar_ isShownAsDetachedBar]
132 withAnimation:NO];
154 } 133 }
155 134
156 // Return a menu item that points to the right URL. 135 // Return a menu item that points to the right URL.
157 NSMenuItem* ItemForBookmarkBarMenu(GURL& gurl) { 136 NSMenuItem* ItemForBookmarkBarMenu(GURL& gurl) {
158 node_.reset(new BookmarkNode(gurl)); 137 node_.reset(new BookmarkNode(gurl));
159 [cell_ setRepresentedObject:[NSValue valueWithPointer:node_.get()]]; 138 [cell_ setRepresentedObject:[NSValue valueWithPointer:node_.get()]];
160 return menu_item_; 139 return menu_item_;
161 } 140 }
162 141
163 // Does NOT take ownership of node. 142 // Does NOT take ownership of node.
164 NSMenuItem* ItemForBookmarkBarMenu(const BookmarkNode* node) { 143 NSMenuItem* ItemForBookmarkBarMenu(const BookmarkNode* node) {
165 [cell_ setRepresentedObject:[NSValue valueWithPointer:node]]; 144 [cell_ setRepresentedObject:[NSValue valueWithPointer:node]];
166 return menu_item_; 145 return menu_item_;
167 } 146 }
168 147
169
170 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... 148 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc...
171 scoped_nsobject<NSView> parent_view_; 149 scoped_nsobject<NSView> parent_view_;
172 BrowserTestHelper helper_; 150 BrowserTestHelper helper_;
173 scoped_nsobject<ViewResizerPong> resizeDelegate_; 151 scoped_nsobject<ViewResizerPong> resizeDelegate_;
174 scoped_nsobject<CompressablePong> compressDelegate_;
175 scoped_nsobject<BookmarkBarControllerNoOpen> bar_; 152 scoped_nsobject<BookmarkBarControllerNoOpen> bar_;
176 scoped_nsobject<NSMenu> menu_; 153 scoped_nsobject<NSMenu> menu_;
177 scoped_nsobject<NSMenuItem> menu_item_; 154 scoped_nsobject<NSMenuItem> menu_item_;
178 scoped_nsobject<NSButtonCell> cell_; 155 scoped_nsobject<NSButtonCell> cell_;
179 scoped_ptr<BookmarkNode> node_; 156 scoped_ptr<BookmarkNode> node_;
180 }; 157 };
181 158
182 TEST_F(BookmarkBarControllerTest, ShowWhenShowBookmarkBarTrue) { 159 TEST_F(BookmarkBarControllerTest, ShowWhenShowBookmarkBarTrue) {
183 helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); 160 [bar_ updateAndShowNormalBar:YES
184 [bar_ updateVisibility]; 161 showDetachedBar:NO
185 162 withAnimation:NO];
163 EXPECT_TRUE([bar_ isShownAsToolbar]);
164 EXPECT_FALSE([bar_ isShownAsDetachedBar]);
186 EXPECT_TRUE([bar_ isVisible]); 165 EXPECT_TRUE([bar_ isVisible]);
187 EXPECT_FALSE([[bar_ view] isHidden]); 166 EXPECT_FALSE([[bar_ view] isHidden]);
188 EXPECT_GT([resizeDelegate_ height], 0); 167 EXPECT_GT([resizeDelegate_ height], 0);
189 EXPECT_GT([[bar_ view] frame].size.height, 0); 168 EXPECT_GT([[bar_ view] frame].size.height, 0);
190 } 169 }
191 170
192 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarFalse) { 171 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarFalse) {
193 helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); 172 [bar_ updateAndShowNormalBar:NO
194 [bar_ updateVisibility]; 173 showDetachedBar:NO
195 174 withAnimation:NO];
175 EXPECT_FALSE([bar_ isShownAsToolbar]);
176 EXPECT_FALSE([bar_ isShownAsDetachedBar]);
196 EXPECT_FALSE([bar_ isVisible]); 177 EXPECT_FALSE([bar_ isVisible]);
197 EXPECT_TRUE([[bar_ view] isHidden]); 178 EXPECT_TRUE([[bar_ view] isHidden]);
198 EXPECT_EQ(0, [resizeDelegate_ height]); 179 EXPECT_EQ(0, [resizeDelegate_ height]);
199 EXPECT_EQ(0, [[bar_ view] frame].size.height); 180 EXPECT_EQ(0, [[bar_ view] frame].size.height);
200 } 181 }
201 182
202 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarTrueButDisabled) { 183 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarTrueButDisabled) {
203 helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
204 [bar_ setBookmarkBarEnabled:NO]; 184 [bar_ setBookmarkBarEnabled:NO];
205 [bar_ updateVisibility]; 185 [bar_ updateAndShowNormalBar:YES
206 186 showDetachedBar:NO
187 withAnimation:NO];
188 EXPECT_TRUE([bar_ isShownAsToolbar]);
189 EXPECT_FALSE([bar_ isShownAsDetachedBar]);
207 EXPECT_FALSE([bar_ isVisible]); 190 EXPECT_FALSE([bar_ isVisible]);
208 EXPECT_TRUE([[bar_ view] isHidden]); 191 EXPECT_TRUE([[bar_ view] isHidden]);
209 EXPECT_EQ(0, [resizeDelegate_ height]); 192 EXPECT_EQ(0, [resizeDelegate_ height]);
210 EXPECT_EQ(0, [[bar_ view] frame].size.height); 193 EXPECT_EQ(0, [[bar_ view] frame].size.height);
211 } 194 }
212 195
213 TEST_F(BookmarkBarControllerTest, ShowOnNewTabPage) { 196 TEST_F(BookmarkBarControllerTest, ShowOnNewTabPage) {
214 helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); 197 [bar_ updateAndShowNormalBar:NO
215 198 showDetachedBar:YES
216 scoped_nsobject<BookmarkBarControllerTogglePong> bar; 199 withAnimation:NO];
217 bar.reset( 200 EXPECT_FALSE([bar_ isShownAsToolbar]);
218 [[AlwaysNewTabPageBookmarkBarController alloc] 201 EXPECT_TRUE([bar_ isShownAsDetachedBar]);
219 initWithBrowser:helper_.browser() 202 EXPECT_TRUE([bar_ isVisible]);
220 initialWidth:800 // arbitrary 203 EXPECT_FALSE([[bar_ view] isHidden]);
221 compressDelegate:compressDelegate_.get() 204 ;EXPECT_GT([resizeDelegate_ height], 0);
222 resizeDelegate:resizeDelegate_.get()]); 205 EXPECT_GT([[bar_ view] frame].size.height, 0);
223 InstallAndToggleBar(bar.get());
224
225 [bar setBookmarkBarEnabled:NO];
226 [bar updateVisibility];
227
228 EXPECT_TRUE([bar isVisible]);
229 EXPECT_FALSE([[bar view] isHidden]);
230 EXPECT_GT([resizeDelegate_ height], 0);
231 EXPECT_GT([[bar view] frame].size.height, 0);
232 206
233 // Make sure no buttons fall off the bar, either now or when resized 207 // Make sure no buttons fall off the bar, either now or when resized
234 // bigger or smaller. 208 // bigger or smaller.
235 CGFloat sizes[] = { 300.0, -100.0, 200.0, -420.0 }; 209 CGFloat sizes[] = { 300.0, -100.0, 200.0, -420.0 };
236 CGFloat previousX = 0.0; 210 CGFloat previousX = 0.0;
237 for (unsigned x = 0; x < arraysize(sizes); x++) { 211 for (unsigned x = 0; x < arraysize(sizes); x++) {
238 // Confirm the buttons moved from the last check (which may be 212 // Confirm the buttons moved from the last check (which may be
239 // init but that's fine). 213 // init but that's fine).
240 CGFloat newX = [[bar offTheSideButton] frame].origin.x; 214 CGFloat newX = [[bar_ offTheSideButton] frame].origin.x;
241 EXPECT_NE(previousX, newX); 215 EXPECT_NE(previousX, newX);
242 previousX = newX; 216 previousX = newX;
243 217
244 // Confirm the buttons have a reasonable bounds. 218 // Confirm the buttons have a reasonable bounds. Recall that |-frame|
245 EXPECT_TRUE(NSContainsRect([[bar buttonView] frame], 219 // returns rectangles in the superview's coordinates.
246 [[bar offTheSideButton] frame])); 220 NSRect buttonViewFrame =
247 EXPECT_TRUE(NSContainsRect([[bar buttonView] frame], 221 [[bar_ buttonView] convertRect:[[bar_ buttonView] frame]
248 [[bar otherBookmarksButton] frame])); 222 fromView:[[bar_ buttonView] superview]];
223 EXPECT_EQ([bar_ buttonView], [[bar_ offTheSideButton] superview]);
224 EXPECT_TRUE(NSContainsRect(buttonViewFrame,
225 [[bar_ offTheSideButton] frame]));
226 EXPECT_EQ([bar_ buttonView], [[bar_ otherBookmarksButton] superview]);
227 EXPECT_TRUE(NSContainsRect(buttonViewFrame,
228 [[bar_ otherBookmarksButton] frame]));
249 229
250 // Now move them implicitly. 230 // Now move them implicitly.
251 // We confirm FrameChangeNotification works in the next unit test; 231 // We confirm FrameChangeNotification works in the next unit test;
252 // we simply assume it works here to resize or reposition the 232 // we simply assume it works here to resize or reposition the
253 // buttons above. 233 // buttons above.
254 NSRect frame = [[bar view] frame]; 234 NSRect frame = [[bar_ view] frame];
255 frame.size.width += sizes[x]; 235 frame.size.width += sizes[x];
256 [[bar view] setFrame:frame]; 236 [[bar_ view] setFrame:frame];
257 } 237 }
258 } 238 }
259 239
260 // Make sure we're watching for frame change notifications. 240 // Make sure we're watching for frame change notifications.
261 TEST_F(BookmarkBarControllerTest, FrameChangeNotification) { 241 TEST_F(BookmarkBarControllerTest, FrameChangeNotification) {
262 scoped_nsobject<BookmarkBarControllerTogglePong> bar; 242 scoped_nsobject<BookmarkBarControllerTogglePong> bar;
263 bar.reset( 243 bar.reset(
264 [[BookmarkBarControllerTogglePong alloc] 244 [[BookmarkBarControllerTogglePong alloc]
265 initWithBrowser:helper_.browser() 245 initWithBrowser:helper_.browser()
266 initialWidth:100 // arbitrary 246 initialWidth:100 // arbitrary
267 compressDelegate:compressDelegate_.get() 247 delegate:nil
268 resizeDelegate:resizeDelegate_.get()]); 248 resizeDelegate:resizeDelegate_.get()]);
269 InstallAndToggleBar(bar.get()); 249 InstallAndToggleBar(bar.get());
270 250
271 // Send a frame did change notification for the pong's view. 251 // Send a frame did change notification for the pong's view.
272 [[NSNotificationCenter defaultCenter] 252 [[NSNotificationCenter defaultCenter]
273 postNotificationName:NSViewFrameDidChangeNotification 253 postNotificationName:NSViewFrameDidChangeNotification
274 object:[bar view]]; 254 object:[bar view]];
275 255
276 EXPECT_GT([bar toggles], 0); 256 EXPECT_GT([bar toggles], 0);
277 } 257 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 NSView* buttonView = [bar_ buttonView]; 388 NSView* buttonView = [bar_ buttonView];
409 EXPECT_EQ(0U, [[bar_ buttons] count]); 389 EXPECT_EQ(0U, [[bar_ buttons] count]);
410 unsigned int initial_subview_count = [[buttonView subviews] count]; 390 unsigned int initial_subview_count = [[buttonView subviews] count];
411 391
412 // Make sure a redundant call doesn't choke 392 // Make sure a redundant call doesn't choke
413 [bar_ clearBookmarkBar]; 393 [bar_ clearBookmarkBar];
414 EXPECT_EQ(0U, [[bar_ buttons] count]); 394 EXPECT_EQ(0U, [[bar_ buttons] count]);
415 EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]); 395 EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]);
416 396
417 GURL gurl1("http://superfriends.hall-of-justice.edu"); 397 GURL gurl1("http://superfriends.hall-of-justice.edu");
418 std::wstring title1(L"Protectors of the Universe"); 398 // Short titles increase the chances of this test succeeding if the view is
399 // narrow.
400 // TODO(viettrungluu): make the test independent of window/view size, font
401 // metrics, button size and spacing, and everything else.
402 std::wstring title1(L"x");
419 model->SetURLStarred(gurl1, title1, true); 403 model->SetURLStarred(gurl1, title1, true);
420 EXPECT_EQ(1U, [[bar_ buttons] count]); 404 EXPECT_EQ(1U, [[bar_ buttons] count]);
421 EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]); 405 EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
422 406
423 GURL gurl2("http://legion-of-doom.gov"); 407 GURL gurl2("http://legion-of-doom.gov");
424 std::wstring title2(L"Bad doodz"); 408 std::wstring title2(L"y");
425 model->SetURLStarred(gurl2, title2, true); 409 model->SetURLStarred(gurl2, title2, true);
426 EXPECT_EQ(2U, [[bar_ buttons] count]); 410 EXPECT_EQ(2U, [[bar_ buttons] count]);
427 EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]); 411 EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
428 412
429 for (int i = 0; i < 3; i++) { 413 for (int i = 0; i < 3; i++) {
430 // is_starred=false --> remove the bookmark 414 // is_starred=false --> remove the bookmark
431 model->SetURLStarred(gurl2, title2, false); 415 model->SetURLStarred(gurl2, title2, false);
432 EXPECT_EQ(1U, [[bar_ buttons] count]); 416 EXPECT_EQ(1U, [[bar_ buttons] count]);
433 EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]); 417 EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
434 418
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 const BookmarkNode* parent = model->GetBookmarkBarNode(); 685 const BookmarkNode* parent = model->GetBookmarkBarNode();
702 [bar_ addURLs:nsurls withTitles:nstitles at:NSZeroPoint]; 686 [bar_ addURLs:nsurls withTitles:nstitles at:NSZeroPoint];
703 EXPECT_EQ(3, parent->GetChildCount()); 687 EXPECT_EQ(3, parent->GetChildCount());
704 for (int i = 0; i < parent->GetChildCount(); ++i) { 688 for (int i = 0; i < parent->GetChildCount(); ++i) {
705 EXPECT_EQ(parent->GetChild(i)->GetURL(), GURL(urls[i])); 689 EXPECT_EQ(parent->GetChild(i)->GetURL(), GURL(urls[i]));
706 EXPECT_EQ(parent->GetChild(i)->GetTitle(), titles[i]); 690 EXPECT_EQ(parent->GetChild(i)->GetTitle(), titles[i]);
707 } 691 }
708 } 692 }
709 693
710 } // namespace 694 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_controller.mm ('k') | chrome/browser/cocoa/bookmark_bar_toolbar_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698