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(); |
80 | 103 |
81 EXPECT_TRUE(child_win->HasObserver(aura_host)); | 104 EXPECT_TRUE(child_win->HasObserver(aura_host)); |
82 DestroyHost(); | 105 DestroyHost(); |
83 EXPECT_FALSE(child_win->HasObserver(aura_host)); | 106 EXPECT_FALSE(child_win->HasObserver(aura_host)); |
84 } | 107 } |
85 | 108 |
86 // Tests that the kHostViewKey is correctly set and cleared. | 109 // Tests that the kHostViewKey is correctly set and cleared. |
87 TEST_F(NativeViewHostAuraTest, HostViewPropertyKey) { | 110 TEST_F(NativeViewHostAuraTest, HostViewPropertyKey) { |
88 // Create the NativeViewHost and attach a NativeView. | 111 // Create the NativeViewHost and attach a NativeView. |
89 CreateHost(); | 112 CreateHost(); |
90 aura::Window* child_win = child()->GetNativeView(); | 113 aura::Window* child_win = child()->GetNativeView(); |
91 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); | 114 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); |
| 115 EXPECT_EQ(host(), clipping_window()->GetProperty(views::kHostViewKey)); |
92 | 116 |
93 host()->Detach(); | 117 host()->Detach(); |
94 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); | 118 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); |
| 119 EXPECT_FALSE(clipping_window()->GetProperty(views::kHostViewKey)); |
95 | 120 |
96 host()->Attach(child_win); | 121 host()->Attach(child_win); |
97 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); | 122 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); |
| 123 EXPECT_EQ(host(), clipping_window()->GetProperty(views::kHostViewKey)); |
98 | 124 |
99 DestroyHost(); | 125 DestroyHost(); |
100 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); | 126 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); |
101 } | 127 } |
102 | 128 |
103 // Tests that the NativeViewHost reports the cursor set on its native view. | 129 // Tests that the NativeViewHost reports the cursor set on its native view. |
104 TEST_F(NativeViewHostAuraTest, CursorForNativeView) { | 130 TEST_F(NativeViewHostAuraTest, CursorForNativeView) { |
105 CreateHost(); | 131 CreateHost(); |
106 | 132 |
107 toplevel()->SetCursor(ui::kCursorHand); | 133 toplevel()->SetCursor(ui::kCursorHand); |
108 child()->SetCursor(ui::kCursorWait); | 134 child()->SetCursor(ui::kCursorWait); |
109 ui::MouseEvent move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0), | 135 ui::MouseEvent move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0), |
110 gfx::Point(0, 0), 0, 0); | 136 gfx::Point(0, 0), 0, 0); |
111 | 137 |
112 EXPECT_EQ(ui::kCursorWait, host()->GetCursor(move_event).native_type()); | 138 EXPECT_EQ(ui::kCursorWait, host()->GetCursor(move_event).native_type()); |
113 | 139 |
114 DestroyHost(); | 140 DestroyHost(); |
115 } | 141 } |
116 | 142 |
| 143 // Test that destroying the top level widget before destroying the attached |
| 144 // NativeViewHost works correctly. Specifically the associated NVH should be |
| 145 // destroyed and there shouldn't be any errors. |
| 146 TEST_F(NativeViewHostAuraTest, DestroyWidget) { |
| 147 NativeViewHostTesting::ResetDestroyedCount(); |
| 148 CreateHost(); |
| 149 ReleaseHost(); |
| 150 EXPECT_EQ(0, NativeViewHostTesting::destroyed_count()); |
| 151 DestroyTopLevel(); |
| 152 EXPECT_EQ(1, NativeViewHostTesting::destroyed_count()); |
| 153 } |
| 154 |
| 155 // Test that the fast resize path places the clipping and content windows were |
| 156 // they are supposed to be. |
| 157 TEST_F(NativeViewHostAuraTest, FastResizePath) { |
| 158 CreateHost(); |
| 159 toplevel()->SetBounds(gfx::Rect(20, 20, 100, 100)); |
| 160 |
| 161 // Without fast resize, the clipping window should size to the native view |
| 162 // with the native view positioned at the origin of the clipping window and |
| 163 // the clipping window positioned where the native view was requested. |
| 164 host()->set_fast_resize(false); |
| 165 native_host()->ShowWidget(5, 10, 100, 100); |
| 166 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 167 host()->native_view()->bounds().ToString()); |
| 168 EXPECT_EQ(gfx::Rect(5, 10, 100, 100).ToString(), |
| 169 clipping_window()->bounds().ToString()); |
| 170 |
| 171 // With fast resize, the native view should remain the same size but be |
| 172 // clipped the requested size. |
| 173 host()->set_fast_resize(true); |
| 174 native_host()->ShowWidget(10, 25, 50, 50); |
| 175 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 176 host()->native_view()->bounds().ToString()); |
| 177 EXPECT_EQ(gfx::Rect(10, 25, 50, 50).ToString(), |
| 178 clipping_window()->bounds().ToString()); |
| 179 |
| 180 // Turning off fast resize should make the native view start resizing again. |
| 181 host()->set_fast_resize(false); |
| 182 native_host()->ShowWidget(10, 25, 50, 50); |
| 183 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(), |
| 184 host()->native_view()->bounds().ToString()); |
| 185 EXPECT_EQ(gfx::Rect(10, 25, 50, 50).ToString(), |
| 186 clipping_window()->bounds().ToString()); |
| 187 |
| 188 DestroyHost(); |
| 189 } |
| 190 |
| 191 // Test installing and uninstalling a clip. |
| 192 TEST_F(NativeViewHostAuraTest, InstallClip) { |
| 193 CreateHost(); |
| 194 toplevel()->SetBounds(gfx::Rect(20, 20, 100, 100)); |
| 195 |
| 196 // Without a clip, the clipping window should always be positioned at the |
| 197 // requested coordinates with the native view positioned at the origin of the |
| 198 // clipping window. |
| 199 native_host()->ShowWidget(10, 20, 100, 100); |
| 200 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 201 host()->native_view()->bounds().ToString()); |
| 202 EXPECT_EQ(gfx::Rect(10, 20, 100, 100).ToString(), |
| 203 clipping_window()->bounds().ToString()); |
| 204 |
| 205 // Clip to the bottom right quarter of the native view. |
| 206 native_host()->InstallClip(60, 70, 50, 50); |
| 207 native_host()->ShowWidget(10, 20, 100, 100); |
| 208 EXPECT_EQ(gfx::Rect(-50, -50, 100, 100).ToString(), |
| 209 host()->native_view()->bounds().ToString()); |
| 210 EXPECT_EQ(gfx::Rect(60, 70, 50, 50).ToString(), |
| 211 clipping_window()->bounds().ToString()); |
| 212 |
| 213 // Clip to the center of the native view. |
| 214 native_host()->InstallClip(35, 45, 50, 50); |
| 215 native_host()->ShowWidget(10, 20, 100, 100); |
| 216 EXPECT_EQ(gfx::Rect(-25, -25, 100, 100).ToString(), |
| 217 host()->native_view()->bounds().ToString()); |
| 218 EXPECT_EQ(gfx::Rect(35, 45, 50, 50).ToString(), |
| 219 clipping_window()->bounds().ToString()); |
| 220 |
| 221 // Uninstalling the clip should make the clipping window match the native view |
| 222 // again. |
| 223 native_host()->UninstallClip(); |
| 224 native_host()->ShowWidget(10, 20, 100, 100); |
| 225 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(), |
| 226 host()->native_view()->bounds().ToString()); |
| 227 EXPECT_EQ(gfx::Rect(10, 20, 100, 100).ToString(), |
| 228 clipping_window()->bounds().ToString()); |
| 229 |
| 230 DestroyHost(); |
| 231 } |
| 232 |
117 } // namespace views | 233 } // namespace views |
OLD | NEW |