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 virtual ~NativeWidgetTest() {}
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
.child
= false; // Implicitly set to true by ctor with TYPE_CONTROL.
46 params
.bounds
= gfx::Rect(10, 10, 200, 200);
48 return widget
->native_widget_private();
51 internal::NativeWidgetPrivate
* CreateNativeWidget() {
52 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_POPUP
);
55 internal::NativeWidgetPrivate
* CreateNativeSubWidget() {
56 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_CONTROL
);
60 DISALLOW_COPY_AND_ASSIGN(NativeWidgetTest
);
63 TEST_F(NativeWidgetTest
, CreateNativeWidget
) {
64 ScopedTestWidget
widget(CreateNativeWidget());
65 EXPECT_TRUE(widget
->GetWidget()->GetNativeView() != NULL
);
68 TEST_F(NativeWidgetTest
, GetNativeWidgetForNativeView
) {
69 ScopedTestWidget
widget(CreateNativeWidget());
70 EXPECT_EQ(widget
.get(),
71 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(
72 widget
->GetWidget()->GetNativeView()));
75 // |widget| has the toplevel NativeWidget.
76 TEST_F(NativeWidgetTest
, GetTopLevelNativeWidget1
) {
77 ScopedTestWidget
widget(CreateNativeWidget());
78 EXPECT_EQ(widget
.get(),
79 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(
80 widget
->GetWidget()->GetNativeView()));
83 // |toplevel_widget| has the toplevel NativeWidget.
84 TEST_F(NativeWidgetTest
, GetTopLevelNativeWidget2
) {
85 ScopedTestWidget
toplevel_widget(CreateNativeWidget());
87 // |toplevel_widget| owns |child_host|.
88 NativeViewHost
* child_host
= new NativeViewHost
;
89 toplevel_widget
->GetWidget()->SetContentsView(child_host
);
91 // |child_host| owns |child_widget|.
92 internal::NativeWidgetPrivate
* child_widget
= CreateNativeSubWidget();
93 child_host
->Attach(child_widget
->GetWidget()->GetNativeView());
95 EXPECT_EQ(toplevel_widget
.get(),
96 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(
97 child_widget
->GetWidget()->GetNativeView()));