| 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/views/controls/native/native_view_host.h" | 10 #include "ui/views/controls/native/native_view_host.h" |
| 11 #include "ui/views/test/views_test_base.h" | 11 #include "ui/views/test/views_test_base.h" |
| 12 #include "ui/views/view.h" | 12 #include "ui/views/view.h" |
| 13 #include "ui/views/view_constants_aura.h" | 13 #include "ui/views/view_constants_aura.h" |
| 14 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 18 // Static class used to track if the NativeViewHostTesting has been destroyed. |
| 19 class DestroyedCount { |
| 20 public: |
| 21 static void Increment() { |
| 22 count_++; |
| 23 } |
| 24 |
| 25 static void Reset() { |
| 26 count_ = 0; |
| 27 } |
| 28 |
| 29 static int count() { |
| 30 return count_; |
| 31 } |
| 32 |
| 33 private: |
| 34 static int count_; |
| 35 }; |
| 36 int DestroyedCount::count_ = 0; |
| 37 |
| 38 } // namespace |
| 39 |
| 16 namespace views { | 40 namespace views { |
| 17 | 41 |
| 42 // Testing wrapper of the NativeViewHost |
| 43 class NativeViewHostTesting : public NativeViewHost { |
| 44 public: |
| 45 NativeViewHostTesting() {} |
| 46 virtual ~NativeViewHostTesting() { |
| 47 DestroyedCount::Increment(); |
| 48 } |
| 49 }; |
| 50 |
| 18 class NativeViewHostAuraTest : public ViewsTestBase { | 51 class NativeViewHostAuraTest : public ViewsTestBase { |
| 19 public: | 52 public: |
| 20 NativeViewHostAuraTest() { | 53 NativeViewHostAuraTest() { |
| 21 } | 54 } |
| 22 | 55 |
| 23 NativeViewHostAura* native_host() { | 56 NativeViewHostAura* native_host() { |
| 24 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get()); | 57 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get()); |
| 25 } | 58 } |
| 26 | 59 |
| 27 NativeViewHost* host() { | 60 NativeViewHost* host() { |
| 28 return host_.get(); | 61 return host_.get(); |
| 29 } | 62 } |
| 30 | 63 |
| 31 Widget* child() { | 64 Widget* child() { |
| 32 return child_.get(); | 65 return child_.get(); |
| 33 } | 66 } |
| 34 | 67 |
| 68 aura::Window* clipping_window() { |
| 69 return &(native_host()->clipping_window_); |
| 70 } |
| 71 |
| 72 Widget* toplevel() { |
| 73 return toplevel_.get(); |
| 74 } |
| 75 |
| 35 void CreateHost() { | 76 void CreateHost() { |
| 36 // Create the top level widget. | 77 // Create the top level widget. |
| 37 toplevel_.reset(new Widget); | 78 toplevel_.reset(new Widget); |
| 38 Widget::InitParams toplevel_params = | 79 Widget::InitParams toplevel_params = |
| 39 CreateParams(Widget::InitParams::TYPE_WINDOW); | 80 CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 40 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 81 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 41 toplevel_->Init(toplevel_params); | 82 toplevel_->Init(toplevel_params); |
| 42 | 83 |
| 43 // And the child widget. | 84 // And the child widget. |
| 44 View* test_view = new View; | 85 View* test_view = new View; |
| 45 child_.reset(new Widget); | 86 child_.reset(new Widget); |
| 46 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); | 87 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); |
| 47 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 88 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 48 child_params.parent = toplevel_->GetNativeView(); | 89 child_params.parent = toplevel_->GetNativeView(); |
| 49 child_->Init(child_params); | 90 child_->Init(child_params); |
| 50 child_->SetContentsView(test_view); | 91 child_->SetContentsView(test_view); |
| 51 | 92 |
| 52 // Owned by |toplevel|. | 93 // Owned by |toplevel|. |
| 53 host_.reset(new NativeViewHost); | 94 host_.reset(new NativeViewHostTesting); |
| 54 toplevel_->GetRootView()->AddChildView(host_.get()); | 95 toplevel_->GetRootView()->AddChildView(host_.get()); |
| 55 host_->Attach(child_->GetNativeView()); | 96 host_->Attach(child_->GetNativeView()); |
| 56 } | 97 } |
| 57 | 98 |
| 58 void DestroyHost() { | 99 void DestroyHost() { |
| 59 host_.reset(); | 100 host_.reset(); |
| 60 } | 101 } |
| 61 | 102 |
| 103 NativeViewHostTesting* ReleaseHost() { |
| 104 return host_.release(); |
| 105 } |
| 106 |
| 107 void DestroyTopLevel() { |
| 108 toplevel_.reset(); |
| 109 } |
| 110 |
| 111 gfx::Point CalculateNativeViewOrigin(gfx::Rect input_rect, |
| 112 gfx::Rect native_rect) { |
| 113 return native_host()->CalculateNativeViewOrigin(input_rect, native_rect); |
| 114 } |
| 115 |
| 62 private: | 116 private: |
| 63 scoped_ptr<Widget> toplevel_; | 117 scoped_ptr<Widget> toplevel_; |
| 64 scoped_ptr<NativeViewHost> host_; | 118 scoped_ptr<NativeViewHostTesting> host_; |
| 65 scoped_ptr<Widget> child_; | 119 scoped_ptr<Widget> child_; |
| 66 | 120 |
| 67 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest); | 121 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest); |
| 68 }; | 122 }; |
| 69 | 123 |
| 70 // Verifies NativeViewHostAura stops observing native view on destruction. | 124 // Verifies NativeViewHostAura stops observing native view on destruction. |
| 71 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) { | 125 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) { |
| 72 CreateHost(); | 126 CreateHost(); |
| 73 aura::Window* child_win = child()->GetNativeView(); | 127 aura::Window* child_win = child()->GetNativeView(); |
| 74 NativeViewHostAura* aura_host = native_host(); | 128 NativeViewHostAura* aura_host = native_host(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 88 host()->Detach(); | 142 host()->Detach(); |
| 89 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); | 143 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); |
| 90 | 144 |
| 91 host()->Attach(child_win); | 145 host()->Attach(child_win); |
| 92 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); | 146 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); |
| 93 | 147 |
| 94 DestroyHost(); | 148 DestroyHost(); |
| 95 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); | 149 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); |
| 96 } | 150 } |
| 97 | 151 |
| 152 // Tests that the values being calculated by CalculateNativeViewOrigin are |
| 153 // correct. |
| 154 TEST_F(NativeViewHostAuraTest, CalculateNewNativeViewOrigin) { |
| 155 CreateHost(); |
| 156 |
| 157 gfx::Rect clip_rect(0, 0, 50, 50); |
| 158 gfx::Rect contents_rect(50, 50, 100, 100); |
| 159 |
| 160 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHWEST); |
| 161 EXPECT_EQ(gfx::Point(0, 0), |
| 162 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 163 |
| 164 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTH); |
| 165 EXPECT_EQ(gfx::Point(-25, 0), |
| 166 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 167 |
| 168 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHEAST); |
| 169 EXPECT_EQ(gfx::Point(-50, 0), |
| 170 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 171 |
| 172 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_EAST); |
| 173 EXPECT_EQ(gfx::Point(-50, -25), |
| 174 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 175 |
| 176 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHEAST); |
| 177 EXPECT_EQ(gfx::Point(-50, -50), |
| 178 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 179 |
| 180 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTH); |
| 181 EXPECT_EQ(gfx::Point(-25, -50), |
| 182 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 183 |
| 184 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHWEST); |
| 185 EXPECT_EQ(gfx::Point(0, -50), |
| 186 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 187 |
| 188 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_WEST); |
| 189 EXPECT_EQ(gfx::Point(0, -25), |
| 190 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 191 |
| 192 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_CENTER); |
| 193 EXPECT_EQ(gfx::Point(-25, -25), |
| 194 CalculateNativeViewOrigin(clip_rect, contents_rect)); |
| 195 |
| 196 DestroyHost(); |
| 197 } |
| 198 |
| 199 // Test that the fast resize path places the clipping and content windows were |
| 200 // they are supposed to be. |
| 201 TEST_F(NativeViewHostAuraTest, FastResizePath) { |
| 202 CreateHost(); |
| 203 host()->set_fast_resize(false); |
| 204 toplevel()->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 205 native_host()->ShowWidget(0, 0, 100, 100); |
| 206 host()->set_fast_resize(true); |
| 207 |
| 208 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHWEST); |
| 209 native_host()->ShowWidget(0, 0, 50, 50); |
| 210 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), |
| 211 host()->native_view()->layer()->bounds()); |
| 212 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), clipping_window()->layer()->bounds()); |
| 213 |
| 214 host()->set_fast_resize(false); |
| 215 native_host()->UninstallClip(); |
| 216 native_host()->ShowWidget(0, 0, 100, 100); |
| 217 host()->set_fast_resize(true); |
| 218 |
| 219 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHEAST); |
| 220 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), |
| 221 host()->native_view()->layer()->bounds()); |
| 222 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), clipping_window()->layer()->bounds()); |
| 223 |
| 224 native_host()->ShowWidget(0, 0, 50, 50); |
| 225 EXPECT_EQ(gfx::Rect(-50, -50, 100, 100), |
| 226 host()->native_view()->layer()->bounds()); |
| 227 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), clipping_window()->layer()->bounds()); |
| 228 |
| 229 DestroyHost(); |
| 230 } |
| 231 |
| 232 // Test that destroying the top level widget before destroying the attached |
| 233 // NativeViewHost works correctly. Specifically the associated NVH should be |
| 234 // destroyed and there shouldn't be any errors. |
| 235 TEST_F(NativeViewHostAuraTest, DestroyWidget) { |
| 236 DestroyedCount::Reset(); |
| 237 CreateHost(); |
| 238 ReleaseHost(); |
| 239 EXPECT_EQ(0, DestroyedCount::count()); |
| 240 DestroyTopLevel(); |
| 241 EXPECT_EQ(1, DestroyedCount::count()); |
| 242 } |
| 243 |
| 98 } // namespace views | 244 } // namespace views |
| OLD | NEW |