Chromium Code Reviews| Index: chrome/browser/ui/cocoa/profiles/avatar_button_unittest.mm |
| diff --git a/chrome/browser/ui/cocoa/profiles/avatar_button_unittest.mm b/chrome/browser/ui/cocoa/profiles/avatar_button_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..516add0f72bf7bcd5412a1d3023a5d570b2d508a |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/profiles/avatar_button_unittest.mm |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2015 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 "chrome/browser/ui/cocoa/profiles/avatar_button.h" |
| + |
| +#import "base/mac/scoped_nsobject.h" |
| +#import "ui/events/test/cocoa_test_event_utils.h" |
| +#import "ui/gfx/test/ui_cocoa_test_helper.h" |
| + |
| +@interface AvatarButton (ExposedForTesting) |
| +- (void)performRightClick; |
| +@end |
| + |
| +@interface AvatarButtonTestObserver : NSObject |
| +@property BOOL clicked; |
| + |
| +- (id)init; |
| +- (void)buttonRightClicked; |
| +@end |
| + |
| +@implementation AvatarButtonTestObserver |
| +@synthesize clicked = clicked_; |
| + |
| +- (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.
|
| + self.clicked = NO; |
| + return self; |
| +} |
| +- (void)buttonRightClicked { |
| + self.clicked = YES; |
| +} |
| +@end |
| + |
| +class AvatarButtonTest : public ui::CocoaTest { |
| + public: |
| + AvatarButtonTest() { |
| + NSRect content_frame = [[test_window() contentView] frame]; |
| + base::scoped_nsobject<AvatarButton> button( |
| + [[AvatarButton alloc] initWithFrame:content_frame]); |
| + button_ = button.get(); |
| + [[test_window() contentView] addSubview:button_]; |
| + } |
| + |
| + AvatarButton* button_; |
| +}; |
| + |
| +TEST_F(AvatarButtonTest, RightClick) { |
| + base::scoped_nsobject<AvatarButtonTestObserver> observer( |
| + [[AvatarButtonTestObserver alloc] init]); |
| + [button_ setTarget:observer.get()]; |
| + [button_ setRightAction:@selector(buttonRightClicked)]; |
| + |
| + ASSERT_FALSE(observer.get().clicked); |
| + |
| + [button_ performRightClick]; |
| + ASSERT_TRUE(observer.get().clicked); |
| +} |
| + |
| +TEST_F(AvatarButtonTest, RightClickInView) { |
| + base::scoped_nsobject<AvatarButtonTestObserver> observer( |
| + [[AvatarButtonTestObserver alloc] init]); |
| + [button_ setTarget:observer.get()]; |
| + [button_ setRightAction:@selector(buttonRightClicked)]; |
| + |
| + ASSERT_FALSE(observer.get().clicked); |
| + |
| + std::pair<NSEvent*, NSEvent*> events = |
| + cocoa_test_event_utils::RightMouseClickInView(button_, 1); |
| + |
| + [NSApp postEvent:events.second atStart:YES]; |
| + [NSApp sendEvent:events.first]; |
| + |
| + ASSERT_TRUE(observer.get().clicked); |
| +} |
| + |
| +TEST_F(AvatarButtonTest, RightMouseUpOutOfView) { |
| + base::scoped_nsobject<AvatarButtonTestObserver> observer( |
| + [[AvatarButtonTestObserver alloc] init]); |
| + [button_ setTarget:observer.get()]; |
| + [button_ setRightAction:@selector(buttonRightClicked)]; |
| + |
| + ASSERT_FALSE(observer.get().clicked); |
| + |
| + const NSRect bounds = [button_ convertRect:[button_ bounds] toView:nil]; |
| + const NSPoint downLocation = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); |
| + NSEvent* down = cocoa_test_event_utils::MouseEventAtPointInWindow( |
| + downLocation, NSRightMouseDown, [button_ window], 1); |
| + |
| + const NSPoint upLocation = NSMakePoint(downLocation.x + bounds.size.width, |
| + downLocation.y + bounds.size.height); |
| + NSEvent* up = cocoa_test_event_utils::MouseEventAtPointInWindow( |
| + upLocation, NSRightMouseUp, [button_ window], 1); |
| + |
| + [NSApp postEvent:up atStart:YES]; |
| + [NSApp sendEvent:down]; |
| + |
| + ASSERT_FALSE(observer.get().clicked); |
| +} |