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

Unified Diff: ui/views/widget/native_widget_mac_unittest.mm

Issue 2056593002: Mac: Retain the child NSWindow in WidgetOwnerNSWindowAdapter before invoking close (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refine DCHECK Created 4 years, 6 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
« no previous file with comments | « ui/views/cocoa/widget_owner_nswindow_adapter.mm ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/widget/native_widget_mac_unittest.mm
diff --git a/ui/views/widget/native_widget_mac_unittest.mm b/ui/views/widget/native_widget_mac_unittest.mm
index a320ad3e873dd3f2cf0e562f1550123ed7d98e1d..f3e149a9d20823a1cb6087acacb465b8ed6110fd 100644
--- a/ui/views/widget/native_widget_mac_unittest.mm
+++ b/ui/views/widget/native_widget_mac_unittest.mm
@@ -8,6 +8,7 @@
#import "base/mac/foundation_util.h"
#import "base/mac/scoped_nsobject.h"
+#import "base/mac/scoped_nsautorelease_pool.h"
#import "base/mac/scoped_objc_class_swizzler.h"
#include "base/macros.h"
#include "base/run_loop.h"
@@ -55,8 +56,10 @@
@interface NativeWidgetMacTestWindow : NativeWidgetMacNSWindow {
@private
int invalidateShadowCount_;
+ bool* deallocFlag_;
}
@property(readonly, nonatomic) int invalidateShadowCount;
+@property(assign, nonatomic) bool* deallocFlag;
@end
// Used to mock BridgedContentView so that calls to drawRect: can be
@@ -77,6 +80,10 @@
@interface FocusableTestNSView : NSView
@end
+@interface TestNativeParentWindow : NSWindow
+@property(assign, nonatomic) bool* deallocFlag;
+@end
+
namespace views {
namespace test {
@@ -138,8 +145,8 @@ class NativeWidgetMacTest : public WidgetTest {
NSRect ParentRect() const { return NSMakeRect(100, 100, 300, 200); }
// Make a native NSWindow with the given |style_mask| to use as a parent.
- NSWindow* MakeNativeParentWithStyle(int style_mask) {
- native_parent_.reset([[NSWindow alloc]
+ TestNativeParentWindow* MakeNativeParentWithStyle(int style_mask) {
+ native_parent_.reset([[TestNativeParentWindow alloc]
initWithContentRect:ParentRect()
styleMask:style_mask
backing:NSBackingStoreBuffered
@@ -150,7 +157,7 @@ class NativeWidgetMacTest : public WidgetTest {
}
// Make a borderless, native NSWindow to use as a parent.
- NSWindow* MakeNativeParent() {
+ TestNativeParentWindow* MakeNativeParent() {
return MakeNativeParentWithStyle(NSBorderlessWindowMask);
}
@@ -167,9 +174,10 @@ class NativeWidgetMacTest : public WidgetTest {
return widget;
}
- private:
- base::scoped_nsobject<NSWindow> native_parent_;
+ protected:
+ base::scoped_nsobject<TestNativeParentWindow> native_parent_;
+ private:
DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacTest);
};
@@ -646,6 +654,37 @@ TEST_F(NativeWidgetMacTest, NonWidgetParent) {
EXPECT_EQ(0u, [[native_parent childWindows] count]);
}
+// Tests closing the last remaining NSWindow reference via -windowWillClose:.
+// This is a regression test for http://crbug.com/616701.
+TEST_F(NativeWidgetMacTest, NonWidgetParentLastReference) {
+ bool child_dealloced = false;
+ bool native_parent_dealloced = false;
+ {
+ base::mac::ScopedNSAutoreleasePool pool;
+ TestNativeParentWindow* native_parent = MakeNativeParent();
+ [native_parent setDeallocFlag:&native_parent_dealloced];
+
+ NativeWidgetMacTestWindow* window;
+ Widget::InitParams init_params =
+ CreateParams(Widget::InitParams::TYPE_POPUP);
+ init_params.parent = [native_parent_ contentView];
+ init_params.bounds = gfx::Rect(0, 0, 100, 200);
+ CreateWidgetWithTestWindow(init_params, &window);
+ [window setDeallocFlag:&child_dealloced];
+ }
+ {
+ // On 10.11, closing a weak reference on the parent window works, but older
+ // versions of AppKit get upset if things are released inside -[NSWindow
+ // close]. This test tries to establish a situation where the last reference
+ // to the child window is released inside WidgetOwnerNSWindowAdapter::
+ // OnWindowWillClose().
+ base::mac::ScopedNSAutoreleasePool pool;
+ [native_parent_.autorelease() close];
+ EXPECT_TRUE(child_dealloced);
+ }
+ EXPECT_TRUE(native_parent_dealloced);
+}
+
// Use Native APIs to query the tooltip text that would be shown once the
// tooltip delay had elapsed.
base::string16 TooltipTextForWidget(Widget* widget) {
@@ -1567,6 +1606,15 @@ TEST_F(NativeWidgetMacViewsOrderTest, UnassociatedViewsIsAbove) {
@implementation NativeWidgetMacTestWindow
@synthesize invalidateShadowCount = invalidateShadowCount_;
+@synthesize deallocFlag = deallocFlag_;
+
+- (void)dealloc {
+ if (deallocFlag_) {
+ DCHECK(!*deallocFlag_);
+ *deallocFlag_ = true;
+ }
+ [super dealloc];
+}
- (void)invalidateShadow {
++invalidateShadowCount_;
@@ -1592,3 +1640,19 @@ TEST_F(NativeWidgetMacViewsOrderTest, UnassociatedViewsIsAbove) {
return YES;
}
@end
+
+@implementation TestNativeParentWindow {
+ bool* deallocFlag_;
+}
+
+@synthesize deallocFlag = deallocFlag_;
+
+- (void)dealloc {
+ if (deallocFlag_) {
+ DCHECK(!*deallocFlag_);
+ *deallocFlag_ = true;
+ }
+ [super dealloc];
+}
+
+@end
« no previous file with comments | « ui/views/cocoa/widget_owner_nswindow_adapter.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698