diff options
author | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-06 20:47:09 +0000 |
---|---|---|
committer | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-06 20:47:09 +0000 |
commit | e79d7dc73530396c27806a6794f4fcd65262926f (patch) | |
tree | 4a0b947c47882fb922ecc64a8b6286c3f9396ade | |
parent | 1299d20ea6f64717b3e66cd96e2e34a42afbffd6 (diff) | |
download | chromium_src-e79d7dc73530396c27806a6794f4fcd65262926f.zip chromium_src-e79d7dc73530396c27806a6794f4fcd65262926f.tar.gz chromium_src-e79d7dc73530396c27806a6794f4fcd65262926f.tar.bz2 |
Fix leak in ProfileMenuButtonTest.MenuTest
Valgrind was reporting a leak in -[ProfileMenuDelegate menuWillOpen:].
I wasn't able to reproduce this on my machine with a Debug or Release build.
As a workaround I'm simply removing that part of the test. The test now just checks that popUpContextMenu:withEvent:forView: is called. This is still pretty good since it tests the mouse down code and the pressed state drawing code.
BUG=77910
TEST=Wasn't able to reproduce the same leak but I did get some other leaks in the menu code. Applied my patch and verified that valgrind ran with no errors.
Review URL: http://codereview.chromium.org/6677169
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80687 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/cocoa/profile_menu_button.h | 4 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/profile_menu_button.mm | 17 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/profile_menu_button_unittest.mm | 81 | ||||
-rw-r--r-- | tools/valgrind/memcheck/suppressions_mac.txt | 23 |
4 files changed, 50 insertions, 75 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]); } diff --git a/tools/valgrind/memcheck/suppressions_mac.txt b/tools/valgrind/memcheck/suppressions_mac.txt index 5888c50..05c4b01 100644 --- a/tools/valgrind/memcheck/suppressions_mac.txt +++ b/tools/valgrind/memcheck/suppressions_mac.txt @@ -1569,26 +1569,3 @@ fun:_ZN20SelectFileDialogImpl10SelectFileEN16SelectFileDialog4TypeERKSbItN4base20string16_char_traitsESaItEERK8FilePathPKNS0_12FileTypeInfoEiRKSsP8NSWindowPv fun:_ZN15DownloadManager24OnPathExistenceAvailableEP18DownloadCreateInfo } -{ - bug_77910 - Memcheck:Leak - ... - fun:-[NSObject(NSDelayedPerforming) performSelector:withObject:afterDelay:inModes:] - fun:-[ProfileMenuDelegate menuWillOpen:] - fun:-[NSMenu _sendMenuOpeningNotification] - fun:AppKitApplicationEventHandler - fun:_Z23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec - fun:_Z30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec - fun:SendEventToEventTargetWithOptions - fun:_Z15SendMenuOpeningP14MenuSelectDataP8MenuDatadmP14__CFDictionaryhPh - fun:_Z19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttmS4_S4_PK10__CFStringPP13OpaqueMenuRefPt - fun:_HandlePopUpMenuSelection7 - fun:_NSPopUpCarbonMenu3 - fun:_NSPopUpCarbonMenu2 - fun:_NSPopUpCarbonMenu1 - fun:-[NSCarbonMenuImpl _popUpContextMenu:withEvent:forView:withFont:] - fun:-[NSMenu _popUpContextMenu:withEvent:forView:withFont:] - fun:-[NSMenu _popUpContextMenu:withEvent:forView:] - fun:-[ProfileMenuButton mouseDown:] - fun:_ZN35ProfileMenuButtonTest_MenuTest_Test8TestBodyEv -} |