• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "VibratorManagerHalControllerTest"
18 
19 #include <cutils/atomic.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include <utils/Log.h>
24 
25 #include <vibratorservice/VibratorManagerHalController.h>
26 
27 #include "test_utils.h"
28 
29 using android::vibrator::HalController;
30 
31 using namespace android;
32 using namespace testing;
33 
34 static constexpr int MAX_ATTEMPTS = 2;
35 static const std::vector<int32_t> VIBRATOR_IDS = {1, 2};
36 static constexpr int VIBRATOR_ID = 1;
37 
38 class MockManagerHalWrapper : public vibrator::ManagerHalWrapper {
39 public:
40     MOCK_METHOD(void, tryReconnect, (), (override));
41     MOCK_METHOD(vibrator::HalResult<void>, ping, (), (override));
42     MOCK_METHOD(vibrator::HalResult<vibrator::ManagerCapabilities>, getCapabilities, (),
43                 (override));
44     MOCK_METHOD(vibrator::HalResult<std::vector<int32_t>>, getVibratorIds, (), (override));
45     MOCK_METHOD(vibrator::HalResult<std::shared_ptr<HalController>>, getVibrator, (int32_t id),
46                 (override));
47     MOCK_METHOD(vibrator::HalResult<void>, prepareSynced, (const std::vector<int32_t>& ids),
48                 (override));
49     MOCK_METHOD(vibrator::HalResult<void>, triggerSynced,
50                 (const std::function<void()>& completionCallback), (override));
51     MOCK_METHOD(vibrator::HalResult<void>, cancelSynced, (), (override));
52 };
53 
54 class VibratorManagerHalControllerTest : public Test {
55 public:
SetUp()56     void SetUp() override {
57         mConnectCounter = 0;
58         auto callbackScheduler = std::make_shared<vibrator::CallbackScheduler>();
59         mMockHal = std::make_shared<StrictMock<MockManagerHalWrapper>>();
60         auto connector = [this](std::shared_ptr<vibrator::CallbackScheduler>) {
61             android_atomic_inc(&mConnectCounter);
62             return mMockHal;
63         };
64         mController = std::make_unique<vibrator::ManagerHalController>(std::move(callbackScheduler),
65                                                                        connector);
66         ASSERT_NE(mController, nullptr);
67     }
68 
69 protected:
70     int32_t mConnectCounter;
71     std::shared_ptr<MockManagerHalWrapper> mMockHal;
72     std::unique_ptr<vibrator::ManagerHalController> mController;
73 
setHalExpectations(int32_t cardinality,vibrator::HalResult<void> voidResult,vibrator::HalResult<vibrator::ManagerCapabilities> capabilitiesResult,vibrator::HalResult<std::vector<int32_t>> idsResult,vibrator::HalResult<std::shared_ptr<HalController>> vibratorResult)74     void setHalExpectations(int32_t cardinality, vibrator::HalResult<void> voidResult,
75                             vibrator::HalResult<vibrator::ManagerCapabilities> capabilitiesResult,
76                             vibrator::HalResult<std::vector<int32_t>> idsResult,
77                             vibrator::HalResult<std::shared_ptr<HalController>> vibratorResult) {
78         EXPECT_CALL(*mMockHal.get(), ping())
79                 .Times(Exactly(cardinality))
80                 .WillRepeatedly(Return(voidResult));
81         EXPECT_CALL(*mMockHal.get(), getCapabilities())
82                 .Times(Exactly(cardinality))
83                 .WillRepeatedly(Return(capabilitiesResult));
84         EXPECT_CALL(*mMockHal.get(), getVibratorIds())
85                 .Times(Exactly(cardinality))
86                 .WillRepeatedly(Return(idsResult));
87         EXPECT_CALL(*mMockHal.get(), getVibrator(_))
88                 .Times(Exactly(cardinality))
89                 .WillRepeatedly(Return(vibratorResult));
90         EXPECT_CALL(*mMockHal.get(), prepareSynced(_))
91                 .Times(Exactly(cardinality))
92                 .WillRepeatedly(Return(voidResult));
93         EXPECT_CALL(*mMockHal.get(), triggerSynced(_))
94                 .Times(Exactly(cardinality))
95                 .WillRepeatedly(Return(voidResult));
96         EXPECT_CALL(*mMockHal.get(), cancelSynced())
97                 .Times(Exactly(cardinality))
98                 .WillRepeatedly(Return(voidResult));
99 
100         if (cardinality > 1) {
101             // One reconnection call after each failure.
102             EXPECT_CALL(*mMockHal.get(), tryReconnect()).Times(Exactly(7 * cardinality));
103         }
104     }
105 };
106 
TEST_F(VibratorManagerHalControllerTest,TestInit)107 TEST_F(VibratorManagerHalControllerTest, TestInit) {
108     mController->init();
109     ASSERT_EQ(1, mConnectCounter);
110 
111     // Noop when wrapper was already initialized.
112     mController->init();
113     ASSERT_EQ(1, mConnectCounter);
114 }
115 
TEST_F(VibratorManagerHalControllerTest,TestApiCallsAreForwardedToHal)116 TEST_F(VibratorManagerHalControllerTest, TestApiCallsAreForwardedToHal) {
117     setHalExpectations(/* cardinality= */ 1, vibrator::HalResult<void>::ok(),
118                        vibrator::HalResult<vibrator::ManagerCapabilities>::ok(
119                                vibrator::ManagerCapabilities::SYNC),
120                        vibrator::HalResult<std::vector<int32_t>>::ok(VIBRATOR_IDS),
121                        vibrator::HalResult<std::shared_ptr<HalController>>::ok(nullptr));
122 
123     ASSERT_TRUE(mController->ping().isOk());
124 
125     auto getCapabilitiesResult = mController->getCapabilities();
126     ASSERT_TRUE(getCapabilitiesResult.isOk());
127     ASSERT_EQ(vibrator::ManagerCapabilities::SYNC, getCapabilitiesResult.value());
128 
129     auto getVibratorIdsResult = mController->getVibratorIds();
130     ASSERT_TRUE(getVibratorIdsResult.isOk());
131     ASSERT_EQ(VIBRATOR_IDS, getVibratorIdsResult.value());
132 
133     auto getVibratorResult = mController->getVibrator(VIBRATOR_ID);
134     ASSERT_TRUE(getVibratorResult.isOk());
135     ASSERT_EQ(nullptr, getVibratorResult.value());
136 
137     ASSERT_TRUE(mController->prepareSynced(VIBRATOR_IDS).isOk());
138     ASSERT_TRUE(mController->triggerSynced([]() {}).isOk());
139     ASSERT_TRUE(mController->cancelSynced().isOk());
140 
141     ASSERT_EQ(1, mConnectCounter);
142 }
143 
TEST_F(VibratorManagerHalControllerTest,TestUnsupportedApiResultDoNotResetHalConnection)144 TEST_F(VibratorManagerHalControllerTest, TestUnsupportedApiResultDoNotResetHalConnection) {
145     setHalExpectations(/* cardinality= */ 1, vibrator::HalResult<void>::unsupported(),
146                        vibrator::HalResult<vibrator::ManagerCapabilities>::unsupported(),
147                        vibrator::HalResult<std::vector<int32_t>>::unsupported(),
148                        vibrator::HalResult<std::shared_ptr<HalController>>::unsupported());
149 
150     ASSERT_EQ(0, mConnectCounter);
151 
152     ASSERT_TRUE(mController->ping().isUnsupported());
153     ASSERT_TRUE(mController->getCapabilities().isUnsupported());
154     ASSERT_TRUE(mController->getVibratorIds().isUnsupported());
155     ASSERT_TRUE(mController->getVibrator(VIBRATOR_ID).isUnsupported());
156     ASSERT_TRUE(mController->prepareSynced(VIBRATOR_IDS).isUnsupported());
157     ASSERT_TRUE(mController->triggerSynced([]() {}).isUnsupported());
158     ASSERT_TRUE(mController->cancelSynced().isUnsupported());
159 
160     ASSERT_EQ(1, mConnectCounter);
161 }
162 
TEST_F(VibratorManagerHalControllerTest,TestFailedApiResultResetsHalConnection)163 TEST_F(VibratorManagerHalControllerTest, TestFailedApiResultResetsHalConnection) {
164     setHalExpectations(MAX_ATTEMPTS, vibrator::HalResult<void>::failed("message"),
165                        vibrator::HalResult<vibrator::ManagerCapabilities>::failed("message"),
166                        vibrator::HalResult<std::vector<int32_t>>::failed("message"),
167                        vibrator::HalResult<std::shared_ptr<HalController>>::failed("message"));
168 
169     ASSERT_EQ(0, mConnectCounter);
170 
171     ASSERT_TRUE(mController->ping().isFailed());
172     ASSERT_TRUE(mController->getCapabilities().isFailed());
173     ASSERT_TRUE(mController->getVibratorIds().isFailed());
174     ASSERT_TRUE(mController->getVibrator(VIBRATOR_ID).isFailed());
175     ASSERT_TRUE(mController->prepareSynced(VIBRATOR_IDS).isFailed());
176     ASSERT_TRUE(mController->triggerSynced([]() {}).isFailed());
177     ASSERT_TRUE(mController->cancelSynced().isFailed());
178 
179     ASSERT_EQ(1, mConnectCounter);
180 }
181 
TEST_F(VibratorManagerHalControllerTest,TestFailedApiResultReturnsSuccessAfterRetries)182 TEST_F(VibratorManagerHalControllerTest, TestFailedApiResultReturnsSuccessAfterRetries) {
183     {
184         InSequence seq;
185         EXPECT_CALL(*mMockHal.get(), ping())
186                 .Times(Exactly(1))
187                 .WillRepeatedly(Return(vibrator::HalResult<void>::failed("message")));
188         EXPECT_CALL(*mMockHal.get(), tryReconnect()).Times(Exactly(1));
189         EXPECT_CALL(*mMockHal.get(), ping())
190                 .Times(Exactly(1))
191                 .WillRepeatedly(Return(vibrator::HalResult<void>::ok()));
192     }
193 
194     ASSERT_EQ(0, mConnectCounter);
195     ASSERT_TRUE(mController->ping().isOk());
196     ASSERT_EQ(1, mConnectCounter);
197 }
198 
TEST_F(VibratorManagerHalControllerTest,TestMultiThreadConnectsOnlyOnce)199 TEST_F(VibratorManagerHalControllerTest, TestMultiThreadConnectsOnlyOnce) {
200     ASSERT_EQ(0, mConnectCounter);
201 
202     EXPECT_CALL(*mMockHal.get(), ping())
203             .Times(Exactly(10))
204             .WillRepeatedly(Return(vibrator::HalResult<void>::ok()));
205 
206     std::vector<std::thread> threads;
207     for (int i = 0; i < 10; i++) {
208         threads.push_back(std::thread([&]() { ASSERT_TRUE(mController->ping().isOk()); }));
209     }
210     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
211 
212     // Connector was called only by the first thread to use the api.
213     ASSERT_EQ(1, mConnectCounter);
214 }
215