xref: /aosp_15_r20/frameworks/native/services/vibratorservice/test/VibratorHalWrapperHidlV1_2Test.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "VibratorHalWrapperHidlV1_2Test"
18 
19 #include <aidl/android/hardware/vibrator/IVibrator.h>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include <utils/Log.h>
25 
26 #include <vibratorservice/VibratorCallbackScheduler.h>
27 #include <vibratorservice/VibratorHalWrapper.h>
28 
29 #include "test_mocks.h"
30 #include "test_utils.h"
31 
32 namespace V1_0 = android::hardware::vibrator::V1_0;
33 namespace V1_1 = android::hardware::vibrator::V1_1;
34 namespace V1_2 = android::hardware::vibrator::V1_2;
35 
36 using aidl::android::hardware::vibrator::Effect;
37 using aidl::android::hardware::vibrator::EffectStrength;
38 
39 using namespace android;
40 using namespace std::chrono_literals;
41 using namespace testing;
42 
43 // -------------------------------------------------------------------------------------------------
44 
45 class MockIVibratorV1_2 : public V1_2::IVibrator {
46 public:
47     MOCK_METHOD(hardware::Return<V1_0::Status>, on, (uint32_t timeoutMs), (override));
48     MOCK_METHOD(hardware::Return<V1_0::Status>, off, (), (override));
49     MOCK_METHOD(hardware::Return<bool>, supportsAmplitudeControl, (), (override));
50     MOCK_METHOD(hardware::Return<V1_0::Status>, setAmplitude, (uint8_t amplitude), (override));
51     MOCK_METHOD(hardware::Return<void>, perform,
52                 (V1_0::Effect effect, V1_0::EffectStrength strength, perform_cb cb), (override));
53     MOCK_METHOD(hardware::Return<void>, perform_1_1,
54                 (V1_1::Effect_1_1 effect, V1_0::EffectStrength strength, perform_cb cb),
55                 (override));
56     MOCK_METHOD(hardware::Return<void>, perform_1_2,
57                 (V1_2::Effect effect, V1_0::EffectStrength strength, perform_cb cb), (override));
58 };
59 
60 // -------------------------------------------------------------------------------------------------
61 
62 class VibratorHalWrapperHidlV1_2Test : public Test {
63 public:
SetUp()64     void SetUp() override {
65         mMockHal = new StrictMock<MockIVibratorV1_2>();
66         mMockScheduler = std::make_shared<StrictMock<vibrator::MockCallbackScheduler>>();
67         mWrapper = std::make_unique<vibrator::HidlHalWrapperV1_2>(mMockScheduler, mMockHal);
68         ASSERT_NE(mWrapper, nullptr);
69     }
70 
71 protected:
72     std::shared_ptr<StrictMock<vibrator::MockCallbackScheduler>> mMockScheduler = nullptr;
73     std::unique_ptr<vibrator::HalWrapper> mWrapper = nullptr;
74     sp<StrictMock<MockIVibratorV1_2>> mMockHal = nullptr;
75 };
76 
77 // -------------------------------------------------------------------------------------------------
78 
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_0)79 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_0) {
80     {
81         InSequence seq;
82         EXPECT_CALL(*mMockHal.get(),
83                     perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::LIGHT), _))
84                 .Times(Exactly(1))
85                 .WillRepeatedly(
86                         [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
87                             cb(V1_0::Status::OK, 10);
88                             return hardware::Return<void>();
89                         });
90         EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
91                 .Times(Exactly(1))
92                 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
93     }
94 
95     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
96     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
97     auto result = mWrapper->performEffect(Effect::CLICK, EffectStrength::LIGHT, callback);
98 
99     ASSERT_TRUE(result.isOk());
100     ASSERT_EQ(10ms, result.value());
101     ASSERT_EQ(1, *callbackCounter.get());
102 }
103 
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_1)104 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_1) {
105     {
106         InSequence seq;
107         EXPECT_CALL(*mMockHal.get(),
108                     perform_1_1(Eq(V1_1::Effect_1_1::TICK), Eq(V1_0::EffectStrength::LIGHT), _))
109                 .Times(Exactly(1))
110                 .WillRepeatedly([](V1_1::Effect_1_1, V1_0::EffectStrength,
111                                    MockIVibratorV1_2::perform_cb cb) {
112                     cb(V1_0::Status::OK, 10);
113                     return hardware::Return<void>();
114                 });
115         EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
116                 .Times(Exactly(1))
117                 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
118     }
119 
120     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
121     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
122     auto result = mWrapper->performEffect(Effect::TICK, EffectStrength::LIGHT, callback);
123 
124     ASSERT_TRUE(result.isOk());
125     ASSERT_EQ(10ms, result.value());
126     ASSERT_EQ(1, *callbackCounter.get());
127 }
128 
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_2)129 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_2) {
130     {
131         InSequence seq;
132         EXPECT_CALL(*mMockHal.get(),
133                     perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::LIGHT), _))
134                 .Times(Exactly(1))
135                 .WillRepeatedly(
136                         [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
137                             cb(V1_0::Status::OK, 10);
138                             return hardware::Return<void>();
139                         });
140         EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
141                 .Times(Exactly(1))
142                 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
143         EXPECT_CALL(*mMockHal.get(),
144                     perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::MEDIUM), _))
145                 .Times(Exactly(1))
146                 .WillRepeatedly(
147                         [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
148                             cb(V1_0::Status::UNSUPPORTED_OPERATION, 10);
149                             return hardware::Return<void>();
150                         });
151         EXPECT_CALL(*mMockHal.get(),
152                     perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::STRONG), _))
153                 .Times(Exactly(2))
154                 .WillOnce([](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
155                     cb(V1_0::Status::BAD_VALUE, 10);
156                     return hardware::Return<void>();
157                 })
158                 .WillRepeatedly(
159                         [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb) {
160                             return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
161                         });
162     }
163 
164     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
165     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
166 
167     auto result = mWrapper->performEffect(Effect::THUD, EffectStrength::LIGHT, callback);
168     ASSERT_TRUE(result.isOk());
169     ASSERT_EQ(10ms, result.value());
170     ASSERT_EQ(1, *callbackCounter.get());
171 
172     result = mWrapper->performEffect(Effect::THUD, EffectStrength::MEDIUM, callback);
173     ASSERT_TRUE(result.isUnsupported());
174 
175     result = mWrapper->performEffect(Effect::THUD, EffectStrength::STRONG, callback);
176     ASSERT_TRUE(result.isFailed());
177 
178     result = mWrapper->performEffect(Effect::THUD, EffectStrength::STRONG, callback);
179     ASSERT_TRUE(result.isFailed());
180 
181     // Callback not triggered for unsupported and on failure
182     ASSERT_EQ(1, *callbackCounter.get());
183 }
184 
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectUnsupported)185 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectUnsupported) {
186     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
187     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
188     // Using TEXTURE_TICK that is only available in v1.3
189     auto result = mWrapper->performEffect(Effect::TEXTURE_TICK, EffectStrength::LIGHT, callback);
190     ASSERT_TRUE(result.isUnsupported());
191     // No callback is triggered.
192     ASSERT_EQ(0, *callbackCounter.get());
193 }
194