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

Side by Side Diff: ui/touch_selection/touch_handle_unittest.cc

Issue 481683003: Support for Adaptive Handle Orientation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/touch_selection/touch_handle.h" 5 #include "ui/touch_selection/touch_handle.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/test/motion_event_test_utils.h" 8 #include "ui/events/test/motion_event_test_utils.h"
9 #include "ui/gfx/geometry/rect_f.h" 9 #include "ui/gfx/geometry/rect_f.h"
10 #include "ui/touch_selection/touch_handle_orientation.h" 10 #include "ui/touch_selection/touch_handle_orientation.h"
11 11
12 using ui::test::MockMotionEvent; 12 using ui::test::MockMotionEvent;
13 13
14 namespace ui { 14 namespace ui {
15 namespace { 15 namespace {
16 16
17 const int kDefaultTapDurationMs = 200; 17 const int kDefaultTapDurationMs = 200;
18 const double kDefaultTapSlop = 10.; 18 const double kDefaultTapSlop = 10.;
19 const float kDefaultDrawableSize = 10.f; 19 const float kDefaultDrawableSize = 10.f;
20 const gfx::RectF kDefaultViewportRect(0, 0, 560, 1200);
20 21
21 struct MockDrawableData { 22 struct MockDrawableData {
22 MockDrawableData() 23 MockDrawableData()
23 : orientation(TouchHandleOrientation::UNDEFINED), 24 : orientation(TouchHandleOrientation::UNDEFINED),
24 alpha(0.f), 25 alpha(0.f),
25 enabled(false), 26 enabled(false),
26 visible(false), 27 visible(false),
27 rect(0, 0, kDefaultDrawableSize, kDefaultDrawableSize) {} 28 rect(0, 0, kDefaultDrawableSize, kDefaultDrawableSize) {}
28 TouchHandleOrientation orientation; 29 TouchHandleOrientation orientation;
29 float alpha; 30 float alpha;
31 bool mirror_horizontal;
32 bool mirror_vertical;
30 bool enabled; 33 bool enabled;
31 bool visible; 34 bool visible;
32 gfx::RectF rect; 35 gfx::RectF rect;
33 }; 36 };
34 37
35 class MockTouchHandleDrawable : public TouchHandleDrawable { 38 class MockTouchHandleDrawable : public TouchHandleDrawable {
36 public: 39 public:
37 explicit MockTouchHandleDrawable(MockDrawableData* data) : data_(data) {} 40 explicit MockTouchHandleDrawable(MockDrawableData* data) : data_(data) {}
38 ~MockTouchHandleDrawable() override {} 41 ~MockTouchHandleDrawable() override {}
39 42
40 void SetEnabled(bool enabled) override { data_->enabled = enabled; } 43 void SetEnabled(bool enabled) override { data_->enabled = enabled; }
41 44
42 void SetOrientation(TouchHandleOrientation orientation) override { 45 void SetOrientation(TouchHandleOrientation orientation,
46 bool mirror_vertical,
47 bool mirror_horizontal) override {
43 data_->orientation = orientation; 48 data_->orientation = orientation;
49 data_->mirror_horizontal = mirror_horizontal;
50 data_->mirror_vertical = mirror_vertical;
51 }
52
53 void SetOrigin(const gfx::PointF& origin) override {
54 data_->rect.set_origin(origin);
44 } 55 }
45 56
46 void SetAlpha(float alpha) override { 57 void SetAlpha(float alpha) override {
47 data_->alpha = alpha; 58 data_->alpha = alpha;
48 data_->visible = alpha > 0; 59 data_->visible = alpha > 0;
49 } 60 }
50 61
51 void SetFocus(const gfx::PointF& position) override { 62 // TODO(AviD): Add unittests for non-zero values of padding ratio once the
52 // Anchor focus to the top left of the rect (regardless of orientation). 63 // code refactoring is completed.
53 data_->rect.set_origin(position); 64 float GetDrawableHorizontalPaddingRatio() const override { return 0; }
54 }
55 65
56 gfx::RectF GetVisibleBounds() const override { 66 gfx::RectF GetVisibleBounds() const override {
57 return data_->rect; 67 return data_->rect;
58 } 68 }
59 69
60 private: 70 private:
61 MockDrawableData* data_; 71 MockDrawableData* data_;
62 }; 72 };
63 73
64 } // namespace 74 } // namespace
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 void SetNeedsAnimate() override { needs_animate_ = true; } 108 void SetNeedsAnimate() override { needs_animate_ = true; }
99 109
100 scoped_ptr<TouchHandleDrawable> CreateDrawable() override { 110 scoped_ptr<TouchHandleDrawable> CreateDrawable() override {
101 return make_scoped_ptr(new MockTouchHandleDrawable(&drawable_data_)); 111 return make_scoped_ptr(new MockTouchHandleDrawable(&drawable_data_));
102 } 112 }
103 113
104 base::TimeDelta GetMaxTapDuration() const override { 114 base::TimeDelta GetMaxTapDuration() const override {
105 return base::TimeDelta::FromMilliseconds(kDefaultTapDurationMs); 115 return base::TimeDelta::FromMilliseconds(kDefaultTapDurationMs);
106 } 116 }
107 117
118 bool IsAdaptiveHandleOrientationEnabled() const override {
119 // Enable adaptive handle orientation by default for unittests
120 return true;
121 }
122
108 void Animate(TouchHandle& handle) { 123 void Animate(TouchHandle& handle) {
109 needs_animate_ = false; 124 needs_animate_ = false;
110 base::TimeTicks now = base::TimeTicks::Now(); 125 base::TimeTicks now = base::TimeTicks::Now();
111 while (handle.Animate(now)) 126 while (handle.Animate(now))
112 now += base::TimeDelta::FromMilliseconds(16); 127 now += base::TimeDelta::FromMilliseconds(16);
113 } 128 }
114 129
115 bool GetAndResetHandleDragged() { 130 bool GetAndResetHandleDragged() {
116 bool dragged = dragged_; 131 bool dragged = dragged_;
117 dragged_ = false; 132 dragged_ = false;
118 return dragged; 133 return dragged;
119 } 134 }
120 135
121 bool GetAndResetHandleTapped() { 136 bool GetAndResetHandleTapped() {
122 bool tapped = tapped_; 137 bool tapped = tapped_;
123 tapped_ = false; 138 tapped_ = false;
124 return tapped; 139 return tapped;
125 } 140 }
126 141
127 bool GetAndResetNeedsAnimate() { 142 bool GetAndResetNeedsAnimate() {
128 bool needs_animate = needs_animate_; 143 bool needs_animate = needs_animate_;
129 needs_animate_ = false; 144 needs_animate_ = false;
130 return needs_animate; 145 return needs_animate;
131 } 146 }
132 147
148 void UpdateHandleFocus(TouchHandle& handle,
149 gfx::PointF& top,
150 gfx::PointF& bottom) {
151 handle.SetFocus(top, bottom);
152 handle.UpdateHandleLayout();
153 }
154
155 void UpdateHandleOrientation(TouchHandle& handle,
156 TouchHandleOrientation orientation) {
157 handle.SetOrientation(orientation);
158 handle.UpdateHandleLayout();
159 }
160
161 void UpdateHandleVisibility(TouchHandle& handle,
162 bool visible,
163 TouchHandle::AnimationStyle animation_style) {
164 handle.SetVisible(visible, animation_style);
165 handle.UpdateHandleLayout();
166 }
167
168 void UpdateViewportRect(TouchHandle& handle, gfx::RectF viewport_rect) {
169 handle.SetViewportRect(viewport_rect);
170 handle.UpdateHandleLayout();
171 }
172
133 bool IsDragging() const { return dragging_; } 173 bool IsDragging() const { return dragging_; }
134 const gfx::PointF& DragPosition() const { return drag_position_; } 174 const gfx::PointF& DragPosition() const { return drag_position_; }
135 bool NeedsAnimate() const { return needs_animate_; } 175 bool NeedsAnimate() const { return needs_animate_; }
136 176
137 const MockDrawableData& drawable() { return drawable_data_; } 177 const MockDrawableData& drawable() { return drawable_data_; }
138 178
139 private: 179 private:
140 gfx::PointF drag_position_; 180 gfx::PointF drag_position_;
141 bool dragging_; 181 bool dragging_;
142 bool dragged_; 182 bool dragged_;
143 bool tapped_; 183 bool tapped_;
144 bool needs_animate_; 184 bool needs_animate_;
145 185
146 MockDrawableData drawable_data_; 186 MockDrawableData drawable_data_;
147 }; 187 };
148 188
149 TEST_F(TouchHandleTest, Visibility) { 189 TEST_F(TouchHandleTest, Visibility) {
150 TouchHandle handle(this, TouchHandleOrientation::CENTER); 190 TouchHandle handle(this, TouchHandleOrientation::CENTER,
191 kDefaultViewportRect);
151 EXPECT_FALSE(drawable().visible); 192 EXPECT_FALSE(drawable().visible);
152 193
153 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 194 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
154 EXPECT_TRUE(drawable().visible); 195 EXPECT_TRUE(drawable().visible);
155 EXPECT_EQ(1.f, drawable().alpha); 196 EXPECT_EQ(1.f, drawable().alpha);
156 197
157 handle.SetVisible(false, TouchHandle::ANIMATION_NONE); 198 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_NONE);
158 EXPECT_FALSE(drawable().visible); 199 EXPECT_FALSE(drawable().visible);
159 200
160 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 201 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
161 EXPECT_TRUE(drawable().visible); 202 EXPECT_TRUE(drawable().visible);
162 EXPECT_EQ(1.f, drawable().alpha); 203 EXPECT_EQ(1.f, drawable().alpha);
163 } 204 }
164 205
165 TEST_F(TouchHandleTest, VisibilityAnimation) { 206 TEST_F(TouchHandleTest, VisibilityAnimation) {
166 TouchHandle handle(this, TouchHandleOrientation::CENTER); 207 TouchHandle handle(this, TouchHandleOrientation::CENTER,
208 kDefaultViewportRect);
167 ASSERT_FALSE(NeedsAnimate()); 209 ASSERT_FALSE(NeedsAnimate());
168 ASSERT_FALSE(drawable().visible); 210 ASSERT_FALSE(drawable().visible);
169 ASSERT_EQ(0.f, drawable().alpha); 211 ASSERT_EQ(0.f, drawable().alpha);
170 212
171 handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); 213 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_SMOOTH);
172 EXPECT_TRUE(NeedsAnimate()); 214 EXPECT_TRUE(NeedsAnimate());
173 EXPECT_FALSE(drawable().visible); 215 EXPECT_FALSE(drawable().visible);
174 EXPECT_EQ(0.f, drawable().alpha); 216 EXPECT_EQ(0.f, drawable().alpha);
175 217
176 Animate(handle); 218 Animate(handle);
177 EXPECT_TRUE(drawable().visible); 219 EXPECT_TRUE(drawable().visible);
178 EXPECT_EQ(1.f, drawable().alpha); 220 EXPECT_EQ(1.f, drawable().alpha);
179 221
180 ASSERT_FALSE(NeedsAnimate()); 222 ASSERT_FALSE(NeedsAnimate());
181 handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); 223 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_SMOOTH);
182 EXPECT_TRUE(NeedsAnimate()); 224 EXPECT_TRUE(NeedsAnimate());
183 EXPECT_TRUE(drawable().visible); 225 EXPECT_TRUE(drawable().visible);
184 EXPECT_EQ(1.f, drawable().alpha); 226 EXPECT_EQ(1.f, drawable().alpha);
185 227
186 Animate(handle); 228 Animate(handle);
187 EXPECT_FALSE(drawable().visible); 229 EXPECT_FALSE(drawable().visible);
188 EXPECT_EQ(0.f, drawable().alpha); 230 EXPECT_EQ(0.f, drawable().alpha);
189 231
190 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 232 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
191 EXPECT_EQ(1.f, drawable().alpha); 233 EXPECT_EQ(1.f, drawable().alpha);
192 EXPECT_FALSE(GetAndResetNeedsAnimate()); 234 EXPECT_FALSE(GetAndResetNeedsAnimate());
193 handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); 235 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_SMOOTH);
194 EXPECT_EQ(1.f, drawable().alpha); 236 EXPECT_EQ(1.f, drawable().alpha);
195 EXPECT_TRUE(GetAndResetNeedsAnimate()); 237 EXPECT_TRUE(GetAndResetNeedsAnimate());
196 handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); 238 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_SMOOTH);
197 EXPECT_EQ(1.f, drawable().alpha); 239 EXPECT_EQ(1.f, drawable().alpha);
198 EXPECT_FALSE(GetAndResetNeedsAnimate()); 240 EXPECT_FALSE(GetAndResetNeedsAnimate());
199 } 241 }
200 242
201 TEST_F(TouchHandleTest, Orientation) { 243 TEST_F(TouchHandleTest, Orientation) {
202 TouchHandle handle(this, TouchHandleOrientation::CENTER); 244 TouchHandle handle(this, TouchHandleOrientation::CENTER,
245 kDefaultViewportRect);
203 EXPECT_EQ(TouchHandleOrientation::CENTER, drawable().orientation); 246 EXPECT_EQ(TouchHandleOrientation::CENTER, drawable().orientation);
204 247
205 handle.SetOrientation(TouchHandleOrientation::LEFT); 248 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
249 UpdateHandleOrientation(handle, TouchHandleOrientation::LEFT);
206 EXPECT_EQ(TouchHandleOrientation::LEFT, drawable().orientation); 250 EXPECT_EQ(TouchHandleOrientation::LEFT, drawable().orientation);
207 251
208 handle.SetOrientation(TouchHandleOrientation::RIGHT); 252 UpdateHandleOrientation(handle, TouchHandleOrientation::RIGHT);
209 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation); 253 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation);
210 254
211 handle.SetOrientation(TouchHandleOrientation::CENTER); 255 UpdateHandleOrientation(handle, TouchHandleOrientation::CENTER);
212 EXPECT_EQ(TouchHandleOrientation::CENTER, drawable().orientation); 256 EXPECT_EQ(TouchHandleOrientation::CENTER, drawable().orientation);
213 } 257 }
214 258
215 TEST_F(TouchHandleTest, Position) { 259 TEST_F(TouchHandleTest, Position) {
216 TouchHandle handle(this, TouchHandleOrientation::CENTER); 260 TouchHandle handle(this, TouchHandleOrientation::CENTER,
217 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 261 kDefaultViewportRect);
262 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
218 263
219 gfx::PointF position; 264 const gfx::Vector2dF koffset_vector(kDefaultDrawableSize / 2.f, 0);
220 EXPECT_EQ(gfx::PointF(), drawable().rect.origin()); 265 gfx::PointF focus_top;
266 gfx::PointF focus_bottom;
267 EXPECT_EQ(gfx::PointF() - koffset_vector, drawable().rect.origin());
221 268
222 position = gfx::PointF(7.3f, -3.7f); 269 focus_top = gfx::PointF(7.3f, -3.7f);
223 handle.SetPosition(position); 270 focus_bottom = gfx::PointF(7.3f, -2.7f);
224 EXPECT_EQ(position, drawable().rect.origin()); 271 UpdateHandleFocus(handle, focus_top, focus_bottom);
272 EXPECT_EQ(focus_bottom - koffset_vector, drawable().rect.origin());
225 273
226 position = gfx::PointF(-7.3f, 3.7f); 274 focus_top = gfx::PointF(-7.3f, 3.7f);
227 handle.SetPosition(position); 275 focus_bottom = gfx::PointF(-7.3f, 4.7f);
228 EXPECT_EQ(position, drawable().rect.origin()); 276 UpdateHandleFocus(handle, focus_top, focus_bottom);
277 EXPECT_EQ(focus_bottom - koffset_vector, drawable().rect.origin());
229 } 278 }
230 279
231 TEST_F(TouchHandleTest, PositionNotUpdatedWhileFadingOrInvisible) { 280 TEST_F(TouchHandleTest, PositionNotUpdatedWhileFadingOrInvisible) {
232 TouchHandle handle(this, TouchHandleOrientation::CENTER); 281 TouchHandle handle(this, TouchHandleOrientation::CENTER,
282 kDefaultViewportRect);
233 283
234 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 284 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
235 ASSERT_TRUE(drawable().visible); 285 ASSERT_TRUE(drawable().visible);
236 ASSERT_FALSE(NeedsAnimate()); 286 ASSERT_FALSE(NeedsAnimate());
237 287
238 gfx::PointF old_position(7.3f, -3.7f); 288 const gfx::Vector2dF koffset_vector(kDefaultDrawableSize / 2.f, 0);
239 handle.SetPosition(old_position); 289 gfx::PointF old_focus_top(7.3f, -3.7f);
240 ASSERT_EQ(old_position, drawable().rect.origin()); 290 gfx::PointF old_focus_bottom(7.3f, -2.7f);
291 UpdateHandleFocus(handle, old_focus_top, old_focus_bottom);
292 ASSERT_EQ(old_focus_bottom - koffset_vector, drawable().rect.origin());
241 293
242 handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); 294 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_SMOOTH);
243 ASSERT_TRUE(NeedsAnimate()); 295 ASSERT_TRUE(NeedsAnimate());
244 296
245 gfx::PointF new_position(3.7f, -3.7f); 297 gfx::PointF new_position_top(3.7f, -3.7f);
246 handle.SetPosition(new_position); 298 gfx::PointF new_position_bottom(3.7f, -2.7f);
247 EXPECT_EQ(old_position, drawable().rect.origin()); 299 UpdateHandleFocus(handle, new_position_top, new_position_bottom);
300 EXPECT_EQ(old_focus_bottom - koffset_vector, drawable().rect.origin());
248 EXPECT_TRUE(NeedsAnimate()); 301 EXPECT_TRUE(NeedsAnimate());
249 302
250 // While the handle is fading, the new position should not take affect. 303 // While the handle is fading, the new position should not take affect.
251 base::TimeTicks now = base::TimeTicks::Now(); 304 base::TimeTicks now = base::TimeTicks::Now();
252 while (handle.Animate(now)) { 305 while (handle.Animate(now)) {
253 EXPECT_EQ(old_position, drawable().rect.origin()); 306 EXPECT_EQ(old_focus_bottom - koffset_vector, drawable().rect.origin());
254 now += base::TimeDelta::FromMilliseconds(16); 307 now += base::TimeDelta::FromMilliseconds(16);
255 } 308 }
256 309
257 // Even after the animation terminates, the new position will not be pushed. 310 // Even after the animation terminates, the new position will not be pushed.
258 EXPECT_EQ(old_position, drawable().rect.origin()); 311 EXPECT_EQ(old_focus_bottom - koffset_vector, drawable().rect.origin());
259 312
260 // As soon as the handle becomes visible, the new position will be pushed. 313 // As soon as the handle becomes visible, the new position will be pushed.
261 handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); 314 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_SMOOTH);
262 EXPECT_EQ(new_position, drawable().rect.origin()); 315 EXPECT_EQ(new_position_bottom - koffset_vector, drawable().rect.origin());
263 } 316 }
264 317
265 TEST_F(TouchHandleTest, Enabled) { 318 TEST_F(TouchHandleTest, Enabled) {
266 // A newly created handle defaults to enabled. 319 // A newly created handle defaults to enabled.
267 TouchHandle handle(this, TouchHandleOrientation::CENTER); 320 TouchHandle handle(this, TouchHandleOrientation::CENTER,
321 kDefaultViewportRect);
268 EXPECT_TRUE(drawable().enabled); 322 EXPECT_TRUE(drawable().enabled);
269 323
270 handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); 324 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_SMOOTH);
271 EXPECT_TRUE(GetAndResetNeedsAnimate()); 325 EXPECT_TRUE(GetAndResetNeedsAnimate());
272 EXPECT_EQ(0.f, drawable().alpha); 326 EXPECT_EQ(0.f, drawable().alpha);
273 handle.SetEnabled(false); 327 handle.SetEnabled(false);
274 EXPECT_FALSE(drawable().enabled); 328 EXPECT_FALSE(drawable().enabled);
275 329
276 // Dragging should not be allowed while the handle is disabled. 330 // Dragging should not be allowed while the handle is disabled.
277 base::TimeTicks event_time = base::TimeTicks::Now(); 331 base::TimeTicks event_time = base::TimeTicks::Now();
278 const float kOffset = kDefaultDrawableSize / 2.f; 332 const float kOffset = kDefaultDrawableSize / 2.f;
279 MockMotionEvent event( 333 MockMotionEvent event(
280 MockMotionEvent::ACTION_DOWN, event_time, kOffset, kOffset); 334 MockMotionEvent::ACTION_DOWN, event_time, kOffset, kOffset);
281 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 335 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
282 336
283 // Disabling mid-animation should cancel the animation. 337 // Disabling mid-animation should cancel the animation.
284 handle.SetEnabled(true); 338 handle.SetEnabled(true);
285 handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); 339 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_SMOOTH);
286 EXPECT_TRUE(drawable().visible); 340 EXPECT_TRUE(drawable().visible);
287 EXPECT_TRUE(GetAndResetNeedsAnimate()); 341 EXPECT_TRUE(GetAndResetNeedsAnimate());
288 handle.SetEnabled(false); 342 handle.SetEnabled(false);
289 EXPECT_FALSE(drawable().enabled); 343 EXPECT_FALSE(drawable().enabled);
290 EXPECT_FALSE(drawable().visible); 344 EXPECT_FALSE(drawable().visible);
291 EXPECT_FALSE(handle.Animate(base::TimeTicks::Now())); 345 EXPECT_FALSE(handle.Animate(base::TimeTicks::Now()));
292 346
293 // Disabling mid-drag should cancel the drag. 347 // Disabling mid-drag should cancel the drag.
294 handle.SetEnabled(true); 348 handle.SetEnabled(true);
295 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 349 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
296 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 350 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
297 EXPECT_TRUE(IsDragging()); 351 EXPECT_TRUE(IsDragging());
298 handle.SetEnabled(false); 352 handle.SetEnabled(false);
299 EXPECT_FALSE(IsDragging()); 353 EXPECT_FALSE(IsDragging());
300 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 354 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
301 } 355 }
302 356
303 TEST_F(TouchHandleTest, Drag) { 357 TEST_F(TouchHandleTest, Drag) {
304 TouchHandle handle(this, TouchHandleOrientation::CENTER); 358 TouchHandle handle(this, TouchHandleOrientation::CENTER,
359 kDefaultViewportRect);
305 360
306 base::TimeTicks event_time = base::TimeTicks::Now(); 361 base::TimeTicks event_time = base::TimeTicks::Now();
307 const float kOffset = kDefaultDrawableSize / 2.f; 362 const float kOffset = kDefaultDrawableSize / 2.f;
308 363
309 // The handle must be visible to trigger drag. 364 // The handle must be visible to trigger drag.
310 MockMotionEvent event( 365 MockMotionEvent event(
311 MockMotionEvent::ACTION_DOWN, event_time, kOffset, kOffset); 366 MockMotionEvent::ACTION_DOWN, event_time, kOffset, kOffset);
312 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 367 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
313 EXPECT_FALSE(IsDragging()); 368 EXPECT_FALSE(IsDragging());
314 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 369 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
315 370
316 // ACTION_DOWN must fall within the drawable region to trigger drag. 371 // ACTION_DOWN must fall within the drawable region to trigger drag.
317 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 50, 50); 372 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 50, 50);
318 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 373 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
319 EXPECT_FALSE(IsDragging()); 374 EXPECT_FALSE(IsDragging());
320 375
321 // Only ACTION_DOWN will trigger drag. 376 // Only ACTION_DOWN will trigger drag.
322 event = MockMotionEvent( 377 event = MockMotionEvent(
323 MockMotionEvent::ACTION_MOVE, event_time, kOffset, kOffset); 378 MockMotionEvent::ACTION_MOVE, event_time, kOffset, kOffset);
324 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 379 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
(...skipping 23 matching lines...) Expand all
348 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 403 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
349 EXPECT_FALSE(GetAndResetHandleDragged()); 404 EXPECT_FALSE(GetAndResetHandleDragged());
350 EXPECT_FALSE(IsDragging()); 405 EXPECT_FALSE(IsDragging());
351 406
352 // Non-ACTION_DOWN events after the drag has terminated should not be handled. 407 // Non-ACTION_DOWN events after the drag has terminated should not be handled.
353 event = MockMotionEvent(MockMotionEvent::ACTION_CANCEL); 408 event = MockMotionEvent(MockMotionEvent::ACTION_CANCEL);
354 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 409 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
355 } 410 }
356 411
357 TEST_F(TouchHandleTest, DragDefersOrientationChange) { 412 TEST_F(TouchHandleTest, DragDefersOrientationChange) {
358 TouchHandle handle(this, TouchHandleOrientation::RIGHT); 413 TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect);
359 ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT); 414 ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT);
360 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 415 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
361 416
362 MockMotionEvent event(MockMotionEvent::ACTION_DOWN); 417 MockMotionEvent event(MockMotionEvent::ACTION_DOWN);
363 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 418 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
364 EXPECT_TRUE(IsDragging()); 419 EXPECT_TRUE(IsDragging());
365 420
366 // Orientation changes will be deferred until the drag ends. 421 // Orientation changes will be deferred until the drag ends.
367 handle.SetOrientation(TouchHandleOrientation::LEFT); 422 UpdateHandleOrientation(handle, TouchHandleOrientation::LEFT);
368 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation); 423 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation);
369 424
370 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE); 425 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE);
371 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 426 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
372 EXPECT_TRUE(GetAndResetHandleDragged()); 427 EXPECT_TRUE(GetAndResetHandleDragged());
373 EXPECT_TRUE(IsDragging()); 428 EXPECT_TRUE(IsDragging());
374 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation); 429 EXPECT_EQ(TouchHandleOrientation::RIGHT, drawable().orientation);
375 430
376 event = MockMotionEvent(MockMotionEvent::ACTION_UP); 431 event = MockMotionEvent(MockMotionEvent::ACTION_UP);
377 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 432 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
378 EXPECT_FALSE(GetAndResetHandleDragged()); 433 EXPECT_FALSE(GetAndResetHandleDragged());
379 EXPECT_FALSE(IsDragging()); 434 EXPECT_FALSE(IsDragging());
380 EXPECT_EQ(TouchHandleOrientation::LEFT, drawable().orientation); 435 EXPECT_EQ(TouchHandleOrientation::LEFT, drawable().orientation);
381 } 436 }
382 437
383 TEST_F(TouchHandleTest, DragDefersFade) { 438 TEST_F(TouchHandleTest, DragDefersFade) {
384 TouchHandle handle(this, TouchHandleOrientation::CENTER); 439 TouchHandle handle(this, TouchHandleOrientation::CENTER,
385 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 440 kDefaultViewportRect);
441 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
386 442
387 MockMotionEvent event(MockMotionEvent::ACTION_DOWN); 443 MockMotionEvent event(MockMotionEvent::ACTION_DOWN);
388 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 444 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
389 EXPECT_TRUE(IsDragging()); 445 EXPECT_TRUE(IsDragging());
390 446
391 // Fade will be deferred until the drag ends. 447 // Fade will be deferred until the drag ends.
392 handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); 448 UpdateHandleVisibility(handle, false, TouchHandle::ANIMATION_SMOOTH);
393 EXPECT_FALSE(NeedsAnimate()); 449 EXPECT_FALSE(NeedsAnimate());
394 EXPECT_TRUE(drawable().visible); 450 EXPECT_TRUE(drawable().visible);
395 EXPECT_EQ(1.f, drawable().alpha); 451 EXPECT_EQ(1.f, drawable().alpha);
396 452
397 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE); 453 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE);
398 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 454 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
399 EXPECT_FALSE(NeedsAnimate()); 455 EXPECT_FALSE(NeedsAnimate());
400 EXPECT_TRUE(drawable().visible); 456 EXPECT_TRUE(drawable().visible);
401 457
402 event = MockMotionEvent(MockMotionEvent::ACTION_UP); 458 event = MockMotionEvent(MockMotionEvent::ACTION_UP);
403 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 459 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
404 EXPECT_FALSE(IsDragging()); 460 EXPECT_FALSE(IsDragging());
405 EXPECT_TRUE(NeedsAnimate()); 461 EXPECT_TRUE(NeedsAnimate());
406 462
407 Animate(handle); 463 Animate(handle);
408 EXPECT_FALSE(drawable().visible); 464 EXPECT_FALSE(drawable().visible);
409 EXPECT_EQ(0.f, drawable().alpha); 465 EXPECT_EQ(0.f, drawable().alpha);
410 } 466 }
411 467
412 TEST_F(TouchHandleTest, DragTargettingUsesTouchSize) { 468 TEST_F(TouchHandleTest, DragTargettingUsesTouchSize) {
413 TouchHandle handle(this, TouchHandleOrientation::CENTER); 469 TouchHandle handle(this, TouchHandleOrientation::CENTER,
414 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 470 kDefaultViewportRect);
471 gfx::PointF focus_top(kDefaultDrawableSize / 2, 0);
472 gfx::PointF focus_bottom(kDefaultDrawableSize / 2, 0);
473 UpdateHandleFocus(handle, focus_top, focus_bottom);
474 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
415 475
416 base::TimeTicks event_time = base::TimeTicks::Now(); 476 base::TimeTicks event_time = base::TimeTicks::Now();
417 const float kTouchSize = 24.f; 477 const float kTouchSize = 24.f;
418 const float kOffset = kDefaultDrawableSize + kTouchSize / 2.001f; 478 const float kOffset = kDefaultDrawableSize + kTouchSize / 2.001f;
419 479
420 MockMotionEvent event( 480 MockMotionEvent event(
421 MockMotionEvent::ACTION_DOWN, event_time, kOffset, 0); 481 MockMotionEvent::ACTION_DOWN, event_time, kOffset, 0);
422 event.SetTouchMajor(0.f); 482 event.SetTouchMajor(0.f);
423 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 483 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
424 EXPECT_FALSE(IsDragging()); 484 EXPECT_FALSE(IsDragging());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 // Touches centered above the handle region should never register a hit, even 522 // Touches centered above the handle region should never register a hit, even
463 // if the touch region intersects the handle region. 523 // if the touch region intersects the handle region.
464 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 524 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time,
465 kDefaultDrawableSize / 2.f, -kTouchSize / 3.f); 525 kDefaultDrawableSize / 2.f, -kTouchSize / 3.f);
466 event.SetTouchMajor(kTouchSize); 526 event.SetTouchMajor(kTouchSize);
467 EXPECT_FALSE(handle.WillHandleTouchEvent(event)); 527 EXPECT_FALSE(handle.WillHandleTouchEvent(event));
468 EXPECT_FALSE(IsDragging()); 528 EXPECT_FALSE(IsDragging());
469 } 529 }
470 530
471 TEST_F(TouchHandleTest, Tap) { 531 TEST_F(TouchHandleTest, Tap) {
472 TouchHandle handle(this, TouchHandleOrientation::CENTER); 532 TouchHandle handle(this, TouchHandleOrientation::CENTER,
473 handle.SetVisible(true, TouchHandle::ANIMATION_NONE); 533 kDefaultViewportRect);
534 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
474 535
475 base::TimeTicks event_time = base::TimeTicks::Now(); 536 base::TimeTicks event_time = base::TimeTicks::Now();
476 537
477 // ACTION_CANCEL shouldn't trigger a tap. 538 // ACTION_CANCEL shouldn't trigger a tap.
478 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0); 539 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
479 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 540 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
480 event_time += base::TimeDelta::FromMilliseconds(50); 541 event_time += base::TimeDelta::FromMilliseconds(50);
481 event = MockMotionEvent(MockMotionEvent::ACTION_CANCEL, event_time, 0, 0); 542 event = MockMotionEvent(MockMotionEvent::ACTION_CANCEL, event_time, 0, 0);
482 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 543 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
483 EXPECT_FALSE(GetAndResetHandleTapped()); 544 EXPECT_FALSE(GetAndResetHandleTapped());
(...skipping 24 matching lines...) Expand all
508 event_time += GetMaxTapDuration() / 2; 569 event_time += GetMaxTapDuration() / 2;
509 event = MockMotionEvent( 570 event = MockMotionEvent(
510 MockMotionEvent::ACTION_MOVE, event_time, kDefaultTapSlop * 2.f, 0); 571 MockMotionEvent::ACTION_MOVE, event_time, kDefaultTapSlop * 2.f, 0);
511 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 572 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
512 event = MockMotionEvent( 573 event = MockMotionEvent(
513 MockMotionEvent::ACTION_UP, event_time, kDefaultTapSlop * 2.f, 0); 574 MockMotionEvent::ACTION_UP, event_time, kDefaultTapSlop * 2.f, 0);
514 EXPECT_TRUE(handle.WillHandleTouchEvent(event)); 575 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
515 EXPECT_FALSE(GetAndResetHandleTapped()); 576 EXPECT_FALSE(GetAndResetHandleTapped());
516 } 577 }
517 578
579 TEST_F(TouchHandleTest, MirrorFocusChange) {
580 TouchHandle handle(this, TouchHandleOrientation::LEFT, kDefaultViewportRect);
581 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
582
583 gfx::PointF focus_top;
584 gfx::PointF focus_bottom;
585 EXPECT_EQ(gfx::PointF(), drawable().rect.origin());
586
587 // Moving the selection to the bottom of the screen
588 // should mirror the handle vertically.
589 focus_top = gfx::PointF(17.3f, 1199.0f);
590 focus_bottom = gfx::PointF(17.3f, 1200.0f);
591 UpdateHandleFocus(handle, focus_top, focus_bottom);
592 EXPECT_TRUE(drawable().mirror_vertical);
593 EXPECT_FALSE(drawable().mirror_horizontal);
594
595 // Moving the left handle to the left edge of the viewport
596 // should mirror the handle horizontally as well.
597 focus_top = gfx::PointF(2.3f, 1199.0f);
598 focus_bottom = gfx::PointF(2.3f, 1200.0f);
599 UpdateHandleFocus(handle, focus_top, focus_bottom);
600 EXPECT_TRUE(drawable().mirror_vertical);
601 EXPECT_TRUE(drawable().mirror_horizontal);
602
603 // When the selection is not at the bottom, only the
604 // horizontal mirror flag should be true.
605 focus_top = gfx::PointF(2.3f, 7.3f);
606 focus_bottom = gfx::PointF(2.3f, 8.3f);
607 UpdateHandleFocus(handle, focus_top, focus_bottom);
608 EXPECT_FALSE(drawable().mirror_vertical);
609 EXPECT_TRUE(drawable().mirror_horizontal);
610
611 // When selection handles intersects the viewport fully,
612 // both mirror values should be false.
613 focus_top = gfx::PointF(23.3f, 7.3f);
614 focus_bottom = gfx::PointF(23.3f, 8.3f);
615 UpdateHandleFocus(handle, focus_top, focus_bottom);
616 EXPECT_FALSE(drawable().mirror_vertical);
617 EXPECT_FALSE(drawable().mirror_horizontal);
618
619 // Horizontal mirror should be true for Right handle when
620 // the handle is at theright edge of the viewport.
621 UpdateHandleOrientation(handle, TouchHandleOrientation::RIGHT);
622 focus_top = gfx::PointF(560.0f, 7.3f);
623 focus_bottom = gfx::PointF(560.0f, 8.3f);
624 UpdateHandleFocus(handle, focus_top, focus_bottom);
625 EXPECT_FALSE(drawable().mirror_vertical);
626 EXPECT_TRUE(drawable().mirror_horizontal);
627 }
628
629 TEST_F(TouchHandleTest, DragDefersMirrorChange) {
630 TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect);
631 ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT);
632 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
633
634 base::TimeTicks event_time = base::TimeTicks::Now();
635 const float kOffset = kDefaultDrawableSize / 2.f;
636
637 // Start the drag.
638 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, kOffset,
639 kOffset);
640 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
641 EXPECT_TRUE(IsDragging());
642
643 UpdateHandleOrientation(handle, TouchHandleOrientation::LEFT);
644 gfx::PointF focus_top(17.3f, 1199.0f);
645 gfx::PointF focus_bottom(17.3f, 1200.0f);
646 UpdateHandleFocus(handle, focus_top, focus_bottom);
647 EXPECT_FALSE(drawable().mirror_vertical);
648 EXPECT_FALSE(drawable().mirror_horizontal);
649
650 // Mirror flag changes will be deferred until the drag ends.
651 event = MockMotionEvent(MockMotionEvent::ACTION_UP);
652 EXPECT_TRUE(handle.WillHandleTouchEvent(event));
653 EXPECT_FALSE(GetAndResetHandleDragged());
654 EXPECT_FALSE(IsDragging());
655 EXPECT_TRUE(drawable().mirror_vertical);
656 EXPECT_FALSE(drawable().mirror_horizontal);
657 }
658
659 TEST_F(TouchHandleTest, ViewportSizeChange) {
660 TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect);
661 ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT);
662 UpdateHandleVisibility(handle, true, TouchHandle::ANIMATION_NONE);
663
664 gfx::PointF focus_top;
665 gfx::PointF focus_bottom;
666 EXPECT_EQ(gfx::PointF(), drawable().rect.origin());
667
668 focus_top = gfx::PointF(230.0f, 599.0f);
669 focus_bottom = gfx::PointF(230.0f, 600.0f);
670 UpdateHandleFocus(handle, focus_top, focus_bottom);
671 EXPECT_FALSE(drawable().mirror_vertical);
672 EXPECT_FALSE(drawable().mirror_horizontal);
673
674 UpdateViewportRect(handle, gfx::RectF(0, 0, 560, 600));
675 EXPECT_TRUE(drawable().mirror_vertical);
676 EXPECT_FALSE(drawable().mirror_horizontal);
677
678 UpdateViewportRect(handle, gfx::RectF(0, 0, 230, 600));
679 EXPECT_TRUE(drawable().mirror_vertical);
680 EXPECT_TRUE(drawable().mirror_horizontal);
681
682 UpdateViewportRect(handle, kDefaultViewportRect);
683 EXPECT_FALSE(drawable().mirror_vertical);
684 EXPECT_FALSE(drawable().mirror_horizontal);
685 }
686
518 } // namespace ui 687 } // namespace ui
OLDNEW
« no previous file with comments | « ui/touch_selection/touch_handle_drawable_aura.cc ('k') | ui/touch_selection/touch_selection_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698