xref: /aosp_15_r20/frameworks/native/services/vibratorservice/test/VibratorHalWrapperHidlV1_1Test.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_1Test"
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 
35 using aidl::android::hardware::vibrator::Effect;
36 using aidl::android::hardware::vibrator::EffectStrength;
37 
38 using namespace android;
39 using namespace std::chrono_literals;
40 using namespace testing;
41 
42 // -------------------------------------------------------------------------------------------------
43 
44 class MockIVibratorV1_1 : public V1_1::IVibrator {
45 public:
46     MOCK_METHOD(hardware::Return<V1_0::Status>, on, (uint32_t timeoutMs), (override));
47     MOCK_METHOD(hardware::Return<V1_0::Status>, off, (), (override));
48     MOCK_METHOD(hardware::Return<bool>, supportsAmplitudeControl, (), (override));
49     MOCK_METHOD(hardware::Return<V1_0::Status>, setAmplitude, (uint8_t amplitude), (override));
50     MOCK_METHOD(hardware::Return<void>, perform,
51                 (V1_0::Effect effect, V1_0::EffectStrength strength, perform_cb cb), (override));
52     MOCK_METHOD(hardware::Return<void>, perform_1_1,
53                 (V1_1::Effect_1_1 effect, V1_0::EffectStrength strength, perform_cb cb),
54                 (override));
55 };
56 
57 // -------------------------------------------------------------------------------------------------
58 
59 class VibratorHalWrapperHidlV1_1Test : public Test {
60 public:
SetUp()61     void SetUp() override {
62         mMockHal = new StrictMock<MockIVibratorV1_1>();
63         mMockScheduler = std::make_shared<StrictMock<vibrator::MockCallbackScheduler>>();
64         mWrapper = std::make_unique<vibrator::HidlHalWrapperV1_1>(mMockScheduler, mMockHal);
65         ASSERT_NE(mWrapper, nullptr);
66     }
67 
68 protected:
69     std::shared_ptr<StrictMock<vibrator::MockCallbackScheduler>> mMockScheduler = nullptr;
70     std::unique_ptr<vibrator::HalWrapper> mWrapper = nullptr;
71     sp<StrictMock<MockIVibratorV1_1>> mMockHal = nullptr;
72 };
73 
74 // -------------------------------------------------------------------------------------------------
75 
TEST_F(VibratorHalWrapperHidlV1_1Test,TestPerformEffectV1_0)76 TEST_F(VibratorHalWrapperHidlV1_1Test, TestPerformEffectV1_0) {
77     {
78         InSequence seq;
79         EXPECT_CALL(*mMockHal.get(),
80                     perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::LIGHT), _))
81                 .Times(Exactly(1))
82                 .WillRepeatedly(
83                         [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_1::perform_cb cb) {
84                             cb(V1_0::Status::OK, 10);
85                             return hardware::Return<void>();
86                         });
87         EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
88                 .Times(Exactly(1))
89                 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
90     }
91 
92     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
93     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
94     auto result = mWrapper->performEffect(Effect::CLICK, EffectStrength::LIGHT, callback);
95 
96     ASSERT_TRUE(result.isOk());
97     ASSERT_EQ(10ms, result.value());
98     ASSERT_EQ(1, *callbackCounter.get());
99 }
100 
TEST_F(VibratorHalWrapperHidlV1_1Test,TestPerformEffectV1_1)101 TEST_F(VibratorHalWrapperHidlV1_1Test, TestPerformEffectV1_1) {
102     {
103         InSequence seq;
104         EXPECT_CALL(*mMockHal.get(),
105                     perform_1_1(Eq(V1_1::Effect_1_1::TICK), Eq(V1_0::EffectStrength::LIGHT), _))
106                 .Times(Exactly(1))
107                 .WillRepeatedly([](V1_1::Effect_1_1, V1_0::EffectStrength,
108                                    MockIVibratorV1_1::perform_cb cb) {
109                     cb(V1_0::Status::OK, 10);
110                     return hardware::Return<void>();
111                 });
112         EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
113                 .Times(Exactly(1))
114                 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
115         EXPECT_CALL(*mMockHal.get(),
116                     perform_1_1(Eq(V1_1::Effect_1_1::TICK), Eq(V1_0::EffectStrength::MEDIUM), _))
117                 .Times(Exactly(1))
118                 .WillRepeatedly([](V1_1::Effect_1_1, V1_0::EffectStrength,
119                                    MockIVibratorV1_1::perform_cb cb) {
120                     cb(V1_0::Status::UNSUPPORTED_OPERATION, 0);
121                     return hardware::Return<void>();
122                 });
123         EXPECT_CALL(*mMockHal.get(),
124                     perform_1_1(Eq(V1_1::Effect_1_1::TICK), Eq(V1_0::EffectStrength::STRONG), _))
125                 .Times(Exactly(2))
126                 .WillOnce([](V1_1::Effect_1_1, V1_0::EffectStrength,
127                              MockIVibratorV1_1::perform_cb cb) {
128                     cb(V1_0::Status::BAD_VALUE, 0);
129                     return hardware::Return<void>();
130                 })
131                 .WillRepeatedly(
132                         [](V1_1::Effect_1_1, V1_0::EffectStrength, MockIVibratorV1_1::perform_cb) {
133                             return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
134                         });
135     }
136 
137     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
138     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
139 
140     auto result = mWrapper->performEffect(Effect::TICK, EffectStrength::LIGHT, callback);
141     ASSERT_TRUE(result.isOk());
142     ASSERT_EQ(10ms, result.value());
143     ASSERT_EQ(1, *callbackCounter.get());
144 
145     result = mWrapper->performEffect(Effect::TICK, EffectStrength::MEDIUM, callback);
146     ASSERT_TRUE(result.isUnsupported());
147 
148     result = mWrapper->performEffect(Effect::TICK, EffectStrength::STRONG, callback);
149     ASSERT_TRUE(result.isFailed());
150 
151     result = mWrapper->performEffect(Effect::TICK, EffectStrength::STRONG, callback);
152     ASSERT_TRUE(result.isFailed());
153 
154     // Callback not triggered for unsupported and on failure
155     ASSERT_EQ(1, *callbackCounter.get());
156 }
157 
TEST_F(VibratorHalWrapperHidlV1_1Test,TestPerformEffectUnsupported)158 TEST_F(VibratorHalWrapperHidlV1_1Test, TestPerformEffectUnsupported) {
159     std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
160     auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
161     // Using THUD that is only available in v1.2
162     auto result = mWrapper->performEffect(Effect::THUD, EffectStrength::LIGHT, callback);
163     ASSERT_TRUE(result.isUnsupported());
164     // No callback is triggered.
165     ASSERT_EQ(0, *callbackCounter.get());
166 }
167