Chromium Code Reviews

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

Issue 2802025: Cascading menus change growth dirction (right or left) when we hit the... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_folder_controller.mm ('k') | 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 (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #import "chrome/browser/cocoa/bookmark_bar_constants.h" // namespace bookmarks 9 #import "chrome/browser/cocoa/bookmark_bar_constants.h" // namespace bookmarks
10 #import "chrome/browser/cocoa/bookmark_bar_controller.h" 10 #import "chrome/browser/cocoa/bookmark_bar_controller.h"
(...skipping 13 matching lines...)
24 @end 24 @end
25 25
26 @implementation BookmarkBarFolderController(MakeTestingEasier) 26 @implementation BookmarkBarFolderController(MakeTestingEasier)
27 - (IBAction)openBookmarkFolderFromButton:(id)sender { 27 - (IBAction)openBookmarkFolderFromButton:(id)sender {
28 [[self folderTarget] openBookmarkFolderFromButton:sender]; 28 [[self folderTarget] openBookmarkFolderFromButton:sender];
29 } 29 }
30 @end 30 @end
31 31
32 // Don't use a high window level when running unit tests -- it'll 32 // Don't use a high window level when running unit tests -- it'll
33 // interfere with anything else you are working on. 33 // interfere with anything else you are working on.
34 @interface BookmarkBarFolderControllerLow : BookmarkBarFolderController { 34 // For testing.
35 @interface BookmarkBarFolderControllerNoLevel : BookmarkBarFolderController
36 @end
37
38 @implementation BookmarkBarFolderControllerNoLevel
39 - (void)configureWindowLevel {
40 // Intentionally empty.
41 }
42 @end
43
44 // No window level and the ability to fake the "top left" point of the window.
45 // For testing.
46 @interface BookmarkBarFolderControllerLow : BookmarkBarFolderControllerNoLevel {
35 BOOL realTopLeft_; // Use the real windowTopLeft call? 47 BOOL realTopLeft_; // Use the real windowTopLeft call?
36 } 48 }
37 @property (nonatomic) BOOL realTopLeft; 49 @property (nonatomic) BOOL realTopLeft;
38 @end 50 @end
39 51
40 52
41 @implementation BookmarkBarFolderControllerLow 53 @implementation BookmarkBarFolderControllerLow
42 54
43 @synthesize realTopLeft = realTopLeft_; 55 @synthesize realTopLeft = realTopLeft_;
44 56
45 - (void)configureWindowLevel { 57 - (NSPoint)windowTopLeftForWidth:(int)width {
46 // Intentionally empty. 58 return realTopLeft_ ? [super windowTopLeftForWidth:width] :
47 } 59 NSMakePoint(200,200);
48
49 - (NSPoint)windowTopLeft {
50 return realTopLeft_ ? [super windowTopLeft] : NSMakePoint(200,200);
51 } 60 }
52 61
53 @end 62 @end
54 63
55 64
56 @interface BookmarkBarFolderControllerPong : BookmarkBarFolderControllerLow { 65 @interface BookmarkBarFolderControllerPong : BookmarkBarFolderControllerLow {
57 BOOL childFolderWillShow_; 66 BOOL childFolderWillShow_;
58 BOOL childFolderWillClose_; 67 BOOL childFolderWillClose_;
59 } 68 }
60 @property (nonatomic, readonly) BOOL childFolderWillShow; 69 @property (nonatomic, readonly) BOOL childFolderWillShow;
(...skipping 55 matching lines...)
116 model->AddURL(folderB, folderB->GetChildCount(), L"t", 125 model->AddURL(folderB, folderB->GetChildCount(), L"t",
117 GURL("http://www.google.com/c")); 126 GURL("http://www.google.com/c"));
118 127
119 parentBarController_.reset( 128 parentBarController_.reset(
120 [[BookmarkBarController alloc] 129 [[BookmarkBarController alloc]
121 initWithBrowser:helper_.browser() 130 initWithBrowser:helper_.browser()
122 initialWidth:300 131 initialWidth:300
123 delegate:nil 132 delegate:nil
124 resizeDelegate:nil]); 133 resizeDelegate:nil]);
125 [parentBarController_ loaded:model]; 134 [parentBarController_ loaded:model];
126 [[test_window() contentView] addSubview:[parentBarController_ view]]; 135 // Make parent frame for bookmark bar then open it.
136 NSRect frame = [[test_window() contentView] frame];
137 frame = NSInsetRect(frame, 100, 200);
138 NSView* fakeToolbarView = [[[NSView alloc] initWithFrame:frame]
139 autorelease];
140 [[test_window() contentView] addSubview:fakeToolbarView];
141 [fakeToolbarView addSubview:[parentBarController_ view]];
142 [parentBarController_ setBookmarkBarEnabled:YES];
127 } 143 }
128 144
129 // Remove the bookmark with the long title. 145 // Remove the bookmark with the long title.
130 void RemoveLongTitleNode() { 146 void RemoveLongTitleNode() {
131 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 147 BookmarkModel* model = helper_.profile()->GetBookmarkModel();
132 model->Remove(longTitleNode_->GetParent(), 148 model->Remove(longTitleNode_->GetParent(),
133 longTitleNode_->GetParent()->IndexOfChild(longTitleNode_)); 149 longTitleNode_->GetParent()->IndexOfChild(longTitleNode_));
134 } 150 }
135 151
136 // Add LOTS of nodes to our model if needed (e.g. scrolling). 152 // Add LOTS of nodes to our model if needed (e.g. scrolling).
(...skipping 56 matching lines...)
193 // (e.g. valgrind shouldn't complain if we do this). 209 // (e.g. valgrind shouldn't complain if we do this).
194 TEST_F(BookmarkBarFolderControllerTest, ReleaseOnClose) { 210 TEST_F(BookmarkBarFolderControllerTest, ReleaseOnClose) {
195 scoped_nsobject<BookmarkBarFolderController> bbfc; 211 scoped_nsobject<BookmarkBarFolderController> bbfc;
196 bbfc.reset(SimpleBookmarkBarFolderController()); 212 bbfc.reset(SimpleBookmarkBarFolderController());
197 EXPECT_TRUE(bbfc.get()); 213 EXPECT_TRUE(bbfc.get());
198 214
199 [bbfc retain]; // stop the scoped_nsobject from doing anything 215 [bbfc retain]; // stop the scoped_nsobject from doing anything
200 [[bbfc window] close]; // trigger an autorelease of bbfc.get() 216 [[bbfc window] close]; // trigger an autorelease of bbfc.get()
201 } 217 }
202 218
203 TEST_F(BookmarkBarFolderControllerTest, Position) { 219 TEST_F(BookmarkBarFolderControllerTest, BasicPosition) {
204 BookmarkButton* parentButton = [[parentBarController_ buttons] 220 BookmarkButton* parentButton = [[parentBarController_ buttons]
205 objectAtIndex:0]; 221 objectAtIndex:0];
206 EXPECT_TRUE(parentButton); 222 EXPECT_TRUE(parentButton);
207 223
208 // If parent is a BookmarkBarController, grow down. 224 // If parent is a BookmarkBarController, grow down.
209 scoped_nsobject<BookmarkBarFolderControllerLow> bbfc; 225 scoped_nsobject<BookmarkBarFolderControllerLow> bbfc;
210 bbfc.reset([[BookmarkBarFolderControllerLow alloc] 226 bbfc.reset([[BookmarkBarFolderControllerLow alloc]
211 initWithParentButton:parentButton 227 initWithParentButton:parentButton
212 parentController:nil 228 parentController:nil
213 barController:parentBarController_]); 229 barController:parentBarController_]);
214 [bbfc window]; 230 [bbfc window];
215 [bbfc setRealTopLeft:YES]; 231 [bbfc setRealTopLeft:YES];
216 NSPoint pt = [bbfc windowTopLeft]; // screen coords 232 NSPoint pt = [bbfc windowTopLeftForWidth:0]; // screen coords
217 NSPoint buttonOriginInScreen = 233 NSPoint buttonOriginInScreen =
218 [[parentButton window] 234 [[parentButton window]
219 convertBaseToScreen:[parentButton 235 convertBaseToScreen:[parentButton
220 convertRectToBase:[parentButton frame]].origin]; 236 convertRectToBase:[parentButton frame]].origin];
221 // Within margin 237 // Within margin
222 EXPECT_LE(abs(pt.x - buttonOriginInScreen.x), 2); 238 EXPECT_LE(abs(pt.x - buttonOriginInScreen.x), 2);
223 EXPECT_LE(abs(pt.y - buttonOriginInScreen.y), 2); 239 EXPECT_LE(abs(pt.y - buttonOriginInScreen.y), 2);
224 240
225 // If parent is a BookmarkBarFolderController, grow right. 241 // If parent is a BookmarkBarFolderController, grow right.
226 scoped_nsobject<BookmarkBarFolderControllerLow> bbfc2; 242 scoped_nsobject<BookmarkBarFolderControllerLow> bbfc2;
227 bbfc2.reset([[BookmarkBarFolderControllerLow alloc] 243 bbfc2.reset([[BookmarkBarFolderControllerLow alloc]
228 initWithParentButton:[[bbfc buttons] objectAtIndex:0] 244 initWithParentButton:[[bbfc buttons] objectAtIndex:0]
229 parentController:bbfc.get() 245 parentController:bbfc.get()
230 barController:parentBarController_]); 246 barController:parentBarController_]);
231 [bbfc2 window]; 247 [bbfc2 window];
232 [bbfc2 setRealTopLeft:YES]; 248 [bbfc2 setRealTopLeft:YES];
233 pt = [bbfc2 windowTopLeft]; 249 pt = [bbfc2 windowTopLeftForWidth:0];
234 // We're now overlapping the window a bit. 250 // We're now overlapping the window a bit.
235 EXPECT_EQ(pt.x, NSMaxX([[bbfc.get() window] frame]) - 251 EXPECT_EQ(pt.x, NSMaxX([[bbfc.get() window] frame]) -
236 bookmarks::kBookmarkMenuOverlap); 252 bookmarks::kBookmarkMenuOverlap);
237 } 253 }
238 254
255 // Confirm we grow right until end of screen, then start growing left
256 // until end of screen again, then right.
257 TEST_F(BookmarkBarFolderControllerTest, PositionRightLeftRight) {
258 BookmarkModel* model = helper_.profile()->GetBookmarkModel();
259 const BookmarkNode* parent = model->GetBookmarkBarNode();
260 const BookmarkNode* folder = parent;
261
262 const int count = 100;
263 int i;
264 // Make some super duper deeply nested folders.
265 for (i=0; i<count; i++) {
266 folder = model->AddGroup(folder, 0, L"nested folder");
267 }
268
269 // Setup initial state for opening all folders.
270 folder = parent;
271 BookmarkButton* parentButton = [[parentBarController_ buttons]
272 objectAtIndex:0];
273 BookmarkBarFolderController* parentController = nil;
274 EXPECT_TRUE(parentButton);
275
276 // Open them all.
277 scoped_nsobject<NSMutableArray> folder_controller_array;
278 folder_controller_array.reset([[NSMutableArray array] retain]);
279 for (i=0; i<count; i++) {
280 BookmarkBarFolderControllerNoLevel* bbfcl =
281 [[BookmarkBarFolderControllerNoLevel alloc]
282 initWithParentButton:parentButton
283 parentController:parentController
284 barController:parentBarController_];
285 [folder_controller_array addObject:bbfcl];
286 [bbfcl autorelease];
287 [bbfcl window];
288 parentController = bbfcl;
289 parentButton = [[bbfcl buttons] objectAtIndex:0];
290 }
291
292 // Make vector of all x positions.
293 std::vector<CGFloat> leftPositions;
294 for (i=0; i<count; i++) {
295 CGFloat x = [[[folder_controller_array objectAtIndex:i] window]
296 frame].origin.x;
297 leftPositions.push_back(x);
298 }
299
300 // Make sure the first few grow right.
301 for (i=0; i<3; i++)
302 EXPECT_TRUE(leftPositions[i+1] > leftPositions[i]);
303
304 // Look for the first "grow left".
305 while (leftPositions[i] > leftPositions[i-1])
306 i++;
307 // Confirm the next few also grow left.
308 int j;
309 for (j=i; j<i+3; j++)
310 EXPECT_TRUE(leftPositions[j+1] < leftPositions[j]);
311 i = j;
312
313 // Finally, confirm we see a "grow right" once more.
314 while (leftPositions[i] < leftPositions[i-1])
315 i++;
316 // (No need to EXPECT a final "grow right"; if we didn't find one
317 // we'd get a C++ array bounds exception).
318 }
319
239 TEST_F(BookmarkBarFolderControllerTest, DropDestination) { 320 TEST_F(BookmarkBarFolderControllerTest, DropDestination) {
240 scoped_nsobject<BookmarkBarFolderController> bbfc; 321 scoped_nsobject<BookmarkBarFolderController> bbfc;
241 bbfc.reset(SimpleBookmarkBarFolderController()); 322 bbfc.reset(SimpleBookmarkBarFolderController());
242 EXPECT_TRUE(bbfc.get()); 323 EXPECT_TRUE(bbfc.get());
243 324
244 // Confirm "off the top" and "off the bottom" match no buttons. 325 // Confirm "off the top" and "off the bottom" match no buttons.
245 NSPoint p = NSMakePoint(NSMidX([[bbfc mainView] frame]), 10000); 326 NSPoint p = NSMakePoint(NSMidX([[bbfc mainView] frame]), 10000);
246 EXPECT_FALSE([bbfc buttonForDroppingOnAtPoint:p]); 327 EXPECT_FALSE([bbfc buttonForDroppingOnAtPoint:p]);
247 EXPECT_TRUE([bbfc shouldShowIndicatorShownForPoint:p]); 328 EXPECT_TRUE([bbfc shouldShowIndicatorShownForPoint:p]);
248 p = NSMakePoint(NSMidX([[bbfc mainView] frame]), -1); 329 p = NSMakePoint(NSMidX([[bbfc mainView] frame]), -1);
(...skipping 995 matching lines...)
1244 [folder deleteBookmark:folder]; 1325 [folder deleteBookmark:folder];
1245 EXPECT_FALSE([folder folderController]); 1326 EXPECT_FALSE([folder folderController]);
1246 } 1327 }
1247 1328
1248 // TODO(jrg): draggingEntered: and draggingExited: trigger timers so 1329 // TODO(jrg): draggingEntered: and draggingExited: trigger timers so
1249 // they are hard to test. Factor out "fire timers" into routines 1330 // they are hard to test. Factor out "fire timers" into routines
1250 // which can be overridden to fire immediately to make behavior 1331 // which can be overridden to fire immediately to make behavior
1251 // confirmable. 1332 // confirmable.
1252 // There is a similar problem with mouseEnteredButton: and 1333 // There is a similar problem with mouseEnteredButton: and
1253 // mouseExitedButton:. 1334 // mouseExitedButton:.
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/bookmark_bar_folder_controller.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine