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
3// found in the LICENSE file.
4
5#include "base/logging.h"
6#include "base/memory/scoped_ptr.h"
7#include "content/common/input/synthetic_web_input_event_builders.h"
8#include "content/renderer/pepper/event_conversion.h"
9#include "ppapi/shared_impl/ppb_input_event_shared.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace content {
13
14class EventConversionTest : public ::testing::Test {
15 protected:
16  void CompareWebTouchEvents(const blink::WebTouchEvent& expected,
17                             const blink::WebTouchEvent& actual) {
18    EXPECT_EQ(expected.type, actual.type);
19    ASSERT_EQ(expected.touchesLength, actual.touchesLength);
20    for (size_t i = 0; i < expected.touchesLength; ++i) {
21      size_t j = 0;
22      for (; j < actual.touchesLength; ++j) {
23        if (actual.touches[j].id == expected.touches[i].id)
24          break;
25      }
26      ASSERT_NE(j, actual.touchesLength);
27      EXPECT_EQ(expected.touches[i].id, actual.touches[j].id);
28      EXPECT_EQ(expected.touches[i].state, actual.touches[j].state);
29      EXPECT_EQ(expected.touches[i].position.x, actual.touches[j].position.x);
30      EXPECT_EQ(expected.touches[i].position.y, actual.touches[j].position.y);
31      EXPECT_EQ(expected.touches[i].radiusX, actual.touches[j].radiusX);
32      EXPECT_EQ(expected.touches[i].radiusY, actual.touches[j].radiusY);
33      EXPECT_EQ(expected.touches[i].rotationAngle,
34                actual.touches[j].rotationAngle);
35      EXPECT_EQ(expected.touches[i].force, actual.touches[j].force);
36    }
37  }
38};
39
40TEST_F(EventConversionTest, TouchStart) {
41  SyntheticWebTouchEvent touch;
42  touch.PressPoint(1.f, 2.f);
43
44  std::vector<ppapi::InputEventData> pp_events;
45  CreateInputEventData(touch, &pp_events);
46  ASSERT_EQ(1U, pp_events.size());
47
48  const ppapi::InputEventData& pp_event = pp_events[0];
49  ASSERT_EQ(PP_INPUTEVENT_TYPE_TOUCHSTART, pp_event.event_type);
50  ASSERT_EQ(1U, pp_event.touches.size());
51  ASSERT_EQ(1U, pp_event.changed_touches.size());
52  ASSERT_EQ(1U, pp_event.target_touches.size());
53
54  scoped_ptr<blink::WebInputEvent> event_out(CreateWebInputEvent(pp_event));
55  const blink::WebTouchEvent* touch_out =
56      static_cast<const blink::WebTouchEvent*>(event_out.get());
57  ASSERT_TRUE(touch_out);
58  EXPECT_EQ(touch.type, touch_out->type);
59  EXPECT_EQ(touch.touchesLength, touch_out->touchesLength);
60  CompareWebTouchEvents(touch, *touch_out);
61}
62
63TEST_F(EventConversionTest, TouchMove) {
64  SyntheticWebTouchEvent touch;
65  touch.PressPoint(1.f, 2.f);
66  touch.ResetPoints();
67  touch.PressPoint(3.f, 4.f);
68  touch.ResetPoints();
69  touch.MovePoint(1, 5.f, 6.f);
70
71  std::vector<ppapi::InputEventData> pp_events;
72  CreateInputEventData(touch, &pp_events);
73  ASSERT_EQ(1U, pp_events.size());
74
75  const ppapi::InputEventData& pp_event = pp_events[0];
76  ASSERT_EQ(PP_INPUTEVENT_TYPE_TOUCHMOVE, pp_event.event_type);
77  ASSERT_EQ(2U, pp_event.touches.size());
78  ASSERT_EQ(1U, pp_event.changed_touches.size());
79  ASSERT_EQ(2U, pp_event.target_touches.size());
80
81  scoped_ptr<blink::WebInputEvent> event_out(CreateWebInputEvent(pp_event));
82  const blink::WebTouchEvent* touch_out =
83      static_cast<const blink::WebTouchEvent*>(event_out.get());
84  ASSERT_TRUE(touch_out);
85  EXPECT_EQ(touch.type, touch_out->type);
86  EXPECT_EQ(touch.touchesLength, touch_out->touchesLength);
87  CompareWebTouchEvents(touch, *touch_out);
88}
89
90TEST_F(EventConversionTest, TouchEnd) {
91  SyntheticWebTouchEvent touch;
92  touch.PressPoint(1.f, 2.f);
93  touch.ResetPoints();
94  touch.PressPoint(3.f, 4.f);
95  touch.ResetPoints();
96  touch.ReleasePoint(0);
97
98  std::vector<ppapi::InputEventData> pp_events;
99  CreateInputEventData(touch, &pp_events);
100  ASSERT_EQ(1U, pp_events.size());
101
102  const ppapi::InputEventData& pp_event = pp_events[0];
103  ASSERT_EQ(PP_INPUTEVENT_TYPE_TOUCHEND, pp_event.event_type);
104  ASSERT_EQ(1U, pp_event.touches.size());
105  ASSERT_EQ(1U, pp_event.changed_touches.size());
106  ASSERT_EQ(2U, pp_event.target_touches.size());
107
108  scoped_ptr<blink::WebInputEvent> event_out(CreateWebInputEvent(pp_event));
109  const blink::WebTouchEvent* touch_out =
110      static_cast<const blink::WebTouchEvent*>(event_out.get());
111  ASSERT_TRUE(touch_out);
112  EXPECT_EQ(touch.type, touch_out->type);
113  ASSERT_EQ(touch.touchesLength, touch_out->touchesLength);
114  CompareWebTouchEvents(touch, *touch_out);
115}
116
117TEST_F(EventConversionTest, TouchCancel) {
118  SyntheticWebTouchEvent touch;
119  touch.PressPoint(1.f, 2.f);
120  touch.ResetPoints();
121  touch.PressPoint(3.f, 4.f);
122  touch.ResetPoints();
123  touch.CancelPoint(1);
124  touch.CancelPoint(0);
125
126  std::vector<ppapi::InputEventData> pp_events;
127  CreateInputEventData(touch, &pp_events);
128  ASSERT_EQ(1U, pp_events.size());
129
130  const ppapi::InputEventData& pp_event = pp_events[0];
131  ASSERT_EQ(PP_INPUTEVENT_TYPE_TOUCHCANCEL, pp_event.event_type);
132  ASSERT_EQ(0U, pp_event.touches.size());
133  ASSERT_EQ(2U, pp_event.changed_touches.size());
134  ASSERT_EQ(2U, pp_event.target_touches.size());
135
136  scoped_ptr<blink::WebInputEvent> event_out(CreateWebInputEvent(pp_event));
137  const blink::WebTouchEvent* touch_out =
138      static_cast<const blink::WebTouchEvent*>(event_out.get());
139  ASSERT_TRUE(touch_out);
140  EXPECT_EQ(touch.type, touch_out->type);
141  EXPECT_EQ(touch.touchesLength, touch_out->touchesLength);
142  CompareWebTouchEvents(touch, *touch_out);
143}
144
145}  // namespace content
146