bookmark_button_cell_unittest.mm revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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 3// found in the LICENSE file. 4 5#include "app/resource_bundle.h" 6#include "base/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 "grit/app_resources.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "testing/platform_test.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 37namespace { 38 39class BookmarkButtonCellTest : public CocoaTest { 40 public: 41 BrowserTestHelper helper_; 42}; 43 44// Make sure it's not totally bogus 45TEST_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. 60TEST_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 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 69 scoped_nsobject<NSImage> image([rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON) 70 retain]); 71 EXPECT_TRUE(image.get()); 72 73 NSRect r = NSMakeRect(0, 0, 100, 100); 74 [cell setBookmarkCellText:@" " image:image]; 75 CGFloat two_space_width = [cell.get() cellSizeForBounds:r].width; 76 [cell setBookmarkCellText:@" " image:image]; 77 CGFloat one_space_width = [cell.get() cellSizeForBounds:r].width; 78 [cell setBookmarkCellText:@"" image:image]; 79 CGFloat zero_space_width = [cell.get() cellSizeForBounds:r].width; 80 81 // Make sure the switch to "no title" is more significant than we 82 // would otherwise see by decreasing the length of the title. 83 CGFloat delta1 = two_space_width - one_space_width; 84 CGFloat delta2 = one_space_width - zero_space_width; 85 EXPECT_GT(delta2, delta1); 86 87} 88 89// Make sure the default from the base class is overridden. 90TEST_F(BookmarkButtonCellTest, MouseEnterStuff) { 91 scoped_nsobject<BookmarkButtonCell> cell( 92 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 93 // Setting the menu should have no affect since we either share or 94 // dynamically compose the menu given a node. 95 [cell setMenu:[[[BookmarkMenu alloc] initWithTitle:@"foo"] autorelease]]; 96 EXPECT_FALSE([cell menu]); 97 98 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 99 const BookmarkNode* node = model->GetBookmarkBarNode(); 100 [cell setEmpty:NO]; 101 [cell setBookmarkNode:node]; 102 EXPECT_TRUE([cell showsBorderOnlyWhileMouseInside]); 103 EXPECT_TRUE([cell menu]); 104 105 [cell setEmpty:YES]; 106 EXPECT_FALSE([cell.get() showsBorderOnlyWhileMouseInside]); 107 EXPECT_FALSE([cell menu]); 108} 109 110TEST_F(BookmarkButtonCellTest, BookmarkNode) { 111 BookmarkModel& model(*(helper_.profile()->GetBookmarkModel())); 112 scoped_nsobject<BookmarkButtonCell> cell( 113 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 114 115 const BookmarkNode* node = model.GetBookmarkBarNode(); 116 [cell setBookmarkNode:node]; 117 EXPECT_EQ(node, [cell bookmarkNode]); 118 119 node = model.other_node(); 120 [cell setBookmarkNode:node]; 121 EXPECT_EQ(node, [cell bookmarkNode]); 122} 123 124TEST_F(BookmarkButtonCellTest, BookmarkMouseForwarding) { 125 scoped_nsobject<BookmarkButtonCell> cell( 126 [[BookmarkButtonCell alloc] initTextCell:@"Testing"]); 127 scoped_nsobject<ButtonRemembersMouseEnterExit> 128 button([[ButtonRemembersMouseEnterExit alloc] 129 initWithFrame:NSMakeRect(0,0,50,50)]); 130 [button setCell:cell.get()]; 131 EXPECT_EQ(0, button.get()->enters_); 132 EXPECT_EQ(0, button.get()->exits_); 133 NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved 134 location:NSMakePoint(10,10) 135 modifierFlags:0 136 timestamp:0 137 windowNumber:0 138 context:nil 139 eventNumber:0 140 clickCount:0 141 pressure:0]; 142 [cell mouseEntered:event]; 143 EXPECT_TRUE(button.get()->enters_ && !button.get()->exits_); 144 145 for (int i = 0; i < 3; i++) 146 [cell mouseExited:event]; 147 EXPECT_EQ(button.get()->enters_, 1); 148 EXPECT_EQ(button.get()->exits_, 3); 149} 150 151// Confirms a cell created in a nib is initialized properly 152TEST_F(BookmarkButtonCellTest, Awake) { 153 scoped_nsobject<BookmarkButtonCell> cell([[BookmarkButtonCell alloc] init]); 154 [cell awakeFromNib]; 155 EXPECT_EQ(NSLeftTextAlignment, [cell alignment]); 156} 157 158// Subfolder arrow details. 159TEST_F(BookmarkButtonCellTest, FolderArrow) { 160 BookmarkModel* model = helper_.profile()->GetBookmarkModel(); 161 const BookmarkNode* bar = model->GetBookmarkBarNode(); 162 const BookmarkNode* node = model->AddURL(bar, bar->GetChildCount(), 163 ASCIIToUTF16("title"), 164 GURL("http://www.google.com")); 165 scoped_nsobject<BookmarkButtonCell> cell( 166 [[BookmarkButtonCell alloc] initForNode:node 167 contextMenu:nil 168 cellText:@"small" 169 cellImage:nil]); 170 EXPECT_TRUE(cell.get()); 171 172 NSSize size = [cell cellSize]; 173 // sanity check 174 EXPECT_GE(size.width, 2); 175 EXPECT_GE(size.height, 2); 176 177 // Once we turn on arrow drawing make sure there is now room for it. 178 [cell setDrawFolderArrow:YES]; 179 NSSize arrowSize = [cell cellSize]; 180 EXPECT_GT(arrowSize.width, size.width); 181} 182 183} // namespace 184