| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ui/views/widget/native_widget_mac.h" | 5 #import "ui/views/widget/native_widget_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 | 548 |
| 549 // Tests creating a views::Widget parented off a native NSWindow. | 549 // Tests creating a views::Widget parented off a native NSWindow. |
| 550 TEST_F(NativeWidgetMacTest, NonWidgetParent) { | 550 TEST_F(NativeWidgetMacTest, NonWidgetParent) { |
| 551 NSWindow* native_parent = MakeNativeParent(); | 551 NSWindow* native_parent = MakeNativeParent(); |
| 552 | 552 |
| 553 base::scoped_nsobject<NSView> anchor_view( | 553 base::scoped_nsobject<NSView> anchor_view( |
| 554 [[NSView alloc] initWithFrame:[[native_parent contentView] bounds]]); | 554 [[NSView alloc] initWithFrame:[[native_parent contentView] bounds]]); |
| 555 [[native_parent contentView] addSubview:anchor_view]; | 555 [[native_parent contentView] addSubview:anchor_view]; |
| 556 | 556 |
| 557 // Note: Don't use WidgetTest::CreateChildPlatformWidget because that makes | 557 // Note: Don't use WidgetTest::CreateChildPlatformWidget because that makes |
| 558 // windows of TYPE_CONTROL which are automatically made visible. But still | 558 // windows of TYPE_CONTROL which need a parent Widget to obtain the focus |
| 559 // mark it as a child to test window positioning. | 559 // manager. |
| 560 Widget* child = new Widget; | 560 Widget* child = new Widget; |
| 561 Widget::InitParams init_params; | 561 Widget::InitParams init_params; |
| 562 init_params.parent = anchor_view; | 562 init_params.parent = anchor_view; |
| 563 init_params.child = true; | 563 init_params.type = Widget::InitParams::TYPE_POPUP; |
| 564 child->Init(init_params); | 564 child->Init(init_params); |
| 565 | 565 |
| 566 TestWidgetObserver child_observer(child); | 566 TestWidgetObserver child_observer(child); |
| 567 | 567 |
| 568 // GetTopLevelNativeWidget() only goes as far as there exists a Widget (i.e. | 568 // GetTopLevelNativeWidget() only goes as far as there exists a Widget (i.e. |
| 569 // must stop at |child|. | 569 // must stop at |child|. |
| 570 internal::NativeWidgetPrivate* top_level_widget = | 570 internal::NativeWidgetPrivate* top_level_widget = |
| 571 internal::NativeWidgetPrivate::GetTopLevelNativeWidget( | 571 internal::NativeWidgetPrivate::GetTopLevelNativeWidget( |
| 572 child->GetNativeView()); | 572 child->GetNativeView()); |
| 573 EXPECT_EQ(child, top_level_widget->GetWidget()); | 573 EXPECT_EQ(child, top_level_widget->GetWidget()); |
| 574 | 574 |
| 575 // To verify the parent, we need to use NativeWidgetMac APIs. | 575 // To verify the parent, we need to use NativeWidgetMac APIs. |
| 576 BridgedNativeWidget* bridged_native_widget = | 576 BridgedNativeWidget* bridged_native_widget = |
| 577 NativeWidgetMac::GetBridgeForNativeWindow(child->GetNativeWindow()); | 577 NativeWidgetMac::GetBridgeForNativeWindow(child->GetNativeWindow()); |
| 578 EXPECT_EQ(native_parent, bridged_native_widget->parent()->GetNSWindow()); | 578 EXPECT_EQ(native_parent, bridged_native_widget->parent()->GetNSWindow()); |
| 579 | 579 |
| 580 child->SetBounds(gfx::Rect(50, 50, 200, 100)); | 580 const gfx::Rect child_bounds(50, 50, 200, 100); |
| 581 child->SetBounds(child_bounds); |
| 581 EXPECT_FALSE(child->IsVisible()); | 582 EXPECT_FALSE(child->IsVisible()); |
| 582 EXPECT_EQ(0u, [[native_parent childWindows] count]); | 583 EXPECT_EQ(0u, [[native_parent childWindows] count]); |
| 583 | 584 |
| 584 child->Show(); | 585 child->Show(); |
| 585 EXPECT_TRUE(child->IsVisible()); | 586 EXPECT_TRUE(child->IsVisible()); |
| 586 EXPECT_EQ(1u, [[native_parent childWindows] count]); | 587 EXPECT_EQ(1u, [[native_parent childWindows] count]); |
| 587 EXPECT_EQ(child->GetNativeWindow(), | 588 EXPECT_EQ(child->GetNativeWindow(), |
| 588 [[native_parent childWindows] objectAtIndex:0]); | 589 [[native_parent childWindows] objectAtIndex:0]); |
| 589 EXPECT_EQ(native_parent, [child->GetNativeWindow() parentWindow]); | 590 EXPECT_EQ(native_parent, [child->GetNativeWindow() parentWindow]); |
| 590 | 591 |
| 591 // Child should be positioned on screen relative to the parent, but note we | 592 // Only non-toplevel Widgets are positioned relative to the parent, so the |
| 592 // positioned the parent in Cocoa coordinates, so we need to convert. | 593 // bounds set above should be in screen coordinates. |
| 593 gfx::Point parent_origin = gfx::ScreenRectFromNSRect(ParentRect()).origin(); | 594 EXPECT_EQ(child_bounds, child->GetWindowBoundsInScreen()); |
| 594 EXPECT_EQ(gfx::Rect(150, parent_origin.y() + 50, 200, 100), | |
| 595 child->GetWindowBoundsInScreen()); | |
| 596 | 595 |
| 597 // Removing the anchor_view from its view hierarchy is permitted. This should | 596 // Removing the anchor_view from its view hierarchy is permitted. This should |
| 598 // not break the relationship between the two windows. | 597 // not break the relationship between the two windows. |
| 599 [anchor_view removeFromSuperview]; | 598 [anchor_view removeFromSuperview]; |
| 600 anchor_view.reset(); | 599 anchor_view.reset(); |
| 601 EXPECT_EQ(native_parent, bridged_native_widget->parent()->GetNSWindow()); | 600 EXPECT_EQ(native_parent, bridged_native_widget->parent()->GetNSWindow()); |
| 602 | 601 |
| 603 // Closing the parent should close and destroy the child. | 602 // Closing the parent should close and destroy the child. |
| 604 EXPECT_FALSE(child_observer.widget_closed()); | 603 EXPECT_FALSE(child_observer.widget_closed()); |
| 605 [native_parent close]; | 604 [native_parent close]; |
| (...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1367 lastDirtyRect_ = dirtyRect; | 1366 lastDirtyRect_ = dirtyRect; |
| 1368 } | 1367 } |
| 1369 | 1368 |
| 1370 @end | 1369 @end |
| 1371 | 1370 |
| 1372 @implementation FocusableTestNSView | 1371 @implementation FocusableTestNSView |
| 1373 - (BOOL)acceptsFirstResponder { | 1372 - (BOOL)acceptsFirstResponder { |
| 1374 return YES; | 1373 return YES; |
| 1375 } | 1374 } |
| 1376 @end | 1375 @end |
| OLD | NEW |