File: motion_event_generic_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (196 lines) | stat: -rw-r--r-- 7,106 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event_constants.h"
#include "ui/events/gesture_detection/motion_event_generic.h"
#include "ui/events/test/motion_event_test_utils.h"

namespace ui {

TEST(MotionEventGenericTest, Basic) {
  base::TimeTicks event_time = base::TimeTicks::Now();
  MotionEventGeneric event(
      MotionEvent::ACTION_DOWN, event_time, PointerProperties());
  EXPECT_EQ(1U, event.GetPointerCount());
  EXPECT_EQ(0U, event.GetHistorySize());
  EXPECT_EQ(event_time, event.GetEventTime());

  event.PushPointer(PointerProperties(8.3f, 4.7f, 0.9f));
  ASSERT_EQ(2U, event.GetPointerCount());
  EXPECT_EQ(8.3f, event.GetX(1));
  EXPECT_EQ(4.7f, event.GetY(1));

  event.PushPointer(PointerProperties(2.3f, -3.7f, 5.8f));
  ASSERT_EQ(3U, event.GetPointerCount());
  EXPECT_EQ(2.3f, event.GetX(2));
  EXPECT_EQ(-3.7f, event.GetY(2));

  event.set_id(1);
  EXPECT_EQ(1, event.GetId());

  event.set_action(MotionEvent::ACTION_POINTER_DOWN);
  EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());

  event_time += base::TimeDelta::FromMilliseconds(5);
  event.set_event_time(event_time);
  EXPECT_EQ(event_time, event.GetEventTime());

  event.set_button_state(MotionEvent::BUTTON_PRIMARY);
  EXPECT_EQ(MotionEvent::BUTTON_PRIMARY, event.GetButtonState());

  event.set_flags(EF_ALT_DOWN | EF_SHIFT_DOWN);
  EXPECT_EQ(EF_ALT_DOWN | EF_SHIFT_DOWN, event.GetFlags());

  event.set_action_index(1);
  EXPECT_EQ(1, event.GetActionIndex());

  event.set_action(MotionEvent::ACTION_MOVE);
  EXPECT_EQ(MotionEvent::ACTION_MOVE, event.GetAction());

  PointerProperties historical_pointer0(1.2f, 2.4f, 1.f);
  PointerProperties historical_pointer1(2.4f, 4.8f, 2.f);
  PointerProperties historical_pointer2(4.8f, 9.6f, 3.f);
  MotionEventGeneric historical_event(
      MotionEvent::ACTION_MOVE,
      event_time - base::TimeDelta::FromMilliseconds(5),
      historical_pointer0);
  historical_event.PushPointer(historical_pointer1);
  historical_event.PushPointer(historical_pointer2);

  event.PushHistoricalEvent(historical_event.Clone());
  EXPECT_EQ(1U, event.GetHistorySize());
  EXPECT_EQ(event_time - base::TimeDelta::FromMilliseconds(5),
            event.GetHistoricalEventTime(0));
  EXPECT_EQ(1.2f, event.GetHistoricalX(0, 0));
  EXPECT_EQ(2.4f, event.GetHistoricalY(0, 0));
  EXPECT_EQ(1.f, event.GetHistoricalTouchMajor(0, 0));
  EXPECT_EQ(2.4f, event.GetHistoricalX(1, 0));
  EXPECT_EQ(4.8f, event.GetHistoricalY(1, 0));
  EXPECT_EQ(2.f, event.GetHistoricalTouchMajor(1, 0));
  EXPECT_EQ(4.8f, event.GetHistoricalX(2, 0));
  EXPECT_EQ(9.6f, event.GetHistoricalY(2, 0));
  EXPECT_EQ(3.f, event.GetHistoricalTouchMajor(2, 0));
}

TEST(MotionEventGenericTest, Clone) {
  MotionEventGeneric event(MotionEvent::ACTION_DOWN,
                           base::TimeTicks::Now(),
                           PointerProperties(8.3f, 4.7f, 2.f));
  event.set_id(1);
  event.set_button_state(MotionEvent::BUTTON_PRIMARY);

  scoped_ptr<MotionEvent> clone = event.Clone();
  ASSERT_TRUE(clone);
  EXPECT_EQ(test::ToString(event), test::ToString(*clone));
}

