blob: a7656598f07590c8f59e38cda93f99850899fc41 [file] [log] [blame]
Lais Andrade4d51f6c2020-03-25 10:58:31 +00001/*
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 "PowerHalWrapperAidlTest"
18
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/Mode.h>
21#include <binder/IServiceManager.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000024#include <powermanager/PowerHalWrapper.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010025#include <utils/Log.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000026
27#include <thread>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000028
29using android::binder::Status;
30using android::hardware::power::Boost;
31using android::hardware::power::IPower;
32using android::hardware::power::Mode;
33
34using namespace android;
Lais Andradeb59a9b52020-05-07 17:23:42 +010035using namespace android::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000036using namespace std::chrono_literals;
37using namespace testing;
38
39// -------------------------------------------------------------------------------------------------
40
41class MockIPower : public IPower {
42public:
43 MOCK_METHOD(Status, isBoostSupported, (Boost boost, bool* ret), (override));
44 MOCK_METHOD(Status, setBoost, (Boost boost, int32_t durationMs), (override));
45 MOCK_METHOD(Status, isModeSupported, (Mode mode, bool* ret), (override));
46 MOCK_METHOD(Status, setMode, (Mode mode, bool enabled), (override));
47 MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
48 MOCK_METHOD(std::string, getInterfaceHash, (), (override));
49 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
50};
51
52// -------------------------------------------------------------------------------------------------
53
54class PowerHalWrapperAidlTest : public Test {
55public:
56 void SetUp() override;
57
58protected:
Lais Andradeb59a9b52020-05-07 17:23:42 +010059 std::unique_ptr<HalWrapper> mWrapper = nullptr;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000060 sp<StrictMock<MockIPower>> mMockHal = nullptr;
61};
62
63// -------------------------------------------------------------------------------------------------
64
65void PowerHalWrapperAidlTest::SetUp() {
66 mMockHal = new StrictMock<MockIPower>();
Lais Andradeb59a9b52020-05-07 17:23:42 +010067 mWrapper = std::make_unique<AidlHalWrapper>(mMockHal);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000068 ASSERT_NE(mWrapper, nullptr);
69}
70
71// -------------------------------------------------------------------------------------------------
72
73TEST_F(PowerHalWrapperAidlTest, TestSetBoostSuccessful) {
74 {
75 InSequence seq;
76 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010077 .Times(Exactly(1))
78 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000079 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::DISPLAY_UPDATE_IMMINENT), Eq(100)))
Lais Andradeb59a9b52020-05-07 17:23:42 +010080 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000081 }
82
83 auto result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 100);
Lais Andradeb59a9b52020-05-07 17:23:42 +010084 ASSERT_EQ(HalResult::SUCCESSFUL, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000085}
86
87TEST_F(PowerHalWrapperAidlTest, TestSetBoostFailed) {
88 {
89 InSequence seq;
90 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010091 .Times(Exactly(1))
92 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000093 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
Lais Andradeb59a9b52020-05-07 17:23:42 +010094 .Times(Exactly(1))
95 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000096 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010097 .Times(Exactly(1))
98 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000099 }
100
101 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100102 ASSERT_EQ(HalResult::FAILED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000103 result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 1000);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100104 ASSERT_EQ(HalResult::FAILED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000105}
106
107TEST_F(PowerHalWrapperAidlTest, TestSetBoostUnsupported) {
108 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100109 .Times(Exactly(1))
110 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000111
112 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100113 ASSERT_EQ(HalResult::UNSUPPORTED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000114 result = mWrapper->setBoost(Boost::CAMERA_SHOT, 10);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100115 ASSERT_EQ(HalResult::UNSUPPORTED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000116}
117
118TEST_F(PowerHalWrapperAidlTest, TestSetBoostMultiThreadCheckSupportedOnlyOnce) {
119 {
120 InSequence seq;
121 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100122 .Times(Exactly(1))
123 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
124 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100))).Times(Exactly(10));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000125 }
126
127 std::vector<std::thread> threads;
128 for (int i = 0; i < 10; i++) {
129 threads.push_back(std::thread([&]() {
130 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100131 ASSERT_EQ(HalResult::SUCCESSFUL, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000132 }));
133 }
134 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
135}
136
137TEST_F(PowerHalWrapperAidlTest, TestSetModeSuccessful) {
138 {
139 InSequence seq;
140 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100141 .Times(Exactly(1))
142 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000143 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::DISPLAY_INACTIVE), Eq(false)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100144 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000145 }
146
147 auto result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100148 ASSERT_EQ(HalResult::SUCCESSFUL, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000149}
150
151TEST_F(PowerHalWrapperAidlTest, TestSetModeFailed) {
152 {
153 InSequence seq;
154 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100155 .Times(Exactly(1))
156 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000157 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100158 .Times(Exactly(1))
159 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000160 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100161 .Times(Exactly(1))
162 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000163 }
164
165 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100166 ASSERT_EQ(HalResult::FAILED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000167 result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100168 ASSERT_EQ(HalResult::FAILED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000169}
170
171TEST_F(PowerHalWrapperAidlTest, TestSetModeUnsupported) {
172 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100173 .Times(Exactly(1))
174 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000175
176 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100177 ASSERT_EQ(HalResult::UNSUPPORTED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000178 result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100179 ASSERT_EQ(HalResult::UNSUPPORTED, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000180}
181
182TEST_F(PowerHalWrapperAidlTest, TestSetModeMultiThreadCheckSupportedOnlyOnce) {
183 {
184 InSequence seq;
185 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100186 .Times(Exactly(1))
187 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
188 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(false))).Times(Exactly(10));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000189 }
190
191 std::vector<std::thread> threads;
192 for (int i = 0; i < 10; i++) {
193 threads.push_back(std::thread([&]() {
194 auto result = mWrapper->setMode(Mode::LAUNCH, false);
Lais Andradeb59a9b52020-05-07 17:23:42 +0100195 ASSERT_EQ(HalResult::SUCCESSFUL, result);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000196 }));
197 }
198 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
199}