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

Side by Side Diff: content/browser/renderer_host/input/synthetic_gesture_controller_unittest.cc

Issue 293683002: Synthetic pinch gesture take scale factor as parameter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 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 19 matching lines...) Expand all
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 int kTouchSlopInDips = 7;
40 const int kMinScalingSpanInDips = 27;
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 int GetTouchSlopInDips() const OVERRIDE {
101 return kTouchSlopInDips; 102 return kTouchSlopInDips;
102 } 103 }
103 104
105 virtual int 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
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),
200 total_num_pixels_covered_(0),
Sami 2014/05/19 14:49:04 Do we still need total_num_pixel_covered here?
Dominik Grewe 2014/05/19 17:31:43 I'm only using for the NoScaling unit test to make
195 last_pointer_distance_(0), 201 last_pointer_distance_(0),
196 zoom_direction_(ZOOM_DIRECTION_UNKNOWN), 202 zoom_direction_(ZOOM_DIRECTION_UNKNOWN),
197 started_(false) {} 203 started_(false) {}
198 virtual ~MockSyntheticPinchTouchTarget() {} 204 virtual ~MockSyntheticPinchTouchTarget() {}
199 205
200 virtual void DispatchInputEventToPlatform( 206 virtual void DispatchInputEventToPlatform(
201 const WebInputEvent& event) OVERRIDE { 207 const WebInputEvent& event) OVERRIDE {
202 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); 208 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type));
203 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); 209 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event);
204 ASSERT_EQ(touch_event.touchesLength, 2U); 210 ASSERT_EQ(touch_event.touchesLength, 2U);
205 211
206 if (!started_) { 212 if (!started_) {
207 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart); 213 ASSERT_EQ(touch_event.type, WebInputEvent::TouchStart);
208 214
209 start_0_ = gfx::PointF(touch_event.touches[0].position); 215 start_0_ = gfx::PointF(touch_event.touches[0].position);
210 start_1_ = gfx::PointF(touch_event.touches[1].position); 216 start_1_ = gfx::PointF(touch_event.touches[1].position);
211 last_pointer_distance_ = (start_0_ - start_1_).Length(); 217 last_pointer_distance_ = (start_0_ - start_1_).Length();
218 initial_pointer_distance_ = last_pointer_distance_;
219 EXPECT_GE(initial_pointer_distance_, GetMinScalingSpanInDips());
212 220
213 started_ = true; 221 started_ = true;
214 } else { 222 } else {
215 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart); 223 ASSERT_NE(touch_event.type, WebInputEvent::TouchStart);
216 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel); 224 ASSERT_NE(touch_event.type, WebInputEvent::TouchCancel);
217 225
218 gfx::PointF current_0 = gfx::PointF(touch_event.touches[0].position); 226 gfx::PointF current_0 = gfx::PointF(touch_event.touches[0].position);
219 gfx::PointF current_1 = gfx::PointF(touch_event.touches[1].position); 227 gfx::PointF current_1 = gfx::PointF(touch_event.touches[1].position);
220 228
221 total_num_pixels_covered_ = 229 total_num_pixels_covered_ =
(...skipping 10 matching lines...) Expand all
232 ComputeZoomDirection(last_pointer_distance_, pointer_distance)); 240 ComputeZoomDirection(last_pointer_distance_, pointer_distance));
233 } 241 }
234 242
235 last_pointer_distance_ = pointer_distance; 243 last_pointer_distance_ = pointer_distance;
236 } 244 }
237 } 245 }
238 246
239 float total_num_pixels_covered() const { return total_num_pixels_covered_; } 247 float total_num_pixels_covered() const { return total_num_pixels_covered_; }
240 ZoomDirection zoom_direction() const { return zoom_direction_; } 248 ZoomDirection zoom_direction() const { return zoom_direction_; }
241 249
250 float ComputeScaleFactor() const {
251 switch (zoom_direction_) {
252 case ZOOM_IN:
253 return last_pointer_distance_ /
254 (initial_pointer_distance_ + 2 * GetTouchSlopInDips());
255 case ZOOM_OUT:
256 return last_pointer_distance_ /
257 (initial_pointer_distance_ - 2 * GetTouchSlopInDips());
258 case ZOOM_DIRECTION_UNKNOWN:
259 return 1.0f;
260 default:
261 EXPECT_TRUE(false);
Sami 2014/05/19 14:49:04 NOTREACHED();
Dominik Grewe 2014/05/19 17:31:43 Done.
262 return 0.0f;
263 }
264 }
265
242 private: 266 private:
243 ZoomDirection ComputeZoomDirection(float last_pointer_distance, 267 ZoomDirection ComputeZoomDirection(float last_pointer_distance,
244 float current_pointer_distance) { 268 float current_pointer_distance) {
245 DCHECK_NE(last_pointer_distance, current_pointer_distance); 269 DCHECK_NE(last_pointer_distance, current_pointer_distance);
246 return last_pointer_distance < current_pointer_distance ? ZOOM_IN 270 return last_pointer_distance < current_pointer_distance ? ZOOM_IN
247 : ZOOM_OUT; 271 : ZOOM_OUT;
248 } 272 }
249 273
274 float initial_pointer_distance_;
250 float total_num_pixels_covered_; 275 float total_num_pixels_covered_;
251 float last_pointer_distance_; 276 float last_pointer_distance_;
252 ZoomDirection zoom_direction_; 277 ZoomDirection zoom_direction_;
253 gfx::PointF start_0_; 278 gfx::PointF start_0_;
254 gfx::PointF start_1_; 279 gfx::PointF start_1_;
255 bool started_; 280 bool started_;
256 }; 281 };
257 282
258 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget { 283 class MockSyntheticTapGestureTarget : public MockSyntheticGestureTarget {
259 public: 284 public:
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 EXPECT_EQ(AddTouchSlopToVector(params.distances[0] + params.distances[1], 901 EXPECT_EQ(AddTouchSlopToVector(params.distances[0] + params.distances[1],
877 target_), 902 target_),
878 scroll_target->start_to_end_distance() - gfx::Vector2dF(0, 0.001f)); 903 scroll_target->start_to_end_distance() - gfx::Vector2dF(0, 0.001f));
879 } 904 }
880 905
881 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) { 906 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomIn) {
882 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); 907 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
883 908
884 SyntheticPinchGestureParams params; 909 SyntheticPinchGestureParams params;
885 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 910 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
886 params.zoom_in = true; 911 params.scale_factor = 2.3f;
887 params.total_num_pixels_covered = 345;
888 params.anchor.SetPoint(54, 89); 912 params.anchor.SetPoint(54, 89);
889 913
890 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); 914 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
891 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); 915 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
892 FlushInputUntilComplete(); 916 FlushInputUntilComplete();
893 917
894 MockSyntheticPinchTouchTarget* pinch_target = 918 MockSyntheticPinchTouchTarget* pinch_target =
895 static_cast<MockSyntheticPinchTouchTarget*>(target_); 919 static_cast<MockSyntheticPinchTouchTarget*>(target_);
896 EXPECT_EQ(1, num_success_); 920 EXPECT_EQ(1, num_success_);
897 EXPECT_EQ(0, num_failure_); 921 EXPECT_EQ(0, num_failure_);
898 EXPECT_EQ(pinch_target->zoom_direction(), 922 EXPECT_EQ(pinch_target->zoom_direction(),
899 MockSyntheticPinchTouchTarget::ZOOM_IN); 923 MockSyntheticPinchTouchTarget::ZOOM_IN);
900 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(), 924 EXPECT_FLOAT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor());
901 pinch_target->total_num_pixels_covered());
902 } 925 }
903 926
904 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) { 927 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZoomOut) {
905 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); 928 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
906 929
907 SyntheticPinchGestureParams params; 930 SyntheticPinchGestureParams params;
908 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 931 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
909 params.zoom_in = false; 932 params.scale_factor = 0.4f;
910 params.total_num_pixels_covered = 456;
911 params.anchor.SetPoint(-12, 93); 933 params.anchor.SetPoint(-12, 93);
912 934
913 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); 935 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
914 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); 936 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
915 FlushInputUntilComplete(); 937 FlushInputUntilComplete();
916 938
917 MockSyntheticPinchTouchTarget* pinch_target = 939 MockSyntheticPinchTouchTarget* pinch_target =
918 static_cast<MockSyntheticPinchTouchTarget*>(target_); 940 static_cast<MockSyntheticPinchTouchTarget*>(target_);
919 EXPECT_EQ(1, num_success_); 941 EXPECT_EQ(1, num_success_);
920 EXPECT_EQ(0, num_failure_); 942 EXPECT_EQ(0, num_failure_);
921 EXPECT_EQ(pinch_target->zoom_direction(), 943 EXPECT_EQ(pinch_target->zoom_direction(),
922 MockSyntheticPinchTouchTarget::ZOOM_OUT); 944 MockSyntheticPinchTouchTarget::ZOOM_OUT);
923 EXPECT_EQ(params.total_num_pixels_covered + 2 * target_->GetTouchSlopInDips(), 945 EXPECT_FLOAT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor());
924 pinch_target->total_num_pixels_covered());
925 } 946 }
926 947
927 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchZeroPixelsCovered) { 948 TEST_F(SyntheticGestureControllerTest, PinchGestureTouchNoScaling) {
928 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>(); 949 CreateControllerAndTarget<MockSyntheticPinchTouchTarget>();
929 950
930 SyntheticPinchGestureParams params; 951 SyntheticPinchGestureParams params;
931 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 952 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
932 params.zoom_in = true; 953 params.scale_factor = 1.0f;
933 params.total_num_pixels_covered = 0;
934 954
935 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params)); 955 scoped_ptr<SyntheticPinchGesture> gesture(new SyntheticPinchGesture(params));
936 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>()); 956 QueueSyntheticGesture(gesture.PassAs<SyntheticGesture>());
937 FlushInputUntilComplete(); 957 FlushInputUntilComplete();
938 958
939 MockSyntheticPinchTouchTarget* pinch_target = 959 MockSyntheticPinchTouchTarget* pinch_target =
940 static_cast<MockSyntheticPinchTouchTarget*>(target_); 960 static_cast<MockSyntheticPinchTouchTarget*>(target_);
941 EXPECT_EQ(1, num_success_); 961 EXPECT_EQ(1, num_success_);
942 EXPECT_EQ(0, num_failure_); 962 EXPECT_EQ(0, num_failure_);
943 EXPECT_EQ(pinch_target->zoom_direction(), 963 EXPECT_EQ(pinch_target->zoom_direction(),
944 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN); 964 MockSyntheticPinchTouchTarget::ZOOM_DIRECTION_UNKNOWN);
945 EXPECT_EQ(0, pinch_target->total_num_pixels_covered()); 965 EXPECT_EQ(0, pinch_target->total_num_pixels_covered());
966 EXPECT_EQ(params.scale_factor, pinch_target->ComputeScaleFactor());
946 } 967 }
947 968
948 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) { 969 TEST_F(SyntheticGestureControllerTest, TapGestureTouch) {
949 CreateControllerAndTarget<MockSyntheticTapTouchTarget>(); 970 CreateControllerAndTarget<MockSyntheticTapTouchTarget>();
950 971
951 SyntheticTapGestureParams params; 972 SyntheticTapGestureParams params;
952 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; 973 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT;
953 params.duration_ms = 123; 974 params.duration_ms = 123;
954 params.position.SetPoint(87, -124); 975 params.position.SetPoint(87, -124);
955 976
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
987 EXPECT_TRUE(tap_target->GestureFinished()); 1008 EXPECT_TRUE(tap_target->GestureFinished());
988 EXPECT_EQ(tap_target->position(), params.position); 1009 EXPECT_EQ(tap_target->position(), params.position);
989 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms); 1010 EXPECT_EQ(tap_target->GetDuration().InMilliseconds(), params.duration_ms);
990 EXPECT_GE(GetTotalTime(), 1011 EXPECT_GE(GetTotalTime(),
991 base::TimeDelta::FromMilliseconds(params.duration_ms)); 1012 base::TimeDelta::FromMilliseconds(params.duration_ms));
992 } 1013 }
993 1014
994 } // namespace 1015 } // namespace
995 1016
996 } // namespace content 1017 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698