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 "VibratorInputMapper.h"
18
19 #include <chrono>
20 #include <list>
21 #include <variant>
22 #include <vector>
23
24 #include <EventHub.h>
25 #include <NotifyArgs.h>
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 #include <input/Input.h>
29
30 #include "InputMapperTest.h"
31 #include "VibrationElement.h"
32
33 namespace android {
34
35 class VibratorInputMapperTest : public InputMapperUnitTest {
36 protected:
SetUp()37 void SetUp() override {
38 InputMapperUnitTest::SetUp();
39 EXPECT_CALL(mMockEventHub, getDeviceClasses(EVENTHUB_ID))
40 .WillRepeatedly(testing::Return(InputDeviceClass::VIBRATOR));
41 EXPECT_CALL(mMockEventHub, getVibratorIds(EVENTHUB_ID))
42 .WillRepeatedly(testing::Return<std::vector<int32_t>>({0, 1}));
43 mMapper = createInputMapper<VibratorInputMapper>(*mDeviceContext,
44 mFakePolicy->getReaderConfiguration());
45 }
46 };
47
TEST_F(VibratorInputMapperTest,GetSources)48 TEST_F(VibratorInputMapperTest, GetSources) {
49 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mMapper->getSources());
50 }
51
TEST_F(VibratorInputMapperTest,GetVibratorIds)52 TEST_F(VibratorInputMapperTest, GetVibratorIds) {
53 ASSERT_EQ(mMapper->getVibratorIds().size(), 2U);
54 }
55
TEST_F(VibratorInputMapperTest,Vibrate)56 TEST_F(VibratorInputMapperTest, Vibrate) {
57 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
58 constexpr int32_t VIBRATION_TOKEN = 100;
59
60 VibrationElement pattern(2);
61 VibrationSequence sequence(2);
62 pattern.duration = std::chrono::milliseconds(200);
63 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
64 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
65 sequence.addElement(pattern);
66 pattern.duration = std::chrono::milliseconds(500);
67 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
68 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
69 sequence.addElement(pattern);
70
71 std::vector<int64_t> timings = {0, 1};
72 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
73
74 ASSERT_FALSE(mMapper->isVibrating());
75 // Start vibrating
76 std::list<NotifyArgs> out = mMapper->vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
77 ASSERT_TRUE(mMapper->isVibrating());
78 // Verify vibrator state listener was notified.
79 ASSERT_EQ(1u, out.size());
80 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
81 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
82 ASSERT_TRUE(vibrateArgs.isOn);
83 // Stop vibrating
84 out = mMapper->cancelVibrate(VIBRATION_TOKEN);
85 ASSERT_FALSE(mMapper->isVibrating());
86 // Verify vibrator state listener was notified.
87 ASSERT_EQ(1u, out.size());
88 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
89 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
90 ASSERT_FALSE(cancelArgs.isOn);
91 }
92
93 } // namespace android