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 <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_utils.h"
30
31 namespace V1_0 = android::hardware::vibrator::V1_0;
32 namespace V1_1 = android::hardware::vibrator::V1_1;
33 namespace V1_2 = android::hardware::vibrator::V1_2;
34
35 using android::hardware::vibrator::Effect;
36 using 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_2 : public V1_2::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 MOCK_METHOD(hardware::Return<void>, perform_1_2,
56 (V1_2::Effect effect, V1_0::EffectStrength strength, perform_cb cb), (override));
57 };
58
59 // -------------------------------------------------------------------------------------------------
60
61 class VibratorHalWrapperHidlV1_2Test : public Test {
62 public:
SetUp()63 void SetUp() override {
64 mMockHal = new StrictMock<MockIVibratorV1_2>();
65 mMockScheduler = std::make_shared<StrictMock<vibrator::MockCallbackScheduler>>();
66 mWrapper = std::make_unique<vibrator::HidlHalWrapperV1_2>(mMockScheduler, mMockHal);
67 ASSERT_NE(mWrapper, nullptr);
68 }
69
70 protected:
71 std::shared_ptr<StrictMock<vibrator::MockCallbackScheduler>> mMockScheduler = nullptr;
72 std::unique_ptr<vibrator::HalWrapper> mWrapper = nullptr;
73 sp<StrictMock<MockIVibratorV1_2>> mMockHal = nullptr;
74 };
75
76 // -------------------------------------------------------------------------------------------------
77
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_0)78 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_0) {
79 {
80 InSequence seq;
81 EXPECT_CALL(*mMockHal.get(),
82 perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::LIGHT), _))
83 .Times(Exactly(1))
84 .WillRepeatedly(
85 [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
86 cb(V1_0::Status::OK, 10);
87 return hardware::Return<void>();
88 });
89 EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
90 .Times(Exactly(1))
91 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
92 }
93
94 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
95 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
96 auto result = mWrapper->performEffect(Effect::CLICK, EffectStrength::LIGHT, callback);
97
98 ASSERT_TRUE(result.isOk());
99 ASSERT_EQ(10ms, result.value());
100 ASSERT_EQ(1, *callbackCounter.get());
101 }
102
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_1)103 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_1) {
104 {
105 InSequence seq;
106 EXPECT_CALL(*mMockHal.get(),
107 perform_1_1(Eq(V1_1::Effect_1_1::TICK), Eq(V1_0::EffectStrength::LIGHT), _))
108 .Times(Exactly(1))
109 .WillRepeatedly([](V1_1::Effect_1_1, V1_0::EffectStrength,
110 MockIVibratorV1_2::perform_cb cb) {
111 cb(V1_0::Status::OK, 10);
112 return hardware::Return<void>();
113 });
114 EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
115 .Times(Exactly(1))
116 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
117 }
118
119 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
120 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
121 auto result = mWrapper->performEffect(Effect::TICK, EffectStrength::LIGHT, callback);
122
123 ASSERT_TRUE(result.isOk());
124 ASSERT_EQ(10ms, result.value());
125 ASSERT_EQ(1, *callbackCounter.get());
126 }
127
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectV1_2)128 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectV1_2) {
129 {
130 InSequence seq;
131 EXPECT_CALL(*mMockHal.get(),
132 perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::LIGHT), _))
133 .Times(Exactly(1))
134 .WillRepeatedly(
135 [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
136 cb(V1_0::Status::OK, 10);
137 return hardware::Return<void>();
138 });
139 EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
140 .Times(Exactly(1))
141 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
142 EXPECT_CALL(*mMockHal.get(),
143 perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::MEDIUM), _))
144 .Times(Exactly(1))
145 .WillRepeatedly(
146 [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
147 cb(V1_0::Status::UNSUPPORTED_OPERATION, 10);
148 return hardware::Return<void>();
149 });
150 EXPECT_CALL(*mMockHal.get(),
151 perform_1_2(Eq(V1_2::Effect::THUD), Eq(V1_0::EffectStrength::STRONG), _))
152 .Times(Exactly(2))
153 .WillOnce([](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb cb) {
154 cb(V1_0::Status::BAD_VALUE, 10);
155 return hardware::Return<void>();
156 })
157 .WillRepeatedly(
158 [](V1_2::Effect, V1_0::EffectStrength, MockIVibratorV1_2::perform_cb) {
159 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
160 });
161 }
162
163 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
164 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
165
166 auto result = mWrapper->performEffect(Effect::THUD, EffectStrength::LIGHT, callback);
167 ASSERT_TRUE(result.isOk());
168 ASSERT_EQ(10ms, result.value());
169 ASSERT_EQ(1, *callbackCounter.get());
170
171 result = mWrapper->performEffect(Effect::THUD, EffectStrength::MEDIUM, callback);
172 ASSERT_TRUE(result.isUnsupported());
173
174 result = mWrapper->performEffect(Effect::THUD, EffectStrength::STRONG, callback);
175 ASSERT_TRUE(result.isFailed());
176
177 result = mWrapper->performEffect(Effect::THUD, EffectStrength::STRONG, callback);
178 ASSERT_TRUE(result.isFailed());
179
180 // Callback not triggered for unsupported and on failure
181 ASSERT_EQ(1, *callbackCounter.get());
182 }
183
TEST_F(VibratorHalWrapperHidlV1_2Test,TestPerformEffectUnsupported)184 TEST_F(VibratorHalWrapperHidlV1_2Test, TestPerformEffectUnsupported) {
185 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
186 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
187 // Using TEXTURE_TICK that is only available in v1.3
188 auto result = mWrapper->performEffect(Effect::TEXTURE_TICK, EffectStrength::LIGHT, callback);
189 ASSERT_TRUE(result.isUnsupported());
190 // No callback is triggered.
191 ASSERT_EQ(0, *callbackCounter.get());
192 }
193