1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "app/mac/nsimage_cache.h" 6 #include "base/memory/scoped_nsobject.h" 7 #include "base/utf_string_conversions.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu.h" 11 #include "chrome/browser/ui/cocoa/browser_test_helper.h" 12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/platform_test.h" 15 #include "ui/gfx/image.h" 16 17 // Simple class to remember how many mouseEntered: and mouseExited: 18 // calls it gets. Only used by BookmarkMouseForwarding but placed 19 // at the top of the file to keep it outside the anon namespace. 20 @interface ButtonRemembersMouseEnterExit : NSButton { 21 @public 22 int enters_; 23 int exits_; 24 } 25 @end 26 27 @implementation ButtonRemembersMouseEnterExit 28 - (void)mouseEntered:(NSEvent*)event { 29 enters_++; 30 } 31 - (void)mouseExited:(NSEvent*)event { 32 exits_++; 33 } 34 @end 35 36 37 namespace { 38 39 class BookmarkButtonCellTest : public CocoaTest { 40 public: 41 BrowserTestHelper helper_; 42 }; 43 44 // Make sure it's not totally bogus 45 TEST_F(BookmarkButtonCellTest, SizeForBounds) { 46 NSRect frame = NSMakeRect(0, 0, 50, 30); 47 scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); 48 scoped_nsobject<BookmarkButtonCell> cell( 49 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 50 [view setCell:cell.get()]; 51 [[test_window() contentView] addSubview:view]; 52 53 NSRect r = NSMakeRect(0, 0, 100, 100); 54 NSSize size = [cell.get() cellSizeForBounds:r]; 55 EXPECT_TRUE(size.width > 0 && size.height > 0); 56 EXPECT_TRUE(size.width < 200 && size.height < 200); 57 } 58 59 // Make sure icon-only buttons are squeezed tightly. 60 TEST_F(BookmarkButtonCellTest, IconOnlySqueeze) { 61 NSRect frame = NSMakeRect(0, 0, 50, 30); 62 scoped_nsobject<NSButton> view([[NSButton alloc] initWithFrame:frame]); 63 scoped_nsobject<BookmarkButtonCell> cell( 64 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 65 [view setCell:cell.get()]; 66 [[test_window() contentView] addSubview:view]; 67 68 scoped_nsobject<NSImage> image( 69 [app::mac::GetCachedImageWithName(@"nav.pdf") retain]); 70 EXPECT_TRUE(image.get()); 71 72 NSRect r = NSMakeRect(0, 0, 100, 100); 73 [cell setBookmarkCellText:@" " image:image]; 74 CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width; 75 [cell setBookmarkCellText:@" " image:image]; 76 CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width; 77 [cell setBookmarkCellText:@"" image:image]; 78 CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width; 79 80 // Make sure the switch to "no title" is more significant than we 81 // would otherwise see by decreasing the length of the title. 82 CGFloat delta1 = two_space_width - one_space_width; 83 CGFloat delta2 = one_space_width - zero_space_width; 84 EXPECT_GT(delta2, delta1); 85 86 } 87 88 // Make sure the default from the base class is overridden. 89 TEST_F(BookmarkButtonCellTest, MouseEnterStuff) { 90 scoped_nsobject<BookmarkButtonCell> cell( 91 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 92 // Setting the menu should have no affect since we either share or 93 // dynamically compose the menu given a node. 94 [cell setMenu:[[[BookmarkMenu alloc] initWithTitle:@"foo"] autorelease]]; 95 EXPECT_FALSE([cell menu]); 96 97 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 98 const BookmarkNode* node = model->GetBookmarkBarNode(); 99 [cell setEmpty:NO]; 100 [cell setBookmarkNode:node]; 101 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]); 102 EXPECT_TRUE([cell menu]); 103 104 [cell setEmpty:YES]; 105 EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]); 106 EXPECT_FALSE([cell menu]); 107 } 108 109 TEST_F(BookmarkButtonCellTest, BookmarkNode) { 110 BookmarkModel& model(*(helper_.profile()->GetBookmarkModel())); 111 scoped_nsobject<BookmarkButtonCell> cell( 112 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 113 114 const BookmarkNode* node = model.GetBookmarkBarNode(); 115 [cell setBookmarkNode:node]; 116 EXPECT_EQ(node, [cell bookmarkNode]); 117 118 node = model.other_node(); 119 [cell setBookmarkNode:node]; 120 EXPECT_EQ(node, [cell bookmarkNode]); 121 } 122 123 TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) { 124 scoped_nsobject<BookmarkButtonCell> cell( 125 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 126 scoped_nsobject<ButtonRemembersMouseEnterExit> 127 button([[ButtonRemembersMouseEnterExit alloc] 128 initWithFrame:NSMakeRect(0,0,50,50)]); 129 [button setCell:cell.get()]; 130 EXPECT_EQ(0, button.get()->enters_); 131 EXPECT_EQ(0, button.get()->exits_); 132 NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved 133 location:NSMakePoint(10,10) 134 modifierFlags:0 135 timestamp:0 136 windowNumber:0 137 context:nil 138 eventNumber:0 139 clickCount:0 140 pressure:0]; 141 [cell mouseEntered:event]; 142 EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_); 143 144 for (int i = 0; i < 3; i++) 145 [cell mouseExited:event]; 146 EXPECT_EQ(button.get()->enters_, 1); 147 EXPECT_EQ(button.get()->exits_, 3); 148 } 149 150 // Confirms a cell created in a nib is initialized properly 151 TEST_F(BookmarkButtonCellTest, Awake) { 152 scoped_nsobject<BookmarkButtonCell> cell([[BookmarkButtonCell alloc] init]); 153 [cell awakeFromNib]; 154 EXPECT_EQ(NSLeftTextAlignment, [cell alignment]); 155 } 156 157 // Subfolder arrow details. 158 TEST_F(BookmarkButtonCellTest, FolderArrow) { 159 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 160 const BookmarkNode* bar = model->GetBookmarkBarNode(); 161 const BookmarkNode* node = model->AddURL(bar, bar->child_count(), 162 ASCIIToUTF16("title"), 163 GURL("http://www.google.com")); 164 scoped_nsobject<BookmarkButtonCell> cell( 165 [[BookmarkButtonCell alloc] initForNode:node 166 contextMenu:nil 167 cellText:@"small" 168 cellImage:nil]); 169 EXPECT_TRUE(cell.get()); 170 171 NSSize size = [cell cellSize]; 172 // sanity check 173 EXPECT_GE(size.width, 2); 174 EXPECT_GE(size.height, 2); 175 176 // Once we turn on arrow drawing make sure there is now room for it. 177 [cell setDrawFolderArrow:YES]; 178 NSSize arrowSize = [cell cellSize]; 179 EXPECT_GT(arrowSize.width, size.width); 180 } 181 182 } // namespace 183