OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "chrome/browser/ui/cocoa/profiles/avatar_button.h" | |
6 | |
7 #import "base/mac/scoped_nsobject.h" | |
8 #import "ui/events/test/cocoa_test_event_utils.h" | |
9 #import "ui/gfx/test/ui_cocoa_test_helper.h" | |
10 | |
11 @interface AvatarButton (ExposedForTesting) | |
12 - (void)performRightClick; | |
13 @end | |
14 | |
15 @interface AvatarButtonTestObserver : NSObject | |
16 @property BOOL clicked; | |
17 | |
18 - (id)init; | |
19 - (void)buttonRightClicked; | |
20 @end | |
21 | |
22 @implementation AvatarButtonTestObserver | |
23 @synthesize clicked = clicked_; | |
24 | |
25 - (id)init { | |
Robert Sesek
2015/02/20 21:56:06
-alloc will 0 all instance variables, so you can d
anthonyvd
2015/02/23 17:21:53
Done.
| |
26 self.clicked = NO; | |
27 return self; | |
28 } | |
29 - (void)buttonRightClicked { | |
30 self.clicked = YES; | |
31 } | |
32 @end | |
33 | |
34 class AvatarButtonTest : public ui::CocoaTest { | |
35 public: | |
36 AvatarButtonTest() { | |
37 NSRect content_frame = [[test_window() contentView] frame]; | |
38 base::scoped_nsobject<AvatarButton> button( | |
39 [[AvatarButton alloc] initWithFrame:content_frame]); | |
40 button_ = button.get(); | |
41 [[test_window() contentView] addSubview:button_]; | |
42 } | |
43 | |
44 AvatarButton* button_; | |
45 }; | |
46 | |
47 TEST_F(AvatarButtonTest, RightClick) { | |
48 base::scoped_nsobject<AvatarButtonTestObserver> observer( | |
49 [[AvatarButtonTestObserver alloc] init]); | |
50 [button_ setTarget:observer.get()]; | |
51 [button_ setRightAction:@selector(buttonRightClicked)]; | |
52 | |
53 ASSERT_FALSE(observer.get().clicked); | |
54 | |
55 [button_ performRightClick]; | |
56 ASSERT_TRUE(observer.get().clicked); | |
57 } | |
58 | |
59 TEST_F(AvatarButtonTest, RightClickInView) { | |
60 base::scoped_nsobject<AvatarButtonTestObserver> observer( | |
61 [[AvatarButtonTestObserver alloc] init]); | |
62 [button_ setTarget:observer.get()]; | |
63 [button_ setRightAction:@selector(buttonRightClicked)]; | |
64 | |
65 ASSERT_FALSE(observer.get().clicked); | |
66 | |
67 std::pair<NSEvent*, NSEvent*> events = | |
68 cocoa_test_event_utils::RightMouseClickInView(button_, 1); | |
69 | |
70 [NSApp postEvent:events.second atStart:YES]; | |
71 [NSApp sendEvent:events.first]; | |
72 | |
73 ASSERT_TRUE(observer.get().clicked); | |
74 } | |
75 | |
76 TEST_F(AvatarButtonTest, RightMouseUpOutOfView) { | |
77 base::scoped_nsobject<AvatarButtonTestObserver> observer( | |
78 [[AvatarButtonTestObserver alloc] init]); | |
79 [button_ setTarget:observer.get()]; | |
80 [button_ setRightAction:@selector(buttonRightClicked)]; | |
81 | |
82 ASSERT_FALSE(observer.get().clicked); | |
83 | |
84 const NSRect bounds = [button_ convertRect:[button_ bounds] toView:nil]; | |
85 const NSPoint downLocation = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); | |
86 NSEvent* down = cocoa_test_event_utils::MouseEventAtPointInWindow( | |
87 downLocation, NSRightMouseDown, [button_ window], 1); | |
88 | |
89 const NSPoint upLocation = NSMakePoint(downLocation.x + bounds.size.width, | |
90 downLocation.y + bounds.size.height); | |
91 NSEvent* up = cocoa_test_event_utils::MouseEventAtPointInWindow( | |
92 upLocation, NSRightMouseUp, [button_ window], 1); | |
93 | |
94 [NSApp postEvent:up atStart:YES]; | |
95 [NSApp sendEvent:down]; | |
96 | |
97 ASSERT_FALSE(observer.get().clicked); | |
98 } | |
OLD | NEW |