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