| OLD | NEW |
| 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 #import "chrome/browser/cocoa/bookmark_bar_controller.h" | 9 #import "chrome/browser/cocoa/bookmark_bar_controller.h" |
| 10 #include "chrome/browser/cocoa/browser_test_helper.h" | 10 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #import "chrome/browser/cocoa/view_resizer_pong.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 // Pretend BookmarkURLOpener delegate to keep track of requests | 15 // Pretend BookmarkURLOpener delegate to keep track of requests |
| 15 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { | 16 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { |
| 16 @public | 17 @public |
| 17 std::vector<GURL> urls_; | 18 std::vector<GURL> urls_; |
| 18 std::vector<WindowOpenDisposition> dispositions_; | 19 std::vector<WindowOpenDisposition> dispositions_; |
| 19 } | 20 } |
| 20 @end | 21 @end |
| 21 | 22 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 75 |
| 75 | 76 |
| 76 namespace { | 77 namespace { |
| 77 | 78 |
| 78 static const int kContentAreaHeight = 500; | 79 static const int kContentAreaHeight = 500; |
| 79 static const int kInfoBarViewHeight = 30; | 80 static const int kInfoBarViewHeight = 30; |
| 80 | 81 |
| 81 class BookmarkBarControllerTest : public testing::Test { | 82 class BookmarkBarControllerTest : public testing::Test { |
| 82 public: | 83 public: |
| 83 BookmarkBarControllerTest() { | 84 BookmarkBarControllerTest() { |
| 84 NSRect content_frame = NSMakeRect(0, 0, 800, kContentAreaHeight); | 85 resizeDelegate_.reset([[ViewResizerPong alloc] init]); |
| 85 // |infobar_frame| is set to be directly above |content_frame|. | |
| 86 NSRect infobar_frame = NSMakeRect(0, kContentAreaHeight, | |
| 87 800, kInfoBarViewHeight); | |
| 88 NSRect parent_frame = NSMakeRect(0, 0, 800, 50); | 86 NSRect parent_frame = NSMakeRect(0, 0, 800, 50); |
| 89 content_area_.reset([[NSView alloc] initWithFrame:content_frame]); | |
| 90 infobar_view_.reset([[NSView alloc] initWithFrame:infobar_frame]); | |
| 91 parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]); | 87 parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]); |
| 92 [parent_view_ setHidden:YES]; | 88 [parent_view_ setHidden:YES]; |
| 93 bar_.reset( | 89 bar_.reset( |
| 94 [[BookmarkBarController alloc] initWithProfile:helper_.profile() | 90 [[BookmarkBarController alloc] initWithProfile:helper_.profile() |
| 95 parentView:parent_view_.get() | 91 initialWidth:NSWidth(parent_frame) |
| 96 webContentView:content_area_.get() | 92 resizeDelegate:resizeDelegate_.get() |
| 97 infoBarsView:infobar_view_.get() | 93 urlDelegate:nil]); |
| 98 delegate:nil]); | |
| 99 | 94 |
| 100 InstallAndToggleBar(bar_.get()); | 95 InstallAndToggleBar(bar_.get()); |
| 101 | 96 |
| 102 // Create a menu/item to act like a sender | 97 // Create a menu/item to act like a sender |
| 103 menu_.reset([[NSMenu alloc] initWithTitle:@"I_dont_care"]); | 98 menu_.reset([[NSMenu alloc] initWithTitle:@"I_dont_care"]); |
| 104 menu_item_.reset([[NSMenuItem alloc] | 99 menu_item_.reset([[NSMenuItem alloc] |
| 105 initWithTitle:@"still_dont_care" | 100 initWithTitle:@"still_dont_care" |
| 106 action:NULL | 101 action:NULL |
| 107 keyEquivalent:@""]); | 102 keyEquivalent:@""]); |
| 108 cell_.reset([[NSButtonCell alloc] init]); | 103 cell_.reset([[NSButtonCell alloc] init]); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 132 } | 127 } |
| 133 | 128 |
| 134 // Does NOT take ownership of node. | 129 // Does NOT take ownership of node. |
| 135 NSMenuItem* ItemForBookmarkBarMenu(const BookmarkNode* node) { | 130 NSMenuItem* ItemForBookmarkBarMenu(const BookmarkNode* node) { |
| 136 [cell_ setRepresentedObject:[NSValue valueWithPointer:node]]; | 131 [cell_ setRepresentedObject:[NSValue valueWithPointer:node]]; |
| 137 return menu_item_; | 132 return menu_item_; |
| 138 } | 133 } |
| 139 | 134 |
| 140 | 135 |
| 141 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 136 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 142 scoped_nsobject<NSView> content_area_; | |
| 143 scoped_nsobject<NSView> infobar_view_; | |
| 144 scoped_nsobject<NSView> parent_view_; | 137 scoped_nsobject<NSView> parent_view_; |
| 145 BrowserTestHelper helper_; | 138 BrowserTestHelper helper_; |
| 139 scoped_nsobject<ViewResizerPong> resizeDelegate_; |
| 146 scoped_nsobject<BookmarkBarController> bar_; | 140 scoped_nsobject<BookmarkBarController> bar_; |
| 147 scoped_nsobject<NSMenu> menu_; | 141 scoped_nsobject<NSMenu> menu_; |
| 148 scoped_nsobject<NSMenuItem> menu_item_; | 142 scoped_nsobject<NSMenuItem> menu_item_; |
| 149 scoped_nsobject<NSButtonCell> cell_; | 143 scoped_nsobject<NSButtonCell> cell_; |
| 150 scoped_ptr<BookmarkNode> node_; | 144 scoped_ptr<BookmarkNode> node_; |
| 151 }; | 145 }; |
| 152 | 146 |
| 153 TEST_F(BookmarkBarControllerTest, ShowHide) { | 147 TEST_F(BookmarkBarControllerTest, ShowHide) { |
| 154 // The test class opens the bar by default since many actions are | 148 // The test class opens the bar by default since many actions are |
| 155 // no-ops with it closed. Set back to closed as a baseline. | 149 // no-ops with it closed. Set back to closed as a baseline. |
| 156 if ([bar_ isBookmarkBarVisible]) | 150 if ([bar_ isBookmarkBarVisible]) |
| 157 [bar_ toggleBookmarkBar]; | 151 [bar_ toggleBookmarkBar]; |
| 158 | 152 |
| 159 // Start hidden. | 153 // Start hidden. |
| 160 EXPECT_FALSE([bar_ isBookmarkBarVisible]); | 154 EXPECT_FALSE([bar_ isBookmarkBarVisible]); |
| 161 EXPECT_TRUE([[bar_ view] isHidden]); | 155 EXPECT_TRUE([[bar_ view] isHidden]); |
| 162 | 156 |
| 163 // Show and hide it by toggling. | 157 // Show and hide it by toggling. |
| 164 [bar_ toggleBookmarkBar]; | 158 [bar_ toggleBookmarkBar]; |
| 165 EXPECT_TRUE([bar_ isBookmarkBarVisible]); | 159 EXPECT_TRUE([bar_ isBookmarkBarVisible]); |
| 166 EXPECT_FALSE([[bar_ view] isHidden]); | 160 EXPECT_FALSE([[bar_ view] isHidden]); |
| 167 NSRect content_frame = [content_area_ frame]; | 161 EXPECT_GT([resizeDelegate_ height], 0); |
| 168 NSRect infobar_frame = [infobar_view_ frame]; | |
| 169 EXPECT_NE(content_frame.size.height, kContentAreaHeight); | |
| 170 EXPECT_EQ(NSMaxY(content_frame), NSMinY(infobar_frame)); | |
| 171 EXPECT_EQ(kInfoBarViewHeight, infobar_frame.size.height); | |
| 172 EXPECT_GT([[bar_ view] frame].size.height, 0); | 162 EXPECT_GT([[bar_ view] frame].size.height, 0); |
| 173 | 163 |
| 174 [bar_ toggleBookmarkBar]; | 164 [bar_ toggleBookmarkBar]; |
| 175 EXPECT_FALSE([bar_ isBookmarkBarVisible]); | 165 EXPECT_FALSE([bar_ isBookmarkBarVisible]); |
| 176 EXPECT_TRUE([[bar_ view] isHidden]); | 166 EXPECT_TRUE([[bar_ view] isHidden]); |
| 177 content_frame = [content_area_ frame]; | 167 EXPECT_EQ(0, [resizeDelegate_ height]); |
| 178 infobar_frame = [infobar_view_ frame]; | 168 EXPECT_EQ(0, [[bar_ view] frame].size.height); |
| 179 EXPECT_EQ(content_frame.size.height, kContentAreaHeight); | |
| 180 EXPECT_EQ(NSMaxY(content_frame), NSMinY(infobar_frame)); | |
| 181 EXPECT_EQ(kInfoBarViewHeight, infobar_frame.size.height); | |
| 182 EXPECT_EQ([[bar_ view] frame].size.height, 0); | |
| 183 } | 169 } |
| 184 | 170 |
| 185 // Make sure we're watching for frame change notifications. | 171 // Make sure we're watching for frame change notifications. |
| 186 TEST_F(BookmarkBarControllerTest, FrameChangeNotification) { | 172 TEST_F(BookmarkBarControllerTest, FrameChangeNotification) { |
| 187 scoped_nsobject<BookmarkBarControllerTogglePong> bar; | 173 scoped_nsobject<BookmarkBarControllerTogglePong> bar; |
| 188 bar.reset( | 174 bar.reset( |
| 189 [[BookmarkBarControllerTogglePong alloc] | 175 [[BookmarkBarControllerTogglePong alloc] |
| 190 initWithProfile:helper_.profile() | 176 initWithProfile:helper_.profile() |
| 191 parentView:parent_view_.get() | 177 initialWidth:100 // arbitrary |
| 192 webContentView:content_area_.get() | 178 resizeDelegate:resizeDelegate_.get() |
| 193 infoBarsView:infobar_view_.get() | 179 urlDelegate:nil]); |
| 194 delegate:nil]); | |
| 195 InstallAndToggleBar(bar.get()); | 180 InstallAndToggleBar(bar.get()); |
| 196 | 181 |
| 197 EXPECT_GT([bar toggles], 0); | 182 EXPECT_GT([bar toggles], 0); |
| 198 | 183 |
| 199 // Hard to force toggles -- simple whacking the frame is inadequate. | 184 // Hard to force toggles -- simple whacking the frame is inadequate. |
| 200 // TODO(jrg): find a way to set frame, force a toggle, and verify it. | 185 // TODO(jrg): find a way to set frame, force a toggle, and verify it. |
| 201 } | 186 } |
| 202 | 187 |
| 203 // Confirm off the side button only enabled when reasonable. | 188 // Confirm off the side button only enabled when reasonable. |
| 204 TEST_F(BookmarkBarControllerTest, OffTheSideButtonEnable) { | 189 TEST_F(BookmarkBarControllerTest, OffTheSideButtonEnable) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 EXPECT_TRUE([submenu itemWithTitle:@"f1"]); | 272 EXPECT_TRUE([submenu itemWithTitle:@"f1"]); |
| 288 EXPECT_TRUE([submenu itemWithTitle:@"f2"]); | 273 EXPECT_TRUE([submenu itemWithTitle:@"f2"]); |
| 289 } | 274 } |
| 290 | 275 |
| 291 // Confirm openBookmark: forwards the request to the controller's delegate | 276 // Confirm openBookmark: forwards the request to the controller's delegate |
| 292 TEST_F(BookmarkBarControllerTest, OpenBookmark) { | 277 TEST_F(BookmarkBarControllerTest, OpenBookmark) { |
| 293 GURL gurl("http://walla.walla.ding.dong.com"); | 278 GURL gurl("http://walla.walla.ding.dong.com"); |
| 294 scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl)); | 279 scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl)); |
| 295 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 280 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 296 init]); | 281 init]); |
| 297 [bar_ setDelegate:pong.get()]; | 282 [bar_ setUrlDelegate:pong.get()]; |
| 298 | 283 |
| 299 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); | 284 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); |
| 300 scoped_nsobject<NSButton> button([[NSButton alloc] init]); | 285 scoped_nsobject<NSButton> button([[NSButton alloc] init]); |
| 301 [button setCell:cell.get()]; | 286 [button setCell:cell.get()]; |
| 302 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; | 287 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; |
| 303 | 288 |
| 304 [bar_ openBookmark:button]; | 289 [bar_ openBookmark:button]; |
| 305 EXPECT_EQ(pong.get()->urls_[0], node->GetURL()); | 290 EXPECT_EQ(pong.get()->urls_[0], node->GetURL()); |
| 306 EXPECT_EQ(pong.get()->dispositions_[0], CURRENT_TAB); | 291 EXPECT_EQ(pong.get()->dispositions_[0], CURRENT_TAB); |
| 307 | 292 |
| 308 [bar_ setDelegate:nil]; | 293 [bar_ setUrlDelegate:nil]; |
| 309 } | 294 } |
| 310 | 295 |
| 311 // Confirm opening of bookmarks works from the menus (different | 296 // Confirm opening of bookmarks works from the menus (different |
| 312 // dispositions than clicking on the button). | 297 // dispositions than clicking on the button). |
| 313 TEST_F(BookmarkBarControllerTest, OpenBookmarkFromMenus) { | 298 TEST_F(BookmarkBarControllerTest, OpenBookmarkFromMenus) { |
| 314 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 299 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 315 init]); | 300 init]); |
| 316 [bar_ setDelegate:pong.get()]; | 301 [bar_ setUrlDelegate:pong.get()]; |
| 317 | 302 |
| 318 const char* urls[] = { "http://walla.walla.ding.dong.com", | 303 const char* urls[] = { "http://walla.walla.ding.dong.com", |
| 319 "http://i_dont_know.com", | 304 "http://i_dont_know.com", |
| 320 "http://cee.enn.enn.dot.com" }; | 305 "http://cee.enn.enn.dot.com" }; |
| 321 SEL selectors[] = { @selector(openBookmarkInNewForegroundTab:), | 306 SEL selectors[] = { @selector(openBookmarkInNewForegroundTab:), |
| 322 @selector(openBookmarkInNewWindow:), | 307 @selector(openBookmarkInNewWindow:), |
| 323 @selector(openBookmarkInIncognitoWindow:) }; | 308 @selector(openBookmarkInIncognitoWindow:) }; |
| 324 WindowOpenDisposition dispositions[] = { NEW_FOREGROUND_TAB, | 309 WindowOpenDisposition dispositions[] = { NEW_FOREGROUND_TAB, |
| 325 NEW_WINDOW, | 310 NEW_WINDOW, |
| 326 OFF_THE_RECORD }; | 311 OFF_THE_RECORD }; |
| 327 for (unsigned int i = 0; | 312 for (unsigned int i = 0; |
| 328 i < sizeof(dispositions)/sizeof(dispositions[0]); | 313 i < sizeof(dispositions)/sizeof(dispositions[0]); |
| 329 i++) { | 314 i++) { |
| 330 GURL gurl(urls[i]); | 315 GURL gurl(urls[i]); |
| 331 [bar_ performSelector:selectors[i] | 316 [bar_ performSelector:selectors[i] |
| 332 withObject:ItemForBookmarkBarMenu(gurl)]; | 317 withObject:ItemForBookmarkBarMenu(gurl)]; |
| 333 EXPECT_EQ(pong.get()->urls_[0], gurl); | 318 EXPECT_EQ(pong.get()->urls_[0], gurl); |
| 334 EXPECT_EQ(pong.get()->dispositions_[0], dispositions[i]); | 319 EXPECT_EQ(pong.get()->dispositions_[0], dispositions[i]); |
| 335 [pong clear]; | 320 [pong clear]; |
| 336 } | 321 } |
| 337 [bar_ setDelegate:nil]; | 322 [bar_ setUrlDelegate:nil]; |
| 338 } | 323 } |
| 339 | 324 |
| 340 TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) { | 325 TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) { |
| 341 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); | 326 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); |
| 342 NSView* buttonView = [bar_ buttonView]; | 327 NSView* buttonView = [bar_ buttonView]; |
| 343 EXPECT_EQ(0U, [[bar_ buttons] count]); | 328 EXPECT_EQ(0U, [[bar_ buttons] count]); |
| 344 unsigned int initial_subview_count = [[buttonView subviews] count]; | 329 unsigned int initial_subview_count = [[buttonView subviews] count]; |
| 345 | 330 |
| 346 // Make sure a redundant call doesn't choke | 331 // Make sure a redundant call doesn't choke |
| 347 [bar_ clearBookmarkBar]; | 332 [bar_ clearBookmarkBar]; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 [bar_ deleteBookmark:item]; | 436 [bar_ deleteBookmark:item]; |
| 452 EXPECT_EQ(2, parent->GetChildCount()); | 437 EXPECT_EQ(2, parent->GetChildCount()); |
| 453 EXPECT_EQ(parent->GetChild(0)->GetURL(), GURL(urls[0])); | 438 EXPECT_EQ(parent->GetChild(0)->GetURL(), GURL(urls[0])); |
| 454 // node 2 moved into spot 1 | 439 // node 2 moved into spot 1 |
| 455 EXPECT_EQ(parent->GetChild(1)->GetURL(), GURL(urls[2])); | 440 EXPECT_EQ(parent->GetChild(1)->GetURL(), GURL(urls[2])); |
| 456 } | 441 } |
| 457 | 442 |
| 458 TEST_F(BookmarkBarControllerTest, OpenAllBookmarks) { | 443 TEST_F(BookmarkBarControllerTest, OpenAllBookmarks) { |
| 459 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 444 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 460 init]); | 445 init]); |
| 461 [bar_ setDelegate:pong.get()]; | 446 [bar_ setUrlDelegate:pong.get()]; |
| 462 | 447 |
| 463 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); | 448 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); |
| 464 const BookmarkNode* parent = model->GetBookmarkBarNode(); | 449 const BookmarkNode* parent = model->GetBookmarkBarNode(); |
| 465 // { one, { two-one, two-two }, three } | 450 // { one, { two-one, two-two }, three } |
| 466 model->AddURL(parent, parent->GetChildCount(), | 451 model->AddURL(parent, parent->GetChildCount(), |
| 467 L"title", GURL("http://one.com")); | 452 L"title", GURL("http://one.com")); |
| 468 const BookmarkNode* folder = model->AddGroup(parent, | 453 const BookmarkNode* folder = model->AddGroup(parent, |
| 469 parent->GetChildCount(), | 454 parent->GetChildCount(), |
| 470 L"group"); | 455 L"group"); |
| 471 model->AddURL(folder, folder->GetChildCount(), | 456 model->AddURL(folder, folder->GetChildCount(), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 486 std::vector<GURL>::iterator end = pong.get()->urls_.end(); | 471 std::vector<GURL>::iterator end = pong.get()->urls_.end(); |
| 487 i = find(begin, end, GURL("http://two-one.com")); | 472 i = find(begin, end, GURL("http://two-one.com")); |
| 488 EXPECT_FALSE(i == end); | 473 EXPECT_FALSE(i == end); |
| 489 i = find(begin, end, GURL("https://three.com")); | 474 i = find(begin, end, GURL("https://three.com")); |
| 490 EXPECT_FALSE(i == end); | 475 EXPECT_FALSE(i == end); |
| 491 i = find(begin, end, GURL("https://will-not-be-found.com")); | 476 i = find(begin, end, GURL("https://will-not-be-found.com")); |
| 492 EXPECT_TRUE(i == end); | 477 EXPECT_TRUE(i == end); |
| 493 | 478 |
| 494 EXPECT_EQ(pong.get()->dispositions_[3], NEW_BACKGROUND_TAB); | 479 EXPECT_EQ(pong.get()->dispositions_[3], NEW_BACKGROUND_TAB); |
| 495 | 480 |
| 496 [bar_ setDelegate:nil]; | 481 [bar_ setUrlDelegate:nil]; |
| 497 } | 482 } |
| 498 | 483 |
| 499 // TODO(jrg): write a test to confirm that nodeFavIconLoaded calls | 484 // TODO(jrg): write a test to confirm that nodeFavIconLoaded calls |
| 500 // checkForBookmarkButtonGrowth:. | 485 // checkForBookmarkButtonGrowth:. |
| 501 | 486 |
| 502 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to | 487 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to |
| 503 // process bookmarks) | 488 // process bookmarks) |
| 504 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { | 489 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { |
| 505 } | 490 } |
| 506 | 491 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 517 TEST_F(BookmarkBarControllerTest, Display) { | 502 TEST_F(BookmarkBarControllerTest, Display) { |
| 518 [[bar_ view] display]; | 503 [[bar_ view] display]; |
| 519 } | 504 } |
| 520 | 505 |
| 521 // Cannot test these methods since they simply call a single static | 506 // Cannot test these methods since they simply call a single static |
| 522 // method, BookmarkEditor::Show(), which is impossible to mock. | 507 // method, BookmarkEditor::Show(), which is impossible to mock. |
| 523 // editBookmark:, addPage: | 508 // editBookmark:, addPage: |
| 524 | 509 |
| 525 | 510 |
| 526 } // namespace | 511 } // namespace |
| OLD | NEW |