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 {
27         InputMapperUnitTest::SetUp();
28         createDevice();
29     }
30 
31     MultiTouchMotionAccumulator mMotionAccumulator;
32 
processMotionEvent(int32_t type,int32_t code,int32_t value)33     void processMotionEvent(int32_t type, int32_t code, int32_t value) {
34         RawEvent event;
35         event.when = ARBITRARY_TIME;
36         event.readTime = READ_TIME;
37         event.deviceId = EVENTHUB_ID;
38         event.type = type;
39         event.code = code;
40         event.value = value;
41         mMotionAccumulator.process(event);
42     }
43 };
44 
TEST_F(MultiTouchMotionAccumulatorTest,ActiveSlotCountUsingSlotsProtocol)45 TEST_F(MultiTouchMotionAccumulatorTest, ActiveSlotCountUsingSlotsProtocol) {
46     mMotionAccumulator.configure(*mDeviceContext, SLOT_COUNT, /*usingSlotsProtocol=*/true);
47     // We expect active slot count to match the touches being tracked
48     // first touch
49     processMotionEvent(EV_ABS, ABS_MT_SLOT, 0);
50     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, 123);
51     processMotionEvent(EV_SYN, SYN_REPORT, 0);
52     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
53 
54     // second touch
55     processMotionEvent(EV_ABS, ABS_MT_SLOT, 1);
56     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, 456);
57     processMotionEvent(EV_SYN, SYN_REPORT, 0);
58     ASSERT_EQ(2u, mMotionAccumulator.getActiveSlotsCount());
59 
60     // second lifted
61     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, -1);
62     processMotionEvent(EV_SYN, SYN_REPORT, 0);
63     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
64 
65     // first lifted
66     processMotionEvent(EV_ABS, ABS_MT_SLOT, 0);
67     processMotionEvent(EV_ABS, ABS_MT_TRACKING_ID, -1);
68     processMotionEvent(EV_SYN, SYN_REPORT, 0);
69     ASSERT_EQ(0u, mMotionAccumulator.getActiveSlotsCount());
70 }
71 
TEST_F(MultiTouchMotionAccumulatorTest,ActiveSlotCountNotUsingSlotsProtocol)72 TEST_F(MultiTouchMotionAccumulatorTest, ActiveSlotCountNotUsingSlotsProtocol) {
73     mMotionAccumulator.configure(*mDeviceContext, SLOT_COUNT, /*usingSlotsProtocol=*/false);
74 
75     // first touch
76     processMotionEvent(EV_ABS, ABS_MT_POSITION_X, 0);
77     processMotionEvent(EV_ABS, ABS_MT_POSITION_Y, 0);
78     processMotionEvent(EV_SYN, SYN_MT_REPORT, 0);
79     ASSERT_EQ(1u, mMotionAccumulator.getActiveSlotsCount());
80 
81     // second touch
82     processMotionEvent(EV_ABS, ABS_MT_POSITION_X, 50);
83     processMotionEvent(EV_ABS, ABS_MT_POSITION_Y, 50);
84     processMotionEvent(EV_SYN, SYN_MT_REPORT, 0);
85     ASSERT_EQ(2u, mMotionAccumulator.getActiveSlotsCount());
86 
87     // reset
88     mMotionAccumulator.finishSync();
89     ASSERT_EQ(0u, mMotionAccumulator.getActiveSlotsCount());
90 }
91 
92 } // namespace android
93