xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/MultiTouchMotionAccumulator_test.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2023 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 "MultiTouchMotionAccumulator.h"
18 #include "InputMapperTest.h"
19 
20 namespace android {
21 
22 class MultiTouchMotionAccumulatorTest : public InputMapperUnitTest {
23 protected:
24     static constexpr size_t SLOT_COUNT = 8;
25 
SetUp()26     void SetUp() override { InputMapperUnitTest::SetUp(); }
27 
28     MultiTouchMotionAccumulator mMotionAccumulator;
29 
processMotionEvent(int32_t type,int32_t code,int32_t value)30     void processMotionEvent(int32_t type, int32_t code, int32_t value) {
31         RawEvent event;
32         event.when = ARBITRARY_TIME;
33         event.readTime = READ_TIME;
34         event.deviceId = EVENTHUB_ID;
35         event.type = type;
36         event.code = code;
37         event.value = value;
38         mMotionAccumulator.process(event);
39     }
40 };
41 
TEST_F(MultiTouchMotionAccumulatorTest,ActiveSlotCountUsingSlotsProtocol)42 TEST_F(MultiTouchMotionAccumulatorTest, ActiveSlotCountUsingSlotsProtocol) {
43     mMotionAccumulator.configure(*mDeviceContext, SLOT_COUNT, /*usingSlotsProtocol=*/true);
44     // We expect active slot count to match the touches being tracked
45     // first touch
46     processMotionEvent(EV_ABS, ABS_MT_SLOT, 0);
47     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, 123);
48     processMotionEvent(EV_SYN, SYN_REPORT, 0);
49     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
50 
51     // second touch
52     processMotionEvent(EV_ABS, ABS_MT_SLOT, 1);
53     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, 456);
54     processMotionEvent(EV_SYN, SYN_REPORT, 0);
55     ASSERT_EQ(2u, mMotionAccumulator.getActiveSlotsCount());
56 
57     // second lifted
58     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, -1);
59     processMotionEvent(EV_SYN, SYN_REPORT, 0);
60     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
61 
62     // first lifted
63     processMotionEvent(EV_ABS, ABS_MT_SLOT, 0);
64     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, -1);
65     processMotionEvent(EV_SYN, SYN_REPORT, 0);
66     ASSERT_EQ(0u, mMotionAccumulator.getActiveSlotsCount());
67 }
68 
TEST_F(MultiTouchMotionAccumulatorTest,ActiveSlotCountNotUsingSlotsProtocol)69 TEST_F(MultiTouchMotionAccumulatorTest, ActiveSlotCountNotUsingSlotsProtocol) {
70     mMotionAccumulator.configure(*mDeviceContext, SLOT_COUNT, /*usingSlotsProtocol=*/false);
71 
72     // first touch
73     processMotionEvent(EV_ABS, ABS_MT_POSITION_X, 0);
74     processMotionEvent(EV_ABS, ABS_MT_POSITION_Y, 0);
75     processMotionEvent(EV_SYN, SYN_MT_REPORT, 0);
76     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
77 
78     // second touch
79     processMotionEvent(EV_ABS, ABS_MT_POSITION_X, 50);
80     processMotionEvent(EV_ABS, ABS_MT_POSITION_Y, 50);
81     processMotionEvent(EV_SYN, SYN_MT_REPORT, 0);
82     ASSERT_EQ(2u, mMotionAccumulator.getActiveSlotsCount());
83 
84     // reset
85     mMotionAccumulator.finishSync();
86     ASSERT_EQ(0u, mMotionAccumulator.getActiveSlotsCount());
87 }
88 
89 } // namespace android
90