Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 18 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 19 | #include <cmath> |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 20 | #include <compare> |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 21 | #include <ios> |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 22 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 24 | #include <android/input.h> |
| 25 | #include <gmock/gmock.h> |
| 26 | #include <gtest/gtest.h> |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 27 | #include <input/Input.h> |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 28 | #include <input/PrintTools.h> |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 29 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 30 | #include "NotifyArgs.h" |
Harry Cutts | 8743f18 | 2023-05-17 12:03:49 +0000 | [diff] [blame] | 31 | #include "TestConstants.h" |
| 32 | |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 35 | struct PointF { |
| 36 | float x; |
| 37 | float y; |
| 38 | auto operator<=>(const PointF&) const = default; |
| 39 | }; |
| 40 | |
| 41 | inline std::string pointFToString(const PointF& p) { |
| 42 | return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")"; |
| 43 | } |
| 44 | |
| 45 | /// Source |
| 46 | class WithSourceMatcher { |
| 47 | public: |
| 48 | using is_gtest_matcher = void; |
| 49 | explicit WithSourceMatcher(uint32_t source) : mSource(source) {} |
| 50 | |
| 51 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 52 | return mSource == args.source; |
| 53 | } |
| 54 | |
| 55 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 56 | return mSource == args.source; |
| 57 | } |
| 58 | |
| 59 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 60 | return mSource == event.getSource(); |
| 61 | } |
| 62 | |
| 63 | void DescribeTo(std::ostream* os) const { |
| 64 | *os << "with source " << inputEventSourceToString(mSource); |
| 65 | } |
| 66 | |
| 67 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong source"; } |
| 68 | |
| 69 | private: |
| 70 | const uint32_t mSource; |
| 71 | }; |
| 72 | |
| 73 | inline WithSourceMatcher WithSource(uint32_t source) { |
| 74 | return WithSourceMatcher(source); |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 77 | /// Key action |
| 78 | class WithKeyActionMatcher { |
| 79 | public: |
| 80 | using is_gtest_matcher = void; |
| 81 | explicit WithKeyActionMatcher(int32_t action) : mAction(action) {} |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 82 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 83 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 84 | return mAction == args.action; |
| 85 | } |
| 86 | |
| 87 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 88 | return mAction == event.getAction(); |
| 89 | } |
| 90 | |
| 91 | void DescribeTo(std::ostream* os) const { |
| 92 | *os << "with key action " << KeyEvent::actionToString(mAction); |
| 93 | } |
| 94 | |
| 95 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; } |
| 96 | |
| 97 | private: |
| 98 | const int32_t mAction; |
| 99 | }; |
| 100 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 101 | inline WithKeyActionMatcher WithKeyAction(int32_t action) { |
| 102 | return WithKeyActionMatcher(action); |
| 103 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 104 | |
| 105 | /// Motion action |
| 106 | class WithMotionActionMatcher { |
| 107 | public: |
| 108 | using is_gtest_matcher = void; |
| 109 | explicit WithMotionActionMatcher(int32_t action) : mAction(action) {} |
| 110 | |
| 111 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 112 | bool matches = mAction == args.action; |
| 113 | if (args.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 114 | matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 115 | } |
| 116 | return matches; |
| 117 | } |
| 118 | |
| 119 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 120 | bool matches = mAction == event.getAction(); |
| 121 | if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) { |
| 122 | matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 123 | } |
| 124 | return matches; |
| 125 | } |
| 126 | |
| 127 | void DescribeTo(std::ostream* os) const { |
| 128 | *os << "with motion action " << MotionEvent::actionToString(mAction); |
| 129 | if (mAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 130 | *os << " and FLAG_CANCELED"; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; } |
| 135 | |
| 136 | private: |
| 137 | const int32_t mAction; |
| 138 | }; |
| 139 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 140 | inline WithMotionActionMatcher WithMotionAction(int32_t action) { |
| 141 | return WithMotionActionMatcher(action); |
| 142 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 143 | |
| 144 | /// Display Id |
| 145 | class WithDisplayIdMatcher { |
| 146 | public: |
| 147 | using is_gtest_matcher = void; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 148 | explicit WithDisplayIdMatcher(ui::LogicalDisplayId displayId) : mDisplayId(displayId) {} |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 149 | |
| 150 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 151 | return mDisplayId == args.displayId; |
| 152 | } |
| 153 | |
| 154 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 155 | return mDisplayId == args.displayId; |
| 156 | } |
| 157 | |
| 158 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 159 | return mDisplayId == event.getDisplayId(); |
| 160 | } |
| 161 | |
| 162 | void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; } |
| 163 | |
| 164 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; } |
| 165 | |
| 166 | private: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 167 | const ui::LogicalDisplayId mDisplayId; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 170 | inline WithDisplayIdMatcher WithDisplayId(ui::LogicalDisplayId displayId) { |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 171 | return WithDisplayIdMatcher(displayId); |
| 172 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 173 | |
| 174 | /// Device Id |
| 175 | class WithDeviceIdMatcher { |
| 176 | public: |
| 177 | using is_gtest_matcher = void; |
| 178 | explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {} |
| 179 | |
| 180 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 181 | return mDeviceId == args.deviceId; |
| 182 | } |
| 183 | |
| 184 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 185 | return mDeviceId == args.deviceId; |
| 186 | } |
| 187 | |
Prabir Pradhan | 85cf63e | 2023-08-07 21:02:13 +0000 | [diff] [blame] | 188 | bool MatchAndExplain(const NotifyDeviceResetArgs& args, std::ostream*) const { |
| 189 | return mDeviceId == args.deviceId; |
| 190 | } |
| 191 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 192 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 193 | return mDeviceId == event.getDeviceId(); |
| 194 | } |
| 195 | |
| 196 | void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; } |
| 197 | |
| 198 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; } |
| 199 | |
| 200 | private: |
| 201 | const int32_t mDeviceId; |
| 202 | }; |
| 203 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 204 | inline WithDeviceIdMatcher WithDeviceId(int32_t deviceId) { |
| 205 | return WithDeviceIdMatcher(deviceId); |
| 206 | } |
| 207 | |
| 208 | /// Flags |
| 209 | class WithFlagsMatcher { |
| 210 | public: |
| 211 | using is_gtest_matcher = void; |
| 212 | explicit WithFlagsMatcher(int32_t flags) : mFlags(flags) {} |
| 213 | |
| 214 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 215 | return mFlags == args.flags; |
| 216 | } |
| 217 | |
| 218 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 219 | return mFlags == args.flags; |
| 220 | } |
| 221 | |
| 222 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 223 | return mFlags == event.getFlags(); |
| 224 | } |
| 225 | |
| 226 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 227 | return mFlags == event.getFlags(); |
| 228 | } |
| 229 | |
| 230 | void DescribeTo(std::ostream* os) const { |
| 231 | *os << "with flags " << base::StringPrintf("0x%x", mFlags); |
| 232 | } |
| 233 | |
| 234 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong flags"; } |
| 235 | |
| 236 | private: |
| 237 | const int32_t mFlags; |
| 238 | }; |
| 239 | |
| 240 | inline WithFlagsMatcher WithFlags(int32_t flags) { |
| 241 | return WithFlagsMatcher(flags); |
| 242 | } |
| 243 | |
| 244 | /// DownTime |
| 245 | class WithDownTimeMatcher { |
| 246 | public: |
| 247 | using is_gtest_matcher = void; |
| 248 | explicit WithDownTimeMatcher(nsecs_t downTime) : mDownTime(downTime) {} |
| 249 | |
| 250 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 251 | return mDownTime == args.downTime; |
| 252 | } |
| 253 | |
| 254 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 255 | return mDownTime == args.downTime; |
| 256 | } |
| 257 | |
| 258 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 259 | return mDownTime == event.getDownTime(); |
| 260 | } |
| 261 | |
| 262 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 263 | return mDownTime == event.getDownTime(); |
| 264 | } |
| 265 | |
| 266 | void DescribeTo(std::ostream* os) const { *os << "with down time " << mDownTime; } |
| 267 | |
| 268 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong down time"; } |
| 269 | |
| 270 | private: |
| 271 | const nsecs_t mDownTime; |
| 272 | }; |
| 273 | |
| 274 | inline WithDownTimeMatcher WithDownTime(nsecs_t downTime) { |
| 275 | return WithDownTimeMatcher(downTime); |
| 276 | } |
| 277 | |
| 278 | /// Coordinate matcher |
| 279 | class WithCoordsMatcher { |
| 280 | public: |
| 281 | using is_gtest_matcher = void; |
| 282 | explicit WithCoordsMatcher(size_t pointerIndex, float x, float y) |
| 283 | : mPointerIndex(pointerIndex), mX(x), mY(y) {} |
| 284 | |
| 285 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 286 | if (mPointerIndex >= event.getPointerCount()) { |
| 287 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | bool matches = mX == event.getX(mPointerIndex) && mY == event.getY(mPointerIndex); |
| 292 | if (!matches) { |
| 293 | *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex |
| 294 | << ", but got (" << event.getX(mPointerIndex) << ", " << event.getY(mPointerIndex) |
| 295 | << ")"; |
| 296 | } |
| 297 | return matches; |
| 298 | } |
| 299 | |
| 300 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 301 | if (mPointerIndex >= event.pointerCoords.size()) { |
| 302 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool matches = mX == event.pointerCoords[mPointerIndex].getX() && |
| 307 | mY == event.pointerCoords[mPointerIndex].getY(); |
| 308 | if (!matches) { |
| 309 | *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex |
| 310 | << ", but got (" << event.pointerCoords[mPointerIndex].getX() << ", " |
| 311 | << event.pointerCoords[mPointerIndex].getY() << ")"; |
| 312 | } |
| 313 | return matches; |
| 314 | } |
| 315 | |
| 316 | void DescribeTo(std::ostream* os) const { |
| 317 | *os << "with coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex; |
| 318 | } |
| 319 | |
| 320 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong coords"; } |
| 321 | |
| 322 | private: |
| 323 | const size_t mPointerIndex; |
| 324 | const float mX; |
| 325 | const float mY; |
| 326 | }; |
| 327 | |
| 328 | inline WithCoordsMatcher WithCoords(float x, float y) { |
| 329 | return WithCoordsMatcher(0, x, y); |
| 330 | } |
| 331 | |
| 332 | inline WithCoordsMatcher WithPointerCoords(size_t pointerIndex, float x, float y) { |
| 333 | return WithCoordsMatcher(pointerIndex, x, y); |
| 334 | } |
| 335 | |
| 336 | /// Raw coordinate matcher |
| 337 | class WithRawCoordsMatcher { |
| 338 | public: |
| 339 | using is_gtest_matcher = void; |
| 340 | explicit WithRawCoordsMatcher(size_t pointerIndex, float rawX, float rawY) |
| 341 | : mPointerIndex(pointerIndex), mRawX(rawX), mRawY(rawY) {} |
| 342 | |
| 343 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 344 | if (mPointerIndex >= event.getPointerCount()) { |
| 345 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | bool matches = |
| 350 | mRawX == event.getRawX(mPointerIndex) && mRawY == event.getRawY(mPointerIndex); |
| 351 | if (!matches) { |
| 352 | *os << "expected raw coords (" << mRawX << ", " << mRawY << ") at pointer index " |
| 353 | << mPointerIndex << ", but got (" << event.getRawX(mPointerIndex) << ", " |
| 354 | << event.getRawY(mPointerIndex) << ")"; |
| 355 | } |
| 356 | return matches; |
| 357 | } |
| 358 | |
| 359 | void DescribeTo(std::ostream* os) const { |
| 360 | *os << "with raw coords (" << mRawX << ", " << mRawY << ") at pointer index " |
| 361 | << mPointerIndex; |
| 362 | } |
| 363 | |
| 364 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong raw coords"; } |
| 365 | |
| 366 | private: |
| 367 | const size_t mPointerIndex; |
| 368 | const float mRawX; |
| 369 | const float mRawY; |
| 370 | }; |
| 371 | |
| 372 | inline WithRawCoordsMatcher WithRawCoords(float rawX, float rawY) { |
| 373 | return WithRawCoordsMatcher(0, rawX, rawY); |
| 374 | } |
| 375 | |
| 376 | inline WithRawCoordsMatcher WithPointerRawCoords(size_t pointerIndex, float rawX, float rawY) { |
| 377 | return WithRawCoordsMatcher(pointerIndex, rawX, rawY); |
| 378 | } |
| 379 | |
| 380 | /// Pointer count |
| 381 | class WithPointerCountMatcher { |
| 382 | public: |
| 383 | using is_gtest_matcher = void; |
| 384 | explicit WithPointerCountMatcher(size_t pointerCount) : mPointerCount(pointerCount) {} |
| 385 | |
| 386 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 387 | if (event.getPointerCount() != mPointerCount) { |
| 388 | *os << "expected pointer count " << mPointerCount << ", but got " |
| 389 | << event.getPointerCount(); |
| 390 | return false; |
| 391 | } |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 396 | if (event.pointerCoords.size() != mPointerCount) { |
| 397 | *os << "expected pointer count " << mPointerCount << ", but got " |
| 398 | << event.pointerCoords.size(); |
| 399 | return false; |
| 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | void DescribeTo(std::ostream* os) const { *os << "with pointer count " << mPointerCount; } |
| 405 | |
| 406 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer count"; } |
| 407 | |
| 408 | private: |
| 409 | const size_t mPointerCount; |
| 410 | }; |
| 411 | |
| 412 | inline WithPointerCountMatcher WithPointerCount(size_t pointerCount) { |
| 413 | return WithPointerCountMatcher(pointerCount); |
| 414 | } |
| 415 | |
| 416 | /// Pointers matcher |
| 417 | class WithPointersMatcher { |
| 418 | public: |
| 419 | using is_gtest_matcher = void; |
| 420 | explicit WithPointersMatcher(std::map<int32_t, PointF> pointers) : mPointers(pointers) {} |
| 421 | |
| 422 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 423 | std::map<int32_t, PointF> actualPointers; |
| 424 | for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) { |
| 425 | const int32_t pointerId = event.getPointerId(pointerIndex); |
| 426 | actualPointers[pointerId] = {event.getX(pointerIndex), event.getY(pointerIndex)}; |
| 427 | } |
| 428 | |
| 429 | if (mPointers != actualPointers) { |
| 430 | *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString) |
| 431 | << ", but got " << dumpMap(actualPointers, constToString, pointFToString); |
| 432 | return false; |
| 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 438 | std::map<int32_t, PointF> actualPointers; |
| 439 | for (size_t pointerIndex = 0; pointerIndex < event.pointerCoords.size(); pointerIndex++) { |
| 440 | const int32_t pointerId = event.pointerProperties[pointerIndex].id; |
| 441 | actualPointers[pointerId] = {event.pointerCoords[pointerIndex].getX(), |
| 442 | event.pointerCoords[pointerIndex].getY()}; |
| 443 | } |
| 444 | |
| 445 | if (mPointers != actualPointers) { |
| 446 | *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString) |
| 447 | << ", but got " << dumpMap(actualPointers, constToString, pointFToString); |
| 448 | return false; |
| 449 | } |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | void DescribeTo(std::ostream* os) const { |
| 454 | *os << "with pointers " << dumpMap(mPointers, constToString, pointFToString); |
| 455 | } |
| 456 | |
| 457 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointers"; } |
| 458 | |
| 459 | private: |
| 460 | const std::map<int32_t, PointF> mPointers; |
| 461 | }; |
| 462 | |
| 463 | inline WithPointersMatcher WithPointers( |
| 464 | const std::map<int32_t /*id*/, PointF /*coords*/>& pointers) { |
| 465 | return WithPointersMatcher(pointers); |
| 466 | } |
Prabir Pradhan | 484d55a | 2022-10-14 23:17:16 +0000 | [diff] [blame] | 467 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 468 | /// Pointer ids matcher |
| 469 | class WithPointerIdsMatcher { |
| 470 | public: |
| 471 | using is_gtest_matcher = void; |
| 472 | explicit WithPointerIdsMatcher(std::set<int32_t> pointerIds) : mPointerIds(pointerIds) {} |
| 473 | |
| 474 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 475 | std::set<int32_t> actualPointerIds; |
| 476 | for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) { |
| 477 | const PointerProperties* properties = event.getPointerProperties(pointerIndex); |
| 478 | actualPointerIds.insert(properties->id); |
| 479 | } |
| 480 | |
| 481 | if (mPointerIds != actualPointerIds) { |
| 482 | *os << "expected pointer ids " << dumpSet(mPointerIds) << ", but got " |
| 483 | << dumpSet(actualPointerIds); |
| 484 | return false; |
| 485 | } |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 490 | std::set<int32_t> actualPointerIds; |
| 491 | for (const PointerProperties& properties : event.pointerProperties) { |
| 492 | actualPointerIds.insert(properties.id); |
| 493 | } |
| 494 | |
| 495 | if (mPointerIds != actualPointerIds) { |
| 496 | *os << "expected pointer ids " << dumpSet(mPointerIds) << ", but got " |
| 497 | << dumpSet(actualPointerIds); |
| 498 | return false; |
| 499 | } |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | void DescribeTo(std::ostream* os) const { *os << "with pointer ids " << dumpSet(mPointerIds); } |
| 504 | |
| 505 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer ids"; } |
| 506 | |
| 507 | private: |
| 508 | const std::set<int32_t> mPointerIds; |
| 509 | }; |
| 510 | |
| 511 | inline WithPointerIdsMatcher WithPointerIds(const std::set<int32_t /*id*/>& pointerIds) { |
| 512 | return WithPointerIdsMatcher(pointerIds); |
| 513 | } |
| 514 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 515 | /// Key code |
| 516 | class WithKeyCodeMatcher { |
| 517 | public: |
| 518 | using is_gtest_matcher = void; |
| 519 | explicit WithKeyCodeMatcher(int32_t keyCode) : mKeyCode(keyCode) {} |
| 520 | |
| 521 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 522 | return mKeyCode == args.keyCode; |
| 523 | } |
| 524 | |
| 525 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 526 | return mKeyCode == event.getKeyCode(); |
| 527 | } |
| 528 | |
| 529 | void DescribeTo(std::ostream* os) const { |
| 530 | *os << "with key code " << KeyEvent::getLabel(mKeyCode); |
| 531 | } |
| 532 | |
| 533 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong key code"; } |
| 534 | |
| 535 | private: |
| 536 | const int32_t mKeyCode; |
| 537 | }; |
| 538 | |
| 539 | inline WithKeyCodeMatcher WithKeyCode(int32_t keyCode) { |
| 540 | return WithKeyCodeMatcher(keyCode); |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 543 | /// EventId |
| 544 | class WithEventIdMatcher { |
| 545 | public: |
| 546 | using is_gtest_matcher = void; |
| 547 | explicit WithEventIdMatcher(int32_t eventId) : mEventId(eventId) {} |
| 548 | |
| 549 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 550 | return mEventId == args.id; |
| 551 | } |
| 552 | |
| 553 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 554 | return mEventId == args.id; |
| 555 | } |
| 556 | |
| 557 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 558 | return mEventId == event.getId(); |
| 559 | } |
| 560 | |
| 561 | void DescribeTo(std::ostream* os) const { *os << "with eventId 0x" << std::hex << mEventId; } |
| 562 | |
| 563 | void DescribeNegationTo(std::ostream* os) const { |
| 564 | *os << "with eventId not equal to 0x" << std::hex << mEventId; |
| 565 | } |
| 566 | |
| 567 | private: |
| 568 | const int32_t mEventId; |
| 569 | }; |
| 570 | |
| 571 | inline WithEventIdMatcher WithEventId(int32_t eventId) { |
| 572 | return WithEventIdMatcher(eventId); |
| 573 | } |
| 574 | |
| 575 | /// EventIdSource |
| 576 | class WithEventIdSourceMatcher { |
| 577 | public: |
| 578 | using is_gtest_matcher = void; |
| 579 | explicit WithEventIdSourceMatcher(IdGenerator::Source eventIdSource) |
| 580 | : mEventIdSource(eventIdSource) {} |
| 581 | |
| 582 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 583 | return mEventIdSource == IdGenerator::getSource(args.id); |
| 584 | } |
| 585 | |
| 586 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 587 | return mEventIdSource == IdGenerator::getSource(args.id); |
| 588 | } |
| 589 | |
| 590 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 591 | return mEventIdSource == IdGenerator::getSource(event.getId()); |
| 592 | } |
| 593 | |
| 594 | void DescribeTo(std::ostream* os) const { |
| 595 | *os << "with eventId from source 0x" << std::hex << ftl::to_underlying(mEventIdSource); |
| 596 | } |
| 597 | |
| 598 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong event from source"; } |
| 599 | |
| 600 | private: |
| 601 | const IdGenerator::Source mEventIdSource; |
| 602 | }; |
| 603 | |
| 604 | inline WithEventIdSourceMatcher WithEventIdSource(IdGenerator::Source eventIdSource) { |
| 605 | return WithEventIdSourceMatcher(eventIdSource); |
| 606 | } |
| 607 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 608 | MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") { |
| 609 | return arg.getRepeatCount() == repeatCount; |
| 610 | } |
| 611 | |
Siarhei Vishniakou | 27fc9f1 | 2024-07-13 23:59:47 -0700 | [diff] [blame] | 612 | class WithPointerIdMatcher { |
| 613 | public: |
| 614 | using is_gtest_matcher = void; |
| 615 | explicit WithPointerIdMatcher(size_t index, int32_t pointerId) |
| 616 | : mIndex(index), mPointerId(pointerId) {} |
| 617 | |
| 618 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 619 | return args.pointerProperties[mIndex].id == mPointerId; |
| 620 | } |
| 621 | |
| 622 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 623 | return event.getPointerId(mIndex) == mPointerId; |
| 624 | } |
| 625 | |
| 626 | void DescribeTo(std::ostream* os) const { |
| 627 | *os << "with pointer[" << mIndex << "] id = " << mPointerId; |
| 628 | } |
| 629 | |
| 630 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointerId"; } |
| 631 | |
| 632 | private: |
| 633 | const size_t mIndex; |
| 634 | const int32_t mPointerId; |
| 635 | }; |
| 636 | |
| 637 | inline WithPointerIdMatcher WithPointerId(size_t index, int32_t pointerId) { |
| 638 | return WithPointerIdMatcher(index, pointerId); |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Siarhei Vishniakou | 5197ce6 | 2023-09-19 08:27:05 -0700 | [diff] [blame] | 641 | MATCHER_P2(WithCursorPosition, x, y, "InputEvent with specified cursor position") { |
| 642 | const auto argX = arg.xCursorPosition; |
| 643 | const auto argY = arg.yCursorPosition; |
| 644 | *result_listener << "expected cursor position (" << x << ", " << y << "), but got (" << argX |
| 645 | << ", " << argY << ")"; |
| 646 | return (isnan(x) ? isnan(argX) : x == argX) && (isnan(y) ? isnan(argY) : y == argY); |
| 647 | } |
| 648 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 649 | MATCHER_P2(WithRelativeMotion, x, y, "InputEvent with specified relative motion") { |
| 650 | const auto argX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 651 | const auto argY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 652 | *result_listener << "expected relative motion (" << x << ", " << y << "), but got (" << argX |
| 653 | << ", " << argY << ")"; |
| 654 | return argX == x && argY == y; |
| 655 | } |
| 656 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 657 | MATCHER_P3(WithGestureOffset, dx, dy, epsilon, |
| 658 | "InputEvent with specified touchpad gesture offset") { |
| 659 | const auto argGestureX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET); |
| 660 | const auto argGestureY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET); |
| 661 | const double xDiff = fabs(argGestureX - dx); |
| 662 | const double yDiff = fabs(argGestureY - dy); |
| 663 | *result_listener << "expected gesture offset (" << dx << ", " << dy << ") within " << epsilon |
| 664 | << ", but got (" << argGestureX << ", " << argGestureY << ")"; |
| 665 | return xDiff <= epsilon && yDiff <= epsilon; |
| 666 | } |
| 667 | |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 668 | MATCHER_P3(WithGestureScrollDistance, x, y, epsilon, |
| 669 | "InputEvent with specified touchpad gesture scroll distance") { |
| 670 | const auto argXDistance = |
| 671 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE); |
| 672 | const auto argYDistance = |
| 673 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE); |
| 674 | const double xDiff = fabs(argXDistance - x); |
| 675 | const double yDiff = fabs(argYDistance - y); |
| 676 | *result_listener << "expected gesture offset (" << x << ", " << y << ") within " << epsilon |
| 677 | << ", but got (" << argXDistance << ", " << argYDistance << ")"; |
| 678 | return xDiff <= epsilon && yDiff <= epsilon; |
| 679 | } |
| 680 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 681 | MATCHER_P2(WithGesturePinchScaleFactor, factor, epsilon, |
| 682 | "InputEvent with specified touchpad pinch gesture scale factor") { |
| 683 | const auto argScaleFactor = |
| 684 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR); |
| 685 | *result_listener << "expected gesture scale factor " << factor << " within " << epsilon |
| 686 | << " but got " << argScaleFactor; |
| 687 | return fabs(argScaleFactor - factor) <= epsilon; |
| 688 | } |
| 689 | |
Harry Cutts | 8743f18 | 2023-05-17 12:03:49 +0000 | [diff] [blame] | 690 | MATCHER_P(WithGestureSwipeFingerCount, count, |
| 691 | "InputEvent with specified touchpad swipe finger count") { |
| 692 | const auto argFingerCount = |
| 693 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT); |
| 694 | *result_listener << "expected gesture swipe finger count " << count << " but got " |
| 695 | << argFingerCount; |
| 696 | return fabs(argFingerCount - count) <= EPSILON; |
| 697 | } |
| 698 | |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 699 | MATCHER_P(WithPressure, pressure, "InputEvent with specified pressure") { |
| 700 | const auto argPressure = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE); |
Prabir Pradhan | 7d04c4b | 2022-10-28 19:23:26 +0000 | [diff] [blame] | 701 | *result_listener << "expected pressure " << pressure << ", but got " << argPressure; |
| 702 | return argPressure == pressure; |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 705 | MATCHER_P(WithSize, size, "MotionEvent with specified size") { |
| 706 | const auto argSize = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_SIZE); |
| 707 | *result_listener << "expected size " << size << ", but got " << argSize; |
| 708 | return argSize == size; |
| 709 | } |
| 710 | |
| 711 | MATCHER_P(WithOrientation, orientation, "MotionEvent with specified orientation") { |
| 712 | const auto argOrientation = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 713 | *result_listener << "expected orientation " << orientation << ", but got " << argOrientation; |
| 714 | return argOrientation == orientation; |
| 715 | } |
| 716 | |
| 717 | MATCHER_P(WithDistance, distance, "MotionEvent with specified distance") { |
| 718 | const auto argDistance = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_DISTANCE); |
| 719 | *result_listener << "expected distance " << distance << ", but got " << argDistance; |
| 720 | return argDistance == distance; |
| 721 | } |
| 722 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 723 | MATCHER_P2(WithTouchDimensions, maj, min, "InputEvent with specified touch dimensions") { |
| 724 | const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR); |
| 725 | const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR); |
| 726 | *result_listener << "expected touch dimensions " << maj << " major x " << min |
| 727 | << " minor, but got " << argMajor << " major x " << argMinor << " minor"; |
| 728 | return argMajor == maj && argMinor == min; |
| 729 | } |
| 730 | |
| 731 | MATCHER_P2(WithToolDimensions, maj, min, "InputEvent with specified tool dimensions") { |
| 732 | const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR); |
| 733 | const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR); |
| 734 | *result_listener << "expected tool dimensions " << maj << " major x " << min |
| 735 | << " minor, but got " << argMajor << " major x " << argMinor << " minor"; |
| 736 | return argMajor == maj && argMinor == min; |
| 737 | } |
| 738 | |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 739 | MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") { |
| 740 | const auto argToolType = arg.pointerProperties[0].toolType; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 741 | *result_listener << "expected tool type " << ftl::enum_string(toolType) << ", but got " |
| 742 | << ftl::enum_string(argToolType); |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 743 | return argToolType == toolType; |
| 744 | } |
| 745 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 746 | MATCHER_P2(WithPointerToolType, pointer, toolType, |
| 747 | "InputEvent with specified tool type for pointer") { |
| 748 | const auto argToolType = arg.pointerProperties[pointer].toolType; |
| 749 | *result_listener << "expected pointer " << pointer << " to have tool type " |
| 750 | << ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType); |
| 751 | return argToolType == toolType; |
| 752 | } |
| 753 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 754 | MATCHER_P(WithMotionClassification, classification, |
| 755 | "InputEvent with specified MotionClassification") { |
| 756 | *result_listener << "expected classification " << motionClassificationToString(classification) |
| 757 | << ", but got " << motionClassificationToString(arg.classification); |
| 758 | return arg.classification == classification; |
| 759 | } |
| 760 | |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 761 | MATCHER_P(WithButtonState, buttons, "InputEvent with specified button state") { |
| 762 | *result_listener << "expected button state " << buttons << ", but got " << arg.buttonState; |
| 763 | return arg.buttonState == buttons; |
| 764 | } |
| 765 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 766 | MATCHER_P(WithMetaState, metaState, "InputEvent with specified meta state") { |
| 767 | *result_listener << "expected meta state 0x" << std::hex << metaState << ", but got 0x" |
| 768 | << arg.metaState; |
| 769 | return arg.metaState == metaState; |
| 770 | } |
| 771 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 772 | MATCHER_P(WithActionButton, actionButton, "InputEvent with specified action button") { |
| 773 | *result_listener << "expected action button " << actionButton << ", but got " |
| 774 | << arg.actionButton; |
| 775 | return arg.actionButton == actionButton; |
| 776 | } |
| 777 | |
Prabir Pradhan | 2f37bcb | 2022-11-08 20:41:28 +0000 | [diff] [blame] | 778 | MATCHER_P(WithEventTime, eventTime, "InputEvent with specified eventTime") { |
| 779 | *result_listener << "expected event time " << eventTime << ", but got " << arg.eventTime; |
| 780 | return arg.eventTime == eventTime; |
| 781 | } |
| 782 | |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 783 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 784 | *result_listener << "expected down time " << downTime << ", but got " << arg.downTime; |
| 785 | return arg.downTime == downTime; |
| 786 | } |
| 787 | |
Prabir Pradhan | a9df316 | 2022-12-05 23:57:27 +0000 | [diff] [blame] | 788 | MATCHER_P2(WithPrecision, xPrecision, yPrecision, "MotionEvent with specified precision") { |
| 789 | *result_listener << "expected x-precision " << xPrecision << " and y-precision " << yPrecision |
| 790 | << ", but got " << arg.xPrecision << " and " << arg.yPrecision; |
| 791 | return arg.xPrecision == xPrecision && arg.yPrecision == yPrecision; |
| 792 | } |
| 793 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 794 | MATCHER_P(WithPolicyFlags, policyFlags, "InputEvent with specified policy flags") { |
| 795 | *result_listener << "expected policy flags 0x" << std::hex << policyFlags << ", but got 0x" |
| 796 | << arg.policyFlags; |
| 797 | return arg.policyFlags == static_cast<uint32_t>(policyFlags); |
| 798 | } |
| 799 | |
| 800 | MATCHER_P(WithEdgeFlags, edgeFlags, "InputEvent with specified edge flags") { |
| 801 | *result_listener << "expected edge flags 0x" << std::hex << edgeFlags << ", but got 0x" |
| 802 | << arg.edgeFlags; |
| 803 | return arg.edgeFlags == edgeFlags; |
| 804 | } |
| 805 | |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 806 | } // namespace android |