Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 021f7c64 authored by Harry Cutts's avatar Harry Cutts Committed by Android (Google) Code Review
Browse files

Merge changes I9f754223,I7d6eaa72 into main

* changes:
  SwitchInputMapperTest: migrate to InputMapperUnitTest
  Extract SwitchInputMapperTest to its own file
parents b03bafc7 aa931dfc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ cc_test {
        "PropertyProvider_test.cpp",
        "RotaryEncoderInputMapper_test.cpp",
        "SlopController_test.cpp",
        "SwitchInputMapper_test.cpp",
        "SyncQueue_test.cpp",
        "TimerProvider_test.cpp",
        "TestInputListener.cpp",
+7 −0
Original line number Diff line number Diff line
@@ -89,6 +89,13 @@ void InputMapperUnitTest::setKeyCodeState(KeyState state, std::set<int> keyCodes
    }
}

void InputMapperUnitTest::setSwitchState(int32_t state, std::set<int32_t> switchCodes) {
    for (const auto& switchCode : switchCodes) {
        EXPECT_CALL(mMockEventHub, getSwitchState(EVENTHUB_ID, switchCode))
                .WillRepeatedly(testing::Return(static_cast<int>(state)));
    }
}

std::list<NotifyArgs> InputMapperUnitTest::process(int32_t type, int32_t code, int32_t value) {
    nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
    return process(when, type, code, value);
+2 −0
Original line number Diff line number Diff line
@@ -58,6 +58,8 @@ protected:

    void setKeyCodeState(KeyState state, std::set<int> keyCodes);

    void setSwitchState(int32_t state, std::set<int32_t> switchCodes);

    std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value);
    std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value);

+0 −43
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@
#include <PeripheralController.h>
#include <SensorInputMapper.h>
#include <SingleTouchInputMapper.h>
#include <SwitchInputMapper.h>
#include <TestEventMatchers.h>
#include <TestInputListener.h>
#include <TouchInputMapper.h>
@@ -3063,48 +3062,6 @@ TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
    mapper.assertProcessWasCalled();
}

// --- SwitchInputMapperTest ---

class SwitchInputMapperTest : public InputMapperTest {
protected:
};

TEST_F(SwitchInputMapperTest, GetSources) {
    SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();

    ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
}

TEST_F(SwitchInputMapperTest, GetSwitchState) {
    SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();

    mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
    ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));

    mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
    ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
}

TEST_F(SwitchInputMapperTest, Process) {
    SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
    std::list<NotifyArgs> out;
    out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
    ASSERT_TRUE(out.empty());
    out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
    ASSERT_TRUE(out.empty());
    out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
    ASSERT_TRUE(out.empty());
    out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);

    ASSERT_EQ(1u, out.size());
    const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
    ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
    ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
            args.switchMask);
    ASSERT_EQ(uint32_t(0), args.policyFlags);
}

// --- VibratorInputMapperTest ---
class VibratorInputMapperTest : public InputMapperTest {
protected:
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SwitchInputMapper.h"

#include <list>
#include <variant>

#include <NotifyArgs.h>
#include <gtest/gtest.h>
#include <input/Input.h>
#include <linux/input-event-codes.h>

#include "InputMapperTest.h"
#include "TestConstants.h"

namespace android {

class SwitchInputMapperTest : public InputMapperUnitTest {
protected:
    void SetUp() override {
        InputMapperUnitTest::SetUp();
        createDevice();
        mMapper = createInputMapper<SwitchInputMapper>(*mDeviceContext,
                                                       mFakePolicy->getReaderConfiguration());
    }
};

TEST_F(SwitchInputMapperTest, GetSources) {
    ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mMapper->getSources());
}

TEST_F(SwitchInputMapperTest, GetSwitchState) {
    setSwitchState(1, {SW_LID});
    ASSERT_EQ(1, mMapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));

    setSwitchState(0, {SW_LID});
    ASSERT_EQ(0, mMapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
}

TEST_F(SwitchInputMapperTest, Process) {
    std::list<NotifyArgs> out;
    out = process(ARBITRARY_TIME, EV_SW, SW_LID, 1);
    ASSERT_TRUE(out.empty());
    out = process(ARBITRARY_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
    ASSERT_TRUE(out.empty());
    out = process(ARBITRARY_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
    ASSERT_TRUE(out.empty());
    out = process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);

    ASSERT_EQ(1u, out.size());
    const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
    ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
    ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
    ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
              args.switchMask);
    ASSERT_EQ(uint32_t(0), args.policyFlags);
}

} // namespace android
 No newline at end of file