1 /*
2  * Copyright (C) 2019 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 "../InputClassifierConverter.h"
18 
19 #include <gtest/gtest.h>
20 #include <gui/constants.h>
21 #include <utils/BitSet.h>
22 
23 using namespace android::hardware::input;
24 
25 namespace android {
26 
27 // --- InputClassifierConverterTest ---
28 
generateBasicMotionArgs()29 static NotifyMotionArgs generateBasicMotionArgs() {
30     // Create a basic motion event for testing
31     PointerProperties properties;
32     properties.id = 0;
33     properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
34 
35     PointerCoords coords;
36     coords.clear();
37     coords.setAxisValue(AMOTION_EVENT_AXIS_X, 1);
38     coords.setAxisValue(AMOTION_EVENT_AXIS_Y, 2);
39     coords.setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.5);
40     static constexpr nsecs_t downTime = 2;
41     NotifyMotionArgs motionArgs(1 /*sequenceNum*/, downTime /*eventTime*/, 2 /*readTime*/,
42                                 3 /*deviceId*/, AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT,
43                                 4 /*policyFlags*/, AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/,
44                                 0 /*flags*/, AMETA_NONE, 0 /*buttonState*/,
45                                 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE,
46                                 1 /*pointerCount*/, &properties, &coords, 0 /*xPrecision*/,
47                                 0 /*yPrecision*/, AMOTION_EVENT_INVALID_CURSOR_POSITION,
48                                 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
49                                 {} /*videoFrames*/);
50     return motionArgs;
51 }
52 
getMotionEventAxis(common::V1_0::PointerCoords coords,common::V1_0::Axis axis)53 static float getMotionEventAxis(common::V1_0::PointerCoords coords,
54         common::V1_0::Axis axis) {
55     uint32_t index = BitSet64::getIndexOfBit(static_cast<uint64_t>(coords.bits),
56             static_cast<uint64_t>(axis));
57     return coords.values[index];
58 }
59 
60 /**
61  * Check that coordinates get converted properly from the framework's PointerCoords
62  * to the hidl PointerCoords in input::common.
63  */
TEST(InputClassifierConverterTest,PointerCoordsAxes)64 TEST(InputClassifierConverterTest, PointerCoordsAxes) {
65     const NotifyMotionArgs motionArgs = generateBasicMotionArgs();
66     ASSERT_EQ(1, motionArgs.pointerCoords[0].getX());
67     ASSERT_EQ(2, motionArgs.pointerCoords[0].getY());
68     ASSERT_EQ(0.5, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_SIZE));
69     ASSERT_EQ(3U, BitSet64::count(motionArgs.pointerCoords[0].bits));
70 
71     common::V1_0::MotionEvent motionEvent = notifyMotionArgsToHalMotionEvent(motionArgs);
72 
73     ASSERT_EQ(getMotionEventAxis(motionEvent.pointerCoords[0], common::V1_0::Axis::X),
74             motionArgs.pointerCoords[0].getX());
75     ASSERT_EQ(getMotionEventAxis(motionEvent.pointerCoords[0], common::V1_0::Axis::Y),
76             motionArgs.pointerCoords[0].getY());
77     ASSERT_EQ(getMotionEventAxis(motionEvent.pointerCoords[0], common::V1_0::Axis::SIZE),
78             motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_SIZE));
79     ASSERT_EQ(BitSet64::count(motionArgs.pointerCoords[0].bits),
80             BitSet64::count(motionEvent.pointerCoords[0].bits));
81 }
82 
83 } // namespace android
84