VR: Remove dependence of gesture detector on TimeTicks::Now

The gesture detector extrapolates scrolling events from previous real scrolling events.
The extrapolated event depends on the time difference between the events, favoring the
longest and more recent event. Here, we make the gesture detector receive the current
time instead of querying it from the system, and thus making the tests deterministic.

Bug: 853086
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:linux_vr;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I2d46f5e5ebceb801aecccf5b2c0a2dbf6b858864
Reviewed-on: https://chromium-review.googlesource.com/1104583
Commit-Queue: Aldo Culquicondor <acondor@chromium.org>
Reviewed-by: Amirhossein Simjour <asimjour@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568043}
diff --git a/chrome/browser/android/vr/vr_controller.cc b/chrome/browser/android/vr/vr_controller.cc
index 48129b0..87e6ade3 100644
--- a/chrome/browser/android/vr/vr_controller.cc
+++ b/chrome/browser/android/vr/vr_controller.cc
@@ -343,7 +343,8 @@
 
   UpdateCurrentTouchInfo();
   return gesture_detector_->DetectGestures(
-      touch_info_, ButtonState(gvr::kControllerButtonClick));
+      touch_info_, base::TimeTicks::Now(),
+      ButtonState(gvr::kControllerButtonClick));
 }
 
 void VrController::UpdateCurrentTouchInfo() {
diff --git a/chrome/browser/vr/gesture_detector.cc b/chrome/browser/vr/gesture_detector.cc
index 6c317985..d1d94a5 100644
--- a/chrome/browser/vr/gesture_detector.cc
+++ b/chrome/browser/vr/gesture_detector.cc
@@ -41,10 +41,11 @@
 
 std::unique_ptr<GestureList> GestureDetector::DetectGestures(
     const TouchInfo& input_touch_info,
+    base::TimeTicks current_timestamp,
     bool force_cancel) {
   touch_position_changed_ = UpdateCurrentTouchPoint(input_touch_info);
   TouchInfo touch_info = input_touch_info;
-  ExtrapolateTouchInfo(&touch_info);
+  ExtrapolateTouchInfo(&touch_info, current_timestamp);
   if (touch_position_changed_)
     UpdateOverallVelocity(touch_info);
 
@@ -184,8 +185,8 @@
   return false;
 }
 
-void GestureDetector::ExtrapolateTouchInfo(TouchInfo* touch_info) {
-  base::TimeTicks current_timestamp = base::TimeTicks::Now();
+void GestureDetector::ExtrapolateTouchInfo(TouchInfo* touch_info,
+                                           base::TimeTicks current_timestamp) {
   const bool effectively_scrolling =
       state_->label == SCROLLING || state_->label == POST_SCROLL;
   if (effectively_scrolling && extrapolated_touch_ < kMaxNumOfExtrapolations &&
diff --git a/chrome/browser/vr/gesture_detector.h b/chrome/browser/vr/gesture_detector.h
index d27b186..07df080 100644
--- a/chrome/browser/vr/gesture_detector.h
+++ b/chrome/browser/vr/gesture_detector.h
@@ -38,6 +38,7 @@
   virtual ~GestureDetector();
 
   std::unique_ptr<GestureList> DetectGestures(const TouchInfo& touch_info,
+                                              base::TimeTicks current_timestamp,
                                               bool force_cancel);
 
  private:
@@ -81,7 +82,8 @@
   // before, update the touch point and return true. Otherwise, return false.
   bool UpdateCurrentTouchPoint(const TouchInfo& touch_info);
 
-  void ExtrapolateTouchInfo(TouchInfo* touch_info);
+  void ExtrapolateTouchInfo(TouchInfo* touch_info,
+                            base::TimeTicks current_timestamp);
 
   void UpdateOverallVelocity(const TouchInfo& touch_info);
 
diff --git a/chrome/browser/vr/gesture_detector_unittest.cc b/chrome/browser/vr/gesture_detector_unittest.cc
index fdecc92f..d7ea291 100644
--- a/chrome/browser/vr/gesture_detector_unittest.cc
+++ b/chrome/browser/vr/gesture_detector_unittest.cc
@@ -18,7 +18,7 @@
 
   TouchInfo touch_info{
       .touch_up = false, .touch_down = false, .is_touching = false};
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, base::TimeTicks(), false);
   EXPECT_TRUE(gestures->empty());
 }
 
@@ -33,7 +33,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureFlingCancel);
 
@@ -45,7 +45,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  gestures = detector.DetectGestures(touch_info, false);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_TRUE(gestures->empty());
 }
 
