Correct blacklist entry message
[chromium-blink-merge.git] / ui / views / widget / native_widget_unittest.cc
blob133f8be04a5fe702c31211aac916ef536457ce04
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"
12 namespace views {
14 class ScopedTestWidget {
15 public:
16 explicit ScopedTestWidget(internal::NativeWidgetPrivate* native_widget)
17 : native_widget_(native_widget) {
19 ~ScopedTestWidget() {
20 // |CloseNow| deletes both |native_widget_| and its associated
21 // |Widget|.
22 native_widget_->GetWidget()->CloseNow();
25 internal::NativeWidgetPrivate* operator->() const {
26 return native_widget_;
28 internal::NativeWidgetPrivate* get() const { return native_widget_; }
30 private:
31 internal::NativeWidgetPrivate* native_widget_;
32 DISALLOW_COPY_AND_ASSIGN(ScopedTestWidget);
35 class NativeWidgetTest : public ViewsTestBase {
36 public:
37 NativeWidgetTest() {}
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);
47 widget->Init(params);
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);
59 private:
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()));
100 } // namespace views