summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/ui/cocoa')
-rw-r--r--chrome/browser/ui/cocoa/profile_menu_button.h4
-rw-r--r--chrome/browser/ui/cocoa/profile_menu_button.mm17
-rw-r--r--chrome/browser/ui/cocoa/profile_menu_button_unittest.mm81
3 files changed, 50 insertions, 52 deletions
diff --git a/chrome/browser/ui/cocoa/profile_menu_button.h b/chrome/browser/ui/cocoa/profile_menu_button.h
index dac632a..ca5dc82 100644
--- a/chrome/browser/ui/cocoa/profile_menu_button.h
+++ b/chrome/browser/ui/cocoa/profile_menu_button.h
@@ -30,6 +30,10 @@
// Gets the minimum size that the control should be resized to.
- (NSSize)minControlSize;
+// Public for testing.
+- (void) mouseDown:(NSEvent*)event
+ withShowMenuTarget:(id)target;
+
@end
#endif // CHROME_BROWSER_UI_COCOA_PROFILE_MENU_BUTTON_H_
diff --git a/chrome/browser/ui/cocoa/profile_menu_button.mm b/chrome/browser/ui/cocoa/profile_menu_button.mm
index 4162b1f..db2f8ee 100644
--- a/chrome/browser/ui/cocoa/profile_menu_button.mm
+++ b/chrome/browser/ui/cocoa/profile_menu_button.mm
@@ -6,6 +6,7 @@
#include <algorithm>
+#include "base/logging.h"
#import "third_party/GTM/AppKit/GTMFadeTruncatingTextFieldCell.h"
namespace {
@@ -330,7 +331,8 @@ NSColor* GetBlackWithAlpha(CGFloat alpha) {
return [self convertPoint:menuPos toView:nil];
}
-- (void)mouseDown:(NSEvent*)event {
+- (void) mouseDown:(NSEvent*)event
+ withShowMenuTarget:(id)target {
if (![self menu]) {
[super mouseDown:event];
return;
@@ -354,13 +356,20 @@ NSColor* GetBlackWithAlpha(CGFloat alpha) {
eventNumber:[event eventNumber]
clickCount:[event clickCount]
pressure:[event pressure]];
- [NSMenu popUpContextMenu:[self menu]
- withEvent:fakeEvent
- forView:self];
+ DCHECK([target respondsToSelector:
+ @selector(popUpContextMenu:withEvent:forView:)]);
+ [target popUpContextMenu:[self menu]
+ withEvent:fakeEvent
+ forView:self];
[self highlight:NO];
}
+- (void)mouseDown:(NSEvent*)event {
+ [self mouseDown:event
+ withShowMenuTarget:[NSMenu class]];
+}
+
- (NSSize)desiredControlSize {
NSSize size = [self tabRect].size;
diff --git a/chrome/browser/ui/cocoa/profile_menu_button_unittest.mm b/chrome/browser/ui/cocoa/profile_menu_button_unittest.mm
index 033dde2..0b96563 100644
--- a/chrome/browser/ui/cocoa/profile_menu_button_unittest.mm
+++ b/chrome/browser/ui/cocoa/profile_menu_button_unittest.mm
@@ -4,12 +4,10 @@
#import <Cocoa/Cocoa.h>
-#import "base/mac/cocoa_protocols.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/profile_menu_button.h"
#import "chrome/browser/ui/cocoa/test_event_utils.h"
#import "testing/gtest_mac.h"
-#import "third_party/ocmock/OCMock/OCMock.h"
class ProfileMenuButtonTest : public CocoaTest {
public:
@@ -24,6 +22,32 @@ class ProfileMenuButtonTest : public CocoaTest {
ProfileMenuButton* button_;
};
+// A stub to check that popUpContextMenu:withEvent:forView: is called.
+@interface ProfileShowMenuHandler : NSObject {
+ int showMenuCount_;
+}
+
+@property(assign, nonatomic) int showMenuCount;
+
+- (void)popUpContextMenu:(NSMenu*)menu
+ withEvent:(NSEvent*)event
+ forView:(NSView*)view;
+
+@end
+
+@implementation ProfileShowMenuHandler
+
+@synthesize showMenuCount = showMenuCount_;
+
+- (void)popUpContextMenu:(NSMenu*)menu
+ withEvent:(NSEvent*)event
+ forView:(NSView*)view {
+ showMenuCount_++;
+}
+
+@end
+
+
TEST_F(ProfileMenuButtonTest, ControlSize) {
scoped_nsobject<ProfileMenuButton> button([[ProfileMenuButton alloc]
initWithFrame:NSZeroRect
@@ -46,41 +70,6 @@ TEST_F(ProfileMenuButtonTest, ControlSize) {
EXPECT_TRUE(NSEqualSizes(minSize, [button desiredControlSize]));
}
-// A menu delegate that will count the number open/close calls it recieves.
-// The delegate will also automatically close the menu after it opens.
-@interface ProfileMenuDelegate : NSObject<NSMenuDelegate> {
- int menuOpenCount_;
- int menuCloseCount_;
-}
-
-@property(assign, nonatomic) int menuOpenCount;
-@property(assign, nonatomic) int menuCloseCount;
-
-@end
-
-@implementation ProfileMenuDelegate
-
-@synthesize menuOpenCount = menuOpenCount_;
-@synthesize menuCloseCount = menuCloseCount_;
-
-- (void)menuWillOpen:(NSMenu*)menu {
- ++menuOpenCount_;
- // Queue a message asking the menu to close.
- NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode,
- NSDefaultRunLoopMode,
- nil];
- [menu performSelector:@selector(cancelTrackingWithoutAnimation)
- withObject:nil
- afterDelay:0
- inModes:modes];
-}
-
-- (void)menuDidClose:(NSMenu*)menu {
- ++menuCloseCount_;
-}
-
-@end
-
// Tests display, add/remove.
TEST_VIEW(ProfileMenuButtonTest, button_);
@@ -114,26 +103,22 @@ TEST_F(ProfileMenuButtonTest, Display) {
[button_ display];
}
+// Checks that a menu is displayed on mouse down. Also makes sure that
+// nothing leaks or crashes when displaying the button in its pressed state.
TEST_F(ProfileMenuButtonTest, MenuTest) {
scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
[button_ setMenu:menu];
- // Hook into menu events.
- scoped_nsobject<ProfileMenuDelegate> delegate(
- [[ProfileMenuDelegate alloc] init]);
- [[button_ menu] setDelegate:delegate];
- EXPECT_EQ([delegate menuOpenCount], 0);
- EXPECT_EQ([delegate menuCloseCount], 0);
-
// Trigger a mouse down to show the menu.
NSPoint point = NSMakePoint(NSMaxX([button_ bounds]) - 1,
NSMaxY([button_ bounds]) - 1);
point = [button_ convertPointToBase:point];
NSEvent* downEvent =
test_event_utils::LeftMouseDownAtPointInWindow(point, test_window());
- [button_ mouseDown:downEvent];
+ scoped_nsobject<ProfileShowMenuHandler> showMenuHandler(
+ [[ProfileShowMenuHandler alloc] init]);
+ [button_ mouseDown:downEvent
+ withShowMenuTarget:showMenuHandler];
- // Verify that the menu was shown.
- EXPECT_EQ([delegate menuOpenCount], 1);
- EXPECT_EQ([delegate menuCloseCount], 1);
+ EXPECT_EQ(1, [showMenuHandler showMenuCount]);
}