blob: b81dd8f669d1963a10d87e5034982c4188bad9a6 [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;
Wei Wang05003412021-04-01 10:44:29 -070037using android::hardware::power::WorkDuration;
Wei Wang61c2a332020-01-08 16:51:47 -080038
Wei Wang05003412021-04-01 10:44:29 -070039const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()};
Wei Wang61c2a332020-01-08 16:51:47 -080040
Wei Wang05003412021-04-01 10:44:29 -070041const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
Wei Wang61c2a332020-01-08 16:51:47 -080042
43const std::vector<Boost> kInvalidBoosts = {
44 static_cast<Boost>(static_cast<int32_t>(kBoosts.front()) - 1),
45 static_cast<Boost>(static_cast<int32_t>(kBoosts.back()) + 1),
46};
47
48const std::vector<Mode> kInvalidModes = {
49 static_cast<Mode>(static_cast<int32_t>(kModes.front()) - 1),
50 static_cast<Mode>(static_cast<int32_t>(kModes.back()) + 1),
51};
52
Wei Wang05003412021-04-01 10:44:29 -070053class DurationWrapper : public WorkDuration {
54 public:
55 DurationWrapper(int64_t dur, int64_t time) {
56 durationNanos = dur;
57 timeStampNanos = time;
58 }
59};
60
61const std::vector<int32_t> kSelfTids = {
62 gettid(),
63};
64
65const std::vector<int32_t> kEmptyTids = {};
66
67const std::vector<WorkDuration> kNoDurations = {};
68
69const std::vector<WorkDuration> kDurationsWithZero = {
70 DurationWrapper(1000L, 1L),
71 DurationWrapper(0L, 2L),
72};
73
74const std::vector<WorkDuration> kDurationsWithNegative = {
75 DurationWrapper(1000L, 1L),
76 DurationWrapper(-1000L, 2L),
77};
78
79const std::vector<WorkDuration> kDurations = {
80 DurationWrapper(1L, 1L),
81 DurationWrapper(1000L, 2L),
82 DurationWrapper(1000000L, 3L),
83 DurationWrapper(1000000000L, 4L),
84};
85
Peiyong Lin3e0eb722022-10-17 19:55:20 +000086// DEVICEs launching with Android 11 MUST meet the requirements for the
87// target-level=5 compatibility_matrix file.
88const uint64_t kCompatibilityMatrix5ApiLevel = 30;
89
90// DEVICEs launching with Android 13 MUST meet the requirements for the
91// target-level=7 compatibility_matrix file.
92const uint64_t kCompatibilityMatrix7ApiLevel = 33;
93
Wei Wang9df909d2021-06-12 18:26:38 -070094inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) {
95 return status.getStatus() == STATUS_UNKNOWN_TRANSACTION ||
96 status.getExceptionCode() == EX_UNSUPPORTED_OPERATION;
97}
98
Wei Wang61c2a332020-01-08 16:51:47 -080099class PowerAidl : public testing::TestWithParam<std::string> {
100 public:
101 virtual void SetUp() override {
Wei Wang05003412021-04-01 10:44:29 -0700102 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
103 ASSERT_NE(binder, nullptr);
104 power = IPower::fromBinder(ndk::SpAIBinder(binder));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000105
106 mApiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0);
107 ASSERT_NE(mApiLevel, 0);
Wei Wang61c2a332020-01-08 16:51:47 -0800108 }
109
Wei Wang05003412021-04-01 10:44:29 -0700110 std::shared_ptr<IPower> power;
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000111 uint64_t mApiLevel;
Wei Wang61c2a332020-01-08 16:51:47 -0800112};
113
114TEST_P(PowerAidl, setMode) {
115 for (const auto& mode : kModes) {
116 ASSERT_TRUE(power->setMode(mode, true).isOk());
117 ASSERT_TRUE(power->setMode(mode, false).isOk());
118 }
119 for (const auto& mode : kInvalidModes) {
120 ASSERT_TRUE(power->setMode(mode, true).isOk());
121 ASSERT_TRUE(power->setMode(mode, false).isOk());
122 }
123}
124
125TEST_P(PowerAidl, isModeSupported) {
126 for (const auto& mode : kModes) {
127 bool supported;
128 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
129 }
130 for (const auto& mode : kInvalidModes) {
131 bool supported;
132 ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk());
Dan Stozacca80272020-01-13 13:06:13 -0800133 // Should return false for values outside enum
Wei Wang61c2a332020-01-08 16:51:47 -0800134 ASSERT_FALSE(supported);
135 }
136}
137
138TEST_P(PowerAidl, setBoost) {
139 for (const auto& boost : kBoosts) {
140 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
141 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
142 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
143 }
144 for (const auto& boost : kInvalidBoosts) {
145 ASSERT_TRUE(power->setBoost(boost, 0).isOk());
146 ASSERT_TRUE(power->setBoost(boost, 1000).isOk());
147 ASSERT_TRUE(power->setBoost(boost, -1).isOk());
148 }
149}
150
151TEST_P(PowerAidl, isBoostSupported) {
152 for (const auto& boost : kBoosts) {
153 bool supported;
154 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
155 }
156 for (const auto& boost : kInvalidBoosts) {
157 bool supported;
158 ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk());
Dan Stozacca80272020-01-13 13:06:13 -0800159 // Should return false for values outside enum
Wei Wang61c2a332020-01-08 16:51:47 -0800160 ASSERT_FALSE(supported);
161 }
162}
163
Wei Wang05003412021-04-01 10:44:29 -0700164TEST_P(PowerAidl, getHintSessionPreferredRate) {
165 int64_t rate = -1;
166 auto status = power->getHintSessionPreferredRate(&rate);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000167 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700168 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000169 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700170 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000171 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700172 // At least 1ms rate limit from HAL
173 ASSERT_GE(rate, 1000000);
174}
175
176TEST_P(PowerAidl, createAndCloseHintSession) {
177 std::shared_ptr<IPowerHintSession> session;
178 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000179 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700180 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000181 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700182 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000183 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700184 ASSERT_NE(nullptr, session);
185 ASSERT_TRUE(session->pause().isOk());
186 ASSERT_TRUE(session->resume().isOk());
187 // Test normal destroy operation
188 ASSERT_TRUE(session->close().isOk());
189 session.reset();
190}
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000191
Wei Wang05003412021-04-01 10:44:29 -0700192TEST_P(PowerAidl, createHintSessionFailed) {
193 std::shared_ptr<IPowerHintSession> session;
194 auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000195
196 // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK.
Wei Wang05003412021-04-01 10:44:29 -0700197 ASSERT_FALSE(status.isOk());
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000198
199 // If device not launching with Android 13 and beyond, check whether it's supported,
200 // if not, skip the test.
201 if (mApiLevel < kCompatibilityMatrix7ApiLevel && isUnknownOrUnsupported(status)) {
202 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700203 }
Wei Wang05003412021-04-01 10:44:29 -0700204 ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
205}
206
207TEST_P(PowerAidl, updateAndReportDurations) {
208 std::shared_ptr<IPowerHintSession> session;
209 auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session);
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000210 if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) {
Wei Wang9df909d2021-06-12 18:26:38 -0700211 EXPECT_TRUE(isUnknownOrUnsupported(status));
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000212 GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond.";
Wei Wang05003412021-04-01 10:44:29 -0700213 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000214 ASSERT_TRUE(status.isOk());
Wei Wang05003412021-04-01 10:44:29 -0700215 ASSERT_NE(nullptr, session);
216
217 ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk());
218 ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk());
219}
220
Dan Stozacca80272020-01-13 13:06:13 -0800221// FIXED_PERFORMANCE mode is required for all devices which ship on Android 11
222// or later
223TEST_P(PowerAidl, hasFixedPerformance) {
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000224 if (mApiLevel < kCompatibilityMatrix5ApiLevel) {
225 GTEST_SKIP() << "FIXED_PERFORMANCE mode is only required for all devices launching Android "
226 "11 or later.";
Dan Stozacca80272020-01-13 13:06:13 -0800227 }
Peiyong Lin3e0eb722022-10-17 19:55:20 +0000228 bool supported;
229 ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk());
230 ASSERT_TRUE(supported);
Dan Stozacca80272020-01-13 13:06:13 -0800231}
232
Dan Shiba4d5322020-07-28 13:09:30 -0700233GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl);
Wei Wang61c2a332020-01-08 16:51:47 -0800234INSTANTIATE_TEST_SUITE_P(Power, PowerAidl,
Wei Wang05003412021-04-01 10:44:29 -0700235 testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)),
236 ::android::PrintInstanceNameToString);
237
238} // namespace
Wei Wang61c2a332020-01-08 16:51:47 -0800239
240int main(int argc, char** argv) {
241 ::testing::InitGoogleTest(&argc, argv);
Wei Wang05003412021-04-01 10:44:29 -0700242 ABinderProcess_setThreadPoolMaxThreadCount(1);
243 ABinderProcess_startThreadPool();
Wei Wang61c2a332020-01-08 16:51:47 -0800244 return RUN_ALL_TESTS();
245}
Wei Wang05003412021-04-01 10:44:29 -0700246
247} // namespace aidl::android::hardware::power