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