diff options
author | johnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 21:10:43 +0000 |
---|---|---|
committer | johnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 21:10:43 +0000 |
commit | 437d1c7f6cf70dbdc3d008745d974714db7dd68a (patch) | |
tree | e4ef9e2aee1cf1e137b9d1feee79db55d05b2c6d | |
parent | 09f89648b7f355fd5b07dcb67c9515f6f47bb0d9 (diff) | |
download | chromium_src-437d1c7f6cf70dbdc3d008745d974714db7dd68a.zip chromium_src-437d1c7f6cf70dbdc3d008745d974714db7dd68a.tar.gz chromium_src-437d1c7f6cf70dbdc3d008745d974714db7dd68a.tar.bz2 |
Fix memory leaks in the balloon controller unit test by loading the NIB in the correct test-friendly way, which allows the windows to be closed correctly and the cleanup code to run as expected.
BUG=49590
TEST=unittests
Review URL: http://codereview.chromium.org/2825073
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54018 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/cocoa/notifications/balloon_controller.mm | 23 | ||||
-rw-r--r-- | chrome/browser/cocoa/notifications/balloon_controller_unittest.mm | 34 |
2 files changed, 36 insertions, 21 deletions
diff --git a/chrome/browser/cocoa/notifications/balloon_controller.mm b/chrome/browser/cocoa/notifications/balloon_controller.mm index 08d47a6..637cfc4 100644 --- a/chrome/browser/cocoa/notifications/balloon_controller.mm +++ b/chrome/browser/cocoa/notifications/balloon_controller.mm @@ -7,6 +7,7 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" #import "base/cocoa_protocols_mac.h" +#include "base/mac_util.h" #import "base/scoped_nsobject.h" #include "base/utf_string_conversions.h" #import "chrome/browser/cocoa/menu_controller.h" @@ -33,7 +34,10 @@ const int kRightMargin = 1; @implementation BalloonController - (id)initWithBalloon:(Balloon*)balloon { - if ((self = [super initWithWindowNibName:@"Notification"])) { + NSString* nibpath = + [mac_util::MainAppBundle() pathForResource:@"Notification" + ofType:@"nib"]; + if ((self = [super initWithWindowNibPath:nibpath owner:self])) { balloon_ = balloon; [self initializeHost]; menuModel_.reset(new NotificationOptionsMenuModel(balloon)); @@ -57,11 +61,14 @@ const int kRightMargin = 1; WideToUTF16(balloon_->notification().display_source())); [originLabel_ setStringValue:sourceLabelText]; - gfx::NativeView contents = htmlContents_->native_view(); - [contents setFrame:NSMakeRect(kLeftMargin, kTopMargin, 0, 0)]; - [[htmlContainer_ superview] addSubview:contents - positioned:NSWindowBelow - relativeTo:nil]; + // This condition is false in unit tests which have no RVH. + if (htmlContents_.get()) { + gfx::NativeView contents = htmlContents_->native_view(); + [contents setFrame:NSMakeRect(kLeftMargin, kTopMargin, 0, 0)]; + [[htmlContainer_ superview] addSubview:contents + positioned:NSWindowBelow + relativeTo:nil]; + } } - (IBAction)optionsButtonPressed:(id)sender { @@ -104,7 +111,9 @@ const int kRightMargin = 1; int w = [self desiredTotalWidth]; int h = [self desiredTotalHeight]; - htmlContents_->UpdateActualSize(balloon_->content_size()); + if (htmlContents_.get()) + htmlContents_->UpdateActualSize(balloon_->content_size()); + [[self window] setFrame:NSMakeRect(x, y, w, h) display:YES animate:YES]; diff --git a/chrome/browser/cocoa/notifications/balloon_controller_unittest.mm b/chrome/browser/cocoa/notifications/balloon_controller_unittest.mm index 56f2008..7433b84 100644 --- a/chrome/browser/cocoa/notifications/balloon_controller_unittest.mm +++ b/chrome/browser/cocoa/notifications/balloon_controller_unittest.mm @@ -13,6 +13,16 @@ #include "chrome/test/testing_profile.h" #import "third_party/ocmock/OCMock/OCMock.h" +// Subclass balloon controller and mock out the initialization of the RVH. +@interface TestBalloonController : BalloonController { +} +- (void)initializeHost; +@end + +@implementation TestBalloonController +- (void)initializeHost {} +@end + namespace { // Use a dummy balloon collection for testing. @@ -67,18 +77,14 @@ TEST_F(BalloonControllerTest, ShowAndCloseTest) { new NotificationObjectProxy(-1, -1, -1, false)); scoped_ptr<Balloon> balloon( new Balloon(n, profile_.get(), collection_.get())); + balloon->SetPosition(gfx::Point(1, 1), false); + balloon->set_content_size(gfx::Size(100, 100)); - BalloonController* controller = [BalloonController alloc]; - - id mock = [OCMockObject partialMockForObject:controller]; - [[mock expect] initializeHost]; + BalloonController* controller = + [[TestBalloonController alloc] initWithBalloon:balloon.get()]; - [controller initWithBalloon:balloon.get()]; [controller showWindow:nil]; [controller closeBalloon:YES]; - - [mock verify]; - [controller release]; } TEST_F(BalloonControllerTest, SizesTest) { @@ -87,19 +93,19 @@ TEST_F(BalloonControllerTest, SizesTest) { new NotificationObjectProxy(-1, -1, -1, false)); scoped_ptr<Balloon> balloon( new Balloon(n, profile_.get(), collection_.get())); + balloon->SetPosition(gfx::Point(1, 1), false); balloon->set_content_size(gfx::Size(100, 100)); - BalloonController* controller = [BalloonController alloc]; - - id mock = [OCMockObject partialMockForObject:controller]; - [[mock expect] initializeHost]; + BalloonController* controller = + [[TestBalloonController alloc] initWithBalloon:balloon.get()]; [controller initWithBalloon:balloon.get()]; + [controller showWindow:nil]; EXPECT_TRUE([controller desiredTotalWidth] > 100); EXPECT_TRUE([controller desiredTotalHeight] > 100); - [mock verify]; - [controller release]; + + [controller closeBalloon:YES]; } } |