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