Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 1 | /* |
| 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 Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/BnPower.h> |
| 20 | #include <aidl/android/hardware/power/BnPowerHintSession.h> |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 22 | #include <android/binder_ibinder.h> |
| 23 | #include <android/binder_manager.h> |
| 24 | #include <android/binder_process.h> |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 25 | #include <android/binder_status.h> |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 26 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 28 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 29 | namespace aidl::android::hardware::power { |
| 30 | namespace { |
| 31 | |
| 32 | using ::android::base::GetUintProperty; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 33 | using android::hardware::power::Boost; |
| 34 | using android::hardware::power::IPower; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 35 | using android::hardware::power::IPowerHintSession; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 36 | using android::hardware::power::Mode; |
Matt Buckley | 1384388 | 2022-09-15 22:32:56 +0000 | [diff] [blame] | 37 | using android::hardware::power::SessionHint; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 38 | using android::hardware::power::WorkDuration; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 39 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 40 | const std::vector<Boost> kBoosts{ndk::enum_range<Boost>().begin(), ndk::enum_range<Boost>().end()}; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 41 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 42 | const std::vector<Mode> kModes{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()}; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 43 | |
Matt Buckley | 1384388 | 2022-09-15 22:32:56 +0000 | [diff] [blame] | 44 | const std::vector<SessionHint> kSessionHints{ndk::enum_range<SessionHint>().begin(), |
| 45 | ndk::enum_range<SessionHint>().end()}; |
| 46 | |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 47 | const 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 | |
| 52 | const 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 Buckley | 1384388 | 2022-09-15 22:32:56 +0000 | [diff] [blame] | 57 | const 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 Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 62 | class DurationWrapper : public WorkDuration { |
| 63 | public: |
| 64 | DurationWrapper(int64_t dur, int64_t time) { |
| 65 | durationNanos = dur; |
| 66 | timeStampNanos = time; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | const std::vector<int32_t> kSelfTids = { |
| 71 | gettid(), |
| 72 | }; |
| 73 | |
| 74 | const std::vector<int32_t> kEmptyTids = {}; |
| 75 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 76 | const std::vector<WorkDuration> kDurationsWithZero = { |
| 77 | DurationWrapper(1000L, 1L), |
| 78 | DurationWrapper(0L, 2L), |
| 79 | }; |
| 80 | |
| 81 | const std::vector<WorkDuration> kDurationsWithNegative = { |
| 82 | DurationWrapper(1000L, 1L), |
| 83 | DurationWrapper(-1000L, 2L), |
| 84 | }; |
| 85 | |
| 86 | const std::vector<WorkDuration> kDurations = { |
| 87 | DurationWrapper(1L, 1L), |
| 88 | DurationWrapper(1000L, 2L), |
| 89 | DurationWrapper(1000000L, 3L), |
| 90 | DurationWrapper(1000000000L, 4L), |
| 91 | }; |
| 92 | |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 93 | // DEVICEs launching with Android 11 MUST meet the requirements for the |
| 94 | // target-level=5 compatibility_matrix file. |
| 95 | const uint64_t kCompatibilityMatrix5ApiLevel = 30; |
| 96 | |
| 97 | // DEVICEs launching with Android 13 MUST meet the requirements for the |
| 98 | // target-level=7 compatibility_matrix file. |
| 99 | const uint64_t kCompatibilityMatrix7ApiLevel = 33; |
| 100 | |
Peiyong Lin | c785459 | 2022-10-13 00:10:31 +0000 | [diff] [blame] | 101 | // DEVICEs launching with Android 14 MUST meet the requirements for the |
| 102 | // target-level=8 compatibility_matrix file. |
| 103 | const uint64_t kCompatibilityMatrix8ApiLevel = 34; |
| 104 | |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 105 | inline bool isUnknownOrUnsupported(const ndk::ScopedAStatus& status) { |
| 106 | return status.getStatus() == STATUS_UNKNOWN_TRANSACTION || |
| 107 | status.getExceptionCode() == EX_UNSUPPORTED_OPERATION; |
| 108 | } |
| 109 | |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 110 | class PowerAidl : public testing::TestWithParam<std::string> { |
| 111 | public: |
| 112 | virtual void SetUp() override { |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 113 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 114 | ASSERT_NE(binder, nullptr); |
| 115 | power = IPower::fromBinder(ndk::SpAIBinder(binder)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 116 | |
| 117 | mApiLevel = GetUintProperty<uint64_t>("ro.vendor.api_level", 0); |
| 118 | ASSERT_NE(mApiLevel, 0); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 121 | std::shared_ptr<IPower> power; |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 122 | uint64_t mApiLevel; |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | TEST_P(PowerAidl, setMode) { |
| 126 | for (const auto& mode : kModes) { |
| 127 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 128 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 129 | } |
| 130 | for (const auto& mode : kInvalidModes) { |
| 131 | ASSERT_TRUE(power->setMode(mode, true).isOk()); |
| 132 | ASSERT_TRUE(power->setMode(mode, false).isOk()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | TEST_P(PowerAidl, isModeSupported) { |
| 137 | for (const auto& mode : kModes) { |
| 138 | bool supported; |
| 139 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
| 140 | } |
| 141 | for (const auto& mode : kInvalidModes) { |
| 142 | bool supported; |
| 143 | ASSERT_TRUE(power->isModeSupported(mode, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 144 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 145 | ASSERT_FALSE(supported); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | TEST_P(PowerAidl, setBoost) { |
| 150 | for (const auto& boost : kBoosts) { |
| 151 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 152 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 153 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 154 | } |
| 155 | for (const auto& boost : kInvalidBoosts) { |
| 156 | ASSERT_TRUE(power->setBoost(boost, 0).isOk()); |
| 157 | ASSERT_TRUE(power->setBoost(boost, 1000).isOk()); |
| 158 | ASSERT_TRUE(power->setBoost(boost, -1).isOk()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | TEST_P(PowerAidl, isBoostSupported) { |
| 163 | for (const auto& boost : kBoosts) { |
| 164 | bool supported; |
| 165 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
| 166 | } |
| 167 | for (const auto& boost : kInvalidBoosts) { |
| 168 | bool supported; |
| 169 | ASSERT_TRUE(power->isBoostSupported(boost, &supported).isOk()); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 170 | // Should return false for values outside enum |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 171 | ASSERT_FALSE(supported); |
| 172 | } |
| 173 | } |
| 174 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 175 | TEST_P(PowerAidl, getHintSessionPreferredRate) { |
| 176 | int64_t rate = -1; |
| 177 | auto status = power->getHintSessionPreferredRate(&rate); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 178 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 179 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 180 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 181 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 182 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 183 | // At least 1ms rate limit from HAL |
| 184 | ASSERT_GE(rate, 1000000); |
| 185 | } |
| 186 | |
| 187 | TEST_P(PowerAidl, createAndCloseHintSession) { |
| 188 | std::shared_ptr<IPowerHintSession> session; |
| 189 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 190 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 191 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 192 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 193 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 194 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 195 | ASSERT_NE(nullptr, session); |
| 196 | ASSERT_TRUE(session->pause().isOk()); |
| 197 | ASSERT_TRUE(session->resume().isOk()); |
| 198 | // Test normal destroy operation |
| 199 | ASSERT_TRUE(session->close().isOk()); |
| 200 | session.reset(); |
| 201 | } |
Matt Buckley | 1384388 | 2022-09-15 22:32:56 +0000 | [diff] [blame] | 202 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 203 | TEST_P(PowerAidl, createHintSessionFailed) { |
| 204 | std::shared_ptr<IPowerHintSession> session; |
| 205 | auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 206 | |
| 207 | // Regardless of whether V2 and beyond is supported, the status is always not STATUS_OK. |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 208 | ASSERT_FALSE(status.isOk()); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 209 | |
| 210 | // If device not launching with Android 13 and beyond, check whether it's supported, |
| 211 | // if not, skip the test. |
| 212 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && isUnknownOrUnsupported(status)) { |
| 213 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 214 | } |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 215 | ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode()); |
| 216 | } |
| 217 | |
| 218 | TEST_P(PowerAidl, updateAndReportDurations) { |
| 219 | std::shared_ptr<IPowerHintSession> session; |
| 220 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 221 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
Wei Wang | 9df909d | 2021-06-12 18:26:38 -0700 | [diff] [blame] | 222 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 223 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 224 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 225 | ASSERT_TRUE(status.isOk()); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 226 | ASSERT_NE(nullptr, session); |
| 227 | |
| 228 | ASSERT_TRUE(session->updateTargetWorkDuration(16666667LL).isOk()); |
| 229 | ASSERT_TRUE(session->reportActualWorkDuration(kDurations).isOk()); |
| 230 | } |
| 231 | |
Matt Buckley | 1384388 | 2022-09-15 22:32:56 +0000 | [diff] [blame] | 232 | TEST_P(PowerAidl, sendSessionHint) { |
| 233 | std::shared_ptr<IPowerHintSession> session; |
| 234 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
| 235 | if (!status.isOk()) { |
| 236 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
| 237 | return; |
| 238 | } |
| 239 | for (const auto& sessionHint : kSessionHints) { |
| 240 | ASSERT_TRUE(session->sendHint(sessionHint).isOk()); |
| 241 | } |
| 242 | for (const auto& sessionHint : kInvalidSessionHints) { |
| 243 | ASSERT_TRUE(session->sendHint(sessionHint).isOk()); |
| 244 | } |
| 245 | } |
| 246 | |
Peiyong Lin | c785459 | 2022-10-13 00:10:31 +0000 | [diff] [blame] | 247 | TEST_P(PowerAidl, setThreads) { |
| 248 | std::shared_ptr<IPowerHintSession> session; |
| 249 | auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &session); |
| 250 | if (mApiLevel < kCompatibilityMatrix7ApiLevel && !status.isOk()) { |
| 251 | EXPECT_TRUE(isUnknownOrUnsupported(status)); |
| 252 | GTEST_SKIP() << "DEVICE not launching with Android 13 and beyond."; |
| 253 | } |
| 254 | ASSERT_TRUE(status.isOk()); |
| 255 | |
Peiyong Lin | 9fb7553 | 2023-06-14 18:12:39 +0000 | [diff] [blame] | 256 | status = session->setThreads(kEmptyTids); |
| 257 | if (mApiLevel < kCompatibilityMatrix8ApiLevel && isUnknownOrUnsupported(status)) { |
Peiyong Lin | c785459 | 2022-10-13 00:10:31 +0000 | [diff] [blame] | 258 | GTEST_SKIP() << "DEVICE not launching with Android 14 and beyond."; |
| 259 | } |
Peiyong Lin | c785459 | 2022-10-13 00:10:31 +0000 | [diff] [blame] | 260 | ASSERT_FALSE(status.isOk()); |
| 261 | ASSERT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode()); |
| 262 | |
| 263 | status = session->setThreads(kSelfTids); |
| 264 | ASSERT_TRUE(status.isOk()); |
| 265 | } |
| 266 | |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 267 | // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11 |
| 268 | // or later |
| 269 | TEST_P(PowerAidl, hasFixedPerformance) { |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 270 | if (mApiLevel < kCompatibilityMatrix5ApiLevel) { |
| 271 | GTEST_SKIP() << "FIXED_PERFORMANCE mode is only required for all devices launching Android " |
| 272 | "11 or later."; |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 273 | } |
Peiyong Lin | 3e0eb72 | 2022-10-17 19:55:20 +0000 | [diff] [blame] | 274 | bool supported; |
| 275 | ASSERT_TRUE(power->isModeSupported(Mode::FIXED_PERFORMANCE, &supported).isOk()); |
| 276 | ASSERT_TRUE(supported); |
Dan Stoza | cca8027 | 2020-01-13 13:06:13 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 279 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerAidl); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 280 | INSTANTIATE_TEST_SUITE_P(Power, PowerAidl, |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 281 | testing::ValuesIn(::android::getAidlHalInstanceNames(IPower::descriptor)), |
| 282 | ::android::PrintInstanceNameToString); |
| 283 | |
| 284 | } // namespace |
Xiang Wang | dd0edc6 | 2023-02-08 16:47:06 -0800 | [diff] [blame] | 285 | } // namespace aidl::android::hardware::power |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 286 | |
| 287 | int main(int argc, char** argv) { |
| 288 | ::testing::InitGoogleTest(&argc, argv); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 289 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 290 | ABinderProcess_startThreadPool(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 291 | return RUN_ALL_TESTS(); |
| 292 | } |