| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "content/browser/renderer_host/input/synthetic_gesture.h" | 8 #include "content/browser/renderer_host/input/synthetic_gesture.h" |
| 9 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" | 9 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" |
| 10 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" | 10 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 using blink::WebMouseEvent; | 29 using blink::WebMouseEvent; |
| 30 using blink::WebMouseWheelEvent; | 30 using blink::WebMouseWheelEvent; |
| 31 using blink::WebTouchEvent; | 31 using blink::WebTouchEvent; |
| 32 | 32 |
| 33 namespace content { | 33 namespace content { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const int kFlushInputRateInMs = 16; | 37 const int kFlushInputRateInMs = 16; |
| 38 const int kPointerAssumedStoppedTimeMs = 43; | 38 const int kPointerAssumedStoppedTimeMs = 43; |
| 39 const int kTouchSlopInDips = 7; | 39 const float kTouchSlopInDips = 7.2f; |
| 40 const float kMinScalingSpanInDips = 27.5f; |
| 40 | 41 |
| 41 class MockSyntheticGesture : public SyntheticGesture { | 42 class MockSyntheticGesture : public SyntheticGesture { |
| 42 public: | 43 public: |
| 43 MockSyntheticGesture(bool* finished, int num_steps) | 44 MockSyntheticGesture(bool* finished, int num_steps) |
| 44 : finished_(finished), | 45 : finished_(finished), |
| 45 num_steps_(num_steps), | 46 num_steps_(num_steps), |
| 46 step_count_(0) { | 47 step_count_(0) { |
| 47 *finished_ = false; | 48 *finished_ = false; |
| 48 } | 49 } |
| 49 virtual ~MockSyntheticGesture() {} | 50 virtual ~MockSyntheticGesture() {} |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 91 } |
| 91 | 92 |
| 92 virtual base::TimeDelta PointerAssumedStoppedTime() const OVERRIDE { | 93 virtual base::TimeDelta PointerAssumedStoppedTime() const OVERRIDE { |
| 93 return base::TimeDelta::FromMilliseconds(pointer_assumed_stopped_time_ms_); | 94 return base::TimeDelta::FromMilliseconds(pointer_assumed_stopped_time_ms_); |
| 94 } | 95 } |
| 95 | 96 |
| 96 void set_pointer_assumed_stopped_time_ms(int time_ms) { | 97 void set_pointer_assumed_stopped_time_ms(int time_ms) { |
| 97 pointer_assumed_stopped_time_ms_ = time_ms; | 98 pointer_assumed_stopped_time_ms_ = time_ms; |
| 98 } | 99 } |
| 99 | 100 |
| 100 virtual int GetTouchSlopInDips() const OVERRIDE { | 101 virtual float GetTouchSlopInDips() const OVERRIDE { |
| 101 return kTouchSlopInDips; | 102 return kTouchSlopInDips; |
| 102 } | 103 } |
| 103 | 104 |
| 105 virtual float GetMinScalingSpanInDips() const OVERRIDE { |
| 106 return kMinScalingSpanInDips; |
| 107 } |
| 108 |
| 104 bool flush_requested() const { return flush_requested_; } | 109 bool flush_requested() const { return flush_requested_; } |
| 105 void ClearFlushRequest() { flush_requested_ = false; } | 110 void ClearFlushRequest() { flush_requested_ = false; } |
| 106 | 111 |
| 107 private: | 112 private: |
| 108 bool flush_requested_; | 113 bool flush_requested_; |
| 109 | 114 |
| 110 int pointer_assumed_stopped_time_ms_; | 115 int pointer_assumed_stopped_time_ms_; |
| 111 }; | 116 }; |
| 112 | 117 |
| 113 class MockScrollGestureTarget : public MockSyntheticGestureTarget { | 118 class MockScrollGestureTarget : public MockSyntheticGestureTarget { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 189 |
| 185 class MockSyntheticPinchTouchTarget : public MockSyntheticGestureTarget { | 190 class MockSyntheticPinchTouchTarget : public MockSyntheticGestureTarget { |
| 186 public: | 191 public: |
| 187 enum ZoomDirection { | 192 enum ZoomDirection { |
| 188 ZOOM_DIRECTION_UNKNOWN, | 193 ZOOM_DIRECTION_UNKNOWN, |
| 189 ZOOM_IN, | 194 ZOOM_IN, |
| 190 ZOOM_OUT | 195 ZOOM_OUT |
| 191 }; | 196 }; |
| 192 | 197 |
| 193 MockSyntheticPinchTouchTarget() | 198 MockSyntheticPinchTouchTarget() |
| 194 : total_num_pixels_covered_(0), | 199 : initial_pointer_distance_(0), |
| 195 last_pointer_distance_(0), | 200 last_pointer_distance_(0), |
| 196 zoom_direction_(ZOOM_DIRECTION_UNKNOWN), | 201 zoom_direction_(ZOOM_DIRECTION_UNKNOWN), |
| 197 started_(false) {} | 202 started_(false) {} |
| 198 virtual ~MockSyntheticPinchTouchTarget() {} | 203 virtual ~MockSyntheticPinchTouchTarget() {} |
| 199 | 204 |
| 200 virtual void DispatchInputEventToPlatform( | 205 virtual void DispatchInputEventToPlatform( |
| 201 const WebInputEvent& event) OVERRIDE { | 206 const WebInputEvent& event) OVERRIDE { |
| 202 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); | 207 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); |
| 203 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); | 208 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); |
| 204 ASSERT_EQ(touch_event.touchesLength, 2U); | 209 ASSERT_EQ(touch_event.touchesLength, 2U); |
| 205 | 210 |
| 206 if (!started_) { | 211 if (!started_) { |
| 207 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart); | 212 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart); |
| 208 | 213 |
| 209 start_0_ = gfx::PointF(touch_event.touches[0].position); | 214 start_0_ = gfx::PointF(touch_event.touches[0].position); |
| 210 start_1_ = gfx::PointF(touch_event.touches[1].position); | 215 start_1_ = gfx::PointF(touch_event.touches[1].position); |
| 211 last_pointer_distance_ = (start_0_ - start_1_).Length(); | 216 last_pointer_distance_ = (start_0_ - start_1_).Length(); |
| 217 initial_pointer_distance_ = last_pointer_distance_; |
| 218 EXPECT_GE(initial_pointer_distance_, GetMinScalingSpanInDips()); |
| 212 | 219 |
| 213 started_ = true; | 220 started_ = true; |
| 214 } else { | 221 } else { |
| 215 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart); | 222 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart); |
| 216 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel); | 223 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel); |
| 217 | 224 |
| 218 gfx::PointF current_0 = gfx::PointF(touch_event.touches[0].position); | 225 gfx::PointF current_0 = gfx::PointF(touch_event.touches[0].position); |
| 219 gfx::PointF current_1 = gfx::PointF(touch_event.touches[1].position); | 226 gfx::PointF current_1 = gfx::PointF(touch_event.touches[1].position); |
| 220 | 227 |
| 221 total_num_pixels_covered_ = | |
| 222 (current_0 - start_0_).Length() + (current_1 - start_1_).Length(); | |
| 223 float pointer_distance = (current_0 - current_1).Length(); | 228 float pointer_distance = (current_0 - current_1).Length(); |
| 224 | 229 |
| 225 if (last_pointer_distance_ != pointer_distance) { | 230 if (last_pointer_distance_ != pointer_distance) { |
| 226 if (zoom_direction_ == ZOOM_DIRECTION_UNKNOWN) | 231 if (zoom_direction_ == ZOOM_DIRECTION_UNKNOWN) |
| 227 zoom_direction_ = | 232 zoom_direction_ = |
| 228 ComputeZoomDirection(last_pointer_distance_, pointer_distance); | 233 ComputeZoomDirection(last_pointer_distance_, pointer_distance); |
| 229 else | 234 else |
| 230 EXPECT_EQ( | 235 EXPECT_EQ( |
| 231 zoom_direction_, | 236 zoom_direction_, |
| 232 ComputeZoomDirection(last_pointer_distance_, pointer_distance)); | 237 ComputeZoomDirection(last_pointer_distance_, pointer_distance)); |
| 233 } | 238 } |
| 234 | 239 |
| 235 last_pointer_distance_ = pointer_distance; | 240 last_pointer_distance_ = pointer_distance; |
| 236 } | 241 } |
| 237 } | 242 } |
| 238 | 243 |
| 239 float total_num_pixels_covered() const { return total_num_pixels_covered_; } | |
| 240 ZoomDirection zoom_direction() const { return zoom_direction_; } | 244 ZoomDirection zoom_direction() const { return zoom_direction_; } |
| 241 | 245 |
| 246 float ComputeScaleFactor() const { |
| 247 switch (zoom_direction_) { |
| 248 case ZOOM_IN: |
| 249 return last_pointer_distance_ / |
| 250 (initial_pointer_distance_ + 2 * GetTouchSlopInDips()); |
| 251 case ZOOM_OUT: |
| 252 return last_pointer_distance_ / |
| 253 (initial_pointer_distance_ - 2 * GetTouchSlopInDips()); |
| 254 case ZOOM_DIRECTION_UNKNOWN: |
| 255 return 1.0f; |
| 256 default: |
| 257 NOTREACHED(); |
| 258 return 0.0f; |
| 259 } |
| 260 } |
| 261 |
| 242 private: | 262 private: |
| 243 ZoomDirection ComputeZoomDirection(float last_pointer_distance, | 263 ZoomDirection ComputeZoomDirection(float last_pointer_distance, |
| 244 float current_pointer_distance) { | 264 float current_pointer_distance) { |
| 245 DCHECK_NE(last_pointer_distance, current_pointer_distance); | 265 DCHECK_NE(last_pointer_distance, current_pointer_distance); |
| 246 return last_pointer_distance < current_pointer_distance ? ZOOM_IN | 266 return last_pointer_distance < current_pointer_distance ? ZOOM_IN |
| 247 : ZOOM_OUT; | 267 : ZOOM_OUT; |
| 248 } | 268 } |
| 249 | 269 |
| 250 float total_num_pixels_covered_; | 270 float initial_pointer_distance_; |
| 251 float last_pointer_distance_; | 271 float last_pointer_distance_; |
| 252 ZoomDirection zoom_direction_; | 272 ZoomDirection zoom_direction_; |
| 253 gfx::PointF start_0_; | 273 gfx::PointF start_0_; |
| 254 gfx::PointF start_1_; | 274 gfx::PointF start_1_; |
| 255 bool started_; | 275 bool started_; |
| 256 }; | 276 }; |
| 257 | 277 |
| 258 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget { | 278 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget { |
| 259 public: | 279 public: |
| 260 MockSyntheticTapGestureTarget() : state_(NOT_STARTED) {} | 280 MockSyntheticTapGestureTarget() : state_(NOT_STARTED) {} |
| (...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 EXPECT_EQ(AddTouchSlopToVector(params.distances[0] + params.distances[1], | 896 EXPECT_EQ(AddTouchSlopToVector(params.distances[0] + params.distances[1], |
| 877 target_), | 897 target_), |
| 878 scroll_target->start_to_end_distance() - gfx::Vector2dF(0, 0.001f)); | 898 scroll_target->start_to_end_distance() - gfx::Vector2dF(0, 0.001f)); |
| 879 } | 899 } |
| 880 | 900 |
| 881 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) { | 901 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) { |
| 882 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); | 902 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); |
| 883 | 903 |
| 884 SyntheticPinchGestureParams params; | 904 SyntheticPinchGestureParams params; |
| 885 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | 905 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 886 params.zoom_in = true; | 906 params.scale_factor = 2.3f; |
| 887 params.total_num_pixels_covered = 345; | |
| 888 params.anchor.SetPoint(54, 89); | 907 params.anchor.SetPoint(54, 89); |
| 889 | 908 |
| 890 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); | 909 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); |
| 891 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); | 910 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); |
| 892 FlushInputUntilComplete(); | 911 FlushInputUntilComplete(); |
| 893 | 912 |
| 894 MockSyntheticPinchTouchTarget* pinch_target = | 913 MockSyntheticPinchTouchTarget* pinch_target = |
| 895 static_cast<MockSyntheticPinchTouchTarget*>(target_); | 914 static_cast<MockSyntheticPinchTouchTarget*>(target_); |
| 896 EXPECT_EQ(1, num_success_); | 915 EXPECT_EQ(1, num_success_); |
| 897 EXPECT_EQ(0, num_failure_); | 916 EXPECT_EQ(0, num_failure_); |
| 898 EXPECT_EQ(pinch_target->zoom_direction(), | 917 EXPECT_EQ(pinch_target->zoom_direction(), |
| 899 MockSyntheticPinchTouchTarget::ZOOM_IN); | 918 MockSyntheticPinchTouchTarget::ZOOM_IN); |
| 900 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(), | 919 EXPECT_FLOAT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor()); |
| 901 pinch_target->total_num_pixels_covered()); | |
| 902 } | 920 } |
| 903 | 921 |
| 904 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) { | 922 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) { |
| 905 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); | 923 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); |
| 906 | 924 |
| 907 SyntheticPinchGestureParams params; | 925 SyntheticPinchGestureParams params; |
| 908 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | 926 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 909 params.zoom_in = false; | 927 params.scale_factor = 0.4f; |
| 910 params.total_num_pixels_covered = 456; | |
| 911 params.anchor.SetPoint(-12, 93); | 928 params.anchor.SetPoint(-12, 93); |
| 912 | 929 |
| 913 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); | 930 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); |
| 914 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); | 931 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); |
| 915 FlushInputUntilComplete(); | 932 FlushInputUntilComplete(); |
| 916 | 933 |
| 917 MockSyntheticPinchTouchTarget* pinch_target = | 934 MockSyntheticPinchTouchTarget* pinch_target = |
| 918 static_cast<MockSyntheticPinchTouchTarget*>(target_); | 935 static_cast<MockSyntheticPinchTouchTarget*>(target_); |
| 919 EXPECT_EQ(1, num_success_); | 936 EXPECT_EQ(1, num_success_); |
| 920 EXPECT_EQ(0, num_failure_); | 937 EXPECT_EQ(0, num_failure_); |
| 921 EXPECT_EQ(pinch_target->zoom_direction(), | 938 EXPECT_EQ(pinch_target->zoom_direction(), |
| 922 MockSyntheticPinchTouchTarget::ZOOM_OUT); | 939 MockSyntheticPinchTouchTarget::ZOOM_OUT); |
| 923 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(), | 940 EXPECT_FLOAT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor()); |
| 924 pinch_target->total_num_pixels_covered()); | |
| 925 } | 941 } |
| 926 | 942 |
| 927 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZeroPixelsCovered) { | 943 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchNoScaling) { |
| 928 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); | 944 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); |
| 929 | 945 |
| 930 SyntheticPinchGestureParams params; | 946 SyntheticPinchGestureParams params; |
| 931 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | 947 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 932 params.zoom_in = true; | 948 params.scale_factor = 1.0f; |
| 933 params.total_num_pixels_covered = 0; | |
| 934 | 949 |
| 935 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); | 950 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); |
| 936 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); | 951 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); |
| 937 FlushInputUntilComplete(); | 952 FlushInputUntilComplete(); |
| 938 | 953 |
| 939 MockSyntheticPinchTouchTarget* pinch_target = | 954 MockSyntheticPinchTouchTarget* pinch_target = |
| 940 static_cast<MockSyntheticPinchTouchTarget*>(target_); | 955 static_cast<MockSyntheticPinchTouchTarget*>(target_); |
| 941 EXPECT_EQ(1, num_success_); | 956 EXPECT_EQ(1, num_success_); |
| 942 EXPECT_EQ(0, num_failure_); | 957 EXPECT_EQ(0, num_failure_); |
| 943 EXPECT_EQ(pinch_target->zoom_direction(), | 958 EXPECT_EQ(pinch_target->zoom_direction(), |
| 944 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN); | 959 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN); |
| 945 EXPECT_EQ(0, pinch_target->total_num_pixels_covered()); | 960 EXPECT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor()); |
| 946 } | 961 } |
| 947 | 962 |
| 948 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) { | 963 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) { |
| 949 CreateControllerAndTarget<MockSyntheticTapTouchTarget>(); | 964 CreateControllerAndTarget<MockSyntheticTapTouchTarget>(); |
| 950 | 965 |
| 951 SyntheticTapGestureParams params; | 966 SyntheticTapGestureParams params; |
| 952 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | 967 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 953 params.duration_ms = 123; | 968 params.duration_ms = 123; |
| 954 params.position.SetPoint(87, -124); | 969 params.position.SetPoint(87, -124); |
| 955 | 970 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 EXPECT_TRUE(tap_target->GestureFinished()); | 1002 EXPECT_TRUE(tap_target->GestureFinished()); |
| 988 EXPECT_EQ(tap_target->position(), params.position); | 1003 EXPECT_EQ(tap_target->position(), params.position); |
| 989 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); | 1004 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); |
| 990 EXPECT_GE(GetTotalTime(), | 1005 EXPECT_GE(GetTotalTime(), |
| 991 base::TimeDelta::FromMilliseconds(params.duration_ms)); | 1006 base::TimeDelta::FromMilliseconds(params.duration_ms)); |
| 992 } | 1007 } |
| 993 | 1008 |
| 994 } // namespace | 1009 } // namespace |
| 995 | 1010 |
| 996 } // namespace content | 1011 } // namespace content |
| OLD | NEW |