@@ -59,7 +59,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  detector.DetectGestures(touch_info, false);
+  detector.DetectGestures(touch_info, timestamp, false);
 
   // Move to the right.
   timestamp += base::TimeDelta::FromMilliseconds(1);
@@ -69,7 +69,7 @@
       .touch_down = false,
       .is_touching = true,
   };
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollBegin);
   auto* gesture = gestures->front().get();
@@ -84,7 +84,7 @@
       .touch_down = false,
       .is_touching = true,
   };
-  gestures = detector.DetectGestures(touch_info, false);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollUpdate);
   gesture = gestures->front().get();
@@ -94,17 +94,20 @@
   // Release touch. Scroll is extrapolated for 2 frames.
   touch_info.touch_up = true;
   touch_info.is_touching = false;
-  gestures = detector.DetectGestures(touch_info, false);
+  timestamp += base::TimeDelta::FromMilliseconds(1);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollUpdate);
   gesture = gestures->front().get();
   EXPECT_GT(gesture->data.scroll_update.delta_x, 0.0f);
   EXPECT_GT(gesture->data.scroll_update.delta_y, 0.0f);
   touch_info.touch_up = false;
-  gestures = detector.DetectGestures(touch_info, false);
+  timestamp += base::TimeDelta::FromMilliseconds(1);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollUpdate);
-  gestures = detector.DetectGestures(touch_info, false);
+  timestamp += base::TimeDelta::FromMilliseconds(1);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollEnd);
 }
@@ -119,7 +122,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  detector.DetectGestures(touch_info, false);
+  detector.DetectGestures(touch_info, timestamp, false);
 
   // Move to the right.
   timestamp += base::TimeDelta::FromMilliseconds(1);
@@ -129,12 +132,12 @@
       .touch_down = false,
       .is_touching = true,
   };
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollBegin);
 
   // Cancel.
-  gestures = detector.DetectGestures(touch_info, true);
+  gestures = detector.DetectGestures(touch_info, timestamp, true);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollEnd);
 }
@@ -149,7 +152,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  detector.DetectGestures(touch_info, false);
+  detector.DetectGestures(touch_info, timestamp, false);
 
   // Move to the right.
   timestamp += base::TimeDelta::FromMilliseconds(1);
@@ -159,20 +162,20 @@
       .touch_down = false,
       .is_touching = true,
   };
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollBegin);
 
   // Release touch. We should see extrapolated scrolling.
   touch_info.touch_up = true;
   touch_info.is_touching = false;
-  gestures = detector.DetectGestures(touch_info, false);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollUpdate);
 
   // Cancel.
   touch_info.touch_up = false;
-  gestures = detector.DetectGestures(touch_info, true);
+  gestures = detector.DetectGestures(touch_info, timestamp, true);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollEnd);
 }
@@ -187,7 +190,7 @@
       .touch_down = true,
       .is_touching = true,
   };
-  detector.DetectGestures(touch_info, false);
+  detector.DetectGestures(touch_info, timestamp, false);
 
   // Move to the right.
   timestamp += base::TimeDelta::FromMilliseconds(1);
@@ -197,22 +200,24 @@
       .touch_down = false,
       .is_touching = true,
   };
-  auto gestures = detector.DetectGestures(touch_info, false);
+  auto gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollBegin);
 
   // Release touch. We should see extrapolated scrolling.
+  timestamp += base::TimeDelta::FromMilliseconds(1);
   touch_info.touch_up = true;
   touch_info.is_touching = false;
-  gestures = detector.DetectGestures(touch_info, false);
+  gestures = detector.DetectGestures(touch_info, timestamp, false);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollUpdate);
 
   // Cancel and touch.
+  timestamp += base::TimeDelta::FromMilliseconds(1);
   touch_info.touch_up = false;
   touch_info.touch_down = true;
   touch_info.is_touching = true;
-  gestures = detector.DetectGestures(touch_info, true);
+  gestures = detector.DetectGestures(touch_info, timestamp, true);
   EXPECT_EQ(gestures->front()->GetType(),
             blink::WebInputEvent::kGestureScrollEnd);
 }