| 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/scoped_nsobject.h" | 8 #include "base/scoped_nsobject.h" |
| 8 #import "chrome/browser/cocoa/bookmark_bar_controller.h" | 9 #import "chrome/browser/cocoa/bookmark_bar_controller.h" |
| 9 #include "chrome/browser/cocoa/browser_test_helper.h" | 10 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 // Pretend BookmarkURLOpener delegate to keep track of requests | 14 // Pretend BookmarkURLOpener delegate to keep track of requests |
| 14 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { | 15 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { |
| 15 @public | 16 @public |
| 16 GURL url_; | 17 std::vector<GURL> urls_; |
| 17 WindowOpenDisposition disposition_; | 18 std::vector<WindowOpenDisposition> dispositions_; |
| 18 } | 19 } |
| 19 @end | 20 @end |
| 20 | 21 |
| 21 @implementation BookmarkURLOpenerPong | 22 @implementation BookmarkURLOpenerPong |
| 22 - (void)openBookmarkURL:(const GURL&)url | 23 - (void)openBookmarkURL:(const GURL&)url |
| 23 disposition:(WindowOpenDisposition)disposition { | 24 disposition:(WindowOpenDisposition)disposition { |
| 24 url_ = url; | 25 urls_.push_back(url); |
| 25 disposition_ = disposition; | 26 dispositions_.push_back(disposition); |
| 27 } |
| 28 - (void)clear { |
| 29 urls_.clear(); |
| 30 dispositions_.clear(); |
| 26 } | 31 } |
| 27 @end | 32 @end |
| 28 | 33 |
| 29 | 34 |
| 30 // NSCell that is pre-provided with a desired size that becomes the | 35 // NSCell that is pre-provided with a desired size that becomes the |
| 31 // return value for -(NSSize)cellSize:. | 36 // return value for -(NSSize)cellSize:. |
| 32 @interface CellWithDesiredSize : NSCell { | 37 @interface CellWithDesiredSize : NSCell { |
| 33 @private | 38 @private |
| 34 NSSize cellSize_; | 39 NSSize cellSize_; |
| 35 } | 40 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 [bar_ view]; // force loading of the nib | 81 [bar_ view]; // force loading of the nib |
| 77 | 82 |
| 78 // Awkwardness to look like we've been installed. | 83 // Awkwardness to look like we've been installed. |
| 79 [parent_view_ addSubview:[bar_ view]]; | 84 [parent_view_ addSubview:[bar_ view]]; |
| 80 NSRect frame = [[[bar_ view] superview] frame]; | 85 NSRect frame = [[[bar_ view] superview] frame]; |
| 81 frame.origin.y = 100; | 86 frame.origin.y = 100; |
| 82 [[[bar_ view] superview] setFrame:frame]; | 87 [[[bar_ view] superview] setFrame:frame]; |
| 83 | 88 |
| 84 // make sure it's open so certain things aren't no-ops | 89 // make sure it's open so certain things aren't no-ops |
| 85 [bar_ toggleBookmarkBar]; | 90 [bar_ toggleBookmarkBar]; |
| 91 |
| 92 // Create a menu/item to act like a sender |
| 93 menu_.reset([[NSMenu alloc] initWithTitle:@"I_dont_care"]); |
| 94 menu_item_.reset([[NSMenuItem alloc] |
| 95 initWithTitle:@"still_dont_care" |
| 96 action:NULL |
| 97 keyEquivalent:@""]); |
| 98 cell_.reset([[NSButtonCell alloc] init]); |
| 99 [menu_item_ setMenu:menu_.get()]; |
| 100 [menu_ setDelegate:cell_.get()]; |
| 86 } | 101 } |
| 87 | 102 |
| 103 // Return a menu item that points to the right URL. |
| 104 NSMenuItem* ItemForBookmarkBarMenu(GURL& gurl) { |
| 105 node_.reset(new BookmarkNode(gurl)); |
| 106 [cell_ setRepresentedObject:[NSValue valueWithPointer:node_.get()]]; |
| 107 return menu_item_; |
| 108 } |
| 109 |
| 110 // Does NOT take ownership of node. |
| 111 NSMenuItem* ItemForBookmarkBarMenu(const BookmarkNode* node) { |
| 112 [cell_ setRepresentedObject:[NSValue valueWithPointer:node]]; |
| 113 return menu_item_; |
| 114 } |
| 115 |
| 116 |
| 88 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 117 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 89 scoped_nsobject<NSView> content_area_; | 118 scoped_nsobject<NSView> content_area_; |
| 90 scoped_nsobject<NSView> infobar_view_; | 119 scoped_nsobject<NSView> infobar_view_; |
| 91 scoped_nsobject<NSView> parent_view_; | 120 scoped_nsobject<NSView> parent_view_; |
| 92 BrowserTestHelper helper_; | 121 BrowserTestHelper helper_; |
| 93 scoped_nsobject<BookmarkBarController> bar_; | 122 scoped_nsobject<BookmarkBarController> bar_; |
| 123 scoped_nsobject<NSMenu> menu_; |
| 124 scoped_nsobject<NSMenuItem> menu_item_; |
| 125 scoped_nsobject<NSButtonCell> cell_; |
| 126 scoped_ptr<BookmarkNode> node_; |
| 94 }; | 127 }; |
| 95 | 128 |
| 96 TEST_F(BookmarkBarControllerTest, ShowHide) { | 129 TEST_F(BookmarkBarControllerTest, ShowHide) { |
| 97 // The test class opens the bar by default since many actions are | 130 // The test class opens the bar by default since many actions are |
| 98 // no-ops with it closed. Set back to closed as a baseline. | 131 // no-ops with it closed. Set back to closed as a baseline. |
| 99 if ([bar_ isBookmarkBarVisible]) | 132 if ([bar_ isBookmarkBarVisible]) |
| 100 [bar_ toggleBookmarkBar]; | 133 [bar_ toggleBookmarkBar]; |
| 101 | 134 |
| 102 // Start hidden. | 135 // Start hidden. |
| 103 EXPECT_FALSE([bar_ isBookmarkBarVisible]); | 136 EXPECT_FALSE([bar_ isBookmarkBarVisible]); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 132 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 165 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 133 init]); | 166 init]); |
| 134 [bar_ setDelegate:pong.get()]; | 167 [bar_ setDelegate:pong.get()]; |
| 135 | 168 |
| 136 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); | 169 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); |
| 137 scoped_nsobject<NSButton> button([[NSButton alloc] init]); | 170 scoped_nsobject<NSButton> button([[NSButton alloc] init]); |
| 138 [button setCell:cell.get()]; | 171 [button setCell:cell.get()]; |
| 139 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; | 172 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; |
| 140 | 173 |
| 141 [bar_ openBookmark:button]; | 174 [bar_ openBookmark:button]; |
| 142 EXPECT_EQ(pong.get()->url_, node->GetURL()); | 175 EXPECT_EQ(pong.get()->urls_[0], node->GetURL()); |
| 143 EXPECT_EQ(pong.get()->disposition_, CURRENT_TAB); | 176 EXPECT_EQ(pong.get()->dispositions_[0], CURRENT_TAB); |
| 144 | 177 |
| 145 [bar_ setDelegate:nil]; | 178 [bar_ setDelegate:nil]; |
| 146 } | 179 } |
| 147 | 180 |
| 148 // Confirm opening of bookmarks works from the menus (different | 181 // Confirm opening of bookmarks works from the menus (different |
| 149 // dispositions than clicking on the button). | 182 // dispositions than clicking on the button). |
| 150 TEST_F(BookmarkBarControllerTest, OpenBookmarkFromMenus) { | 183 TEST_F(BookmarkBarControllerTest, OpenBookmarkFromMenus) { |
| 151 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 184 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 152 init]); | 185 init]); |
| 153 [bar_ setDelegate:pong.get()]; | 186 [bar_ setDelegate:pong.get()]; |
| 154 | 187 |
| 155 scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"I_dont_care"]); | |
| 156 scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc] | |
| 157 initWithTitle:@"still_dont_care" | |
| 158 action:NULL | |
| 159 keyEquivalent:@""]); | |
| 160 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); | |
| 161 [item setMenu:menu.get()]; | |
| 162 [menu setDelegate:cell]; | |
| 163 | |
| 164 const char* urls[] = { "http://walla.walla.ding.dong.com", | 188 const char* urls[] = { "http://walla.walla.ding.dong.com", |
| 165 "http://i_dont_know.com", | 189 "http://i_dont_know.com", |
| 166 "http://cee.enn.enn.dot.com" }; | 190 "http://cee.enn.enn.dot.com" }; |
| 167 SEL selectors[] = { @selector(openBookmarkInNewForegroundTab:), | 191 SEL selectors[] = { @selector(openBookmarkInNewForegroundTab:), |
| 168 @selector(openBookmarkInNewWindow:), | 192 @selector(openBookmarkInNewWindow:), |
| 169 @selector(openBookmarkInIncognitoWindow:) }; | 193 @selector(openBookmarkInIncognitoWindow:) }; |
| 170 WindowOpenDisposition dispositions[] = { NEW_FOREGROUND_TAB, | 194 WindowOpenDisposition dispositions[] = { NEW_FOREGROUND_TAB, |
| 171 NEW_WINDOW, | 195 NEW_WINDOW, |
| 172 OFF_THE_RECORD }; | 196 OFF_THE_RECORD }; |
| 173 for (unsigned int i = 0; | 197 for (unsigned int i = 0; |
| 174 i < sizeof(dispositions)/sizeof(dispositions[0]); | 198 i < sizeof(dispositions)/sizeof(dispositions[0]); |
| 175 i++) { | 199 i++) { |
| 176 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(urls[i]))); | 200 GURL gurl(urls[i]); |
| 177 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; | 201 [bar_ performSelector:selectors[i] |
| 178 [bar_ performSelector:selectors[i] withObject:item.get()]; | 202 withObject:ItemForBookmarkBarMenu(gurl)]; |
| 179 EXPECT_EQ(pong.get()->url_, node->GetURL()); | 203 EXPECT_EQ(pong.get()->urls_[0], gurl); |
| 180 EXPECT_EQ(pong.get()->disposition_, dispositions[i]); | 204 EXPECT_EQ(pong.get()->dispositions_[0], dispositions[i]); |
| 181 [cell setRepresentedObject:nil]; | 205 [pong clear]; |
| 182 } | 206 } |
| 207 [bar_ setDelegate:nil]; |
| 183 } | 208 } |
| 184 | 209 |
| 185 TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) { | 210 TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) { |
| 186 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); | 211 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); |
| 187 | 212 |
| 188 EXPECT_EQ(0U, [[bar_ buttons] count]); | 213 EXPECT_EQ(0U, [[bar_ buttons] count]); |
| 189 unsigned int initial_subview_count = [[[bar_ view] subviews] count]; | 214 unsigned int initial_subview_count = [[[bar_ view] subviews] count]; |
| 190 | 215 |
| 191 // Make sure a redundant call doesn't choke | 216 // Make sure a redundant call doesn't choke |
| 192 [bar_ clearBookmarkBar]; | 217 [bar_ clearBookmarkBar]; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 296 |
| 272 // Make sure the 1st button is now wider, the 2nd one is moved over, | 297 // Make sure the 1st button is now wider, the 2nd one is moved over, |
| 273 // and they don't overlap. | 298 // and they don't overlap. |
| 274 NSRect frame_1 = [[[bar_ buttons] objectAtIndex:0] frame]; | 299 NSRect frame_1 = [[[bar_ buttons] objectAtIndex:0] frame]; |
| 275 NSRect frame_2 = [[[bar_ buttons] objectAtIndex:1] frame]; | 300 NSRect frame_2 = [[[bar_ buttons] objectAtIndex:1] frame]; |
| 276 EXPECT_GT(frame_1.size.width, width_1); | 301 EXPECT_GT(frame_1.size.width, width_1); |
| 277 EXPECT_GT(frame_2.origin.x, x_2); | 302 EXPECT_GT(frame_2.origin.x, x_2); |
| 278 EXPECT_GE(frame_2.origin.x, frame_1.origin.x + frame_1.size.width); | 303 EXPECT_GE(frame_2.origin.x, frame_1.origin.x + frame_1.size.width); |
| 279 } | 304 } |
| 280 | 305 |
| 306 TEST_F(BookmarkBarControllerTest, DeleteBookmark) { |
| 307 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); |
| 308 |
| 309 const char* urls[] = { "https://secret.url.com", |
| 310 "http://super.duper.web.site.for.doodz.gov", |
| 311 "http://www.foo-bar-baz.com/" }; |
| 312 const BookmarkNode* parent = model->GetBookmarkBarNode(); |
| 313 for (unsigned int i = 0; i < arraysize(urls); i++) { |
| 314 model->AddURL(parent, parent->GetChildCount(), |
| 315 L"title", GURL(urls[i])); |
| 316 } |
| 317 EXPECT_EQ(3, parent->GetChildCount()); |
| 318 const BookmarkNode* middle_node = parent->GetChild(1); |
| 319 |
| 320 NSMenuItem* item = ItemForBookmarkBarMenu(middle_node); |
| 321 [bar_ deleteBookmark:item]; |
| 322 EXPECT_EQ(2, parent->GetChildCount()); |
| 323 EXPECT_EQ(parent->GetChild(0)->GetURL(), GURL(urls[0])); |
| 324 // node 2 moved into spot 1 |
| 325 EXPECT_EQ(parent->GetChild(1)->GetURL(), GURL(urls[2])); |
| 326 } |
| 327 |
| 328 TEST_F(BookmarkBarControllerTest, OpenAllBookmarks) { |
| 329 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 330 init]); |
| 331 [bar_ setDelegate:pong.get()]; |
| 332 |
| 333 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); |
| 334 const BookmarkNode* parent = model->GetBookmarkBarNode(); |
| 335 // { one, { two-one, two-two }, three } |
| 336 model->AddURL(parent, parent->GetChildCount(), |
| 337 L"title", GURL("http://one.com")); |
| 338 const BookmarkNode* folder = model->AddGroup(parent, |
| 339 parent->GetChildCount(), |
| 340 L"group"); |
| 341 model->AddURL(folder, folder->GetChildCount(), |
| 342 L"title", GURL("http://two-one.com")); |
| 343 model->AddURL(folder, folder->GetChildCount(), |
| 344 L"title", GURL("http://two-two.com")); |
| 345 model->AddURL(parent, parent->GetChildCount(), |
| 346 L"title", GURL("https://three.com")); |
| 347 [bar_ openAllBookmarks:nil]; |
| 348 |
| 349 EXPECT_EQ(pong.get()->urls_.size(), 4U); |
| 350 EXPECT_EQ(pong.get()->dispositions_.size(), 4U); |
| 351 |
| 352 // I can't use EXPECT_EQ() here since the macro can't expand |
| 353 // properly (no way to print the value of an iterator). |
| 354 std::vector<GURL>::iterator i; |
| 355 std::vector<GURL>::iterator begin = pong.get()->urls_.begin(); |
| 356 std::vector<GURL>::iterator end = pong.get()->urls_.end(); |
| 357 i = find(begin, end, GURL("http://two-one.com")); |
| 358 EXPECT_FALSE(i == end); |
| 359 i = find(begin, end, GURL("https://three.com")); |
| 360 EXPECT_FALSE(i == end); |
| 361 i = find(begin, end, GURL("https://will-not-be-found.com")); |
| 362 EXPECT_TRUE(i == end); |
| 363 |
| 364 EXPECT_EQ(pong.get()->dispositions_[3], NEW_BACKGROUND_TAB); |
| 365 |
| 366 [bar_ setDelegate:nil]; |
| 367 } |
| 368 |
| 281 // TODO(jrg): write a test to confirm that nodeFavIconLoaded calls | 369 // TODO(jrg): write a test to confirm that nodeFavIconLoaded calls |
| 282 // checkForBookmarkButtonGrowth:. | 370 // checkForBookmarkButtonGrowth:. |
| 283 | 371 |
| 284 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to | 372 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to |
| 285 // process bookmarks) | 373 // process bookmarks) |
| 286 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { | 374 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { |
| 287 } | 375 } |
| 288 | 376 |
| 289 // TODO(jrg): Test cellForBookmarkNode: | 377 // TODO(jrg): Test cellForBookmarkNode: |
| 290 TEST_F(BookmarkBarControllerTest, Cell) { | 378 TEST_F(BookmarkBarControllerTest, Cell) { |
| 291 } | 379 } |
| 292 | 380 |
| 293 TEST_F(BookmarkBarControllerTest, Contents) { | 381 TEST_F(BookmarkBarControllerTest, Contents) { |
| 294 // TODO(jrg): addNodesToBar has a LOT of TODOs; when flushed out, write | 382 // TODO(jrg): addNodesToBar has a LOT of TODOs; when flushed out, write |
| 295 // appropriate tests. | 383 // appropriate tests. |
| 296 } | 384 } |
| 297 | 385 |
| 298 // Test drawing, mostly to ensure nothing leaks or crashes. | 386 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 299 TEST_F(BookmarkBarControllerTest, Display) { | 387 TEST_F(BookmarkBarControllerTest, Display) { |
| 300 [[bar_ view] display]; | 388 [[bar_ view] display]; |
| 301 } | 389 } |
| 302 | 390 |
| 391 // Cannot test these methods since they simply call a single static |
| 392 // method, BookmarkEditor::Show(), which is impossible to mock. |
| 393 // editBookmark:, addPage: |
| 394 |
| 395 |
| 303 } // namespace | 396 } // namespace |
| OLD | NEW |