1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/views/controls/native/native_view_host.h"
7 #include "ui/views/test/views_test_base.h"
8 #include "ui/views/view.h"
9 #include "ui/views/widget/native_widget_private.h"
10 #include "ui/views/widget/widget.h"
14 class ScopedTestWidget
{
16 explicit ScopedTestWidget(internal::NativeWidgetPrivate
* native_widget
)
17 : native_widget_(native_widget
) {
20 // |CloseNow| deletes both |native_widget_| and its associated
22 native_widget_
->GetWidget()->CloseNow();
25 internal::NativeWidgetPrivate
* operator->() const {
26 return native_widget_
;
28 internal::NativeWidgetPrivate
* get() const { return native_widget_
; }
31 internal::NativeWidgetPrivate
* native_widget_
;
32 DISALLOW_COPY_AND_ASSIGN(ScopedTestWidget
);
35 class NativeWidgetTest
: public ViewsTestBase
{
38 ~NativeWidgetTest() override
{}
40 internal::NativeWidgetPrivate
* CreateNativeWidgetOfType(
41 Widget::InitParams::Type type
) {
42 Widget
* widget
= new Widget
;
43 Widget::InitParams params
= CreateParams(type
);
44 params
.ownership
= views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
;
45 params
.bounds
= gfx::Rect(10, 10, 200, 200);
47 return widget
->native_widget_private();
50 internal::NativeWidgetPrivate
* CreateNativeWidget() {
51 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_POPUP
);
54 internal::NativeWidgetPrivate
* CreateNativeSubWidget() {
55 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_CONTROL
);
59 DISALLOW_COPY_AND_ASSIGN(NativeWidgetTest
);
62 TEST_F(NativeWidgetTest
, CreateNativeWidget
) {
63 ScopedTestWidget
widget(CreateNativeWidget());
64 EXPECT_TRUE(widget
->GetWidget()->GetNativeView() != NULL
);
67 TEST_F(NativeWidgetTest
, GetNativeWidgetForNativeView
) {
68 ScopedTestWidget
widget(CreateNativeWidget());
69 EXPECT_EQ(widget
.get(),
70 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(
71 widget
->GetWidget()->GetNativeView()));
74 // |widget| has the toplevel NativeWidget.
75 TEST_F(NativeWidgetTest
, GetTopLevelNativeWidget1
) {
76 ScopedTestWidget
widget(CreateNativeWidget());
77 EXPECT_EQ(widget
.get(),
78 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(
79 widget
->GetWidget()->GetNativeView()));
82 // |toplevel_widget| has the toplevel NativeWidget.
83 TEST_F(NativeWidgetTest
, GetTopLevelNativeWidget2
) {
84 ScopedTestWidget
toplevel_widget(CreateNativeWidget());
86 // |toplevel_widget| owns |child_host|.
87 NativeViewHost
* child_host
= new NativeViewHost
;
88 toplevel_widget
->GetWidget()->SetContentsView(child_host
);
90 // |child_host| owns |child_widget|.
91 internal::NativeWidgetPrivate
* child_widget
= CreateNativeSubWidget();
92 child_host
->Attach(child_widget
->GetWidget()->GetNativeView());
94 EXPECT_EQ(toplevel_widget
.get(),
95 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(
96 child_widget
->GetWidget()->GetNativeView()));