xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/SwitchInputMapper_test.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2024 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 "SwitchInputMapper.h"
18 
19 #include <list>
20 #include <variant>
21 
22 #include <NotifyArgs.h>
23 #include <gtest/gtest.h>
24 #include <input/Input.h>
25 #include <linux/input-event-codes.h>
26 
27 #include "InputMapperTest.h"
28 #include "TestConstants.h"
29 
30 namespace android {
31 
32 class SwitchInputMapperTest : public InputMapperUnitTest {
33 protected:
SetUp()34     void SetUp() override {
35         InputMapperUnitTest::SetUp();
36         mMapper = createInputMapper<SwitchInputMapper>(*mDeviceContext,
37                                                        mFakePolicy->getReaderConfiguration());
38     }
39 };
40 
TEST_F(SwitchInputMapperTest,GetSources)41 TEST_F(SwitchInputMapperTest, GetSources) {
42     ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mMapper->getSources());
43 }
44 
TEST_F(SwitchInputMapperTest,GetSwitchState)45 TEST_F(SwitchInputMapperTest, GetSwitchState) {
46     setSwitchState(1, {SW_LID});
47     ASSERT_EQ(1, mMapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
48 
49     setSwitchState(0, {SW_LID});
50     ASSERT_EQ(0, mMapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
51 }
52 
TEST_F(SwitchInputMapperTest,Process)53 TEST_F(SwitchInputMapperTest, Process) {
54     std::list<NotifyArgs> out;
55     out = process(ARBITRARY_TIME, EV_SW, SW_LID, 1);
56     ASSERT_TRUE(out.empty());
57     out = process(ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
58     ASSERT_TRUE(out.empty());
59     out = process(ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
60     ASSERT_TRUE(out.empty());
61     out = process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
62 
63     ASSERT_EQ(1u, out.size());
64     const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
65     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
66     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
67     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
68               args.switchMask);
69     ASSERT_EQ(uint32_t(0), args.policyFlags);
70 }
71 
72 } // namespace android