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.
5 #import "chrome/browser/ui/cocoa/profiles/avatar_button.h"
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"
11 @interface AvatarButton (ExposedForTesting)
12 - (void)performRightClick;
15 @interface AvatarButtonTestObserver : NSObject
16 @property BOOL clicked;
18 - (void)buttonRightClicked;
21 @implementation AvatarButtonTestObserver
22 @synthesize clicked = clicked_;
24 - (void)buttonRightClicked {
29 class AvatarButtonTest : public ui::CocoaTest {
32 NSRect content_frame = [[test_window() contentView] frame];
33 base::scoped_nsobject<AvatarButton> button(
34 [[AvatarButton alloc] initWithFrame:content_frame]);
35 button_ = button.get();
36 [[test_window() contentView] addSubview:button_];
39 AvatarButton* button_;
42 TEST_F(AvatarButtonTest, RightClick) {
43 base::scoped_nsobject<AvatarButtonTestObserver> observer(
44 [[AvatarButtonTestObserver alloc] init]);
45 [button_ setTarget:observer.get()];
46 [button_ setRightAction:@selector(buttonRightClicked)];
48 ASSERT_FALSE(observer.get().clicked);
50 [button_ performRightClick];
51 ASSERT_TRUE(observer.get().clicked);
54 TEST_F(AvatarButtonTest, RightClickInView) {
55 base::scoped_nsobject<AvatarButtonTestObserver> observer(
56 [[AvatarButtonTestObserver alloc] init]);
57 [button_ setTarget:observer.get()];
58 [button_ setRightAction:@selector(buttonRightClicked)];
60 ASSERT_FALSE(observer.get().clicked);
62 std::pair<NSEvent*, NSEvent*> events =
63 cocoa_test_event_utils::RightMouseClickInView(button_, 1);
65 [NSApp postEvent:events.second atStart:YES];
66 [NSApp sendEvent:events.first];
68 ASSERT_TRUE(observer.get().clicked);
71 TEST_F(AvatarButtonTest, RightMouseUpOutOfView) {
72 base::scoped_nsobject<AvatarButtonTestObserver> observer(
73 [[AvatarButtonTestObserver alloc] init]);
74 [button_ setTarget:observer.get()];
75 [button_ setRightAction:@selector(buttonRightClicked)];
77 ASSERT_FALSE(observer.get().clicked);
79 const NSRect bounds = [button_ convertRect:[button_ bounds] toView:nil];
80 const NSPoint downLocation = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
81 NSEvent* down = cocoa_test_event_utils::MouseEventAtPointInWindow(
82 downLocation, NSRightMouseDown, [button_ window], 1);
84 const NSPoint upLocation = NSMakePoint(downLocation.x + bounds.size.width,
85 downLocation.y + bounds.size.height);
86 NSEvent* up = cocoa_test_event_utils::MouseEventAtPointInWindow(
87 upLocation, NSRightMouseUp, [button_ window], 1);
89 [NSApp postEvent:up atStart:YES];
90 [NSApp sendEvent:down];
92 ASSERT_FALSE(observer.get().clicked);