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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cmath>
#include <compare>
#include <ios>
#include <android-base/stringprintf.h>
#include <android/input.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <input/Input.h>
#include <input/PrintTools.h>
#include "NotifyArgs.h"
#include "TestConstants.h"
namespace android {
struct PointF {
float x;
float y;
auto operator<=>(const PointF&) const = default;
};
inline std::string pointFToString(const PointF& p) {
return std::string("(") + std::to_string(p.x) + ", " + std::to_string(p.y) + ")";
}
/// Source
class WithSourceMatcher {
public:
using is_gtest_matcher = void;
explicit WithSourceMatcher(uint32_t source) : mSource(source) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mSource == args.source;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mSource == args.source;
}
bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
return mSource == event.getSource();
}
void DescribeTo(std::ostream* os) const {
*os << "with source " << inputEventSourceToString(mSource);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong source"; }
private:
const uint32_t mSource;
};
inline WithSourceMatcher WithSource(uint32_t source) {
return WithSourceMatcher(source);
}
/// Key action
class WithKeyActionMatcher {
public:
using is_gtest_matcher = void;
explicit WithKeyActionMatcher(int32_t action) : mAction(action) {}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mAction == args.action;
}
bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
return mAction == event.getAction();
}
void DescribeTo(std::ostream* os) const {
*os << "with key action " << KeyEvent::actionToString(mAction);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
private:
const int32_t mAction;
};
inline WithKeyActionMatcher WithKeyAction(int32_t action) {
return WithKeyActionMatcher(action);
}
/// Motion action
class WithMotionActionMatcher {
public:
using is_gtest_matcher = void;
explicit WithMotionActionMatcher(int32_t action) : mAction(action) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
bool matches = mAction == args.action;
if (args.action == AMOTION_EVENT_ACTION_CANCEL) {
matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0;
}
return matches;
}
bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
bool matches = mAction == event.getAction();
if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) {
matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
}
return matches;
}
void DescribeTo(std::ostream* os) const {
*os << "with motion action " << MotionEvent::actionToString(mAction);
if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
*os << " and FLAG_CANCELED";
}
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }
private:
const int32_t mAction;
};
inline WithMotionActionMatcher WithMotionAction(int32_t action) {
return WithMotionActionMatcher(action);
}
/// Display Id
class WithDisplayIdMatcher {
public:
using is_gtest_matcher = void;
explicit WithDisplayIdMatcher(int32_t displayId) : mDisplayId(displayId) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mDisplayId == args.displayId;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mDisplayId == args.displayId;
}
bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
return mDisplayId == event.getDisplayId();
}
void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; }
void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; }
private:
const int32_t mDisplayId;
};
inline WithDisplayIdMatcher WithDisplayId(int32_t displayId) {
return WithDisplayIdMatcher(displayId);
}
/// Device Id
class WithDeviceIdMatcher {
public:
using is_gtest_matcher = void;
explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mDeviceId == args.deviceId;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mDeviceId == args.deviceId;
}
bool MatchAndExplain(const NotifyDeviceResetArgs& args, std::ostream*) const {
return mDeviceId == args.deviceId;
}
bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
return mDeviceId == event.getDeviceId();
}
void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; }
void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; }
private:
const int32_t mDeviceId;
};
inline WithDeviceIdMatcher WithDeviceId(int32_t deviceId) {
return WithDeviceIdMatcher(deviceId);
}
/// Flags
class WithFlagsMatcher {
public:
using is_gtest_matcher = void;
explicit WithFlagsMatcher(int32_t flags) : mFlags(flags) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mFlags == args.flags;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mFlags == args.flags;
}
bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
return mFlags == event.getFlags();
}
bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
return mFlags == event.getFlags();
}
void DescribeTo(std::ostream* os) const {
*os << "with flags " << base::StringPrintf("0x%x", mFlags);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong flags"; }
private:
const int32_t mFlags;
};
inline WithFlagsMatcher WithFlags(int32_t flags) {
return WithFlagsMatcher(flags);
}
/// DownTime
class WithDownTimeMatcher {
public:
using is_gtest_matcher = void;
explicit WithDownTimeMatcher(nsecs_t downTime) : mDownTime(downTime) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mDownTime == args.downTime;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mDownTime == args.downTime;
}
bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
return mDownTime == event.getDownTime();
}
bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
return mDownTime == event.getDownTime();
}
void DescribeTo(std::ostream* os) const { *os << "with down time " << mDownTime; }
void DescribeNegationTo(std::ostream* os) const { *os << "wrong down time"; }
private:
const nsecs_t mDownTime;
};
inline WithDownTimeMatcher WithDownTime(nsecs_t downTime) {
return WithDownTimeMatcher(downTime);
}
/// Coordinate matcher
class WithCoordsMatcher {
public:
using is_gtest_matcher = void;
explicit WithCoordsMatcher(size_t pointerIndex, float x, float y)
: mPointerIndex(pointerIndex), mX(x), mY(y) {}
bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
if (mPointerIndex >= event.getPointerCount()) {
*os << "Pointer index " << mPointerIndex << " is out of bounds";
return false;
}
bool matches = mX == event.getX(mPointerIndex) && mY == event.getY(mPointerIndex);
if (!matches) {
*os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex
<< ", but got (" << event.getX(mPointerIndex) << ", " << event.getY(mPointerIndex)
<< ")";
}
return matches;
}
bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
if (mPointerIndex >= event.pointerCoords.size()) {
*os << "Pointer index " << mPointerIndex << " is out of bounds";
return false;
}
bool matches = mX == event.pointerCoords[mPointerIndex].getX() &&
mY == event.pointerCoords[mPointerIndex].getY();
if (!matches) {
*os << "expected coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex
<< ", but got (" << event.pointerCoords[mPointerIndex].getX() << ", "
<< event.pointerCoords[mPointerIndex].getY() << ")";
}
return matches;
}
void DescribeTo(std::ostream* os) const {
*os << "with coords (" << mX << ", " << mY << ") at pointer index " << mPointerIndex;
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong coords"; }
private:
const size_t mPointerIndex;
const float mX;
const float mY;
};
inline WithCoordsMatcher WithCoords(float x, float y) {
return WithCoordsMatcher(0, x, y);
}
inline WithCoordsMatcher WithPointerCoords(size_t pointerIndex, float x, float y) {
return WithCoordsMatcher(pointerIndex, x, y);
}
/// Raw coordinate matcher
class WithRawCoordsMatcher {
public:
using is_gtest_matcher = void;
explicit WithRawCoordsMatcher(size_t pointerIndex, float rawX, float rawY)
: mPointerIndex(pointerIndex), mRawX(rawX), mRawY(rawY) {}
bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
if (mPointerIndex >= event.getPointerCount()) {
*os << "Pointer index " << mPointerIndex << " is out of bounds";
return false;
}
bool matches =
mRawX == event.getRawX(mPointerIndex) && mRawY == event.getRawY(mPointerIndex);
if (!matches) {
*os << "expected raw coords (" << mRawX << ", " << mRawY << ") at pointer index "
<< mPointerIndex << ", but got (" << event.getRawX(mPointerIndex) << ", "
<< event.getRawY(mPointerIndex) << ")";
}
return matches;
}
void DescribeTo(std::ostream* os) const {
*os << "with raw coords (" << mRawX << ", " << mRawY << ") at pointer index "
<< mPointerIndex;
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong raw coords"; }
private:
const size_t mPointerIndex;
const float mRawX;
const float mRawY;
};
inline WithRawCoordsMatcher WithRawCoords(float rawX, float rawY) {
return WithRawCoordsMatcher(0, rawX, rawY);
}
inline WithRawCoordsMatcher WithPointerRawCoords(size_t pointerIndex, float rawX, float rawY) {
return WithRawCoordsMatcher(pointerIndex, rawX, rawY);
}
/// Pointer count
class WithPointerCountMatcher {
public:
using is_gtest_matcher = void;
explicit WithPointerCountMatcher(size_t pointerCount) : mPointerCount(pointerCount) {}
bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
if (event.getPointerCount() != mPointerCount) {
*os << "expected pointer count " << mPointerCount << ", but got "
<< event.getPointerCount();
return false;
}
return true;
}
bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
if (event.pointerCoords.size() != mPointerCount) {
*os << "expected pointer count " << mPointerCount << ", but got "
<< event.pointerCoords.size();
return false;
}
return true;
}
void DescribeTo(std::ostream* os) const { *os << "with pointer count " << mPointerCount; }
void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer count"; }
private:
const size_t mPointerCount;
};
inline WithPointerCountMatcher WithPointerCount(size_t pointerCount) {
return WithPointerCountMatcher(pointerCount);
}
/// Pointers matcher
class WithPointersMatcher {
public:
using is_gtest_matcher = void;
explicit WithPointersMatcher(std::map<int32_t, PointF> pointers) : mPointers(pointers) {}
bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
std::map<int32_t, PointF> actualPointers;
for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) {
const int32_t pointerId = event.getPointerId(pointerIndex);
actualPointers[pointerId] = {event.getX(pointerIndex), event.getY(pointerIndex)};
}
if (mPointers != actualPointers) {
*os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString)
<< ", but got " << dumpMap(actualPointers, constToString, pointFToString);
return false;
}
return true;
}
bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
std::map<int32_t, PointF> actualPointers;
for (size_t pointerIndex = 0; pointerIndex < event.pointerCoords.size(); pointerIndex++) {
const int32_t pointerId = event.pointerProperties[pointerIndex].id;
actualPointers[pointerId] = {event.pointerCoords[pointerIndex].getX(),
event.pointerCoords[pointerIndex].getY()};
}
if (mPointers != actualPointers) {
*os << "expected pointers " << dumpMap(mPointers, constToString, pointFToString)
<< ", but got " << dumpMap(actualPointers, constToString, pointFToString);
return false;
}
return true;
}
void DescribeTo(std::ostream* os) const {
*os << "with pointers " << dumpMap(mPointers, constToString, pointFToString);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointers"; }
private:
const std::map<int32_t, PointF> mPointers;
};
inline WithPointersMatcher WithPointers(
const std::map<int32_t /*id*/, PointF /*coords*/>& pointers) {
return WithPointersMatcher(pointers);
}
/// Pointer ids matcher
class WithPointerIdsMatcher {
public:
using is_gtest_matcher = void;
explicit WithPointerIdsMatcher(std::set<int32_t> pointerIds) : mPointerIds(pointerIds) {}
bool MatchAndExplain(const MotionEvent& event, std::ostream* os) const {
std::set<int32_t> actualPointerIds;
for (size_t pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++) {
const PointerProperties* properties = event.getPointerProperties(pointerIndex);
actualPointerIds.insert(properties->id);
}
if (mPointerIds != actualPointerIds) {
*os << "expected pointer ids " << dumpSet(mPointerIds) << ", but got "
<< dumpSet(actualPointerIds);
return false;
}
return true;
}
bool MatchAndExplain(const NotifyMotionArgs& event, std::ostream* os) const {
std::set<int32_t> actualPointerIds;
for (const PointerProperties& properties : event.pointerProperties) {
actualPointerIds.insert(properties.id);
}
if (mPointerIds != actualPointerIds) {
*os << "expected pointer ids " << dumpSet(mPointerIds) << ", but got "
<< dumpSet(actualPointerIds);
return false;
}
return true;
}
void DescribeTo(std::ostream* os) const { *os << "with pointer ids " << dumpSet(mPointerIds); }
void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointer ids"; }
private:
const std::set<int32_t> mPointerIds;
};
inline WithPointerIdsMatcher WithPointerIds(const std::set<int32_t /*id*/>& pointerIds) {
return WithPointerIdsMatcher(pointerIds);
}
/// Key code
class WithKeyCodeMatcher {
public:
using is_gtest_matcher = void;
explicit WithKeyCodeMatcher(int32_t keyCode) : mKeyCode(keyCode) {}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mKeyCode == args.keyCode;
}
bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
return mKeyCode == event.getKeyCode();
}
void DescribeTo(std::ostream* os) const {
*os << "with key code " << KeyEvent::getLabel(mKeyCode);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong key code"; }
private:
const int32_t mKeyCode;
};
inline WithKeyCodeMatcher WithKeyCode(int32_t keyCode) {
return WithKeyCodeMatcher(keyCode);
}
/// EventId
class WithEventIdMatcher {
public:
using is_gtest_matcher = void;
explicit WithEventIdMatcher(int32_t eventId) : mEventId(eventId) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mEventId == args.id;
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mEventId == args.id;
}
bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
return mEventId == event.getId();
}
void DescribeTo(std::ostream* os) const { *os << "with eventId 0x" << std::hex << mEventId; }
void DescribeNegationTo(std::ostream* os) const {
*os << "with eventId not equal to 0x" << std::hex << mEventId;
}
private:
const int32_t mEventId;
};
inline WithEventIdMatcher WithEventId(int32_t eventId) {
return WithEventIdMatcher(eventId);
}
/// EventIdSource
class WithEventIdSourceMatcher {
public:
using is_gtest_matcher = void;
explicit WithEventIdSourceMatcher(IdGenerator::Source eventIdSource)
: mEventIdSource(eventIdSource) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return mEventIdSource == IdGenerator::getSource(args.id);
}
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
return mEventIdSource == IdGenerator::getSource(args.id);
}
bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
return mEventIdSource == IdGenerator::getSource(event.getId());
}
void DescribeTo(std::ostream* os) const {
*os << "with eventId from source 0x" << std::hex << ftl::to_underlying(mEventIdSource);
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong event from source"; }
private:
const IdGenerator::Source mEventIdSource;
};
inline WithEventIdSourceMatcher WithEventIdSource(IdGenerator::Source eventIdSource) {
return WithEventIdSourceMatcher(eventIdSource);
}
MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") {
return arg.getRepeatCount() == repeatCount;
}
class WithPointerIdMatcher {
public:
using is_gtest_matcher = void;
explicit WithPointerIdMatcher(size_t index, int32_t pointerId)
: mIndex(index), mPointerId(pointerId) {}
bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
return args.pointerProperties[mIndex].id == mPointerId;
}
bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
return event.getPointerId(mIndex) == mPointerId;
}
void DescribeTo(std::ostream* os) const {
*os << "with pointer[" << mIndex << "] id = " << mPointerId;
}
void DescribeNegationTo(std::ostream* os) const { *os << "wrong pointerId"; }
private:
const size_t mIndex;
const int32_t mPointerId;
};
inline WithPointerIdMatcher WithPointerId(size_t index, int32_t pointerId) {
return WithPointerIdMatcher(index, pointerId);
}
MATCHER_P2(WithCursorPosition, x, y, "InputEvent with specified cursor position") {
const auto argX = arg.xCursorPosition;
const auto argY = arg.yCursorPosition;
*result_listener << "expected cursor position (" << x << ", " << y << "), but got (" << argX
<< ", " << argY << ")";
return (isnan(x) ? isnan(argX) : x == argX) && (isnan(y) ? isnan(argY) : y == argY);
}
MATCHER_P2(WithRelativeMotion, x, y, "InputEvent with specified relative motion") {
const auto argX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
const auto argY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
*result_listener << "expected relative motion (" << x << ", " << y << "), but got (" << argX
<< ", " << argY << ")";
return argX == x && argY == y;
}
MATCHER_P3(WithGestureOffset, dx, dy, epsilon,
"InputEvent with specified touchpad gesture offset") {
const auto argGestureX = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET);
const auto argGestureY = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET);
const double xDiff = fabs(argGestureX - dx);
const double yDiff = fabs(argGestureY - dy);
*result_listener << "expected gesture offset (" << dx << ", " << dy << ") within " << epsilon
<< ", but got (" << argGestureX << ", " << argGestureY << ")";
return xDiff <= epsilon && yDiff <= epsilon;
}
MATCHER_P3(WithGestureScrollDistance, x, y, epsilon,
"InputEvent with specified touchpad gesture scroll distance") {
const auto argXDistance =
arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_X_DISTANCE);
const auto argYDistance =
arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SCROLL_Y_DISTANCE);
const double xDiff = fabs(argXDistance - x);
const double yDiff = fabs(argYDistance - y);
*result_listener << "expected gesture offset (" << x << ", " << y << ") within " << epsilon
<< ", but got (" << argXDistance << ", " << argYDistance << ")";
return xDiff <= epsilon && yDiff <= epsilon;
}
MATCHER_P2(WithGesturePinchScaleFactor, factor, epsilon,
"InputEvent with specified touchpad pinch gesture scale factor") {
const auto argScaleFactor =
arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_PINCH_SCALE_FACTOR);
*result_listener << "expected gesture scale factor " << factor << " within " << epsilon
<< " but got " << argScaleFactor;
return fabs(argScaleFactor - factor) <= epsilon;
}
MATCHER_P(WithGestureSwipeFingerCount, count,
"InputEvent with specified touchpad swipe finger count") {
const auto argFingerCount =
arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT);
*result_listener << "expected gesture swipe finger count " << count << " but got "
<< argFingerCount;
return fabs(argFingerCount - count) <= EPSILON;
}
MATCHER_P(WithPressure, pressure, "InputEvent with specified pressure") {
const auto argPressure = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE);
*result_listener << "expected pressure " << pressure << ", but got " << argPressure;
return argPressure == pressure;
}
MATCHER_P(WithSize, size, "MotionEvent with specified size") {
const auto argSize = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_SIZE);
*result_listener << "expected size " << size << ", but got " << argSize;
return argSize == size;
}
MATCHER_P(WithOrientation, orientation, "MotionEvent with specified orientation") {
const auto argOrientation = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
*result_listener << "expected orientation " << orientation << ", but got " << argOrientation;
return argOrientation == orientation;
}
MATCHER_P(WithDistance, distance, "MotionEvent with specified distance") {
const auto argDistance = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_DISTANCE);
*result_listener << "expected distance " << distance << ", but got " << argDistance;
return argDistance == distance;
}
MATCHER_P2(WithTouchDimensions, maj, min, "InputEvent with specified touch dimensions") {
const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR);
*result_listener << "expected touch dimensions " << maj << " major x " << min
<< " minor, but got " << argMajor << " major x " << argMinor << " minor";
return argMajor == maj && argMinor == min;
}
MATCHER_P2(WithToolDimensions, maj, min, "InputEvent with specified tool dimensions") {
const auto argMajor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR);
const auto argMinor = arg.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR);
*result_listener << "expected tool dimensions " << maj << " major x " << min
<< " minor, but got " << argMajor << " major x " << argMinor << " minor";
return argMajor == maj && argMinor == min;
}
MATCHER_P(WithToolType, toolType, "InputEvent with specified tool type") {
const auto argToolType = arg.pointerProperties[0].toolType;
*result_listener << "expected tool type " << ftl::enum_string(toolType) << ", but got "
<< ftl::enum_string(argToolType);
return argToolType == toolType;
}
MATCHER_P2(WithPointerToolType, pointer, toolType,
"InputEvent with specified tool type for pointer") {
const auto argToolType = arg.pointerProperties[pointer].toolType;
*result_listener << "expected pointer " << pointer << " to have tool type "
<< ftl::enum_string(toolType) << ", but got " << ftl::enum_string(argToolType);
return argToolType == toolType;
}
MATCHER_P(WithMotionClassification, classification,
"InputEvent with specified MotionClassification") {
*result_listener << "expected classification " << motionClassificationToString(classification)
<< ", but got " << motionClassificationToString(arg.classification);
return arg.classification == classification;
}
MATCHER_P(WithButtonState, buttons, "InputEvent with specified button state") {
*result_listener << "expected button state " << buttons << ", but got " << arg.buttonState;
return arg.buttonState == buttons;
}
MATCHER_P(WithMetaState, metaState, "InputEvent with specified meta state") {
*result_listener << "expected meta state 0x" << std::hex << metaState << ", but got 0x"
<< arg.metaState;
return arg.metaState == metaState;
}
MATCHER_P(WithActionButton, actionButton, "InputEvent with specified action button") {
*result_listener << "expected action button " << actionButton << ", but got "
<< arg.actionButton;
return arg.actionButton == actionButton;
}
MATCHER_P(WithEventTime, eventTime, "InputEvent with specified eventTime") {
*result_listener << "expected event time " << eventTime << ", but got " << arg.eventTime;
return arg.eventTime == eventTime;
}
MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
*result_listener << "expected down time " << downTime << ", but got " << arg.downTime;
return arg.downTime == downTime;
}
MATCHER_P2(WithPrecision, xPrecision, yPrecision, "MotionEvent with specified precision") {
*result_listener << "expected x-precision " << xPrecision << " and y-precision " << yPrecision
<< ", but got " << arg.xPrecision << " and " << arg.yPrecision;
return arg.xPrecision == xPrecision && arg.yPrecision == yPrecision;
}
MATCHER_P(WithPolicyFlags, policyFlags, "InputEvent with specified policy flags") {
*result_listener << "expected policy flags 0x" << std::hex << policyFlags << ", but got 0x"
<< arg.policyFlags;
return arg.policyFlags == static_cast<uint32_t>(policyFlags);
}
MATCHER_P(WithEdgeFlags, edgeFlags, "InputEvent with specified edge flags") {
*result_listener << "expected edge flags 0x" << std::hex << edgeFlags << ", but got 0x"
<< arg.edgeFlags;
return arg.edgeFlags == edgeFlags;
}
} // namespace android
|