OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/native/native_view_host_aura.h" | 5 #include "ui/views/controls/native/native_view_host_aura.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
10 #include "ui/base/cursor/cursor.h" | 10 #include "ui/base/cursor/cursor.h" |
11 #include "ui/views/controls/native/native_view_host.h" | 11 #include "ui/views/controls/native/native_view_host.h" |
12 #include "ui/views/test/views_test_base.h" | 12 #include "ui/views/test/views_test_base.h" |
13 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
14 #include "ui/views/view_constants_aura.h" | 14 #include "ui/views/view_constants_aura.h" |
15 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
16 | 16 |
17 namespace views { | 17 namespace views { |
18 | 18 |
| 19 // Testing wrapper of the NativeViewHost |
| 20 class NativeViewHostTesting : public NativeViewHost { |
| 21 public: |
| 22 NativeViewHostTesting() {} |
| 23 virtual ~NativeViewHostTesting() { destroyed_count_++; } |
| 24 |
| 25 static void ResetDestroyedCount() { destroyed_count_ = 0; } |
| 26 |
| 27 static int destroyed_count() { return destroyed_count_; } |
| 28 |
| 29 private: |
| 30 static int destroyed_count_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(NativeViewHostTesting); |
| 33 }; |
| 34 int NativeViewHostTesting::destroyed_count_ = 0; |
| 35 |
19 class NativeViewHostAuraTest : public ViewsTestBase { | 36 class NativeViewHostAuraTest : public ViewsTestBase { |
20 public: | 37 public: |
21 NativeViewHostAuraTest() { | 38 NativeViewHostAuraTest() { |
22 } | 39 } |
23 | 40 |
24 NativeViewHostAura* native_host() { | 41 NativeViewHostAura* native_host() { |
25 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get()); | 42 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get()); |
26 } | 43 } |
27 | 44 |
28 Widget* toplevel() { | 45 Widget* toplevel() { |
29 return toplevel_.get(); | 46 return toplevel_.get(); |
30 } | 47 } |
31 | 48 |
32 NativeViewHost* host() { | 49 NativeViewHost* host() { |
33 return host_.get(); | 50 return host_.get(); |
34 } | 51 } |
35 | 52 |
36 Widget* child() { | 53 Widget* child() { |
37 return child_.get(); | 54 return child_.get(); |
38 } | 55 } |
39 | 56 |
| 57 aura::Window* clipping_window() { return &(native_host()->clipping_window_); } |
| 58 |
40 void CreateHost() { | 59 void CreateHost() { |
41 // Create the top level widget. | 60 // Create the top level widget. |
42 toplevel_.reset(new Widget); | 61 toplevel_.reset(new Widget); |
43 Widget::InitParams toplevel_params = | 62 Widget::InitParams toplevel_params = |
44 CreateParams(Widget::InitParams::TYPE_WINDOW); | 63 CreateParams(Widget::InitParams::TYPE_WINDOW); |
45 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 64 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
46 toplevel_->Init(toplevel_params); | 65 toplevel_->Init(toplevel_params); |
47 | 66 |
48 // And the child widget. | 67 // And the child widget. |
49 View* test_view = new View; | 68 View* test_view = new View; |
50 child_.reset(new Widget); | 69 child_.reset(new Widget); |
51 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); | 70 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); |
52 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 71 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
53 child_params.parent = toplevel_->GetNativeView(); | 72 child_params.parent = toplevel_->GetNativeView(); |
54 child_->Init(child_params); | 73 child_->Init(child_params); |
55 child_->SetContentsView(test_view); | 74 child_->SetContentsView(test_view); |
56 | 75 |
57 // Owned by |toplevel|. | 76 // Owned by |toplevel|. |
58 host_.reset(new NativeViewHost); | 77 host_.reset(new NativeViewHostTesting); |
59 toplevel_->GetRootView()->AddChildView(host_.get()); | 78 toplevel_->GetRootView()->AddChildView(host_.get()); |
60 host_->Attach(child_->GetNativeView()); | 79 host_->Attach(child_->GetNativeView()); |
61 } | 80 } |
62 | 81 |
63 void DestroyHost() { | 82 void DestroyHost() { |
64 host_.reset(); | 83 host_.reset(); |
65 } | 84 } |
66 | 85 |
| 86 NativeViewHostTesting* ReleaseHost() { return host_.release(); } |
| 87 |
| 88 void DestroyTopLevel() { toplevel_.reset(); } |
| 89 |
67 private: | 90 private: |
68 scoped_ptr<Widget> toplevel_; | 91 scoped_ptr<Widget> toplevel_; |
69 scoped_ptr<NativeViewHost> host_; | 92 scoped_ptr<NativeViewHostTesting> host_; |
70 scoped_ptr<Widget> child_; | 93 scoped_ptr<Widget> child_; |
71 | 94 |
72 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest); | 95 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest); |
73 }; | 96 }; |
74 | 97 |
75 // Verifies NativeViewHostAura stops observing native view on destruction. | 98 // Verifies NativeViewHostAura stops observing native view on destruction. |
76 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) { | 99 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) { |
77 CreateHost(); | 100 CreateHost(); |
78 aura::Window* child_win = child()->GetNativeView(); | 101 aura::Window* child_win = child()->GetNativeView(); |
79 NativeViewHostAura* aura_host = native_host(); | 102 NativeViewHostAura* aura_host = native_host(); |
(...skipping 27 matching lines...) Expand all Loading... |
107 toplevel()->SetCursor(ui::kCursorHand); | 130 toplevel()->SetCursor(ui::kCursorHand); |
108 child()->SetCursor(ui::kCursorWait); | 131 child()->SetCursor(ui::kCursorWait); |
109 ui::MouseEvent move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0), | 132 ui::MouseEvent move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0), |
110 gfx::Point(0, 0), 0, 0); | 133 gfx::Point(0, 0), 0, 0); |
111 | 134 |
112 EXPECT_EQ(ui::kCursorWait, host()->GetCursor(move_event).native_type()); | 135 EXPECT_EQ(ui::kCursorWait, host()->GetCursor(move_event).native_type()); |
113 | 136 |
114 DestroyHost(); | 137 DestroyHost(); |
115 } | 138 } |
116 | 139 |
| 140 // Test that the fast resize path places the clipping and content windows were |
| 141 // they are supposed to be. |
| 142 TEST_F(NativeViewHostAuraTest, FastResizePath) { |
| 143 CreateHost(); |
| 144 |
| 145 host()->set_fast_resize(false); |
| 146 toplevel()->SetBounds(gfx::Rect(20, 20, 100, 100)); |
| 147 native_host()->ShowWidget(0, 0, 100, 100); |
| 148 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 149 host()->native_view()->layer()->bounds().ToString()); |
| 150 |
| 151 host()->set_fast_resize(true); |
| 152 native_host()->ShowWidget(10, 25, 50, 50); |
| 153 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(), |
| 154 host()->native_view()->layer()->bounds().ToString()); |
| 155 EXPECT_EQ(gfx::Rect(10, 25, 50, 50).ToString(), |
| 156 clipping_window()->layer()->bounds().ToString()); |
| 157 |
| 158 DestroyHost(); |
| 159 } |
| 160 |
| 161 // Test that destroying the top level widget before destroying the attached |
| 162 // NativeViewHost works correctly. Specifically the associated NVH should be |
| 163 // destroyed and there shouldn't be any errors. |
| 164 TEST_F(NativeViewHostAuraTest, DestroyWidget) { |
| 165 NativeViewHostTesting::ResetDestroyedCount(); |
| 166 CreateHost(); |
| 167 ReleaseHost(); |
| 168 EXPECT_EQ(0, NativeViewHostTesting::destroyed_count()); |
| 169 DestroyTopLevel(); |
| 170 EXPECT_EQ(1, NativeViewHostTesting::destroyed_count()); |
| 171 } |
| 172 |
117 } // namespace views | 173 } // namespace views |
OLD | NEW |