TEST(MotionEventGenericTest, CloneWithHistory) {
  base::TimeTicks event_time = base::TimeTicks::Now();
  base::TimeTicks historical_event_time =
      event_time - base::TimeDelta::FromMilliseconds(5);

  PointerProperties pointer(8.3f, 4.7f, 10.1f);
  MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer);

  PointerProperties historical_pointer(3.4f, -4.3f, 11.5);
  scoped_ptr<MotionEvent> historical_event(new MotionEventGeneric(
      MotionEvent::ACTION_MOVE, historical_event_time, historical_pointer));

  event.PushHistoricalEvent(historical_event.Pass());
  EXPECT_EQ(1U, event.GetHistorySize());

  scoped_ptr<MotionEvent> clone = event.Clone();
  ASSERT_TRUE(clone);
  EXPECT_EQ(test::ToString(event), test::ToString(*clone));
}

TEST(MotionEventGenericTest, Cancel) {
  MotionEventGeneric event(MotionEvent::ACTION_UP,
                           base::TimeTicks::Now(),
                           PointerProperties(8.7f, 4.3f, 1.f));
  event.set_id(2);
  event.set_button_state(MotionEvent::BUTTON_SECONDARY);

  scoped_ptr<MotionEvent> cancel = event.Cancel();
  event.set_action(MotionEvent::ACTION_CANCEL);
  ASSERT_TRUE(cancel);
  EXPECT_EQ(test::ToString(event), test::ToString(*cancel));
}

TEST(MotionEventGenericTest, FindPointerIndexOfId) {
  base::TimeTicks event_time = base::TimeTicks::Now();
  PointerProperties pointer;
  pointer.id = 0;
  MotionEventGeneric event0(MotionEvent::ACTION_DOWN, event_time, pointer);
  EXPECT_EQ(0, event0.FindPointerIndexOfId(0));
  EXPECT_EQ(-1, event0.FindPointerIndexOfId(1));
  EXPECT_EQ(-1, event0.FindPointerIndexOfId(-1));

  MotionEventGeneric event1(event0);
  pointer.id = 7;
  event1.PushPointer(pointer);
  EXPECT_EQ(0, event1.FindPointerIndexOfId(0));
  EXPECT_EQ(1, event1.FindPointerIndexOfId(7));
  EXPECT_EQ(-1, event1.FindPointerIndexOfId(6));
  EXPECT_EQ(-1, event1.FindPointerIndexOfId(1));

  MotionEventGeneric event2(event1);
  pointer.id = 3;
  event2.PushPointer(pointer);
  EXPECT_EQ(0, event2.FindPointerIndexOfId(0));
  EXPECT_EQ(1, event2.FindPointerIndexOfId(7));
  EXPECT_EQ(2, event2.FindPointerIndexOfId(3));
  EXPECT_EQ(-1, event2.FindPointerIndexOfId(1));
  EXPECT_EQ(-1, event2.FindPointerIndexOfId(2));
}

TEST(MotionEventGenericTest, ToString) {
  base::TimeTicks event_time = base::TimeTicks::Now();
  base::TimeTicks historical_event_time0 =
      event_time - base::TimeDelta::FromMilliseconds(10);
  base::TimeTicks historical_event_time1 =
      event_time - base::TimeDelta::FromMilliseconds(5);

  PointerProperties pointer0(1, 2, 3);
  pointer0.id = 7;
  pointer0.pressure = 10;
  pointer0.touch_minor = 15;
  pointer0.touch_major = 20;
  pointer0.orientation = 1;

  PointerProperties pointer1(4, 5, 6);
  pointer1.id = 3;
  pointer0.pressure = 25;
  pointer0.touch_minor = 30;
  pointer0.touch_major = 35;
  pointer0.orientation = -1;

  MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer0);
  event.PushPointer(pointer1);

  pointer0.x += 50;
  pointer1.x -= 50;
  scoped_ptr<MotionEventGeneric> historical_event0(new MotionEventGeneric(
      MotionEvent::ACTION_MOVE, historical_event_time0, pointer0));
  historical_event0->PushPointer(pointer1);

  pointer0.x += 100;
  pointer1.x -= 100;
  scoped_ptr<MotionEventGeneric> historical_event1(new MotionEventGeneric(
      MotionEvent::ACTION_MOVE, historical_event_time1, pointer0));
  historical_event1->PushPointer(pointer1);

  event.PushHistoricalEvent(historical_event0.Pass());
  event.PushHistoricalEvent(historical_event1.Pass());
  ASSERT_EQ(2U, event.GetHistorySize());
  ASSERT_EQ(2U, event.GetPointerCount());

  // Do a basic smoke exercise of event stringification to ensure things don't
  // explode in the process.
  std::string event_string = test::ToString(event);
  EXPECT_FALSE(event_string.empty());
}

}  // namespace ui