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 "SwitchInputMapper.h"
26
27using ::testing::_;
28using ::testing::Args;
29using ::testing::InSequence;
30using ::testing::Return;
31using ::testing::UnorderedElementsAre;
32
33namespace android {
34namespace tests {
35
36class SwitchInputMapperTest : public ::testing::Test {
37protected:
38     virtual void SetUp() override {
39         mMapper = std::make_unique<SwitchInputMapper>();
40     }
41
42     MockInputHost mHost;
43     std::unique_ptr<SwitchInputMapper> mMapper;
44};
45
46TEST_F(SwitchInputMapperTest, testConfigureDevice) {
47    MockInputReportDefinition reportDef;
48    MockInputDeviceNode deviceNode;
49    deviceNode.addSwitch(SW_LID);
50    deviceNode.addSwitch(SW_CAMERA_LENS_COVER);
51
52    EXPECT_CALL(reportDef, addCollection(INPUT_COLLECTION_ID_SWITCH, 1));
53    EXPECT_CALL(reportDef, declareUsages(INPUT_COLLECTION_ID_SWITCH, _, 2))
54        .With(Args<1,2>(UnorderedElementsAre(INPUT_USAGE_SWITCH_LID,
55                        INPUT_USAGE_SWITCH_CAMERA_LENS_COVER)));
56
57    EXPECT_TRUE(mMapper->configureInputReport(&deviceNode, &reportDef));
58}
59
60TEST_F(SwitchInputMapperTest, testConfigureDevice_noSwitches) {
61    MockInputReportDefinition reportDef;
62    MockInputDeviceNode deviceNode;
63
64    EXPECT_CALL(reportDef, addCollection(_, _)).Times(0);
65    EXPECT_CALL(reportDef, declareUsages(_, _, _)).Times(0);
66
67    EXPECT_FALSE(mMapper->configureInputReport(&deviceNode, &reportDef));
68}
69
70TEST_F(SwitchInputMapperTest, testProcessInput) {
71    MockInputReportDefinition reportDef;
72    MockInputDeviceNode deviceNode;
73    deviceNode.addSwitch(SW_LID);
74
75    EXPECT_CALL(reportDef, addCollection(_, _));
76    EXPECT_CALL(reportDef, declareUsages(_, _, _));
77
78    mMapper->configureInputReport(&deviceNode, &reportDef);
79
80    MockInputReport report;
81    EXPECT_CALL(reportDef, allocateReport())
82        .WillOnce(Return(&report));
83
84    {
85        // Test two switch events in order
86        InSequence s;
87        EXPECT_CALL(report, setBoolUsage(INPUT_COLLECTION_ID_SWITCH, INPUT_USAGE_SWITCH_LID, 1, 0));
88        EXPECT_CALL(report, reportEvent(_));
89        EXPECT_CALL(report, setBoolUsage(INPUT_COLLECTION_ID_SWITCH, INPUT_USAGE_SWITCH_LID, 0, 0));
90        EXPECT_CALL(report, reportEvent(_));
91    }
92
93    InputEvent events[] = {
94        {0, EV_SW, SW_LID, 1},
95        {1, EV_SYN, SYN_REPORT, 0},
96        {2, EV_SW, SW_LID, 0},
97        {3, EV_SYN, SYN_REPORT, 0},
98    };
99    for (auto e : events) {
100        mMapper->process(e);
101    }
102}
103
104}  // namespace tests
105}  // namespace android
106
107