• 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 "VibratorManagerHalWrapperLegacyTest"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <utils/Log.h>
23 
24 #include <vibratorservice/VibratorManagerHalWrapper.h>
25 
26 using android::hardware::vibrator::CompositeEffect;
27 using android::hardware::vibrator::CompositePrimitive;
28 using android::hardware::vibrator::Effect;
29 using android::hardware::vibrator::EffectStrength;
30 
31 using std::chrono::milliseconds;
32 
33 using namespace android;
34 using namespace testing;
35 
36 // -------------------------------------------------------------------------------------------------
37 
38 class MockHalController : public vibrator::HalController {
39 public:
40     MockHalController() = default;
41     virtual ~MockHalController() = default;
42 
43     MOCK_METHOD(bool, init, (), (override));
44     MOCK_METHOD(void, tryReconnect, (), (override));
45 };
46 
47 // -------------------------------------------------------------------------------------------------
48 
49 class VibratorManagerHalWrapperLegacyTest : public Test {
50 public:
SetUp()51     void SetUp() override {
52         mMockController = std::make_shared<StrictMock<MockHalController>>();
53         mWrapper = std::make_unique<vibrator::LegacyManagerHalWrapper>(mMockController);
54         ASSERT_NE(mWrapper, nullptr);
55     }
56 
57 protected:
58     std::shared_ptr<StrictMock<MockHalController>> mMockController = nullptr;
59     std::unique_ptr<vibrator::ManagerHalWrapper> mWrapper = nullptr;
60 };
61 
62 // -------------------------------------------------------------------------------------------------
63 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestPing)64 TEST_F(VibratorManagerHalWrapperLegacyTest, TestPing) {
65     EXPECT_CALL(*mMockController.get(), init()).Times(Exactly(1)).WillOnce(Return(false));
66 
67     ASSERT_TRUE(mWrapper->ping().isUnsupported());
68 }
69 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestTryReconnect)70 TEST_F(VibratorManagerHalWrapperLegacyTest, TestTryReconnect) {
71     EXPECT_CALL(*mMockController.get(), tryReconnect()).Times(Exactly(1));
72 
73     mWrapper->tryReconnect();
74 }
75 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestGetCapabilities)76 TEST_F(VibratorManagerHalWrapperLegacyTest, TestGetCapabilities) {
77     auto result = mWrapper->getCapabilities();
78     ASSERT_TRUE(result.isOk());
79     ASSERT_EQ(vibrator::ManagerCapabilities::NONE, result.value());
80 }
81 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestGetVibratorIds)82 TEST_F(VibratorManagerHalWrapperLegacyTest, TestGetVibratorIds) {
83     std::vector<int> expectedIds = {0};
84 
85     EXPECT_CALL(*mMockController.get(), init())
86             .Times(Exactly(2))
87             .WillOnce(Return(false))
88             .WillRepeatedly(Return(true));
89 
90     auto result = mWrapper->getVibratorIds();
91     ASSERT_TRUE(result.isOk());
92     ASSERT_EQ(std::vector<int32_t>(), result.value());
93 
94     result = mWrapper->getVibratorIds();
95     ASSERT_TRUE(result.isOk());
96     ASSERT_EQ(expectedIds, result.value());
97 }
98 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestGetVibratorWithValidIdReturnsController)99 TEST_F(VibratorManagerHalWrapperLegacyTest, TestGetVibratorWithValidIdReturnsController) {
100     EXPECT_CALL(*mMockController.get(), init())
101             .Times(Exactly(2))
102             .WillOnce(Return(false))
103             .WillRepeatedly(Return(true));
104 
105     ASSERT_TRUE(mWrapper->getVibrator(0).isFailed());
106 
107     auto result = mWrapper->getVibrator(0);
108     ASSERT_TRUE(result.isOk());
109     ASSERT_EQ(mMockController.get(), result.value().get());
110 }
111 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestGetVibratorWithInvalidIdFails)112 TEST_F(VibratorManagerHalWrapperLegacyTest, TestGetVibratorWithInvalidIdFails) {
113     ASSERT_TRUE(mWrapper->getVibrator(-1).isFailed());
114 }
115 
TEST_F(VibratorManagerHalWrapperLegacyTest,TestSyncedOperationsUnsupported)116 TEST_F(VibratorManagerHalWrapperLegacyTest, TestSyncedOperationsUnsupported) {
117     std::vector<int32_t> vibratorIds;
118     vibratorIds.push_back(0);
119 
120     ASSERT_TRUE(mWrapper->prepareSynced(vibratorIds).isUnsupported());
121     ASSERT_TRUE(mWrapper->triggerSynced([]() {}).isUnsupported());
122     ASSERT_TRUE(mWrapper->cancelSynced().isUnsupported());
123 }
124