| 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/scoped_nsobject.h" | 7 #include "base/scoped_nsobject.h" |
| 8 #import "chrome/browser/cocoa/bookmark_bar_controller.h" | 8 #import "chrome/browser/cocoa/bookmark_bar_controller.h" |
| 9 #include "chrome/browser/cocoa/browser_test_helper.h" | 9 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 // Pretend BookmarkURLOpener delegate to keep track of requests | 13 // Pretend BookmarkURLOpener delegate to keep track of requests |
| 14 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { | 14 @interface BookmarkURLOpenerPong : NSObject<BookmarkURLOpener> { |
| 15 @public | 15 @public |
| 16 GURL url_; | 16 GURL url_; |
| 17 WindowOpenDisposition disposition_; |
| 17 } | 18 } |
| 18 @end | 19 @end |
| 19 | 20 |
| 20 @implementation BookmarkURLOpenerPong | 21 @implementation BookmarkURLOpenerPong |
| 21 - (void)openBookmarkURL:(const GURL&)url | 22 - (void)openBookmarkURL:(const GURL&)url |
| 22 disposition:(WindowOpenDisposition)disposition { | 23 disposition:(WindowOpenDisposition)disposition { |
| 23 url_ = url; | 24 url_ = url; |
| 25 disposition_ = disposition; |
| 24 } | 26 } |
| 25 @end | 27 @end |
| 26 | 28 |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 static const int kContentAreaHeight = 500; | 32 static const int kContentAreaHeight = 500; |
| 31 | 33 |
| 32 class BookmarkBarControllerTest : public testing::Test { | 34 class BookmarkBarControllerTest : public testing::Test { |
| 33 public: | 35 public: |
| 34 BookmarkBarControllerTest() { | 36 BookmarkBarControllerTest() { |
| 35 NSRect content_frame = NSMakeRect(0, 0, 800, kContentAreaHeight); | 37 NSRect content_frame = NSMakeRect(0, 0, 800, kContentAreaHeight); |
| 36 NSRect bar_frame = NSMakeRect(0, 0, 800, 0); | 38 NSRect parent_frame = NSMakeRect(0, 0, 800, 50); |
| 37 content_area_.reset([[NSView alloc] initWithFrame:content_frame]); | 39 content_area_.reset([[NSView alloc] initWithFrame:content_frame]); |
| 38 bar_view_.reset([[NSView alloc] initWithFrame:bar_frame]); | 40 parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]); |
| 39 [bar_view_ setHidden:YES]; | 41 [parent_view_ setHidden:YES]; |
| 40 BookmarkBarView *bbv = (BookmarkBarView*)bar_view_.get(); | |
| 41 bar_.reset( | 42 bar_.reset( |
| 42 [[BookmarkBarController alloc] initWithProfile:helper_.profile() | 43 [[BookmarkBarController alloc] initWithProfile:helper_.profile() |
| 43 view:bbv | 44 parentView:parent_view_.get() |
| 44 webContentView:content_area_.get() | 45 webContentView:content_area_.get() |
| 45 delegate:nil]); | 46 delegate:nil]); |
| 46 NSView* parent = cocoa_helper_.contentView(); | 47 [bar_ view]; // force loading of the nib |
| 47 [parent addSubview:content_area_.get()]; | |
| 48 [parent addSubview:[bar_ view]]; | |
| 49 } | 48 } |
| 50 | 49 |
| 51 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 50 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
| 52 scoped_nsobject<NSView> content_area_; | 51 scoped_nsobject<NSView> content_area_; |
| 53 scoped_nsobject<NSView> bar_view_; | 52 scoped_nsobject<NSView> parent_view_; |
| 54 BrowserTestHelper helper_; | 53 BrowserTestHelper helper_; |
| 55 scoped_nsobject<BookmarkBarController> bar_; | 54 scoped_nsobject<BookmarkBarController> bar_; |
| 56 }; | 55 }; |
| 57 | 56 |
| 58 TEST_F(BookmarkBarControllerTest, ShowHide) { | 57 TEST_F(BookmarkBarControllerTest, ShowHide) { |
| 59 // Assume hidden by default in a new profile. | 58 // Assume hidden by default in a new profile. |
| 60 EXPECT_FALSE([bar_ isBookmarkBarVisible]); | 59 EXPECT_FALSE([bar_ isBookmarkBarVisible]); |
| 61 EXPECT_TRUE([[bar_ view] isHidden]); | 60 EXPECT_TRUE([[bar_ view] isHidden]); |
| 62 EXPECT_EQ([bar_view_ frame].size.height, 0); | |
| 63 | 61 |
| 64 // Show and hide it by toggling. | 62 // Show and hide it by toggling. |
| 65 [bar_ toggleBookmarkBar]; | 63 [bar_ toggleBookmarkBar]; |
| 66 EXPECT_TRUE([bar_ isBookmarkBarVisible]); | 64 EXPECT_TRUE([bar_ isBookmarkBarVisible]); |
| 67 EXPECT_FALSE([[bar_ view] isHidden]); | 65 EXPECT_FALSE([[bar_ view] isHidden]); |
| 68 NSRect content_frame = [content_area_ frame]; | 66 NSRect content_frame = [content_area_ frame]; |
| 69 EXPECT_NE(content_frame.size.height, kContentAreaHeight); | 67 EXPECT_NE(content_frame.size.height, kContentAreaHeight); |
| 70 EXPECT_GT([bar_view_ frame].size.height, 0); | 68 EXPECT_GT([[bar_ view] frame].size.height, 0); |
| 69 |
| 71 [bar_ toggleBookmarkBar]; | 70 [bar_ toggleBookmarkBar]; |
| 72 EXPECT_FALSE([bar_ isBookmarkBarVisible]); | 71 EXPECT_FALSE([bar_ isBookmarkBarVisible]); |
| 73 EXPECT_TRUE([[bar_ view] isHidden]); | 72 EXPECT_TRUE([[bar_ view] isHidden]); |
| 74 content_frame = [content_area_ frame]; | 73 content_frame = [content_area_ frame]; |
| 75 EXPECT_EQ(content_frame.size.height, kContentAreaHeight); | 74 EXPECT_EQ(content_frame.size.height, kContentAreaHeight); |
| 76 EXPECT_EQ([bar_view_ frame].size.height, 0); | 75 EXPECT_EQ([[bar_ view] frame].size.height, 0); |
| 77 } | 76 } |
| 78 | 77 |
| 79 // Confirm openBookmark: forwards the request to the controller's delegate | 78 // Confirm openBookmark: forwards the request to the controller's delegate |
| 80 TEST_F(BookmarkBarControllerTest, OpenBookmark) { | 79 TEST_F(BookmarkBarControllerTest, OpenBookmark) { |
| 81 GURL gurl("http://walla.walla.ding.dong.com"); | 80 GURL gurl("http://walla.walla.ding.dong.com"); |
| 82 scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl)); | 81 scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl)); |
| 83 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] | 82 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 84 init]); | 83 init]); |
| 85 [bar_ setDelegate:pong.get()]; | 84 [bar_ setDelegate:pong.get()]; |
| 86 | 85 |
| 87 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); | 86 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); |
| 88 scoped_nsobject<NSButton> button([[NSButton alloc] init]); | 87 scoped_nsobject<NSButton> button([[NSButton alloc] init]); |
| 89 [button setCell:cell.get()]; | 88 [button setCell:cell.get()]; |
| 90 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; | 89 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; |
| 90 |
| 91 [bar_ openBookmark:button]; | 91 [bar_ openBookmark:button]; |
| 92 EXPECT_EQ(pong.get()->url_, node->GetURL()); |
| 93 EXPECT_EQ(pong.get()->disposition_, CURRENT_TAB); |
| 92 | 94 |
| 93 EXPECT_EQ(pong.get()->url_, node->GetURL()); | 95 [bar_ setDelegate:nil]; |
| 94 } | 96 } |
| 95 | 97 |
| 98 // Confirm opening of bookmarks works from the menus (different |
| 99 // dispositions than clicking on the button). |
| 100 TEST_F(BookmarkBarControllerTest, OpenBookmarkFromMenus) { |
| 101 scoped_nsobject<BookmarkURLOpenerPong> pong([[BookmarkURLOpenerPong alloc] |
| 102 init]); |
| 103 [bar_ setDelegate:pong.get()]; |
| 104 |
| 105 scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"I_dont_care"]); |
| 106 scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc] |
| 107 initWithTitle:@"still_dont_care" |
| 108 action:NULL |
| 109 keyEquivalent:@""]); |
| 110 scoped_nsobject<NSButtonCell> cell([[NSButtonCell alloc] init]); |
| 111 [item setMenu:menu.get()]; |
| 112 [menu setDelegate:cell]; |
| 113 |
| 114 const char* urls[] = { "http://walla.walla.ding.dong.com", |
| 115 "http://i_dont_know.com", |
| 116 "http://cee.enn.enn.dot.com" }; |
| 117 SEL selectors[] = { @selector(openBookmarkInNewForegroundTab:), |
| 118 @selector(openBookmarkInNewWindow:), |
| 119 @selector(openBookmarkInIncognitoWindow:) }; |
| 120 WindowOpenDisposition dispositions[] = { NEW_FOREGROUND_TAB, |
| 121 NEW_WINDOW, |
| 122 OFF_THE_RECORD }; |
| 123 for (unsigned int i = 0; |
| 124 i < sizeof(dispositions)/sizeof(dispositions[0]); |
| 125 i++) { |
| 126 scoped_ptr<BookmarkNode> node(new BookmarkNode(GURL(urls[i]))); |
| 127 [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]]; |
| 128 [bar_ performSelector:selectors[i] withObject:item.get()]; |
| 129 EXPECT_EQ(pong.get()->url_, node->GetURL()); |
| 130 EXPECT_EQ(pong.get()->disposition_, dispositions[i]); |
| 131 [cell setRepresentedObject:nil]; |
| 132 } |
| 133 } |
| 134 |
| 135 |
| 96 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to | 136 // TODO(jrg): Make sure showing the bookmark bar calls loaded: (to |
| 97 // process bookmarks) | 137 // process bookmarks) |
| 98 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { | 138 TEST_F(BookmarkBarControllerTest, ShowAndLoad) { |
| 99 } | 139 } |
| 100 | 140 |
| 101 // TODO(jrg): Make sure a cleared bar has no subviews | 141 // TODO(jrg): Make sure adding 1 bookmark adds 1 subview, and removing |
| 102 TEST_F(BookmarkBarControllerTest, Clear) { | 142 // 1 removes 1 subview. (We can't test for a simple Clear since there |
| 143 // will soon be views in here which aren't bookmarks.) |
| 144 TEST_F(BookmarkBarControllerTest, ViewChanges) { |
| 103 } | 145 } |
| 104 | 146 |
| 105 // TODO(jrg): Make sure loaded: does something useful | 147 // TODO(jrg): Make sure loaded: does something useful |
| 106 TEST_F(BookmarkBarControllerTest, Loaded) { | 148 TEST_F(BookmarkBarControllerTest, Loaded) { |
| 107 // Clear; make sure no views | 149 // Clear; make sure no views |
| 108 // Call loaded: | 150 // Call loaded: |
| 109 // Make sure subviews | 151 // Make sure subviews |
| 110 } | 152 } |
| 111 | 153 |
| 112 // TODO(jrg): Test cellForBookmarkNode: | 154 // TODO(jrg): Test cellForBookmarkNode: |
| 113 TEST_F(BookmarkBarControllerTest, Cell) { | 155 TEST_F(BookmarkBarControllerTest, Cell) { |
| 114 } | 156 } |
| 115 | 157 |
| 116 // TODO(jrg): Test frameForBookmarkAtIndex | 158 // TODO(jrg): Test frameForBookmarkAtIndex |
| 117 TEST_F(BookmarkBarControllerTest, FrameAtIndex) { | 159 TEST_F(BookmarkBarControllerTest, FrameAtIndex) { |
| 118 } | 160 } |
| 119 | 161 |
| 120 TEST_F(BookmarkBarControllerTest, Contents) { | 162 TEST_F(BookmarkBarControllerTest, Contents) { |
| 121 // TODO(jrg): addNodesToBar has a LOT of TODOs; when flushed out, write | 163 // TODO(jrg): addNodesToBar has a LOT of TODOs; when flushed out, write |
| 122 // appropriate tests. | 164 // appropriate tests. |
| 123 } | 165 } |
| 124 | 166 |
| 125 // Test drawing, mostly to ensure nothing leaks or crashes. | 167 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 126 TEST_F(BookmarkBarControllerTest, Display) { | 168 TEST_F(BookmarkBarControllerTest, Display) { |
| 127 [[bar_ view] display]; | 169 [[bar_ view] display]; |
| 128 } | 170 } |
| 129 | 171 |
| 130 } // namespace | 172 } // namespace |
| OLD | NEW |