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 - (void)buttonRightClicked; |
| 19 @end |
| 20 |
| 21 @implementation AvatarButtonTestObserver |
| 22 @synthesize clicked = clicked_; |
| 23 |
| 24 - (void)buttonRightClicked { |
| 25 self.clicked = YES; |
| 26 } |
| 27 @end |
| 28 |
| 29 class AvatarButtonTest : public ui::CocoaTest { |
| 30 public: |
| 31 AvatarButtonTest() { |
| 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_]; |
| 37 } |
| 38 |
| 39 AvatarButton* button_; |
| 40 }; |
| 41 |
| 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)]; |
| 47 |
| 48 ASSERT_FALSE(observer.get().clicked); |
| 49 |
| 50 [button_ performRightClick]; |
| 51 ASSERT_TRUE(observer.get().clicked); |
| 52 } |
| 53 |
| 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)]; |
| 59 |
| 60 ASSERT_FALSE(observer.get().clicked); |
| 61 |
| 62 std::pair<NSEvent*, NSEvent*> events = |
| 63 cocoa_test_event_utils::RightMouseClickInView(button_, 1); |
| 64 |
| 65 [NSApp postEvent:events.second atStart:YES]; |
| 66 [NSApp sendEvent:events.first]; |
| 67 |
| 68 ASSERT_TRUE(observer.get().clicked); |
| 69 } |
| 70 |
| 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)]; |
| 76 |
| 77 ASSERT_FALSE(observer.get().clicked); |
| 78 |
| 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); |
| 83 |
| 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); |
| 88 |
| 89 [NSApp postEvent:up atStart:YES]; |
| 90 [NSApp sendEvent:down]; |
| 91 |
| 92 ASSERT_FALSE(observer.get().clicked); |
| 93 } |
OLD | NEW |