Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(942)

Unified Diff: chrome/browser/ui/cocoa/profiles/avatar_button_unittest.mm

Issue 916523003: Bring up fast user switcher on right-click of the avatar menu on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unit testing Avatar Button Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f9941e13cae7d374aa55b1067709f96507fe1271
--- /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_;
+
+- (id)init {
+ 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_);
+}

Powered by Google App Engine
This is Rietveld 408576698