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