Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1338)

Side by Side Diff: ui/views/controls/native/native_view_host_aura_unittest.cc

Issue 30993004: Reland: Implement features in NativeViewHostAura for scroll end effect (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 views { 16 namespace views {
17 17
18 // Testing wrapper of the NativeViewHost
19 class NativeViewHostTesting : public NativeViewHost {
20 public:
21 NativeViewHostTesting() {}
22 virtual ~NativeViewHostTesting() {
23 destroyed_count_++;
24 }
25
26 static void ResetDestroyedCount() {
27 destroyed_count_ = 0;
28 }
29
30 static int destroyed_count() {
31 return destroyed_count_;
32 }
33
34 private:
35 static int destroyed_count_;
36 };
37 int NativeViewHostTesting::destroyed_count_ = 0;
38
18 class NativeViewHostAuraTest : public ViewsTestBase { 39 class NativeViewHostAuraTest : public ViewsTestBase {
19 public: 40 public:
20 NativeViewHostAuraTest() { 41 NativeViewHostAuraTest() {
21 } 42 }
22 43
23 NativeViewHostAura* native_host() { 44 NativeViewHostAura* native_host() {
24 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get()); 45 return static_cast<NativeViewHostAura*>(host_->native_wrapper_.get());
25 } 46 }
26 47
27 NativeViewHost* host() { 48 NativeViewHost* host() {
28 return host_.get(); 49 return host_.get();
29 } 50 }
30 51
31 Widget* child() { 52 Widget* child() {
32 return child_.get(); 53 return child_.get();
33 } 54 }
34 55
56 aura::Window* clipping_window() {
57 return &(native_host()->clipping_window_);
58 }
59
60 Widget* toplevel() {
61 return toplevel_.get();
62 }
63
35 void CreateHost() { 64 void CreateHost() {
36 // Create the top level widget. 65 // Create the top level widget.
37 toplevel_.reset(new Widget); 66 toplevel_.reset(new Widget);
38 Widget::InitParams toplevel_params = 67 Widget::InitParams toplevel_params =
39 CreateParams(Widget::InitParams::TYPE_WINDOW); 68 CreateParams(Widget::InitParams::TYPE_WINDOW);
40 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 69 toplevel_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
41 toplevel_->Init(toplevel_params); 70 toplevel_->Init(toplevel_params);
42 71
43 // And the child widget. 72 // And the child widget.
44 View* test_view = new View; 73 View* test_view = new View;
45 child_.reset(new Widget); 74 child_.reset(new Widget);
46 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); 75 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
47 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 76 child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
48 child_params.parent = toplevel_->GetNativeView(); 77 child_params.parent = toplevel_->GetNativeView();
49 child_->Init(child_params); 78 child_->Init(child_params);
50 child_->SetContentsView(test_view); 79 child_->SetContentsView(test_view);
51 80
52 // Owned by |toplevel|. 81 // Owned by |toplevel|.
53 host_.reset(new NativeViewHost); 82 host_.reset(new NativeViewHostTesting);
54 toplevel_->GetRootView()->AddChildView(host_.get()); 83 toplevel_->GetRootView()->AddChildView(host_.get());
55 host_->Attach(child_->GetNativeView()); 84 host_->Attach(child_->GetNativeView());
56 } 85 }
57 86
58 void DestroyHost() { 87 void DestroyHost() {
59 host_.reset(); 88 host_.reset();
60 } 89 }
61 90
91 NativeViewHostTesting* ReleaseHost() {
92 return host_.release();
93 }
94
95 void DestroyTopLevel() {
96 toplevel_.reset();
97 }
98
99 gfx::Point CalculateNativeViewOrigin(gfx::Rect input_rect,
100 gfx::Rect native_rect) {
101 return native_host()->CalculateNativeViewOrigin(input_rect, native_rect);
102 }
103
62 private: 104 private:
63 scoped_ptr<Widget> toplevel_; 105 scoped_ptr<Widget> toplevel_;
64 scoped_ptr<NativeViewHost> host_; 106 scoped_ptr<NativeViewHostTesting> host_;
65 scoped_ptr<Widget> child_; 107 scoped_ptr<Widget> child_;
66 108
67 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest); 109 DISALLOW_COPY_AND_ASSIGN(NativeViewHostAuraTest);
68 }; 110 };
69 111
70 // Verifies NativeViewHostAura stops observing native view on destruction. 112 // Verifies NativeViewHostAura stops observing native view on destruction.
71 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) { 113 TEST_F(NativeViewHostAuraTest, StopObservingNativeViewOnDestruct) {
72 CreateHost(); 114 CreateHost();
73 aura::Window* child_win = child()->GetNativeView(); 115 aura::Window* child_win = child()->GetNativeView();
74 NativeViewHostAura* aura_host = native_host(); 116 NativeViewHostAura* aura_host = native_host();
(...skipping 13 matching lines...) Expand all
88 host()->Detach(); 130 host()->Detach();
89 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); 131 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey));
90 132
91 host()->Attach(child_win); 133 host()->Attach(child_win);
92 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey)); 134 EXPECT_EQ(host(), child_win->GetProperty(views::kHostViewKey));
93 135
94 DestroyHost(); 136 DestroyHost();
95 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey)); 137 EXPECT_FALSE(child_win->GetProperty(views::kHostViewKey));
96 } 138 }
97 139
140 // Tests that the values being calculated by CalculateNativeViewOrigin are
141 // correct.
142 TEST_F(NativeViewHostAuraTest, CalculateNewNativeViewOrigin) {
143 CreateHost();
144
145 gfx::Rect clip_rect(0, 0, 50, 50);
146 gfx::Rect contents_rect(50, 50, 100, 100);
147
148 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHWEST);
149 EXPECT_EQ(gfx::Point(0, 0).ToString(),
150 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
151
152 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTH);
153 EXPECT_EQ(gfx::Point(-25, 0).ToString(),
154 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
155
156 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHEAST);
157 EXPECT_EQ(gfx::Point(-50, 0).ToString(),
158 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
159
160 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_EAST);
161 EXPECT_EQ(gfx::Point(-50, -25).ToString(),
162 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
163
164 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHEAST);
165 EXPECT_EQ(gfx::Point(-50, -50).ToString(),
166 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
167
168 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTH);
169 EXPECT_EQ(gfx::Point(-25, -50).ToString(),
170 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
171
172 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHWEST);
173 EXPECT_EQ(gfx::Point(0, -50).ToString(),
174 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
175
176 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_WEST);
177 EXPECT_EQ(gfx::Point(0, -25).ToString(),
178 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
179
180 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_CENTER);
181 EXPECT_EQ(gfx::Point(-25, -25).ToString(),
182 CalculateNativeViewOrigin(clip_rect, contents_rect).ToString());
183
184 DestroyHost();
185 }
186
187 // Test that the fast resize path places the clipping and content windows were
188 // they are supposed to be.
189 TEST_F(NativeViewHostAuraTest, FastResizePath) {
190 CreateHost();
191 host()->set_fast_resize(false);
192 toplevel()->SetBounds(gfx::Rect(0, 0, 100, 100));
193 native_host()->ShowWidget(0, 0, 100, 100);
194 host()->set_fast_resize(true);
195
196 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_CENTER);
197 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
198 host()->native_view()->layer()->bounds().ToString());
199 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
200 clipping_window()->layer()->bounds().ToString());
201
202 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_NORTHWEST);
203 native_host()->ShowWidget(0, 0, 50, 50);
204 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
205 host()->native_view()->layer()->bounds().ToString());
206 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
207 clipping_window()->layer()->bounds().ToString());
208
209 host()->set_fast_resize_gravity(NativeViewHost::GRAVITY_SOUTHEAST);
210 native_host()->ShowWidget(0, 0, 50, 50);
211 EXPECT_EQ(gfx::Rect(-50, -50, 100, 100).ToString(),
212 host()->native_view()->layer()->bounds().ToString());
213 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
214 clipping_window()->layer()->bounds().ToString());
215
216 DestroyHost();
217 }
218
219 // Test that destroying the top level widget before destroying the attached
220 // NativeViewHost works correctly. Specifically the associated NVH should be
221 // destroyed and there shouldn't be any errors.
222 TEST_F(NativeViewHostAuraTest, DestroyWidget) {
223 NativeViewHostTesting::ResetDestroyedCount();
224 CreateHost();
225 ReleaseHost();
226 EXPECT_EQ(0, NativeViewHostTesting::destroyed_count());
227 DestroyTopLevel();
228 EXPECT_EQ(1, NativeViewHostTesting::destroyed_count());
229 }
230
98 } // namespace views 231 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698