Fix coordinate space for touch pointer events

Consider frame scale for calculating coordinates
of touch pointer events.

TBR=nzolghadr@chromium.org

(cherry picked from commit 735fa061d0f531bd0704c4c368d59edc1c1fea3a)
(cherry picked from commit 1478ec50aeb48fe9a10e51576a07cbe01d4a095f)

Bug: 809106
Change-Id: I5503583cd8c0e8b41b7e17fd59170a6f1dff7fa1
Reviewed-on: https://chromium-review.googlesource.com/914690
Reviewed-by: Dave Tapuska <dtapuska@chromium.org>
Commit-Queue: Navid Zolghadr <nzolghadr@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#536813}
Reviewed-on: https://chromium-review.googlesource.com/931902
Reviewed-by: Navid Zolghadr <nzolghadr@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#548}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/third_party/WebKit/Source/core/events/PointerEventFactory.cpp b/third_party/WebKit/Source/core/events/PointerEventFactory.cpp
index 48d8ec2..36f838c4 100644
--- a/third_party/WebKit/Source/core/events/PointerEventFactory.cpp
+++ b/third_party/WebKit/Source/core/events/PointerEventFactory.cpp
@@ -106,10 +106,13 @@
   // and pointerType which is the same among the coalesced events and the
   // dispatched event.
 
+  WebPointerEvent web_pointer_event_in_root_frame =
+      web_pointer_event.WebPointerEventInRootFrame();
+
   if (dom_window && dom_window->GetFrame() && dom_window->GetFrame()->View()) {
     LocalFrame* frame = dom_window->GetFrame();
     FloatPoint page_point = frame->View()->RootFrameToContents(
-        web_pointer_event.PositionInWidget());
+        web_pointer_event_in_root_frame.PositionInWidget());
     float scale_factor = 1.0f / frame->PageZoomFactor();
     FloatPoint scroll_position(frame->View()->GetScrollOffset());
     FloatPoint client_point = page_point.ScaledBy(scale_factor);
@@ -119,13 +122,15 @@
     pointer_event_init->setClientY(client_point.Y());
 
     if (web_pointer_event.GetType() == WebInputEvent::kPointerMove) {
-      pointer_event_init->setMovementX(web_pointer_event.movement_x);
-      pointer_event_init->setMovementY(web_pointer_event.movement_y);
+      pointer_event_init->setMovementX(
+          web_pointer_event_in_root_frame.movement_x);
+      pointer_event_init->setMovementY(
+          web_pointer_event_in_root_frame.movement_y);
     }
 
-    FloatSize point_shape =
-        FloatSize(web_pointer_event.width, web_pointer_event.height)
-            .ScaledBy(scale_factor);
+    FloatSize point_shape = FloatSize(web_pointer_event_in_root_frame.width,
+                                      web_pointer_event_in_root_frame.height)
+                                .ScaledBy(scale_factor);
     pointer_event_init->setWidth(point_shape.Width());
     pointer_event_init->setHeight(point_shape.Height());
   }
diff --git a/third_party/WebKit/Source/core/input/PointerEventManagerTest.cpp b/third_party/WebKit/Source/core/input/PointerEventManagerTest.cpp
index 762142f..702d503 100644
--- a/third_party/WebKit/Source/core/input/PointerEventManagerTest.cpp
+++ b/third_party/WebKit/Source/core/input/PointerEventManagerTest.cpp
@@ -45,6 +45,42 @@
   int pen_event_received_count_ = 0;
 };
 
