blob: e51f756e605c2454604464d6d76b1feeaf38dbf0 [file] [log] [blame]
Wei Wang61c2a332020-01-08 16:51:47 -08001/*
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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
Wei Wang05003412021-04-01 10:44:29 -070019#include <aidl/android/hardware/power/BnPower.h>
20#include <aidl/android/hardware/power/BnPowerHintSession.h>
Dan Stozacca80272020-01-13 13:06:13 -080021#include <android-base/properties.h>
Wei Wang05003412021-04-01 10:44:29 -070022#include <android/binder_ibinder.h>
23#include <android/binder_manager.h>
24#include <android/binder_process.h>
Wei Wang9df909d2021-06-12 18:26:38 -070025#include <android/binder_status.h>
Wei Wang61c2a332020-01-08 16:51:47 -080026
Wei Wang05003412021-04-01 10:44:29 -070027#include <unistd.h>
Wei Wang61c2a332020-01-08 16:51:47 -080028
Wei Wang05003412021-04-01 10:44:29 -070029namespace aidl::android::hardware::power {
30namespace {
31
32using ::android::base::GetUintProperty;
Wei Wang61c2a332020-01-08 16:51:47 -080033using android::hardware::power::Boost;
34using android::hardware::power::IPower;
Wei Wang05003412021-04-01 10:44:29 -070035using android::hardware::power::IPowerHintSession;
Wei Wang61c2a332020-01-08 16:51:47 -080036using android::hardware::power::Mode;
Matt Buckley13843882022-09-15 22:32:56 +000037using android::hardware::power::SessionHint;
Wei Wang05003412021-04-01 10:44:29 -070038using android::hardware::power::WorkDuration;
Wei Wang61c2a332020-01-08 16:51:47 -080039
Wei Wang05003412021-04-01 10:44:29 -070040const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()};
Wei Wang61c2a332020-01-08 16:51:47 -080041
Wei Wang05003412021-04-01 10:44:29 -070042const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
Wei Wang61c2a332020-01-08 16:51:47 -080043
Matt Buckley13843882022-09-15 22:32:56 +000044const std::vector<SessionHint> kSessionHints{ndk::enum_range<SessionHint>().begin(),
45 ndk::enum_range<SessionHint>().end()};
46
Wei Wang61c2a332020-01-08 16:51:47 -080047const std::vector<Boost> kInvalidBoosts = {
48 static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1),
49 static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1),
50};
51
52const std::vector<Mode> kInvalidModes = {
53 static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1),
54 static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1),
55};
56
Matt Buckley13843882022-09-15 22:32:56 +000057const std::vector<SessionHint> kInvalidSessionHints = {
58 static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.front()) - 1),
59 static_cast<SessionHint>(static_cast<int32_t>(kSessionHints.back()) + 1),
60};
61
Wei Wang05003412021-04-01 10:44:29 -070062class DurationWrapper : public WorkDuration {
63 public:
64 DurationWrapper(int64_t dur, int64_t time) {
65 durationNanos = dur;
66 timeStampNanos = time;
67 }
68};
69
70const std::vector<int32_t> kSelfTids = {
71 gettid(),
72};
73
74const std::vector<int32_t> kEmptyTids = {};
75
76const std::vector<WorkDuration> kNoDurations = {};
77
78const std::vector<WorkDuration> kDurationsWithZero = {
79 DurationWrapper(1000L, 1L),
80 DurationWrapper(0L, 2L),
81};
82
83const std::vector<WorkDuration> kDurationsWithNegative = {
84 DurationWrapper(1000L, 1L),
85 DurationWrapper(-1000L, 2L),
86};
87
88const std::vector<WorkDuration> kDurations = {
89 DurationWrapper(1L, 1L),
90 DurationWrapper(1000L, 2L),
91 DurationWrapper(1000000L, 3L),
92 DurationWrapper(1000000000L, 4L),
93};
94
Peiyong Lin3e0eb722022-10-17 19:55:20 +000095// DEVICEs launching with Android 11 MUST meet the requirements for the
96// target-level=5 compatibility_matrix file.
97const uint64_t kCompatibilityMatrix5ApiLevel = 30;
98
99// DEVICEs launching with Android 13 MUST meet the requirements for the
100// target-level=7 compatibility_matrix file.
101const uint64_t kCompatibilityMatrix7ApiLevel = 33;
102
Wei Wang9df909d2021-06-12 18:26:38 -0700103inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) {
104 return status.getStatus() == STATUS_UNKNOWN_TRANSACTION ||
105 status.getExceptionCode() == EX_UNSUPPORTED_OPERATION;
106}
107
Wei Wang61c2a332020-01-08 16:51:47 -0800108class PowerAidl : public testing::TestWithParam<std::string> {
109 public:
110 virtual void SetUp() override {
Wei Wang05003412021-04-01 10:44:29 -0700111 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
112 ASSERT_NE(binder, nullptr);
113 power = IPower::fromBinder(ndk::SpAIBinder(binder));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000114
115 mApiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0);
116 ASSERT_NE(mApiLevel, 0);
Wei Wang61c2a332020-01-08 16:51:47 -0800117 }
118
Wei Wang05003412021-04-01 10:44:29 -0700119 std::shared_ptr<IPower> power;
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000120 uint64_t mApiLevel;
Wei Wang61c2a332020-01-08 16:51:47 -0800121};
122
123TEST_P(PowerAidl, setMode) {
124 for (const auto& mode : kModes) {
125 ASSERT_TRUE(power->setMode(mode, true).isOk());
126 ASSERT_TRUE(power->setMode(mode, false).isOk());
127 }
128 for (const auto& mode : kInvalidModes) {
129 ASSERT_TRUE(power->setMode(mode, true).isOk());
130 ASSERT_TRUE(power->setMode(mode, false).isOk());
131 }
132}
133
134TEST_P(PowerAidl, isModeSupported) {
135 for (const auto& mode : kModes) {
136 bool supported;
137 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
138 }
139 for (const auto& mode : kInvalidModes) {
140 bool supported;
141 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
Dan Stozacca80272020-01-13 13:06:13 -0800142 // Should return false for values outside enum
Wei Wang61c2a332020-01-08 16:51:47 -0800143 ASSERT_FALSE(supported);
144 }
145}
146
147TEST_P(PowerAidl, setBoost) {
148 for (const auto& boost : kBoosts) {
149 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
150 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
151 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
152 }
153 for (const auto& boost : kInvalidBoosts) {
154 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
155 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
156 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
157 }
158}
159
160TEST_P(PowerAidl, isBoostSupported) {
161 for (const auto& boost : kBoosts) {
162 bool supported;
163 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
164 }
165 for (const auto& boost : kInvalidBoosts) {
166 bool supported;
167 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
Dan Stozacca80272020-01-13 13:06:13 -0800168 // Should return false for values outside enum
Wei Wang61c2a332020-01-08 16:51:47 -0800169 ASSERT_FALSE(supported);
170 }
171}
172
Wei Wang05003412021-04-01 10:44:29 -0700173TEST_P(PowerAidl, getHintSessionPreferredRate) {
174 int64_t rate = -1;
175 auto status = power->getHintSessionPreferredRate(&rate);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000176 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700177 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000178 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700179 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000180 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700181 // At least 1ms rate limit from HAL
182 ASSERT_GE(rate, 1000000);
183}
184
185TEST_P(PowerAidl, createAndCloseHintSession) {
186 std::shared_ptr<IPowerHintSession> session;
187 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000188 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700189 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000190 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700191 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000192 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700193 ASSERT_NE(nullptr, session);
194 ASSERT_TRUE(session->pause().isOk());
195 ASSERT_TRUE(session->resume().isOk());
196 // Test normal destroy operation
197 ASSERT_TRUE(session->close().isOk());
198 session.reset();
199}
Matt Buckley13843882022-09-15 22:32:56 +0000200
Wei Wang05003412021-04-01 10:44:29 -0700201TEST_P(PowerAidl, createHintSessionFailed) {
202 std::shared_ptr<IPowerHintSession> session;
203 auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000204
205 // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK.
Wei Wang05003412021-04-01 10:44:29 -0700206 ASSERT_FALSE(status.isOk());
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000207
208 // If device not launching with Android 13 and beyond, check whether it's supported,
209 // if not, skip the test.
210 if (mApiLevel < kCompatibilityMatrix7ApiLevel && isUnknownOrUnsupported(status)) {
211 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700212 }
Wei Wang05003412021-04-01 10:44:29 -0700213 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
214}
215
216TEST_P(PowerAidl, updateAndReportDurations) {
217 std::shared_ptr<IPowerHintSession> session;
218 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000219 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700220 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000221 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700222 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000223 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700224 ASSERT_NE(nullptr, session);
225
226 ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk());
227 ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk());
228}
229
Matt Buckley13843882022-09-15 22:32:56 +0000230TEST_P(PowerAidl, sendSessionHint) {
231 std::shared_ptr<IPowerHintSession> session;
232 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
233 if (!status.isOk()) {
234 EXPECT_TRUE(isUnknownOrUnsupported(status));
235 return;
236 }
237 for (const auto& sessionHint : kSessionHints) {
238 ASSERT_TRUE(session->sendHint(sessionHint).isOk());
239 }
240 for (const auto& sessionHint : kInvalidSessionHints) {
241 ASSERT_TRUE(session->sendHint(sessionHint).isOk());
242 }
243}
244
Dan Stozacca80272020-01-13 13:06:13 -0800245// FIXED_PERFORMANCE mode is required for all devices which ship on Android 11
246// or later
247TEST_P(PowerAidl, hasFixedPerformance) {
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000248 if (mApiLevel < kCompatibilityMatrix5ApiLevel) {
249 GTEST_SKIP() << "FIXED_PERFORMANCE mode is only required for all devices launching Android "
250 "11 or later.";
Dan Stozacca80272020-01-13 13:06:13 -0800251 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000252 bool supported;
253 ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk());
254 ASSERT_TRUE(supported);
Dan Stozacca80272020-01-13 13:06:13 -0800255}
256
Dan Shiba4d5322020-07-28 13:09:30 -0700257GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl);
Wei Wang61c2a332020-01-08 16:51:47 -0800258INSTANTIATE_TEST_SUITE_P(Power, PowerAidl,
Wei Wang05003412021-04-01 10:44:29 -0700259 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
260 ::android::PrintInstanceNameToString);
261
262} // namespace
Wei Wang61c2a332020-01-08 16:51:47 -0800263
264int main(int argc, char** argv) {
265 ::testing::InitGoogleTest(&argc, argv);
Wei Wang05003412021-04-01 10:44:29 -0700266 ABinderProcess_setThreadPoolMaxThreadCount(1);
267 ABinderProcess_startThreadPool();
Wei Wang61c2a332020-01-08 16:51:47 -0800268 return RUN_ALL_TESTS();
269}
Wei Wang05003412021-04-01 10:44:29 -0700270
271} // namespace aidl::android::hardware::power