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 | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 21 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 23 | #include <android/input.h> |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 26 | #include <input/Input.h> |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 27 | #include <input/PrintTools.h> |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 28 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 29 | #include "NotifyArgs.h" |
Harry Cutts | 8743f18 | 2023-05-17 12:03:49 +0000 | [diff] [blame] | 30 | #include "TestConstants.h" |
| 31 | |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 34 | struct PointF { |
| 35 | float x; |
| 36 | float y; |
| 37 | auto operator<=>(const PointF&) const = default; |
| 38 | }; |
| 39 | |
| 40 | inline std::string pointFToString(const PointF& p) { |
| 41 | return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")"; |
| 42 | } |
| 43 | |
| 44 | /// Source |
| 45 | class WithSourceMatcher { |
| 46 | public: |
| 47 | using is_gtest_matcher = void; |
| 48 | explicit WithSourceMatcher(uint32_t source) : mSource(source) {} |
| 49 | |
| 50 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 51 | return mSource == args.source; |
| 52 | } |
| 53 | |
| 54 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 55 | return mSource == args.source; |
| 56 | } |
| 57 | |
| 58 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 59 | return mSource == event.getSource(); |
| 60 | } |
| 61 | |
| 62 | void DescribeTo(std::ostream* os) const { |
| 63 | *os << "with source " << inputEventSourceToString(mSource); |
| 64 | } |
| 65 | |
| 66 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong source"; } |
| 67 | |
| 68 | private: |
| 69 | const uint32_t mSource; |
| 70 | }; |
| 71 | |
| 72 | inline WithSourceMatcher WithSource(uint32_t source) { |
| 73 | return WithSourceMatcher(source); |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 76 | /// Key action |
| 77 | class WithKeyActionMatcher { |
| 78 | public: |
| 79 | using is_gtest_matcher = void; |
| 80 | explicit WithKeyActionMatcher(int32_t action) : mAction(action) {} |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 81 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 82 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 83 | return mAction == args.action; |
| 84 | } |
| 85 | |
| 86 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 87 | return mAction == event.getAction(); |
| 88 | } |
| 89 | |
| 90 | void DescribeTo(std::ostream* os) const { |
| 91 | *os << "with key action " << KeyEvent::actionToString(mAction); |
| 92 | } |
| 93 | |
| 94 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; } |
| 95 | |
| 96 | private: |
| 97 | const int32_t mAction; |
| 98 | }; |
| 99 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 100 | inline WithKeyActionMatcher WithKeyAction(int32_t action) { |
| 101 | return WithKeyActionMatcher(action); |
| 102 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 103 | |
| 104 | /// Motion action |
| 105 | class WithMotionActionMatcher { |
| 106 | public: |
| 107 | using is_gtest_matcher = void; |
| 108 | explicit WithMotionActionMatcher(int32_t action) : mAction(action) {} |
| 109 | |
| 110 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 111 | bool matches = mAction == args.action; |
| 112 | if (args.action == AMOTION_EVENT_ACTION_CANCEL) { |
| 113 | matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 114 | } |
| 115 | return matches; |
| 116 | } |
| 117 | |
| 118 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 119 | bool matches = mAction == event.getAction(); |
| 120 | if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) { |
| 121 | matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 122 | } |
| 123 | return matches; |
| 124 | } |
| 125 | |
| 126 | void DescribeTo(std::ostream* os) const { |
| 127 | *os << "with motion action " << MotionEvent::actionToString(mAction); |
| 128 | if (mAction == AMOTION_EVENT_ACTION_CANCEL) { |
| 129 | *os << " and FLAG_CANCELED"; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; } |
| 134 | |
| 135 | private: |
| 136 | const int32_t mAction; |
| 137 | }; |
| 138 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 139 | inline WithMotionActionMatcher WithMotionAction(int32_t action) { |
| 140 | return WithMotionActionMatcher(action); |
| 141 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 142 | |
| 143 | /// Display Id |
| 144 | class WithDisplayIdMatcher { |
| 145 | public: |
| 146 | using is_gtest_matcher = void; |
| 147 | explicit WithDisplayIdMatcher(int32_t displayId) : mDisplayId(displayId) {} |
| 148 | |
| 149 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 150 | return mDisplayId == args.displayId; |
| 151 | } |
| 152 | |
| 153 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 154 | return mDisplayId == args.displayId; |
| 155 | } |
| 156 | |
| 157 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 158 | return mDisplayId == event.getDisplayId(); |
| 159 | } |
| 160 | |
| 161 | void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; } |
| 162 | |
| 163 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; } |
| 164 | |
| 165 | private: |
| 166 | const int32_t mDisplayId; |
| 167 | }; |
| 168 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 169 | inline WithDisplayIdMatcher WithDisplayId(int32_t displayId) { |
| 170 | return WithDisplayIdMatcher(displayId); |
| 171 | } |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 172 | |
| 173 | /// Device Id |
| 174 | class WithDeviceIdMatcher { |
| 175 | public: |
| 176 | using is_gtest_matcher = void; |
| 177 | explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {} |
| 178 | |
| 179 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 180 | return mDeviceId == args.deviceId; |
| 181 | } |
| 182 | |
| 183 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 184 | return mDeviceId == args.deviceId; |
| 185 | } |
| 186 | |
Prabir Pradhan | 85cf63e | 2023-08-07 21:02:13 +0000 | [diff] [blame] | 187 | bool MatchAndExplain(const NotifyDeviceResetArgs& args, std::ostream*) const { |
| 188 | return mDeviceId == args.deviceId; |
| 189 | } |
| 190 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 191 | bool MatchAndExplain(const InputEvent& event, std::ostream*) const { |
| 192 | return mDeviceId == event.getDeviceId(); |
| 193 | } |
| 194 | |
| 195 | void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; } |
| 196 | |
| 197 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; } |
| 198 | |
| 199 | private: |
| 200 | const int32_t mDeviceId; |
| 201 | }; |
| 202 | |
Prabir Pradhan | 7b6502d | 2023-10-06 04:08:39 +0000 | [diff] [blame] | 203 | inline WithDeviceIdMatcher WithDeviceId(int32_t deviceId) { |
| 204 | return WithDeviceIdMatcher(deviceId); |
| 205 | } |
| 206 | |
| 207 | /// Flags |
| 208 | class WithFlagsMatcher { |
| 209 | public: |
| 210 | using is_gtest_matcher = void; |
| 211 | explicit WithFlagsMatcher(int32_t flags) : mFlags(flags) {} |
| 212 | |
| 213 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 214 | return mFlags == args.flags; |
| 215 | } |
| 216 | |
| 217 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 218 | return mFlags == args.flags; |
| 219 | } |
| 220 | |
| 221 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 222 | return mFlags == event.getFlags(); |
| 223 | } |
| 224 | |
| 225 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 226 | return mFlags == event.getFlags(); |
| 227 | } |
| 228 | |
| 229 | void DescribeTo(std::ostream* os) const { |
| 230 | *os << "with flags " << base::StringPrintf("0x%x", mFlags); |
| 231 | } |
| 232 | |
| 233 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong flags"; } |
| 234 | |
| 235 | private: |
| 236 | const int32_t mFlags; |
| 237 | }; |
| 238 | |
| 239 | inline WithFlagsMatcher WithFlags(int32_t flags) { |
| 240 | return WithFlagsMatcher(flags); |
| 241 | } |
| 242 | |
| 243 | /// DownTime |
| 244 | class WithDownTimeMatcher { |
| 245 | public: |
| 246 | using is_gtest_matcher = void; |
| 247 | explicit WithDownTimeMatcher(nsecs_t downTime) : mDownTime(downTime) {} |
| 248 | |
| 249 | bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const { |
| 250 | return mDownTime == args.downTime; |
| 251 | } |
| 252 | |
| 253 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 254 | return mDownTime == args.downTime; |
| 255 | } |
| 256 | |
| 257 | bool MatchAndExplain(const MotionEvent& event, std::ostream*) const { |
| 258 | return mDownTime == event.getDownTime(); |
| 259 | } |
| 260 | |
| 261 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 262 | return mDownTime == event.getDownTime(); |
| 263 | } |
| 264 | |
| 265 | void DescribeTo(std::ostream* os) const { *os << "with down time " << mDownTime; } |
| 266 | |
| 267 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong down time"; } |
| 268 | |
| 269 | private: |
| 270 | const nsecs_t mDownTime; |
| 271 | }; |
| 272 | |
| 273 | inline WithDownTimeMatcher WithDownTime(nsecs_t downTime) { |
| 274 | return WithDownTimeMatcher(downTime); |
| 275 | } |
| 276 | |
| 277 | /// Coordinate matcher |
| 278 | class WithCoordsMatcher { |
| 279 | public: |
| 280 | using is_gtest_matcher = void; |
| 281 | explicit WithCoordsMatcher(size_t pointerIndex, float x, float y) |
| 282 | : mPointerIndex(pointerIndex), mX(x), mY(y) {} |
| 283 | |
| 284 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 285 | if (mPointerIndex >= event.getPointerCount()) { |
| 286 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool matches = mX == event.getX(mPointerIndex) && mY == event.getY(mPointerIndex); |
| 291 | if (!matches) { |
| 292 | *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex |
| 293 | << ", but got (" << event.getX(mPointerIndex) << ", " << event.getY(mPointerIndex) |
| 294 | << ")"; |
| 295 | } |
| 296 | return matches; |
| 297 | } |
| 298 | |
| 299 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 300 | if (mPointerIndex >= event.pointerCoords.size()) { |
| 301 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | bool matches = mX == event.pointerCoords[mPointerIndex].getX() && |
| 306 | mY == event.pointerCoords[mPointerIndex].getY(); |
| 307 | if (!matches) { |
| 308 | *os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex |
| 309 | << ", but got (" << event.pointerCoords[mPointerIndex].getX() << ", " |
| 310 | << event.pointerCoords[mPointerIndex].getY() << ")"; |
| 311 | } |
| 312 | return matches; |
| 313 | } |
| 314 | |
| 315 | void DescribeTo(std::ostream* os) const { |
| 316 | *os << "with coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex; |
| 317 | } |
| 318 | |
| 319 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong coords"; } |
| 320 | |
| 321 | private: |
| 322 | const size_t mPointerIndex; |
| 323 | const float mX; |
| 324 | const float mY; |
| 325 | }; |
| 326 | |
| 327 | inline WithCoordsMatcher WithCoords(float x, float y) { |
| 328 | return WithCoordsMatcher(0, x, y); |
| 329 | } |
| 330 | |
| 331 | inline WithCoordsMatcher WithPointerCoords(size_t pointerIndex, float x, float y) { |
| 332 | return WithCoordsMatcher(pointerIndex, x, y); |
| 333 | } |
| 334 | |
| 335 | /// Raw coordinate matcher |
| 336 | class WithRawCoordsMatcher { |
| 337 | public: |
| 338 | using is_gtest_matcher = void; |
| 339 | explicit WithRawCoordsMatcher(size_t pointerIndex, float rawX, float rawY) |
| 340 | : mPointerIndex(pointerIndex), mRawX(rawX), mRawY(rawY) {} |
| 341 | |
| 342 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 343 | if (mPointerIndex >= event.getPointerCount()) { |
| 344 | *os << "Pointer index " << mPointerIndex << " is out of bounds"; |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | bool matches = |
| 349 | mRawX == event.getRawX(mPointerIndex) && mRawY == event.getRawY(mPointerIndex); |
| 350 | if (!matches) { |
| 351 | *os << "expected raw coords (" << mRawX << ", " << mRawY << ") at pointer index " |
| 352 | << mPointerIndex << ", but got (" << event.getRawX(mPointerIndex) << ", " |
| 353 | << event.getRawY(mPointerIndex) << ")"; |
| 354 | } |
| 355 | return matches; |
| 356 | } |
| 357 | |
| 358 | void DescribeTo(std::ostream* os) const { |
| 359 | *os << "with raw coords (" << mRawX << ", " << mRawY << ") at pointer index " |
| 360 | << mPointerIndex; |
| 361 | } |
| 362 | |
| 363 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong raw coords"; } |
| 364 | |
| 365 | private: |
| 366 | const size_t mPointerIndex; |
| 367 | const float mRawX; |
| 368 | const float mRawY; |
| 369 | }; |
| 370 | |
| 371 | inline WithRawCoordsMatcher WithRawCoords(float rawX, float rawY) { |
| 372 | return WithRawCoordsMatcher(0, rawX, rawY); |
| 373 | } |
| 374 | |
| 375 | inline WithRawCoordsMatcher WithPointerRawCoords(size_t pointerIndex, float rawX, float rawY) { |
| 376 | return WithRawCoordsMatcher(pointerIndex, rawX, rawY); |
| 377 | } |
| 378 | |
| 379 | /// Pointer count |
| 380 | class WithPointerCountMatcher { |
| 381 | public: |
| 382 | using is_gtest_matcher = void; |
| 383 | explicit WithPointerCountMatcher(size_t pointerCount) : mPointerCount(pointerCount) {} |
| 384 | |
| 385 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 386 | if (event.getPointerCount() != mPointerCount) { |
| 387 | *os << "expected pointer count " << mPointerCount << ", but got " |
| 388 | << event.getPointerCount(); |
| 389 | return false; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 395 | if (event.pointerCoords.size() != mPointerCount) { |
| 396 | *os << "expected pointer count " << mPointerCount << ", but got " |
| 397 | << event.pointerCoords.size(); |
| 398 | return false; |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | void DescribeTo(std::ostream* os) const { *os << "with pointer count " << mPointerCount; } |
| 404 | |
| 405 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer count"; } |
| 406 | |
| 407 | private: |
| 408 | const size_t mPointerCount; |
| 409 | }; |
| 410 | |
| 411 | inline WithPointerCountMatcher WithPointerCount(size_t pointerCount) { |
| 412 | return WithPointerCountMatcher(pointerCount); |
| 413 | } |
| 414 | |
| 415 | /// Pointers matcher |
| 416 | class WithPointersMatcher { |
| 417 | public: |
| 418 | using is_gtest_matcher = void; |
| 419 | explicit WithPointersMatcher(std::map<int32_t, PointF> pointers) : mPointers(pointers) {} |
| 420 | |
| 421 | bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const { |
| 422 | std::map<int32_t, PointF> actualPointers; |
| 423 | for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) { |
| 424 | const int32_t pointerId = event.getPointerId(pointerIndex); |
| 425 | actualPointers[pointerId] = {event.getX(pointerIndex), event.getY(pointerIndex)}; |
| 426 | } |
| 427 | |
| 428 | if (mPointers != actualPointers) { |
| 429 | *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString) |
| 430 | << ", but got " << dumpMap(actualPointers, constToString, pointFToString); |
| 431 | return false; |
| 432 | } |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const { |
| 437 | std::map<int32_t, PointF> actualPointers; |
| 438 | for (size_t pointerIndex = 0; pointerIndex < event.pointerCoords.size(); pointerIndex++) { |
| 439 | const int32_t pointerId = event.pointerProperties[pointerIndex].id; |
| 440 | actualPointers[pointerId] = {event.pointerCoords[pointerIndex].getX(), |
| 441 | event.pointerCoords[pointerIndex].getY()}; |
| 442 | } |
| 443 | |
| 444 | if (mPointers != actualPointers) { |
| 445 | *os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString) |
| 446 | << ", but got " << dumpMap(actualPointers, constToString, pointFToString); |
| 447 | return false; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | void DescribeTo(std::ostream* os) const { |
| 453 | *os << "with pointers " << dumpMap(mPointers, constToString, pointFToString); |
| 454 | } |
| 455 | |
| 456 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointers"; } |
| 457 | |
| 458 | private: |
| 459 | const std::map<int32_t, PointF> mPointers; |
| 460 | }; |
| 461 | |
| 462 | inline WithPointersMatcher WithPointers( |
| 463 | const std::map<int32_t /*id*/, PointF /*coords*/>& pointers) { |
| 464 | return WithPointersMatcher(pointers); |
| 465 | } |
Prabir Pradhan | 484d55a | 2022-10-14 23:17:16 +0000 | [diff] [blame] | 466 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame^] | 467 | /// Key code |
| 468 | class WithKeyCodeMatcher { |
| 469 | public: |
| 470 | using is_gtest_matcher = void; |
| 471 | explicit WithKeyCodeMatcher(int32_t keyCode) : mKeyCode(keyCode) {} |
| 472 | |
| 473 | bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const { |
| 474 | return mKeyCode == args.keyCode; |
| 475 | } |
| 476 | |
| 477 | bool MatchAndExplain(const KeyEvent& event, std::ostream*) const { |
| 478 | return mKeyCode == event.getKeyCode(); |
| 479 | } |
| 480 | |
| 481 | void DescribeTo(std::ostream* os) const { |
| 482 | *os << "with key code " << KeyEvent::getLabel(mKeyCode); |
| 483 | } |
| 484 | |
| 485 | void DescribeNegationTo(std::ostream* os) const { *os << "wrong key code"; } |
| 486 | |
| 487 | private: |
| 488 | const int32_t mKeyCode; |
| 489 | }; |
| 490 | |
| 491 | inline WithKeyCodeMatcher WithKeyCode(int32_t keyCode) { |
| 492 | return WithKeyCodeMatcher(keyCode); |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 495 | MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") { |
| 496 | return arg.getRepeatCount() == repeatCount; |
| 497 | } |
| 498 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 499 | MATCHER_P2(WithPointerId, index, id, "MotionEvent with specified pointer ID for pointer index") { |
| 500 | const auto argPointerId = arg.pointerProperties[index].id; |
| 501 | *result_listener << "expected pointer with index " << index << " to have ID " << argPointerId; |
| 502 | return argPointerId == id; |
| 503 | } |
| 504 | |
Siarhei Vishniakou | 5197ce6 | 2023-09-19 08:27:05 -0700 | [diff] [blame] | 505 | MATCHER_P2(WithCursorPosition, x, y, "InputEvent with specified cursor position") { |
| 506 | const auto argX = arg.xCursorPosition; |
| 507 | const auto argY = arg.yCursorPosition; |
| 508 | *result_listener << "expected cursor position (" << x << ", " << y << "), but got (" << argX |
| 509 | << ", " << argY << ")"; |
| 510 | return (isnan(x) ? isnan(argX) : x == argX) && (isnan(y) ? isnan(argY) : y == argY); |
| 511 | } |
| 512 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 513 | MATCHER_P2(WithRelativeMotion, x, y, "InputEvent with specified relative motion") { |
| 514 | const auto argX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X); |
| 515 | const auto argY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y); |
| 516 | *result_listener << "expected relative motion (" << x << ", " << y << "), but got (" << argX |
| 517 | << ", " << argY << ")"; |
| 518 | return argX == x && argY == y; |
| 519 | } |
| 520 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 521 | MATCHER_P3(WithGestureOffset, dx, dy, epsilon, |
| 522 | "InputEvent with specified touchpad gesture offset") { |
| 523 | const auto argGestureX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET); |
| 524 | const auto argGestureY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET); |
| 525 | const double xDiff = fabs(argGestureX - dx); |
| 526 | const double yDiff = fabs(argGestureY - dy); |
| 527 | *result_listener << "expected gesture offset (" << dx << ", " << dy << ") within " << epsilon |
| 528 | << ", but got (" << argGestureX << ", " << argGestureY << ")"; |
| 529 | return xDiff <= epsilon && yDiff <= epsilon; |
| 530 | } |
| 531 | |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 532 | MATCHER_P3(WithGestureScrollDistance, x, y, epsilon, |
| 533 | "InputEvent with specified touchpad gesture scroll distance") { |
| 534 | const auto argXDistance = |
| 535 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE); |
| 536 | const auto argYDistance = |
| 537 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE); |
| 538 | const double xDiff = fabs(argXDistance - x); |
| 539 | const double yDiff = fabs(argYDistance - y); |
| 540 | *result_listener << "expected gesture offset (" << x << ", " << y << ") within " << epsilon |
| 541 | << ", but got (" << argXDistance << ", " << argYDistance << ")"; |
| 542 | return xDiff <= epsilon && yDiff <= epsilon; |
| 543 | } |
| 544 | |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 545 | MATCHER_P2(WithGesturePinchScaleFactor, factor, epsilon, |
| 546 | "InputEvent with specified touchpad pinch gesture scale factor") { |
| 547 | const auto argScaleFactor = |
| 548 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR); |
| 549 | *result_listener << "expected gesture scale factor " << factor << " within " << epsilon |
| 550 | << " but got " << argScaleFactor; |
| 551 | return fabs(argScaleFactor - factor) <= epsilon; |
| 552 | } |
| 553 | |
Harry Cutts | 8743f18 | 2023-05-17 12:03:49 +0000 | [diff] [blame] | 554 | MATCHER_P(WithGestureSwipeFingerCount, count, |
| 555 | "InputEvent with specified touchpad swipe finger count") { |
| 556 | const auto argFingerCount = |
| 557 | arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT); |
| 558 | *result_listener << "expected gesture swipe finger count " << count << " but got " |
| 559 | << argFingerCount; |
| 560 | return fabs(argFingerCount - count) <= EPSILON; |
| 561 | } |
| 562 | |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 563 | MATCHER_P(WithPressure, pressure, "InputEvent with specified pressure") { |
| 564 | const auto argPressure = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE); |
Prabir Pradhan | 7d04c4b | 2022-10-28 19:23:26 +0000 | [diff] [blame] | 565 | *result_listener << "expected pressure " << pressure << ", but got " << argPressure; |
| 566 | return argPressure == pressure; |
Prabir Pradhan | afabcde | 2022-09-27 19:32:43 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 569 | MATCHER_P2(WithTouchDimensions, maj, min, "InputEvent with specified touch dimensions") { |
| 570 | const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR); |
| 571 | const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR); |
| 572 | *result_listener << "expected touch dimensions " << maj << " major x " << min |
| 573 | << " minor, but got " << argMajor << " major x " << argMinor << " minor"; |
| 574 | return argMajor == maj && argMinor == min; |
| 575 | } |
| 576 | |
| 577 | MATCHER_P2(WithToolDimensions, maj, min, "InputEvent with specified tool dimensions") { |
| 578 | const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR); |
| 579 | const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR); |
| 580 | *result_listener << "expected tool dimensions " << maj << " major x " << min |
| 581 | << " minor, but got " << argMajor << " major x " << argMinor << " minor"; |
| 582 | return argMajor == maj && argMinor == min; |
| 583 | } |
| 584 | |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 585 | MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") { |
| 586 | const auto argToolType = arg.pointerProperties[0].toolType; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 587 | *result_listener << "expected tool type " << ftl::enum_string(toolType) << ", but got " |
| 588 | << ftl::enum_string(argToolType); |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 589 | return argToolType == toolType; |
| 590 | } |
| 591 | |
Harry Cutts | bb24e27 | 2023-03-21 10:49:47 +0000 | [diff] [blame] | 592 | MATCHER_P2(WithPointerToolType, pointer, toolType, |
| 593 | "InputEvent with specified tool type for pointer") { |
| 594 | const auto argToolType = arg.pointerProperties[pointer].toolType; |
| 595 | *result_listener << "expected pointer " << pointer << " to have tool type " |
| 596 | << ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType); |
| 597 | return argToolType == toolType; |
| 598 | } |
| 599 | |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 600 | MATCHER_P(WithMotionClassification, classification, |
| 601 | "InputEvent with specified MotionClassification") { |
| 602 | *result_listener << "expected classification " << motionClassificationToString(classification) |
| 603 | << ", but got " << motionClassificationToString(arg.classification); |
| 604 | return arg.classification == classification; |
| 605 | } |
| 606 | |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 607 | MATCHER_P(WithButtonState, buttons, "InputEvent with specified button state") { |
| 608 | *result_listener << "expected button state " << buttons << ", but got " << arg.buttonState; |
| 609 | return arg.buttonState == buttons; |
| 610 | } |
| 611 | |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 612 | MATCHER_P(WithActionButton, actionButton, "InputEvent with specified action button") { |
| 613 | *result_listener << "expected action button " << actionButton << ", but got " |
| 614 | << arg.actionButton; |
| 615 | return arg.actionButton == actionButton; |
| 616 | } |
| 617 | |
Prabir Pradhan | 2f37bcb | 2022-11-08 20:41:28 +0000 | [diff] [blame] | 618 | MATCHER_P(WithEventTime, eventTime, "InputEvent with specified eventTime") { |
| 619 | *result_listener << "expected event time " << eventTime << ", but got " << arg.eventTime; |
| 620 | return arg.eventTime == eventTime; |
| 621 | } |
| 622 | |
Harry Cutts | ef400b2 | 2022-12-16 21:26:24 +0000 | [diff] [blame] | 623 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 624 | *result_listener << "expected down time " << downTime << ", but got " << arg.downTime; |
| 625 | return arg.downTime == downTime; |
| 626 | } |
| 627 | |
Prabir Pradhan | a9df316 | 2022-12-05 23:57:27 +0000 | [diff] [blame] | 628 | MATCHER_P2(WithPrecision, xPrecision, yPrecision, "MotionEvent with specified precision") { |
| 629 | *result_listener << "expected x-precision " << xPrecision << " and y-precision " << yPrecision |
| 630 | << ", but got " << arg.xPrecision << " and " << arg.yPrecision; |
| 631 | return arg.xPrecision == xPrecision && arg.yPrecision == yPrecision; |
| 632 | } |
| 633 | |
Prabir Pradhan | 739dca4 | 2022-09-09 20:12:01 +0000 | [diff] [blame] | 634 | } // namespace android |