+class PointerEventCoordinateListenerCallback final : public EventListener {
+ public:
+  static PointerEventCoordinateListenerCallback* Create() {
+    return new PointerEventCoordinateListenerCallback();
+  }
+
+  bool operator==(const EventListener& other) const override {
+    return this == &other;
+  }
+
+  void handleEvent(ExecutionContext*, Event* event) override {
+    const PointerEvent* pointer_event = (PointerEvent*)event;
+    last_client_x_ = pointer_event->clientX();
+    last_client_y_ = pointer_event->clientY();
+    last_page_x_ = pointer_event->pageX();
+    last_page_y_ = pointer_event->pageY();
+    last_screen_x_ = pointer_event->screenX();
+    last_screen_y_ = pointer_event->screenY();
+    last_width_ = pointer_event->width();
+    last_height_ = pointer_event->height();
+  }
+
+  double last_client_x_ = 0;
+  double last_client_y_ = 0;
+  double last_page_x_ = 0;
+  double last_page_y_ = 0;
+  double last_screen_x_ = 0;
+  double last_screen_y_ = 0;
+  double last_width_ = 0;
+  double last_height_ = 0;
+
+ private:
+  PointerEventCoordinateListenerCallback()
+      : EventListener(EventListener::kCPPEventListenerType) {}
+};
+
 }  // namespace
 
 class PointerEventManagerTest : public SimTest {
@@ -54,14 +90,19 @@
   }
   WebPointerEvent CreateTestPointerEvent(
       WebInputEvent::Type type,
-      WebPointerProperties::PointerType pointer_type) {
+      WebPointerProperties::PointerType pointer_type,
+      WebFloatPoint position_in_widget = WebFloatPoint(100, 100),
+      WebFloatPoint position_in_screen = WebFloatPoint(100, 100),
+      int movement_x = 0,
+      int movement_y = 0,
+      float width = 1,
+      float height = 1) {
     WebPointerEvent event(
         type,
-        WebPointerProperties(1, pointer_type,
-                             WebPointerProperties::Button::kLeft,
-                             WebFloatPoint(100, 100), WebFloatPoint(100, 100)),
-        1, 1);
-    event.SetFrameScale(1);
+        WebPointerProperties(
+            1, pointer_type, WebPointerProperties::Button::kLeft,
+            position_in_widget, position_in_screen, movement_x, movement_y),
+        width, height);
     return event;
   }
   WebMouseEvent CreateTestMouseEvent(WebInputEvent::Type type,
@@ -86,15 +127,15 @@
   GetDocument().body()->addEventListener(EventTypeNames::pointercancel,
                                          callback);
 
-  EventHandler().HandlePointerEvent(
+  WebView().HandleInputEvent(WebCoalescedInputEvent(
       CreateTestPointerEvent(WebInputEvent::kPointerDown,
                              WebPointerProperties::PointerType::kTouch),
-      Vector<WebPointerEvent>());
+      std::vector<WebPointerEvent>()));
 
-  EventHandler().HandlePointerEvent(
+  WebView().HandleInputEvent(WebCoalescedInputEvent(
       CreateTestPointerEvent(WebInputEvent::kPointerDown,
                              WebPointerProperties::PointerType::kPen),
-      Vector<WebPointerEvent>());
+      std::vector<WebPointerEvent>()));
 
   EventHandler().HandleMousePressEvent(
       CreateTestMouseEvent(WebInputEvent::kMouseDown, WebFloatPoint(100, 100)));
@@ -103,18 +144,18 @@
   ASSERT_EQ(callback->touchEventCount(), 0);
   ASSERT_EQ(callback->penEventCount(), 0);
 
-  EventHandler().HandlePointerEvent(
+  WebView().HandleInputEvent(WebCoalescedInputEvent(
       CreateTestPointerEvent(WebInputEvent::kPointerCausedUaAction,
                              WebPointerProperties::PointerType::kPen),
-      Vector<WebPointerEvent>());
+      std::vector<WebPointerEvent>()));
   ASSERT_EQ(callback->mouseEventCount(), 0);
   ASSERT_EQ(callback->touchEventCount(), 1);
   ASSERT_EQ(callback->penEventCount(), 1);
 
-  EventHandler().HandlePointerEvent(
+  WebView().HandleInputEvent(WebCoalescedInputEvent(
       CreateTestPointerEvent(WebInputEvent::kPointerCausedUaAction,
                              WebPointerProperties::PointerType::kTouch),
-      Vector<WebPointerEvent>());
+      std::vector<WebPointerEvent>()));
   ASSERT_EQ(callback->mouseEventCount(), 0);
   ASSERT_EQ(callback->touchEventCount(), 1);
   ASSERT_EQ(callback->penEventCount(), 1);
@@ -128,4 +169,33 @@
   ASSERT_EQ(callback->penEventCount(), 1);
 }
 
+TEST_F(PointerEventManagerTest, PointerEventCoordinates) {
+  WebView().Resize(WebSize(400, 400));
+  SimRequest request("https://example.com/test.html", "text/html");
+  LoadURL("https://example.com/test.html");
+  request.Complete(
+      "<body style='padding: 0px; width: 400px; height: 400px;'>"
+      "</body>");
+  WebView().SetPageScaleFactor(2);
+  PointerEventCoordinateListenerCallback* callback =
+      PointerEventCoordinateListenerCallback::Create();
+  GetDocument().body()->addEventListener(EventTypeNames::pointerdown, callback);
+
+  WebView().HandleInputEvent(WebCoalescedInputEvent(
+      CreateTestPointerEvent(WebInputEvent::kPointerDown,
+                             WebPointerProperties::PointerType::kTouch,
+                             WebFloatPoint(150, 200), WebFloatPoint(100, 50),
+                             10, 10, 16, 24),
+      std::vector<WebPointerEvent>()));
+
+  ASSERT_EQ(callback->last_client_x_, 75);
+  ASSERT_EQ(callback->last_client_y_, 100);
+  ASSERT_EQ(callback->last_page_x_, 75);
+  ASSERT_EQ(callback->last_page_y_, 100);
+  ASSERT_EQ(callback->last_screen_x_, 100);
+  ASSERT_EQ(callback->last_screen_y_, 50);
+  ASSERT_EQ(callback->last_width_, 8);
+  ASSERT_EQ(callback->last_height_, 12);
+}
+
 }  // namespace blink