| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 |  | 
|  | 17 | #include <gtest/gtest.h> | 
|  | 18 | #include <input/Input.h> | 
|  | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
|  | 22 | static KeyEvent getKeyEventWithFlags(int32_t flags) { | 
|  | 23 | KeyEvent event; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 24 | event.initialize(InputEvent::nextId(), 2 /*deviceId*/, AINPUT_SOURCE_GAMEPAD, | 
|  | 25 | ADISPLAY_ID_DEFAULT, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, flags, | 
|  | 26 | AKEYCODE_BUTTON_X, 121 /*scanCode*/, AMETA_ALT_ON, 1 /*repeatCount*/, | 
|  | 27 | 1000 /*downTime*/, 2000 /*eventTime*/); | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 28 | return event; | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | static MotionEvent getMotionEventWithFlags(int32_t flags) { | 
|  | 32 | MotionEvent event; | 
|  | 33 | constexpr size_t pointerCount = 1; | 
|  | 34 | PointerProperties pointerProperties[pointerCount]; | 
|  | 35 | PointerCoords pointerCoords[pointerCount]; | 
|  | 36 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 37 | pointerProperties[i].clear(); | 
|  | 38 | pointerProperties[i].id = i; | 
|  | 39 | pointerCoords[i].clear(); | 
|  | 40 | } | 
|  | 41 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 42 | event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_MOUSE, ADISPLAY_ID_DEFAULT, | 
|  | 43 | INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/, flags, | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 44 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/, | 
|  | 45 | MotionClassification::NONE, 2 /*xScale*/, 3 /*yScale*/, 4 /*xOffset*/, | 
|  | 46 | 5 /*yOffset*/, 0.1 /*xPrecision*/, 0.2 /*yPrecision*/, 280 /*xCursorPosition*/, | 
|  | 47 | 540 /*yCursorPosition*/, 100 /*downTime*/, 200 /*eventTime*/, pointerCount, | 
|  | 48 | pointerProperties, pointerCoords); | 
|  | 49 | return event; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | TEST(VerifiedKeyEventTest, ConvertKeyEventToVerifiedKeyEvent) { | 
|  | 53 | KeyEvent event = getKeyEventWithFlags(0); | 
|  | 54 | VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event); | 
|  | 55 |  | 
|  | 56 | ASSERT_EQ(VerifiedInputEvent::Type::KEY, verified.type); | 
|  | 57 |  | 
|  | 58 | ASSERT_EQ(event.getDeviceId(), verified.deviceId); | 
|  | 59 | ASSERT_EQ(event.getEventTime(), verified.eventTimeNanos); | 
|  | 60 | ASSERT_EQ(event.getSource(), verified.source); | 
|  | 61 | ASSERT_EQ(event.getDisplayId(), verified.displayId); | 
|  | 62 |  | 
|  | 63 | ASSERT_EQ(event.getAction(), verified.action); | 
|  | 64 | ASSERT_EQ(event.getDownTime(), verified.downTimeNanos); | 
|  | 65 | ASSERT_EQ(event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, verified.flags); | 
|  | 66 | ASSERT_EQ(event.getKeyCode(), verified.keyCode); | 
|  | 67 | ASSERT_EQ(event.getScanCode(), verified.scanCode); | 
|  | 68 | ASSERT_EQ(event.getMetaState(), verified.metaState); | 
|  | 69 | ASSERT_EQ(event.getRepeatCount(), verified.repeatCount); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | TEST(VerifiedKeyEventTest, VerifiedKeyEventContainsOnlyVerifiedFlags) { | 
|  | 73 | KeyEvent event = getKeyEventWithFlags(AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_FALLBACK); | 
|  | 74 | VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event); | 
|  | 75 | ASSERT_EQ(AKEY_EVENT_FLAG_CANCELED, verified.flags); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | TEST(VerifiedKeyEventTest, VerifiedKeyEventDoesNotContainUnverifiedFlags) { | 
|  | 79 | KeyEvent event = getKeyEventWithFlags(AKEY_EVENT_FLAG_EDITOR_ACTION); | 
|  | 80 | VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event); | 
|  | 81 | ASSERT_EQ(0, verified.flags); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | TEST(VerifiedMotionEventTest, ConvertMotionEventToVerifiedMotionEvent) { | 
|  | 85 | MotionEvent event = getMotionEventWithFlags(0); | 
|  | 86 | VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event); | 
|  | 87 |  | 
|  | 88 | ASSERT_EQ(VerifiedInputEvent::Type::MOTION, verified.type); | 
|  | 89 |  | 
|  | 90 | ASSERT_EQ(event.getDeviceId(), verified.deviceId); | 
|  | 91 | ASSERT_EQ(event.getEventTime(), verified.eventTimeNanos); | 
|  | 92 | ASSERT_EQ(event.getSource(), verified.source); | 
|  | 93 | ASSERT_EQ(event.getDisplayId(), verified.displayId); | 
|  | 94 |  | 
|  | 95 | ASSERT_EQ(event.getRawX(0), verified.rawX); | 
|  | 96 | ASSERT_EQ(event.getRawY(0), verified.rawY); | 
|  | 97 | ASSERT_EQ(event.getAction(), verified.actionMasked); | 
|  | 98 | ASSERT_EQ(event.getDownTime(), verified.downTimeNanos); | 
|  | 99 | ASSERT_EQ(event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, verified.flags); | 
|  | 100 | ASSERT_EQ(event.getMetaState(), verified.metaState); | 
|  | 101 | ASSERT_EQ(event.getButtonState(), verified.buttonState); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | TEST(VerifiedMotionEventTest, VerifiedMotionEventContainsOnlyVerifiedFlags) { | 
|  | 105 | MotionEvent event = getMotionEventWithFlags(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | | 
|  | 106 | AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE); | 
|  | 107 | VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event); | 
|  | 108 | ASSERT_EQ(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED, verified.flags); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | TEST(VerifiedMotionEventTest, VerifiedMotionEventDoesNotContainUnverifiedFlags) { | 
|  | 112 | MotionEvent event = getMotionEventWithFlags(AMOTION_EVENT_FLAG_TAINTED); | 
|  | 113 | VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event); | 
|  | 114 | ASSERT_EQ(0, verified.flags); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | } // namespace android |