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. 4 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" 11 12 namespace views { 13 14 class ScopedTestWidget { 15 public: 16 explicit ScopedTestWidget(internal::NativeWidgetPrivate* native_widget) 17 : native_widget_(native_widget) { 18 } 19 ~ScopedTestWidget() { 20 // |CloseNow| deletes both |native_widget_| and its associated 21 // |Widget|. 22 native_widget_->GetWidget()->CloseNow(); 23 } 24 25 internal::NativeWidgetPrivate* operator->() const { 26 return native_widget_; 27 } 28 internal::NativeWidgetPrivate* get() const { return native_widget_; } 29 30 private: 31 internal::NativeWidgetPrivate* native_widget_; 32 DISALLOW_COPY_AND_ASSIGN(ScopedTestWidget); 33 }; 34 35 class NativeWidgetTest : public ViewsTestBase { 36 public: 37 NativeWidgetTest() {} 38 virtual ~NativeWidgetTest() {} 39 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); 46 widget->Init(params); 47 return widget->native_widget_private(); 48 } 49 50 internal::NativeWidgetPrivate* CreateNativeWidget() { 51 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_POPUP); 52 } 53 54 internal::NativeWidgetPrivate* CreateNativeSubWidget() { 55 return CreateNativeWidgetOfType(Widget::InitParams::TYPE_CONTROL); 56 } 57 58 private: 59 DISALLOW_COPY_AND_ASSIGN(NativeWidgetTest); 60 }; 61 62 TEST_F(NativeWidgetTest, CreateNativeWidget) { 63 ScopedTestWidget widget(CreateNativeWidget()); 64 EXPECT_TRUE(widget->GetWidget()->GetNativeView() != NULL); 65 } 66 67 TEST_F(NativeWidgetTest, GetNativeWidgetForNativeView) { 68 ScopedTestWidget widget(CreateNativeWidget()); 69 EXPECT_EQ(widget.get(), 70 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView( 71 widget->GetWidget()->GetNativeView())); 72 } 73 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())); 80 } 81 82 // |toplevel_widget| has the toplevel NativeWidget. 83 TEST_F(NativeWidgetTest, GetTopLevelNativeWidget2) { 84 ScopedTestWidget toplevel_widget(CreateNativeWidget()); 85 86 // |toplevel_widget| owns |child_host|. 87 NativeViewHost* child_host = new NativeViewHost; 88 toplevel_widget->GetWidget()->SetContentsView(child_host); 89 90 // |child_host| owns |child_widget|. 91 internal::NativeWidgetPrivate* child_widget = CreateNativeSubWidget(); 92 child_host->Attach(child_widget->GetWidget()->GetNativeView()); 93 94 EXPECT_EQ(toplevel_widget.get(), 95 internal::NativeWidgetPrivate::GetTopLevelNativeWidget( 96 child_widget->GetWidget()->GetNativeView())); 97 } 98 99 } // namespace views 100