1 /*
2  * Copyright (C) 2015 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 <memory>
18 
19 #include <linux/input.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include "InputMocks.h"
24 #include "MockInputHost.h"
25 #include "MouseInputMapper.h"
26 
27 using ::testing::_;
28 using ::testing::Args;
29 using ::testing::InSequence;
30 using ::testing::Return;
31 using ::testing::UnorderedElementsAre;
32 
33 namespace android {
34 namespace tests {
35 
36 class MouseInputMapperTest : public ::testing::Test {
37 protected:
SetUp()38      virtual void SetUp() override {
39          mMapper = std::make_unique<MouseInputMapper>();
40      }
41 
42      MockInputHost mHost;
43      std::unique_ptr<MouseInputMapper> mMapper;
44 };
45 
TEST_F(MouseInputMapperTest,testConfigureDevice)46 TEST_F(MouseInputMapperTest, testConfigureDevice) {
47     MockInputReportDefinition reportDef;
48     MockInputDeviceNode deviceNode;
49     deviceNode.addKeys(BTN_LEFT, BTN_RIGHT, BTN_MIDDLE);
50     deviceNode.addRelAxis(REL_X);
51     deviceNode.addRelAxis(REL_Y);
52 
53     const auto id = INPUT_COLLECTION_ID_MOUSE;
54     EXPECT_CALL(reportDef, addCollection(id, 1));
55     EXPECT_CALL(reportDef, declareUsage(id, INPUT_USAGE_AXIS_X, _, _, _));
56     EXPECT_CALL(reportDef, declareUsage(id, INPUT_USAGE_AXIS_Y, _, _, _));
57     EXPECT_CALL(reportDef, declareUsages(id, _, 3))
58         .With(Args<1,2>(UnorderedElementsAre(
59                         INPUT_USAGE_BUTTON_PRIMARY,
60                         INPUT_USAGE_BUTTON_SECONDARY,
61                         INPUT_USAGE_BUTTON_TERTIARY)));
62 
63     EXPECT_TRUE(mMapper->configureInputReport(&deviceNode, &reportDef));
64 }
65 
TEST_F(MouseInputMapperTest,testConfigureDevice_noXAxis)66 TEST_F(MouseInputMapperTest, testConfigureDevice_noXAxis) {
67     MockInputReportDefinition reportDef;
68     MockInputDeviceNode deviceNode;
69 
70     EXPECT_CALL(reportDef, addCollection(INPUT_COLLECTION_ID_MOUSE, 1));
71     EXPECT_CALL(reportDef, declareUsage(_, _, _, _, _)).Times(0);
72     EXPECT_CALL(reportDef, declareUsages(_, _, _)).Times(0);
73 
74     EXPECT_FALSE(mMapper->configureInputReport(&deviceNode, &reportDef));
75 }
76 
TEST_F(MouseInputMapperTest,testProcessInput)77 TEST_F(MouseInputMapperTest, testProcessInput) {
78     MockInputReportDefinition reportDef;
79     MockInputDeviceNode deviceNode;
80     deviceNode.addKeys(BTN_LEFT, BTN_RIGHT, BTN_MIDDLE);
81     deviceNode.addRelAxis(REL_X);
82     deviceNode.addRelAxis(REL_Y);
83 
84     EXPECT_CALL(reportDef, addCollection(_, _));
85     EXPECT_CALL(reportDef, declareUsage(_, _, _, _, _)).Times(2);
86     EXPECT_CALL(reportDef, declareUsages(_, _, 3));
87 
88     mMapper->configureInputReport(&deviceNode, &reportDef);
89 
90     MockInputReport report;
91     EXPECT_CALL(reportDef, allocateReport())
92         .WillOnce(Return(&report));
93 
94     {
95         // Test two switch events in order
96         InSequence s;
97         const auto id = INPUT_COLLECTION_ID_MOUSE;
98         EXPECT_CALL(report, setIntUsage(id, INPUT_USAGE_AXIS_X, 5, 0));
99         EXPECT_CALL(report, setIntUsage(id, INPUT_USAGE_AXIS_Y, -3, 0));
100         EXPECT_CALL(report, reportEvent(_));
101         EXPECT_CALL(report, setBoolUsage(id, INPUT_USAGE_BUTTON_PRIMARY, 1, 0));
102         EXPECT_CALL(report, reportEvent(_));
103         EXPECT_CALL(report, setBoolUsage(id, INPUT_USAGE_BUTTON_PRIMARY, 0, 0));
104         EXPECT_CALL(report, reportEvent(_));
105     }
106 
107     InputEvent events[] = {
108         {0, EV_REL, REL_X, 5},
109         {1, EV_REL, REL_Y, -3},
110         {2, EV_SYN, SYN_REPORT, 0},
111         {0, EV_KEY, BTN_LEFT, 1},
112         {1, EV_SYN, SYN_REPORT, 0},
113         {2, EV_KEY, BTN_LEFT, 0},
114         {3, EV_SYN, SYN_REPORT, 0},
115     };
116     for (auto e : events) {
117         mMapper->process(e);
118     }
119 }
120 
121 }  // namespace tests
122 }  // namespace android
123 
124