blob: cb1a77a45ff550eb04e4f85a1eee2af19e8706b8 [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>
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080020#include <android/hardware/power/IPowerHintSession.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000021#include <android/hardware/power/Mode.h>
22#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
31using android::binder::Status;
32using android::hardware::power::Boost;
33using android::hardware::power::IPower;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080034using android::hardware::power::IPowerHintSession;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000035using android::hardware::power::Mode;
36
37using namespace android;
Lais Andradeb59a9b52020-05-07 17:23:42 +010038using namespace android::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000039using namespace std::chrono_literals;
40using namespace testing;
41
42// -------------------------------------------------------------------------------------------------
43
44class MockIPower : public IPower {
45public:
46 MOCK_METHOD(Status, isBoostSupported, (Boost boost, bool* ret), (override));
47 MOCK_METHOD(Status, setBoost, (Boost boost, int32_t durationMs), (override));
48 MOCK_METHOD(Status, isModeSupported, (Mode mode, bool* ret), (override));
49 MOCK_METHOD(Status, setMode, (Mode mode, bool enabled), (override));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080050 MOCK_METHOD(Status, createHintSession,
51 (int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
52 int64_t durationNanos, sp<IPowerHintSession>* session),
53 (override));
54 MOCK_METHOD(Status, getHintSessionPreferredRate, (int64_t * rate), (override));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000055 MOCK_METHOD(int32_t, getInterfaceVersion, (), (override));
56 MOCK_METHOD(std::string, getInterfaceHash, (), (override));
57 MOCK_METHOD(IBinder*, onAsBinder, (), (override));
58};
59
60// -------------------------------------------------------------------------------------------------
61
62class PowerHalWrapperAidlTest : public Test {
63public:
64 void SetUp() override;
65
66protected:
Lais Andradeb59a9b52020-05-07 17:23:42 +010067 std::unique_ptr<HalWrapper> mWrapper = nullptr;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000068 sp<StrictMock<MockIPower>> mMockHal = nullptr;
69};
70
71// -------------------------------------------------------------------------------------------------
72
73void PowerHalWrapperAidlTest::SetUp() {
74 mMockHal = new StrictMock<MockIPower>();
Lais Andradeb59a9b52020-05-07 17:23:42 +010075 mWrapper = std::make_unique<AidlHalWrapper>(mMockHal);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080076 ASSERT_NE(nullptr, mWrapper);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000077}
78
79// -------------------------------------------------------------------------------------------------
80
81TEST_F(PowerHalWrapperAidlTest, TestSetBoostSuccessful) {
82 {
83 InSequence seq;
84 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010085 .Times(Exactly(1))
86 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000087 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::DISPLAY_UPDATE_IMMINENT), Eq(100)))
Lais Andradeb59a9b52020-05-07 17:23:42 +010088 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000089 }
90
91 auto result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080092 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +000093}
94
95TEST_F(PowerHalWrapperAidlTest, TestSetBoostFailed) {
96 {
97 InSequence seq;
98 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +010099 .Times(Exactly(1))
100 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000101 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100102 .Times(Exactly(1))
103 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000104 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::DISPLAY_UPDATE_IMMINENT), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100105 .Times(Exactly(1))
106 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000107 }
108
109 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800110 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000111 result = mWrapper->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000113}
114
115TEST_F(PowerHalWrapperAidlTest, TestSetBoostUnsupported) {
116 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100117 .Times(Exactly(1))
118 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000119
120 auto result = mWrapper->setBoost(Boost::INTERACTION, 1000);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800121 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000122 result = mWrapper->setBoost(Boost::CAMERA_SHOT, 10);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800123 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000124}
125
126TEST_F(PowerHalWrapperAidlTest, TestSetBoostMultiThreadCheckSupportedOnlyOnce) {
127 {
128 InSequence seq;
129 EXPECT_CALL(*mMockHal.get(), isBoostSupported(Eq(Boost::INTERACTION), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100130 .Times(Exactly(1))
131 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
132 EXPECT_CALL(*mMockHal.get(), setBoost(Eq(Boost::INTERACTION), Eq(100))).Times(Exactly(10));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000133 }
134
135 std::vector<std::thread> threads;
136 for (int i = 0; i < 10; i++) {
137 threads.push_back(std::thread([&]() {
138 auto result = mWrapper->setBoost(Boost::INTERACTION, 100);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800139 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000140 }));
141 }
142 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
143}
144
145TEST_F(PowerHalWrapperAidlTest, TestSetModeSuccessful) {
146 {
147 InSequence seq;
148 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100149 .Times(Exactly(1))
150 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000151 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::DISPLAY_INACTIVE), Eq(false)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100152 .Times(Exactly(1));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000153 }
154
155 auto result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800156 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000157}
158
159TEST_F(PowerHalWrapperAidlTest, TestSetModeFailed) {
160 {
161 InSequence seq;
162 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100163 .Times(Exactly(1))
164 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000165 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(true)))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100166 .Times(Exactly(1))
167 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000168 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::DISPLAY_INACTIVE), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100169 .Times(Exactly(1))
170 .WillRepeatedly(Return(Status::fromExceptionCode(-1)));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000171 }
172
173 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800174 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000175 result = mWrapper->setMode(Mode::DISPLAY_INACTIVE, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800176 ASSERT_TRUE(result.isFailed());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000177}
178
179TEST_F(PowerHalWrapperAidlTest, TestSetModeUnsupported) {
180 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100181 .Times(Exactly(1))
182 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000183
184 auto result = mWrapper->setMode(Mode::LAUNCH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800185 ASSERT_TRUE(result.isUnsupported());
Jim Blackler559361b2021-11-29 00:07:39 +0000186
187 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::CAMERA_STREAMING_HIGH), _))
188 .Times(Exactly(1))
189 .WillRepeatedly(DoAll(SetArgPointee<1>(false), Return(Status())));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000190 result = mWrapper->setMode(Mode::CAMERA_STREAMING_HIGH, true);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800191 ASSERT_TRUE(result.isUnsupported());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000192}
193
194TEST_F(PowerHalWrapperAidlTest, TestSetModeMultiThreadCheckSupportedOnlyOnce) {
195 {
196 InSequence seq;
197 EXPECT_CALL(*mMockHal.get(), isModeSupported(Eq(Mode::LAUNCH), _))
Lais Andradeb59a9b52020-05-07 17:23:42 +0100198 .Times(Exactly(1))
199 .WillRepeatedly(DoAll(SetArgPointee<1>(true), Return(Status())));
200 EXPECT_CALL(*mMockHal.get(), setMode(Eq(Mode::LAUNCH), Eq(false))).Times(Exactly(10));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000201 }
202
203 std::vector<std::thread> threads;
204 for (int i = 0; i < 10; i++) {
205 threads.push_back(std::thread([&]() {
206 auto result = mWrapper->setMode(Mode::LAUNCH, false);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800207 ASSERT_TRUE(result.isOk());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000208 }));
209 }
210 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
211}
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800212
213TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionSuccessful) {
214 std::vector<int> threadIds{gettid()};
215 int32_t tgid = 999;
216 int32_t uid = 1001;
217 int64_t durationNanos = 16666666L;
218 EXPECT_CALL(*mMockHal.get(),
219 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
220 .Times(Exactly(1));
221 auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
222 ASSERT_TRUE(result.isOk());
223}
224
225TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionFailed) {
226 int32_t tgid = 999;
227 int32_t uid = 1001;
228 std::vector<int> threadIds{};
229 int64_t durationNanos = 16666666L;
230 EXPECT_CALL(*mMockHal.get(),
231 createHintSession(Eq(tgid), Eq(uid), Eq(threadIds), Eq(durationNanos), _))
232 .Times(Exactly(1))
233 .WillRepeatedly(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT)));
234 auto result = mWrapper->createHintSession(tgid, uid, threadIds, durationNanos);
235 ASSERT_TRUE(result.isFailed());
236}
237
238TEST_F(PowerHalWrapperAidlTest, TestGetHintSessionPreferredRate) {
239 EXPECT_CALL(*mMockHal.get(), getHintSessionPreferredRate(_)).Times(Exactly(1));
240 auto result = mWrapper->getHintSessionPreferredRate();
241 ASSERT_TRUE(result.isOk());
242 int64_t rate = result.value();
243 ASSERT_GE(0, rate);
244}