Commit d6725376 authored by tapted's avatar tapted Committed by Commit bot

MacViews: Fix Widget::GetAllChildWidgets(gfx::NativeView) for non-Widgets.

Currently GetAllChildWidgets only works for Widgets, but the API doesn't
require that. Passing a non-Widget allows the Widgets parented off a
Cocoa browser window to be easily obtained. This will help with testing
of browser dialogs.

BUG=654151

Review-Url: https://codereview.chromium.org/2535163002
Cr-Commit-Position: refs/heads/master@{#434911}
parent f160854f
......@@ -689,8 +689,18 @@ void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
Widget::Widgets* children) {
BridgedNativeWidget* bridge =
NativeWidgetMac::GetBridgeForNativeWindow([native_view window]);
if (!bridge)
if (!bridge) {
// The NSWindow is not itself a views::Widget, but it may have children that
// are. Support returning Widgets that are parented to the NSWindow, except:
// - Ignore requests for children of an NSView that is not a contentView.
// - We do not add a Widget for |native_view| to |children| (there is none).
if ([[native_view window] contentView] != native_view)
return;
for (NSWindow* native_child in [[native_view window] childWindows])
GetAllChildWidgets([native_child contentView], children);
return;
}
// If |native_view| is a subview of the contentView, it will share an
// NSWindow, but will itself be a native child of the Widget. That is, adding
......@@ -705,6 +715,10 @@ void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
if (bridge->native_widget_mac()->GetWidget())
children->insert(bridge->native_widget_mac()->GetWidget());
// When the NSWindow *is* a Widget, only consider child_windows(). I.e. do not
// look through -[NSWindow childWindows] as done for the (!bridge) case above.
// -childWindows does not support hidden windows, and anything in there which
// is not in child_windows() would have been added by AppKit.
for (BridgedNativeWidget* child : bridge->child_windows())
GetAllChildWidgets(child->ns_view(), children);
}
......
......@@ -692,6 +692,11 @@ Widget* AttachPopupToNativeParent(NSWindow* native_parent) {
// Tests creating a views::Widget parented off a native NSWindow.
TEST_F(NativeWidgetMacTest, NonWidgetParent) {
NSWindow* native_parent = MakeNativeParent();
Widget::Widgets children;
Widget::GetAllChildWidgets([native_parent contentView], &children);
EXPECT_TRUE(children.empty());
Widget* child = AttachPopupToNativeParent(native_parent);
TestWidgetObserver child_observer(child);
......@@ -719,6 +724,10 @@ TEST_F(NativeWidgetMacTest, NonWidgetParent) {
[[native_parent childWindows] objectAtIndex:0]);
EXPECT_EQ(native_parent, [child->GetNativeWindow() parentWindow]);
Widget::GetAllChildWidgets([native_parent contentView], &children);
ASSERT_EQ(1u, children.size());
EXPECT_EQ(child, *children.begin());
// Only non-toplevel Widgets are positioned relative to the parent, so the
// bounds set above should be in screen coordinates.
EXPECT_EQ(child_bounds, child->GetWindowBoundsInScreen());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment