xref: /aosp_15_r20/hardware/libhardware/tests/input/evdev/SwitchInputMapper_test.cpp (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #include <memory>
18*e01b6f76SAndroid Build Coastguard Worker 
19*e01b6f76SAndroid Build Coastguard Worker #include <linux/input.h>
20*e01b6f76SAndroid Build Coastguard Worker 
21*e01b6f76SAndroid Build Coastguard Worker #include <gtest/gtest.h>
22*e01b6f76SAndroid Build Coastguard Worker 
23*e01b6f76SAndroid Build Coastguard Worker #include "InputMocks.h"
24*e01b6f76SAndroid Build Coastguard Worker #include "MockInputHost.h"
25*e01b6f76SAndroid Build Coastguard Worker #include "SwitchInputMapper.h"
26*e01b6f76SAndroid Build Coastguard Worker 
27*e01b6f76SAndroid Build Coastguard Worker using ::testing::_;
28*e01b6f76SAndroid Build Coastguard Worker using ::testing::Args;
29*e01b6f76SAndroid Build Coastguard Worker using ::testing::InSequence;
30*e01b6f76SAndroid Build Coastguard Worker using ::testing::Return;
31*e01b6f76SAndroid Build Coastguard Worker using ::testing::UnorderedElementsAre;
32*e01b6f76SAndroid Build Coastguard Worker 
33*e01b6f76SAndroid Build Coastguard Worker namespace android {
34*e01b6f76SAndroid Build Coastguard Worker namespace tests {
35*e01b6f76SAndroid Build Coastguard Worker 
36*e01b6f76SAndroid Build Coastguard Worker class SwitchInputMapperTest : public ::testing::Test {
37*e01b6f76SAndroid Build Coastguard Worker protected:
SetUp()38*e01b6f76SAndroid Build Coastguard Worker      virtual void SetUp() override {
39*e01b6f76SAndroid Build Coastguard Worker          mMapper = std::make_unique<SwitchInputMapper>();
40*e01b6f76SAndroid Build Coastguard Worker      }
41*e01b6f76SAndroid Build Coastguard Worker 
42*e01b6f76SAndroid Build Coastguard Worker      MockInputHost mHost;
43*e01b6f76SAndroid Build Coastguard Worker      std::unique_ptr<SwitchInputMapper> mMapper;
44*e01b6f76SAndroid Build Coastguard Worker };
45*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(SwitchInputMapperTest,testConfigureDevice)46*e01b6f76SAndroid Build Coastguard Worker TEST_F(SwitchInputMapperTest, testConfigureDevice) {
47*e01b6f76SAndroid Build Coastguard Worker     MockInputReportDefinition reportDef;
48*e01b6f76SAndroid Build Coastguard Worker     MockInputDeviceNode deviceNode;
49*e01b6f76SAndroid Build Coastguard Worker     deviceNode.addSwitch(SW_LID);
50*e01b6f76SAndroid Build Coastguard Worker     deviceNode.addSwitch(SW_CAMERA_LENS_COVER);
51*e01b6f76SAndroid Build Coastguard Worker 
52*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, addCollection(INPUT_COLLECTION_ID_SWITCH, 1));
53*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, declareUsages(INPUT_COLLECTION_ID_SWITCH, _, 2))
54*e01b6f76SAndroid Build Coastguard Worker         .With(Args<1,2>(UnorderedElementsAre(INPUT_USAGE_SWITCH_LID,
55*e01b6f76SAndroid Build Coastguard Worker                         INPUT_USAGE_SWITCH_CAMERA_LENS_COVER)));
56*e01b6f76SAndroid Build Coastguard Worker 
57*e01b6f76SAndroid Build Coastguard Worker     EXPECT_TRUE(mMapper->configureInputReport(&deviceNode, &reportDef));
58*e01b6f76SAndroid Build Coastguard Worker }
59*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(SwitchInputMapperTest,testConfigureDevice_noSwitches)60*e01b6f76SAndroid Build Coastguard Worker TEST_F(SwitchInputMapperTest, testConfigureDevice_noSwitches) {
61*e01b6f76SAndroid Build Coastguard Worker     MockInputReportDefinition reportDef;
62*e01b6f76SAndroid Build Coastguard Worker     MockInputDeviceNode deviceNode;
63*e01b6f76SAndroid Build Coastguard Worker 
64*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, addCollection(_, _)).Times(0);
65*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, declareUsages(_, _, _)).Times(0);
66*e01b6f76SAndroid Build Coastguard Worker 
67*e01b6f76SAndroid Build Coastguard Worker     EXPECT_FALSE(mMapper->configureInputReport(&deviceNode, &reportDef));
68*e01b6f76SAndroid Build Coastguard Worker }
69*e01b6f76SAndroid Build Coastguard Worker 
TEST_F(SwitchInputMapperTest,testProcessInput)70*e01b6f76SAndroid Build Coastguard Worker TEST_F(SwitchInputMapperTest, testProcessInput) {
71*e01b6f76SAndroid Build Coastguard Worker     MockInputReportDefinition reportDef;
72*e01b6f76SAndroid Build Coastguard Worker     MockInputDeviceNode deviceNode;
73*e01b6f76SAndroid Build Coastguard Worker     deviceNode.addSwitch(SW_LID);
74*e01b6f76SAndroid Build Coastguard Worker 
75*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, addCollection(_, _));
76*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, declareUsages(_, _, _));
77*e01b6f76SAndroid Build Coastguard Worker 
78*e01b6f76SAndroid Build Coastguard Worker     mMapper->configureInputReport(&deviceNode, &reportDef);
79*e01b6f76SAndroid Build Coastguard Worker 
80*e01b6f76SAndroid Build Coastguard Worker     MockInputReport report;
81*e01b6f76SAndroid Build Coastguard Worker     EXPECT_CALL(reportDef, allocateReport())
82*e01b6f76SAndroid Build Coastguard Worker         .WillOnce(Return(&report));
83*e01b6f76SAndroid Build Coastguard Worker 
84*e01b6f76SAndroid Build Coastguard Worker     {
85*e01b6f76SAndroid Build Coastguard Worker         // Test two switch events in order
86*e01b6f76SAndroid Build Coastguard Worker         InSequence s;
87*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(report, setBoolUsage(INPUT_COLLECTION_ID_SWITCH, INPUT_USAGE_SWITCH_LID, 1, 0));
88*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(report, reportEvent(_));
89*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(report, setBoolUsage(INPUT_COLLECTION_ID_SWITCH, INPUT_USAGE_SWITCH_LID, 0, 0));
90*e01b6f76SAndroid Build Coastguard Worker         EXPECT_CALL(report, reportEvent(_));
91*e01b6f76SAndroid Build Coastguard Worker     }
92*e01b6f76SAndroid Build Coastguard Worker 
93*e01b6f76SAndroid Build Coastguard Worker     InputEvent events[] = {
94*e01b6f76SAndroid Build Coastguard Worker         {0, EV_SW, SW_LID, 1},
95*e01b6f76SAndroid Build Coastguard Worker         {1, EV_SYN, SYN_REPORT, 0},
96*e01b6f76SAndroid Build Coastguard Worker         {2, EV_SW, SW_LID, 0},
97*e01b6f76SAndroid Build Coastguard Worker         {3, EV_SYN, SYN_REPORT, 0},
98*e01b6f76SAndroid Build Coastguard Worker     };
99*e01b6f76SAndroid Build Coastguard Worker     for (auto e : events) {
100*e01b6f76SAndroid Build Coastguard Worker         mMapper->process(e);
101*e01b6f76SAndroid Build Coastguard Worker     }
102*e01b6f76SAndroid Build Coastguard Worker }
103*e01b6f76SAndroid Build Coastguard Worker 
104*e01b6f76SAndroid Build Coastguard Worker }  // namespace tests
105*e01b6f76SAndroid Build Coastguard Worker }  // namespace android
106*e01b6f76SAndroid Build Coastguard Worker 
107