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 #import <Cocoa/Cocoa.h> 6 7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 8 #import "chrome/browser/ui/cocoa/profile_menu_button.h" 9 #import "chrome/browser/ui/cocoa/test_event_utils.h" 10 #import "testing/gtest_mac.h" 11 12 class ProfileMenuButtonTest : public CocoaTest { 13 public: 14 ProfileMenuButtonTest() { 15 scoped_nsobject<ProfileMenuButton> button([[ProfileMenuButton alloc] 16 initWithFrame:NSMakeRect(50, 50, 100, 100) 17 pullsDown:NO]); 18 button_ = button.get(); 19 [[test_window() contentView] addSubview:button_]; 20 } 21 22 ProfileMenuButton* button_; 23 }; 24 25 // A stub to check that popUpContextMenu:withEvent:forView: is called. 26 @interface ProfileShowMenuHandler : NSObject { 27 int showMenuCount_; 28 } 29 30 @property(assign, nonatomic) int showMenuCount; 31 32 - (void)popUpContextMenu:(NSMenu*)menu 33 withEvent:(NSEvent*)event 34 forView:(NSView*)view; 35 36 @end 37 38 @implementation ProfileShowMenuHandler 39 40 @synthesize showMenuCount = showMenuCount_; 41 42 - (void)popUpContextMenu:(NSMenu*)menu 43 withEvent:(NSEvent*)event 44 forView:(NSView*)view { 45 showMenuCount_++; 46 } 47 48 @end 49 50 51 TEST_F(ProfileMenuButtonTest, ControlSize) { 52 scoped_nsobject<ProfileMenuButton> button([[ProfileMenuButton alloc] 53 initWithFrame:NSZeroRect 54 pullsDown:NO]); 55 56 NSSize minSize = [button minControlSize]; 57 EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize])); 58 59 [button setProfileDisplayName:@"Test"]; 60 EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize])); 61 EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize])); 62 63 [button setShouldShowProfileDisplayName:YES]; 64 EXPECT_TRUE(NSEqualSizes(minSize, [button minControlSize])); 65 EXPECT_GT([button desiredControlSize].height, minSize.height); 66 EXPECT_GT([button desiredControlSize].width, minSize.width); 67 68 [button setShouldShowProfileDisplayName:NO]; 69 EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize])); 70 EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize])); 71 } 72 73 // Tests display, add/remove. 74 TEST_VIEW(ProfileMenuButtonTest, button_); 75 76 TEST_F(ProfileMenuButtonTest, HitTest) { 77 NSRect mouseRect = NSInsetRect([button_ frame], 1, 1); 78 NSPoint topRight = NSMakePoint(NSMaxX(mouseRect), NSMaxY(mouseRect)); 79 NSPoint bottomRight = NSMakePoint(NSMaxX(mouseRect), NSMinY(mouseRect)); 80 NSPoint outsidePoint = NSOffsetRect(mouseRect, -10, -10).origin; 81 82 // Without profile display name. Only topRight should hit. 83 EXPECT_NSEQ([button_ hitTest:topRight], button_); 84 EXPECT_NSEQ([button_ hitTest:bottomRight], NULL); 85 EXPECT_NSEQ([button_ hitTest:outsidePoint], NULL); 86 87 // With profile display name. The profile display name should not hit. 88 [button_ setProfileDisplayName:@"Test"]; 89 [button_ setShouldShowProfileDisplayName:YES]; 90 EXPECT_NSEQ([button_ hitTest:topRight], button_); 91 EXPECT_NSEQ([button_ hitTest:bottomRight], NULL); 92 EXPECT_NSEQ([button_ hitTest:outsidePoint], NULL); 93 } 94 95 // Test drawing, mostly to ensure nothing leaks or crashes. 96 TEST_F(ProfileMenuButtonTest, Display) { 97 // With profile display name. 98 [button_ display]; 99 100 // With profile display name. 101 [button_ setProfileDisplayName:@"Test"]; 102 [button_ setShouldShowProfileDisplayName:YES]; 103 [button_ display]; 104 } 105 106 // Checks that a menu is displayed on mouse down. Also makes sure that 107 // nothing leaks or crashes when displaying the button in its pressed state. 108 TEST_F(ProfileMenuButtonTest, MenuTest) { 109 scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]); 110 [button_ setMenu:menu]; 111 112 // Trigger a mouse down to show the menu. 113 NSPoint point = NSMakePoint(NSMaxX([button_ bounds]) - 1, 114 NSMaxY([button_ bounds]) - 1); 115 point = [button_ convertPointToBase:point]; 116 NSEvent* downEvent = 117 test_event_utils::LeftMouseDownAtPointInWindow(point, test_window()); 118 scoped_nsobject<ProfileShowMenuHandler> showMenuHandler( 119 [[ProfileShowMenuHandler alloc] init]); 120 [button_ mouseDown:downEvent 121 withShowMenuTarget:showMenuHandler]; 122 123 EXPECT_EQ(1, [showMenuHandler showMenuCount]); 124 } 125