diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-03 02:03:08 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-03 02:03:08 +0000 |
commit | d71d672dc432e262ccbaca5e9afd7341a0722c5d (patch) | |
tree | d7a1a6ad835400336bffd1f66c9ba77f88b4c91f | |
parent | 223c449d19eb5d889bc828e011c1a23e5d52b4c9 (diff) | |
download | chromium_src-d71d672dc432e262ccbaca5e9afd7341a0722c5d.zip chromium_src-d71d672dc432e262ccbaca5e9afd7341a0722c5d.tar.gz chromium_src-d71d672dc432e262ccbaca5e9afd7341a0722c5d.tar.bz2 |
[Mac] Deregister for the old windowWillClose/windowDidResignMain notifications when moving the BookmarkBar between windows.
BUG=37228
TEST=Go fullscreen and back. Find a bookmarkbar folder with a subfolder. Hover over the subfolder to open it, then move the mouse away. Should not crash.
Review URL: http://codereview.chromium.org/661425
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40480 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller.mm | 9 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller_unittest.mm | 78 |
2 files changed, 87 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/bookmark_bar_controller.mm b/chrome/browser/cocoa/bookmark_bar_controller.mm index 6d376f37..9bbf8c8 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller.mm +++ b/chrome/browser/cocoa/bookmark_bar_controller.mm @@ -318,6 +318,15 @@ const NSTimeInterval kBookmarkBarAnimationDuration = 0.12; // earlier (such as in awakeFromNib). - (void)viewDidMoveToWindow { NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; + + // Remove any existing notifications before registering for new ones. + [defaultCenter removeObserver:self + name:NSWindowWillCloseNotification + object:nil]; + [defaultCenter removeObserver:self + name:NSWindowDidResignKeyNotification + object:nil]; + [defaultCenter addObserver:self selector:@selector(parentWindowWillClose:) name:NSWindowWillCloseNotification diff --git a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm index 10fa215..9b08e90 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm +++ b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm @@ -83,6 +83,32 @@ @end +// Remembers if a notification callback was called. +@interface BookmarkBarControllerNotificationPong : BookmarkBarControllerNoOpen { + BOOL windowWillCloseReceived_; + BOOL windowDidResignKeyReceived_; +} +@property(readonly) BOOL windowWillCloseReceived; +@property(readonly) BOOL windowDidResignKeyReceived; +@end + +@implementation BookmarkBarControllerNotificationPong +@synthesize windowWillCloseReceived = windowWillCloseReceived_; +@synthesize windowDidResignKeyReceived = windowDidResignKeyReceived_; + +// Override NSNotificationCenter callback. +- (void)parentWindowWillClose:(NSNotification*)notification { + windowWillCloseReceived_ = YES; +} + +// NSNotificationCenter callback. +- (void)parentWindowDidResignKey:(NSNotification*)notification { + windowDidResignKeyReceived_ = YES; +} +@end + + + class FakeTheme : public ThemeProvider { public: FakeTheme(NSColor* color) : color_(color) { } @@ -1066,6 +1092,58 @@ TEST_F(BookmarkBarControllerTest, DropDestination) { } } +class BookmarkBarControllerNotificationTest : public CocoaTest { + public: + BookmarkBarControllerNotificationTest() { + resizeDelegate_.reset([[ViewResizerPong alloc] init]); + NSRect parent_frame = NSMakeRect(0, 0, 800, 50); + parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]); + [parent_view_ setHidden:YES]; + bar_.reset( + [[BookmarkBarControllerNotificationPong alloc] + initWithBrowser:helper_.browser() + initialWidth:NSWidth(parent_frame) + delegate:nil + resizeDelegate:resizeDelegate_.get()]); + + // Force loading of the nib. + [bar_ view]; + // Awkwardness to look like we've been installed. + [parent_view_ addSubview:[bar_ view]]; + NSRect frame = [[[bar_ view] superview] frame]; + frame.origin.y = 100; + [[[bar_ view] superview] setFrame:frame]; + + // Do not add the bar to a window, yet. + } + + BrowserTestHelper helper_; + scoped_nsobject<NSView> parent_view_; + scoped_nsobject<ViewResizerPong> resizeDelegate_; + scoped_nsobject<BookmarkBarControllerNotificationPong> bar_; +}; + +TEST_F(BookmarkBarControllerNotificationTest, DeregistersForNotifications) { + NSWindow* window = [[CocoaTestHelperWindow alloc] init]; + [window setReleasedWhenClosed:YES]; + + // First add the bookmark bar to the temp window, then to another window. + [[window contentView] addSubview:parent_view_]; + [[test_window() contentView] addSubview:parent_view_]; + + // Post a fake windowDidResignKey notification for the temp window and make + // sure the bookmark bar controller wasn't listening. + [[NSNotificationCenter defaultCenter] + postNotificationName:NSWindowDidResignKeyNotification + object:window]; + EXPECT_FALSE([bar_ windowDidResignKeyReceived]); + + // Close the temp window and make sure no notification was received. + [window close]; + EXPECT_FALSE([bar_ windowWillCloseReceived]); +} + + // TODO(jrg): draggingEntered: and draggingExited: trigger timers so // they are hard to test. Factor out "fire timers" into routines // which can be overridden to fire immediately to make behavior |