File: motion_event_android_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 12,245 bytes parent folder | download | duplicates (6)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <android/input.h>
#include <stddef.h>

#include <cmath>
#include <limits>

#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/numerics/angle_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/android/motion_event_android_java.h"
#include "ui/events/android/motion_event_android_native.h"
#include "ui/events/event_constants.h"
#include "ui/events/test/motion_event_test_utils.h"
#include "ui/events/test/scoped_event_test_tick_clock.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "ui/events/motionevent_jni_headers/MotionEvent_jni.h"

namespace ui {

class MotionEvent;

namespace {

constexpr float kPixToDip = 0.5f;

constexpr int kAndroidActionButton = 0;
constexpr int kAndroidActionDown = AMOTION_EVENT_ACTION_DOWN;
constexpr int kAndroidActionUp = AMOTION_EVENT_ACTION_UP;
constexpr int kAndroidActionPointerDown = AMOTION_EVENT_ACTION_POINTER_DOWN;
constexpr int kAndroidAltKeyDown = AMETA_ALT_ON;

// Corresponds to TOOL_TYPE_FINGER, see
// developer.android.com/reference/android/view/MotionEvent.html
//     #TOOL_TYPE_FINGER.
constexpr int kAndroidToolTypeFinger = 1;

// Corresponds to BUTTON_PRIMARY, see
// developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY.
constexpr int kAndroidButtonPrimary = 1;

// This function convert tilt_x and tilt_y back to tilt_rad.
float ConvertToTiltRad(float tilt_x, float tilt_y) {
  float tilt_x_r = sinf(base::DegToRad(tilt_x));
  float tilt_x_z = cosf(base::DegToRad(tilt_x));
  float tilt_y_r = sinf(base::DegToRad(tilt_y));
  float tilt_y_z = cosf(base::DegToRad(tilt_y));
  float r_x = tilt_x_r * tilt_y_z;
  float r_y = tilt_y_r * tilt_x_z;
  float r = sqrtf(r_x * r_x + r_y * r_y);
  float z = tilt_x_z * tilt_y_z;
  return atan2f(r, z);
}

}  // namespace

constexpr float float_error = 0.0001f;
// Note that these tests avoid creating a Java instance of the MotionEvent, as
// we're primarily testing caching behavior, and the code necessary to
// construct a Java-backed MotionEvent itself adds unnecessary complexity.
TEST(MotionEventAndroidTest, Constructor) {
  constexpr int kLatestEventTimeNS = 5'123'456;
  constexpr int kOldestEventTimeNS = 4'123'456;
  base::TimeTicks latest_event_time =
      base::TimeTicks() + base::Nanoseconds(kLatestEventTimeNS);
  base::TimeTicks oldest_event_time =
      base::TimeTicks() + base::Nanoseconds(kOldestEventTimeNS);
  const base::TimeTicks down_time_ms =
      base::TimeTicks::FromUptimeMillis(oldest_event_time.ToUptimeMillis());

  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(latest_event_time);

  const float pressure_0 = 1.5f;
  const float pressure_1 = 2.5f;
  MotionEventAndroid::Pointer p0(1, 13.7f, -7.13f, 5.3f, 1.2f, pressure_0, 0.1f,
                                 0.2f, kAndroidToolTypeFinger);
  MotionEventAndroid::Pointer p1(2, -13.7f, 7.13f, 3.5f, 12.1f, pressure_1,
                                 -0.1f, 0.4f, kAndroidToolTypeFinger);
  float raw_offset = -3.f;
  int pointer_count = 2;
  int history_size = 0;
  int action_index = -1;
  MotionEventAndroidJava event(
      base::android::AttachCurrentThread(), nullptr, kPixToDip, 0.f, 0.f, 0.f,
      oldest_event_time, latest_event_time, down_time_ms, kAndroidActionDown,
      pointer_count, history_size, action_index, kAndroidActionButton, 0,
      kAndroidButtonPrimary, kAndroidAltKeyDown, 0, raw_offset, -raw_offset,
      false, &p0, &p1, false);

  EXPECT_EQ(MotionEvent::Action::DOWN, event.GetAction());
  EXPECT_EQ(oldest_event_time, event.GetEventTime());
  EXPECT_EQ(latest_event_time, event.GetLatestEventTime());
  EXPECT_EQ(event.GetDownTime(), down_time_ms);
  EXPECT_EQ(p0.pos_x_pixels * kPixToDip, event.GetX(0));
  EXPECT_EQ(p0.pos_y_pixels * kPixToDip, event.GetY(0));
  EXPECT_EQ(p1.pos_x_pixels * kPixToDip, event.GetX(1));
  EXPECT_EQ(p1.pos_y_pixels * kPixToDip, event.GetY(1));
  EXPECT_FLOAT_EQ((p0.pos_x_pixels + raw_offset) * kPixToDip, event.GetRawX(0));
  EXPECT_FLOAT_EQ((p0.pos_y_pixels - raw_offset) * kPixToDip, event.GetRawY(0));
  EXPECT_FLOAT_EQ((p1.pos_x_pixels + raw_offset) * kPixToDip, event.GetRawX(1));
  EXPECT_FLOAT_EQ((p1.pos_y_pixels - raw_offset) * kPixToDip, event.GetRawY(1));
  EXPECT_EQ(p0.touch_major_pixels * kPixToDip, event.GetTouchMajor(0));
  EXPECT_EQ(p1.touch_major_pixels * kPixToDip, event.GetTouchMajor(1));
  EXPECT_EQ(p0.touch_minor_pixels * kPixToDip, event.GetTouchMinor(0));
  EXPECT_EQ(p1.touch_minor_pixels * kPixToDip, event.GetTouchMinor(1));
  EXPECT_EQ(event.GetPressure(0), pressure_0);
  EXPECT_EQ(event.GetPressure(1), pressure_1);
  EXPECT_EQ(p0.orientation_rad, event.GetOrientation(0));
  EXPECT_EQ(p1.orientation_rad, event.GetOrientation(1));
  EXPECT_NEAR(p0.tilt_rad,
              ConvertToTiltRad(event.GetTiltX(0), event.GetTiltY(0)),
              float_error);
  EXPECT_NEAR(p1.tilt_rad,
              ConvertToTiltRad(event.GetTiltX(1), event.GetTiltY(1)),
              float_error);
  EXPECT_EQ(p0.id, event.GetPointerId(0));
  EXPECT_EQ(p1.id, event.GetPointerId(1));
  EXPECT_EQ(MotionEvent::ToolType::FINGER, event.GetToolType(0));
  EXPECT_EQ(MotionEvent::ToolType::FINGER, event.GetToolType(1));
  EXPECT_EQ(MotionEvent::BUTTON_PRIMARY, event.GetButtonState());
  EXPECT_EQ(ui::EF_ALT_DOWN | ui::EF_LEFT_MOUSE_BUTTON, event.GetFlags());
  EXPECT_EQ(static_cast<size_t>(pointer_count), event.GetPointerCount());
  EXPECT_EQ(static_cast<size_t>(history_size), event.GetHistorySize());
}

TEST(MotionEventAndroidTest, ZeroPressureOnActionUpEvent) {
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(base::TimeTicks());

  const float pressure = 0.4f;
  MotionEventAndroid::Pointer p0(1, 13.7f, -7.13f, 5.3f, 1.2f, pressure, 0.1f,
                                 0.2f, kAndroidToolTypeFinger);
  MotionEventAndroidJava event(base::android::AttachCurrentThread(), nullptr,
                               kPixToDip, 0, 0, 0, base::TimeTicks(),
                               kAndroidActionUp, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                               false, &p0, nullptr);

  EXPECT_EQ(event.GetPressure(0), 0.f);
}

TEST(MotionEventAndroidTest, Clone) {
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(base::TimeTicks());

  const int pointer_count = 1;
  MotionEventAndroid::Pointer p0(1, 13.7f, -7.13f, 5.3f, 1.2f, 0.4f, 0.1f, 0.2f,
                                 kAndroidToolTypeFinger);
  MotionEventAndroidJava event(base::android::AttachCurrentThread(), nullptr,
                               kPixToDip, 0, 0, 0, base::TimeTicks(),
                               kAndroidActionDown, pointer_count, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, false, &p0, nullptr);

  std::unique_ptr<MotionEvent> clone = event.Clone();
  EXPECT_EQ(ui::test::ToString(event), ui::test::ToString(*clone));
}

TEST(MotionEventAndroidTest, Cancel) {
  constexpr const int kEventTimeNS = 5'123'456;
  base::TimeTicks event_time =
      base::TimeTicks() + base::Nanoseconds(kEventTimeNS);
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(event_time);

  const int pointer_count = 1;
  MotionEventAndroid::Pointer p0(1, 13.7f, -7.13f, 5.3f, 1.2f, 0.4f, 0.1f, 0.2f,
                                 kAndroidToolTypeFinger);
  MotionEventAndroidJava event(
      base::android::AttachCurrentThread(), nullptr, kPixToDip, 0, 0, 0,
      base::TimeTicks() + base::Nanoseconds(kEventTimeNS), kAndroidActionDown,
      pointer_count, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, &p0, nullptr);

  std::unique_ptr<MotionEvent> cancel_event = event.Cancel();
  EXPECT_EQ(MotionEvent::Action::CANCEL, cancel_event->GetAction());
  EXPECT_EQ(event_time, cancel_event->GetEventTime());
  EXPECT_EQ(p0.pos_x_pixels * kPixToDip, cancel_event->GetX(0));
  EXPECT_EQ(p0.pos_y_pixels * kPixToDip, cancel_event->GetY(0));
  EXPECT_EQ(static_cast<size_t>(pointer_count),
            cancel_event->GetPointerCount());
  EXPECT_EQ(0U, cancel_event->GetHistorySize());
}

TEST(MotionEventAndroidTest, InvalidOrientationsSanitized) {
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(base::TimeTicks());

  int pointer_count = 2;
  float orientation0 = 1e10f;
  float orientation1 = std::numeric_limits<float>::quiet_NaN();
  MotionEventAndroid::Pointer p0(0, 0, 0, 0, 0, 0, orientation0, 0, 0);
  MotionEventAndroid::Pointer p1(1, 0, 0, 0, 0, 0, orientation1, 0, 0);
  MotionEventAndroidJava event(base::android::AttachCurrentThread(), nullptr,
                               kPixToDip, 0, 0, 0, base::TimeTicks(),
                               kAndroidActionDown, pointer_count, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, false, &p0, &p1);

  EXPECT_EQ(0.f, event.GetOrientation(0));
  EXPECT_EQ(0.f, event.GetOrientation(1));
}

TEST(MotionEventAndroidTest, NonEmptyHistoryForNonMoveEventsSanitized) {
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(base::TimeTicks());

  int pointer_count = 1;
  size_t history_size = 5;
  MotionEventAndroid::Pointer p0(0, 0, 0, 0, 0, 0, 0, 0, 0);
  MotionEventAndroidJava event(
      base::android::AttachCurrentThread(), nullptr, kPixToDip, 0, 0, 0,
      base::TimeTicks(), base::TimeTicks(), base::TimeTicks(),
      kAndroidActionDown, pointer_count, history_size, 0, 0, 0, 0, 0, 0, 0, 0,
      false, &p0, nullptr, false);

  EXPECT_EQ(0U, event.GetHistorySize());
}

TEST(MotionEventAndroidTest, ActionIndexForPointerDown) {
  ui::test::ScopedEventTestTickClock clock;
  clock.SetNowTicks(base::TimeTicks());

  MotionEventAndroid::Pointer p0(1, 13.7f, -7.13f, 5.3f, 1.2f, 0.4f, 0.1f, 0.2f,
                                 kAndroidToolTypeFinger);
  MotionEventAndroid::Pointer p1(2, -13.7f, 7.13f, 3.5f, 12.1f, 4.0f, -0.1f,
                                 -0.4f, kAndroidToolTypeFinger);
  int pointer_count = 2;
  int history_size = 0;
  int action_index = 1;
  MotionEventAndroidJava event(
      base::android::AttachCurrentThread(), nullptr, kPixToDip, 0, 0, 0,
      base::TimeTicks(), kAndroidActionPointerDown, pointer_count, history_size,
      action_index, 0, 0, 0, 0, 0, 0, 0, false, &p0, &p1);

  EXPECT_EQ(MotionEvent::Action::POINTER_DOWN, event.GetAction());
  EXPECT_EQ(action_index, event.GetActionIndex());
}

TEST(MotionEventAndroidTest, NativeBackedConstructor) {
  if (base::android::BuildInfo::GetInstance()->sdk_int() <
      base::android::SDK_VERSION_S) {
    GTEST_SKIP()
        << "AMotionEvent_fromJava used in test is only available on S+";
  }

  const float x = 100;
  const float y = 200;
  // Java_MotionEvent_obtain expects timestamps(down time, event time) obtained
  // from |SystemClock#uptimeMillis()|.
  const jlong down_time_ms = base::TimeTicks::Now().ToUptimeMillis();
  const jlong event_time_ms = down_time_ms;
  JNIEnv* env = base::android::AttachCurrentThread();
  base::android::ScopedJavaLocalRef<jobject> java_motion_event =
      JNI_MotionEvent::Java_MotionEvent_obtain(env, down_time_ms, event_time_ms,
                                               kAndroidActionDown, x, y,
                                               kAndroidAltKeyDown);
  const AInputEvent* native_event = nullptr;
  if (__builtin_available(android 31, *)) {
    native_event = AMotionEvent_fromJava(env, java_motion_event.obj());
  }
  CHECK(native_event != nullptr);

  std::unique_ptr<MotionEventAndroid> event = MotionEventAndroidNative::Create(
      base::android::ScopedInputEvent(native_event), kPixToDip,
      /* y_offset_pix= */ 0,
      /* event_times= */ std::nullopt);

  EXPECT_EQ(event->GetX(0), x * kPixToDip);
  EXPECT_EQ(event->GetY(0), y * kPixToDip);
  EXPECT_EQ(event->GetAction(), MotionEvent::Action::DOWN);
  EXPECT_EQ(event->GetFlags(), ui::EF_ALT_DOWN);

  EXPECT_EQ(event->GetEventTime(),
            base::TimeTicks::FromUptimeMillis(event_time_ms));
  EXPECT_EQ(event->GetDownTime(),
            base::TimeTicks::FromUptimeMillis(down_time_ms));
}

}  // namespace ui