blob: a7202969ad30d4f10bffa336f7e11940afe24327 [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
Xiang Wang99f6f3c2023-05-22 13:12:16 -070019#include <aidl/android/hardware/power/Boost.h>
20#include <aidl/android/hardware/power/IPowerHintSession.h>
21#include <aidl/android/hardware/power/Mode.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000022#include <binder/IServiceManager.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000023#include <gmock/gmock.h>
24#include <gtest/gtest.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000025#include <powermanager/PowerHalWrapper.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010026#include <utils/Log.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000027
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080028#include <unistd.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000029#include <thread>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000030
Xiang Wang99f6f3c2023-05-22 13:12:16 -070031using aidl::android::hardware::power::Boost;
Matt Buckley104f53a2023-12-14 00:19:20 +000032using aidl::android::hardware::power::ChannelConfig;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070033using aidl::android::hardware::power::IPower;
34using aidl::android::hardware::power::IPowerHintSession;
35using aidl::android::hardware::power::Mode;
Matt Buckley104f53a2023-12-14 00:19:20 +000036using aidl::android::hardware::power::SessionConfig;
37using aidl::android::hardware::power::SessionTag;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000038using android::binder::Status;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000039
40using namespace android;
Lais Andradeb59a9b52020-05-07 17:23:42 +010041using namespace android::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000042using namespace std::chrono_literals;
43using namespace testing;
44
45// -------------------------------------------------------------------------------------------------
46
47class MockIPower : public IPower {
48public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -070049 MockIPower() = default;
50
51 MOCK_METHOD(ndk::ScopedAStatus, isBoostSupported, (Boost boost, bool* ret), (override));
52 MOCK_METHOD(ndk::ScopedAStatus, setBoost, (Boost boost, int32_t durationMs), (override));
53 MOCK_METHOD(ndk::ScopedAStatus, isModeSupported, (Mode mode, bool* ret), (override));
54 MOCK_METHOD(ndk::ScopedAStatus, setMode, (Mode mode, bool enabled), (override));
55 MOCK_METHOD(ndk::ScopedAStatus, createHintSession,
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080056 (int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
Xiang Wang99f6f3c2023-05-22 13:12:16 -070057 int64_t durationNanos, std::shared_ptr<IPowerHintSession>* session),
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080058 (override));
Matt Buckley104f53a2023-12-14 00:19:20 +000059 MOCK_METHOD(ndk::ScopedAStatus, createHintSessionWithConfig,
60 (int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
61 int64_t durationNanos, SessionTag tag, SessionConfig* config,
62 std::shared_ptr<IPowerHintSession>* _aidl_return),
63 (override));
64 MOCK_METHOD(ndk::ScopedAStatus, getSessionChannel,
65 (int32_t tgid, int32_t uid, ChannelConfig* _aidl_return), (override));
66 MOCK_METHOD(ndk::ScopedAStatus, closeSessionChannel, (int32_t tgid, int32_t uid), (override));
Xiang Wang99f6f3c2023-05-22 13:12:16 -070067 MOCK_METHOD(ndk::ScopedAStatus, getHintSessionPreferredRate, (int64_t * rate), (override));
68 MOCK_METHOD(ndk::ScopedAStatus, getInterfaceVersion, (int32_t * version), (override));
69 MOCK_METHOD(ndk::ScopedAStatus, getInterfaceHash, (std::string * hash), (override));
70 MOCK_METHOD(ndk::SpAIBinder, asBinder, (), (override));
71 MOCK_METHOD(bool, isRemote, (), (override));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000072};
73
74// -------------------------------------------------------------------------------------------------
75
76class PowerHalWrapperAidlTest : public Test {
77public:
78 void SetUp() override;
79
80protected:
Lais Andradeb59a9b52020-05-07 17:23:42 +010081 std::unique_ptr<HalWrapper> mWrapper = nullptr;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070082 std::shared_ptr<StrictMock<MockIPower>> mMockHal = nullptr;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000083};
84
85// -------------------------------------------------------------------------------------------------
86
87void PowerHalWrapperAidlTest::SetUp() {
Xiang Wang99f6f3c2023-05-22 13:12:16 -070088 mMockHal = ndk::SharedRefBase::make<StrictMock<MockIPower>>();
Lais Andradeb59a9b52020-05-07 17:23:42 +010089 mWrapper = std::make_unique<AidlHalWrapper>(mMockHal);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080090 ASSERT_NE(nullptr, mWrapper);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000091}
92
93// -------------------------------------------------------------------------------------------------
94
95TEST_F(PowerHalWrapperAidlTest, TestSetBoostSuccessful) {
96 {
97 InSequence seq;
98 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010099 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700100 .WillOnce(DoAll(SetArgPointee<1>(true),
101 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000102 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::DISPLAY_UPDATE_IMMINENT), Eq(100)))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700103 .Times(Exactly(1))
104 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000105 }
106
107 auto result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800108 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000109}
110
111TEST_F(PowerHalWrapperAidlTest, TestSetBoostFailed) {
112 {
113 InSequence seq;
114 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100115 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700116 .WillOnce(DoAll(SetArgPointee<1>(true),
117 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000118 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100119 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700120 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::fromExceptionCode(-1))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000121 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100122 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700123 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::fromExceptionCode(-1))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000124 }
125
126 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800127 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000128 result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800129 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000130}
131
132TEST_F(PowerHalWrapperAidlTest, TestSetBoostUnsupported) {
133 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100134 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700135 .WillOnce(DoAll(SetArgPointee<1>(false),
136 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000137
138 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800139 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000140 result = mWrapper->setBoost(Boost::CAMERA_SHOT, 10);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800141 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000142}
143
144TEST_F(PowerHalWrapperAidlTest, TestSetBoostMultiThreadCheckSupportedOnlyOnce) {
145 {
146 InSequence seq;
147 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100148 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700149 .WillOnce(DoAll(SetArgPointee<1>(true),
150 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
151 auto& exp = EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
152 .Times(Exactly(10));
153 for (int i = 0; i < 10; i++) {
154 exp.WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
155 }
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000156 }
157
158 std::vector<std::thread> threads;
159 for (int i = 0; i < 10; i++) {
160 threads.push_back(std::thread([&]() {
161 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800162 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000163 }));
164 }
165 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
166}
167
168TEST_F(PowerHalWrapperAidlTest, TestSetModeSuccessful) {
169 {
170 InSequence seq;
171 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100172 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700173 .WillOnce(DoAll(SetArgPointee<1>(true),
174 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000175 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::DISPLAY_INACTIVE), Eq(false)))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700176 .Times(Exactly(1))
177 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000178 }
179
180 auto result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800181 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000182}
183
184TEST_F(PowerHalWrapperAidlTest, TestSetModeFailed) {
185 {
186 InSequence seq;
187 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100188 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700189 .WillOnce(DoAll(SetArgPointee<1>(true),
190 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000191 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100192 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700193 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::fromExceptionCode(-1))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000194 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100195 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700196 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::fromExceptionCode(-1))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000197 }
198
199 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800200 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000201 result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800202 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000203}
204
205TEST_F(PowerHalWrapperAidlTest, TestSetModeUnsupported) {
206 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100207 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700208 .WillOnce(DoAll(SetArgPointee<1>(false),
209 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000210
211 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800212 ASSERT_TRUE(result.isUnsupported());
Jim Blackler559361b2021-11-29 00:07:39 +0000213
214 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::CAMERA_STREAMING_HIGH), _))
215 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700216 .WillOnce(DoAll(SetArgPointee<1>(false),
217 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000218 result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800219 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000220}
221
222TEST_F(PowerHalWrapperAidlTest, TestSetModeMultiThreadCheckSupportedOnlyOnce) {
223 {
224 InSequence seq;
225 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100226 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700227 .WillOnce(DoAll(SetArgPointee<1>(true),
228 Return(testing::ByMove(ndk::ScopedAStatus::ok()))));
229 auto& exp = EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(false)))
230 .Times(Exactly(10));
231 for (int i = 0; i < 10; i++) {
232 exp.WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
233 }
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000234 }
235
236 std::vector<std::thread> threads;
237 for (int i = 0; i < 10; i++) {
238 threads.push_back(std::thread([&]() {
239 auto result = mWrapper->setMode(Mode::LAUNCH, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800240 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000241 }));
242 }
243 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
244}
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800245
246TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionSuccessful) {
247 std::vector<int> threadIds{gettid()};
248 int32_t tgid = 999;
249 int32_t uid = 1001;
250 int64_t durationNanos = 16666666L;
251 EXPECT_CALL(*mMockHal.get(),
252 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700253 .Times(Exactly(1))
254 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800255 auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
256 ASSERT_TRUE(result.isOk());
257}
258
Matt Buckleydb4192a2023-12-21 20:00:32 +0000259TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionWithConfigSuccessful) {
260 std::vector<int> threadIds{gettid()};
261 int32_t tgid = 999;
262 int32_t uid = 1001;
263 int64_t durationNanos = 16666666L;
264 SessionTag tag = SessionTag::OTHER;
265 SessionConfig out;
266 EXPECT_CALL(*mMockHal.get(),
267 createHintSessionWithConfig(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos),
268 Eq(tag), _, _))
269 .Times(Exactly(1))
270 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
271 auto result =
272 mWrapper->createHintSessionWithConfig(tgid, uid, threadIds, durationNanos, tag, &out);
273 ASSERT_TRUE(result.isOk());
274}
275
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800276TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionFailed) {
277 int32_t tgid = 999;
278 int32_t uid = 1001;
279 std::vector<int> threadIds{};
280 int64_t durationNanos = 16666666L;
281 EXPECT_CALL(*mMockHal.get(),
282 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
283 .Times(Exactly(1))
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700284 .WillOnce(Return(testing::ByMove(
285 ndk::ScopedAStatus::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT))));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800286 auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
287 ASSERT_TRUE(result.isFailed());
288}
289
290TEST_F(PowerHalWrapperAidlTest, TestGetHintSessionPreferredRate) {
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700291 EXPECT_CALL(*mMockHal.get(), getHintSessionPreferredRate(_))
292 .Times(Exactly(1))
293 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800294 auto result = mWrapper->getHintSessionPreferredRate();
295 ASSERT_TRUE(result.isOk());
296 int64_t rate = result.value();
297 ASSERT_GE(0, rate);
298}
Matt Buckleydb4192a2023-12-21 20:00:32 +0000299
300TEST_F(PowerHalWrapperAidlTest, TestSessionChannel) {
301 int32_t tgid = 999;
302 int32_t uid = 1001;
303 EXPECT_CALL(*mMockHal.get(), getSessionChannel(Eq(tgid), Eq(uid), _))
304 .Times(Exactly(1))
305 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
306 EXPECT_CALL(*mMockHal.get(), closeSessionChannel(Eq(tgid), Eq(uid)))
307 .Times(Exactly(1))
308 .WillOnce(Return(testing::ByMove(ndk::ScopedAStatus::ok())));
309 auto createResult = mWrapper->getSessionChannel(tgid, uid);
310 ASSERT_TRUE(createResult.isOk());
311 auto closeResult = mWrapper->closeSessionChannel(tgid, uid);
312 ASSERT_TRUE(closeResult.isOk());
313}