Yifan Hong | 2200cff | 2021-10-28 12:18:35 -0700 | [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 | |
| 17 | #define LOG_TAG "health_aidl_hal_test" |
| 18 | |
| 19 | #include <chrono> |
| 20 | #include <memory> |
| 21 | #include <thread> |
| 22 | |
| 23 | #include <aidl/Gtest.h> |
| 24 | #include <aidl/Vintf.h> |
| 25 | #include <aidl/android/hardware/health/BnHealthInfoCallback.h> |
| 26 | #include <aidl/android/hardware/health/IHealth.h> |
| 27 | #include <android/binder_auto_utils.h> |
| 28 | #include <android/binder_enums.h> |
| 29 | #include <android/binder_interface_utils.h> |
| 30 | #include <android/binder_manager.h> |
| 31 | #include <android/binder_process.h> |
| 32 | #include <gmock/gmock.h> |
| 33 | #include <gtest/gtest.h> |
| 34 | #include <health-test/TestUtils.h> |
| 35 | |
| 36 | using android::getAidlHalInstanceNames; |
| 37 | using android::PrintInstanceNameToString; |
| 38 | using android::hardware::health::test_utils::SucceedOnce; |
| 39 | using ndk::enum_range; |
| 40 | using ndk::ScopedAStatus; |
| 41 | using ndk::SharedRefBase; |
| 42 | using ndk::SpAIBinder; |
| 43 | using testing::AllOf; |
| 44 | using testing::AnyOf; |
| 45 | using testing::AnyOfArray; |
| 46 | using testing::AssertionFailure; |
| 47 | using testing::AssertionResult; |
| 48 | using testing::AssertionSuccess; |
| 49 | using testing::Contains; |
| 50 | using testing::Each; |
| 51 | using testing::Eq; |
| 52 | using testing::ExplainMatchResult; |
| 53 | using testing::Ge; |
| 54 | using testing::Gt; |
| 55 | using testing::Le; |
| 56 | using testing::Lt; |
| 57 | using testing::Matcher; |
| 58 | using testing::Not; |
| 59 | using namespace std::string_literals; |
| 60 | using namespace std::chrono_literals; |
| 61 | |
| 62 | namespace aidl::android::hardware::health { |
| 63 | |
| 64 | static constexpr int32_t kFullChargeDesignCapMinUah = 100 * 1000; |
| 65 | static constexpr int32_t kFullChargeDesignCapMaxUah = 100 * 1000 * 1000; |
| 66 | |
| 67 | MATCHER(IsOk, "") { |
| 68 | *result_listener << "status is " << arg.getDescription(); |
| 69 | return arg.isOk(); |
| 70 | } |
| 71 | |
| 72 | MATCHER_P(ExceptionIs, exception_code, "") { |
| 73 | *result_listener << "status is " << arg.getDescription(); |
| 74 | return arg.getExceptionCode() == exception_code; |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | Matcher<T> InClosedRange(const T& lo, const T& hi) { |
| 79 | return AllOf(Ge(lo), Le(hi)); |
| 80 | } |
| 81 | |
| 82 | template <typename T> |
| 83 | Matcher<T> IsValidEnum() { |
| 84 | return AnyOfArray(enum_range<T>().begin(), enum_range<T>().end()); |
| 85 | } |
| 86 | |
| 87 | class HealthAidl : public testing::TestWithParam<std::string> { |
| 88 | public: |
| 89 | void SetUp() override { |
| 90 | SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 91 | health = IHealth::fromBinder(binder); |
| 92 | ASSERT_NE(health, nullptr); |
| 93 | } |
| 94 | std::shared_ptr<IHealth> health; |
| 95 | }; |
| 96 | |
| 97 | class Callback : public BnHealthInfoCallback { |
| 98 | public: |
| 99 | ScopedAStatus healthInfoChanged(const HealthInfo&) override { |
| 100 | { |
| 101 | std::lock_guard<std::mutex> lock(mutex_); |
| 102 | invoked_ = true; |
| 103 | } |
| 104 | invoked_notify_.notify_all(); |
| 105 | return ScopedAStatus::ok(); |
| 106 | } |
| 107 | template <typename R, typename P> |
| 108 | [[nodiscard]] bool waitInvoke(std::chrono::duration<R, P> duration) { |
| 109 | std::unique_lock<std::mutex> lock(mutex_); |
| 110 | bool r = invoked_notify_.wait_for(lock, duration, [this] { return this->invoked_; }); |
| 111 | invoked_ = false; |
| 112 | return r; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | std::mutex mutex_; |
| 117 | std::condition_variable invoked_notify_; |
| 118 | bool invoked_ = false; |
| 119 | }; |
| 120 | |
| 121 | TEST_P(HealthAidl, Callbacks) { |
| 122 | auto first_callback = SharedRefBase::make<Callback>(); |
| 123 | auto second_callback = SharedRefBase::make<Callback>(); |
| 124 | |
| 125 | ASSERT_THAT(health->registerCallback(first_callback), IsOk()); |
| 126 | ASSERT_THAT(health->registerCallback(second_callback), IsOk()); |
| 127 | |
| 128 | // registerCallback may or may not invoke the callback immediately, so the test needs |
| 129 | // to wait for the invocation. If the implementation chooses not to invoke the callback |
| 130 | // immediately, just wait for some time. |
| 131 | (void)first_callback->waitInvoke(200ms); |
| 132 | (void)second_callback->waitInvoke(200ms); |
| 133 | |
| 134 | // assert that the first callback is invoked when update is called. |
| 135 | ASSERT_THAT(health->update(), IsOk()); |
| 136 | |
| 137 | ASSERT_TRUE(first_callback->waitInvoke(1s)); |
| 138 | ASSERT_TRUE(second_callback->waitInvoke(1s)); |
| 139 | |
| 140 | ASSERT_THAT(health->unregisterCallback(first_callback), IsOk()); |
| 141 | |
| 142 | // clear any potentially pending callbacks result from wakealarm / kernel events |
| 143 | // If there is none, just wait for some time. |
| 144 | (void)first_callback->waitInvoke(200ms); |
| 145 | (void)second_callback->waitInvoke(200ms); |
| 146 | |
| 147 | // assert that the second callback is still invoked even though the first is unregistered. |
| 148 | ASSERT_THAT(health->update(), IsOk()); |
| 149 | |
| 150 | ASSERT_FALSE(first_callback->waitInvoke(200ms)); |
| 151 | ASSERT_TRUE(second_callback->waitInvoke(1s)); |
| 152 | |
| 153 | ASSERT_THAT(health->unregisterCallback(second_callback), IsOk()); |
| 154 | } |
| 155 | |
| 156 | TEST_P(HealthAidl, UnregisterNonExistentCallback) { |
| 157 | auto callback = SharedRefBase::make<Callback>(); |
| 158 | auto ret = health->unregisterCallback(callback); |
| 159 | ASSERT_THAT(ret, ExceptionIs(EX_ILLEGAL_ARGUMENT)); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Tests the values returned by getChargeCounterUah() from interface IHealth. |
| 164 | */ |
| 165 | TEST_P(HealthAidl, getChargeCounterUah) { |
| 166 | int32_t value; |
| 167 | auto status = health->getChargeCounterUah(&value); |
| 168 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 169 | if (!status.isOk()) return; |
| 170 | ASSERT_THAT(value, Ge(0)); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Tests the values returned by getCurrentNowMicroamps() from interface IHealth. |
| 175 | */ |
| 176 | TEST_P(HealthAidl, getCurrentNowMicroamps) { |
| 177 | int32_t value; |
| 178 | auto status = health->getCurrentNowMicroamps(&value); |
| 179 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 180 | if (!status.isOk()) return; |
| 181 | ASSERT_THAT(value, Not(INT32_MIN)); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Tests the values returned by getCurrentAverageMicroamps() from interface IHealth. |
| 186 | */ |
| 187 | TEST_P(HealthAidl, getCurrentAverageMicroamps) { |
| 188 | int32_t value; |
| 189 | auto status = health->getCurrentAverageMicroamps(&value); |
| 190 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 191 | if (!status.isOk()) return; |
| 192 | ASSERT_THAT(value, Not(INT32_MIN)); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Tests the values returned by getCapacity() from interface IHealth. |
| 197 | */ |
| 198 | TEST_P(HealthAidl, getCapacity) { |
| 199 | int32_t value; |
| 200 | auto status = health->getCapacity(&value); |
| 201 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 202 | if (!status.isOk()) return; |
| 203 | ASSERT_THAT(value, InClosedRange(0, 100)); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Tests the values returned by getEnergyCounterNwh() from interface IHealth. |
| 208 | */ |
| 209 | TEST_P(HealthAidl, getEnergyCounterNwh) { |
| 210 | int64_t value; |
| 211 | auto status = health->getEnergyCounterNwh(&value); |
| 212 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 213 | if (!status.isOk()) return; |
| 214 | ASSERT_THAT(value, Not(INT64_MIN)); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Tests the values returned by getChargeStatus() from interface IHealth. |
| 219 | */ |
| 220 | TEST_P(HealthAidl, getChargeStatus) { |
| 221 | BatteryStatus value; |
| 222 | auto status = health->getChargeStatus(&value); |
| 223 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 224 | if (!status.isOk()) return; |
| 225 | ASSERT_THAT(value, IsValidEnum<BatteryStatus>()); |
| 226 | } |
| 227 | |
Jack Wu | 3356161 | 2022-11-24 12:10:55 +0800 | [diff] [blame^] | 228 | /* |
| 229 | * Tests the values returned by getChargingPolicy() from interface IHealth. |
| 230 | */ |
| 231 | TEST_P(HealthAidl, getChargingPolicy) { |
| 232 | BatteryChargingPolicy value; |
| 233 | auto status = health->getChargingPolicy(&value); |
| 234 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 235 | if (!status.isOk()) return; |
| 236 | ASSERT_THAT(value, IsValidEnum<BatteryChargingPolicy>()); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Tests that setChargingPolicy() writes the value and compared the returned |
| 241 | * value by getChargingPolicy() from interface IHealth. |
| 242 | */ |
| 243 | TEST_P(HealthAidl, setChargingPolicy) { |
| 244 | BatteryChargingPolicy value; |
| 245 | |
| 246 | /* set ChargingPolicy*/ |
| 247 | auto status = health->setChargingPolicy(static_cast<BatteryChargingPolicy>(2)); // LONG_LIFE |
| 248 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 249 | if (!status.isOk()) return; |
| 250 | |
| 251 | /* get ChargingPolicy*/ |
| 252 | status = health->getChargingPolicy(&value); |
| 253 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 254 | if (!status.isOk()) return; |
| 255 | ASSERT_THAT(static_cast<int>(value), Eq(2)); |
| 256 | } |
| 257 | |
| 258 | MATCHER(IsValidHealthData, "") { |
| 259 | *result_listener << "value is " << arg.toString() << "."; |
| 260 | if (!ExplainMatchResult(Ge(-1), arg.batteryManufacturingDateSeconds, result_listener)) { |
| 261 | *result_listener << " for batteryManufacturingDateSeconds."; |
| 262 | return false; |
| 263 | } |
| 264 | if (!ExplainMatchResult(Ge(-1), arg.batteryFirstUsageSeconds, result_listener)) { |
| 265 | *result_listener << " for batteryFirstUsageSeconds."; |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Tests the values returned by getBatteryHealthData() from interface IHealth. |
| 274 | */ |
| 275 | TEST_P(HealthAidl, getBatteryHealthData) { |
| 276 | BatteryHealthData value; |
| 277 | auto status = health->getBatteryHealthData(&value); |
| 278 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 279 | if (!status.isOk()) return; |
| 280 | ASSERT_THAT(value, IsValidHealthData()); |
| 281 | } |
| 282 | |
Yifan Hong | 2200cff | 2021-10-28 12:18:35 -0700 | [diff] [blame] | 283 | MATCHER(IsValidStorageInfo, "") { |
| 284 | *result_listener << "value is " << arg.toString() << "."; |
| 285 | if (!ExplainMatchResult(InClosedRange(0, 3), arg.eol, result_listener)) { |
| 286 | *result_listener << " for eol."; |
| 287 | return false; |
| 288 | } |
| 289 | if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeA, result_listener)) { |
| 290 | *result_listener << " for lifetimeA."; |
| 291 | return false; |
| 292 | } |
| 293 | if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeB, result_listener)) { |
| 294 | *result_listener << " for lifetimeB."; |
| 295 | return false; |
| 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Tests the values returned by getStorageInfo() from interface IHealth. |
| 302 | */ |
| 303 | TEST_P(HealthAidl, getStorageInfo) { |
| 304 | std::vector<StorageInfo> value; |
| 305 | auto status = health->getStorageInfo(&value); |
| 306 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 307 | if (!status.isOk()) return; |
| 308 | ASSERT_THAT(value, Each(IsValidStorageInfo())); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Tests the values returned by getDiskStats() from interface IHealth. |
| 313 | */ |
| 314 | TEST_P(HealthAidl, getDiskStats) { |
| 315 | std::vector<DiskStats> value; |
| 316 | auto status = health->getDiskStats(&value); |
| 317 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 318 | } |
| 319 | |
| 320 | MATCHER(IsValidHealthInfo, "") { |
| 321 | *result_listener << "value is " << arg.toString() << "."; |
| 322 | if (!ExplainMatchResult(Each(IsValidStorageInfo()), arg.storageInfos, result_listener)) { |
| 323 | *result_listener << " for storageInfos."; |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | if (!ExplainMatchResult(Not(INT32_MIN), arg.batteryCurrentMicroamps, result_listener)) { |
| 328 | *result_listener << " for batteryCurrentMicroamps."; |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | if (!ExplainMatchResult(InClosedRange(0, 100), arg.batteryLevel, result_listener)) { |
| 333 | *result_listener << " for batteryLevel."; |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | if (!ExplainMatchResult(IsValidEnum<BatteryHealth>(), arg.batteryHealth, result_listener)) { |
| 338 | *result_listener << " for batteryHealth."; |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (!ExplainMatchResult(IsValidEnum<BatteryStatus>(), arg.batteryStatus, result_listener)) { |
| 343 | *result_listener << " for batteryStatus."; |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if (arg.batteryPresent) { |
| 348 | if (!ExplainMatchResult(Gt(0), arg.batteryChargeCounterUah, result_listener)) { |
| 349 | *result_listener << " for batteryChargeCounterUah when battery is present."; |
| 350 | return false; |
| 351 | } |
| 352 | if (!ExplainMatchResult(Not(BatteryStatus::UNKNOWN), arg.batteryStatus, result_listener)) { |
| 353 | *result_listener << " for batteryStatus when battery is present."; |
| 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!ExplainMatchResult(IsValidEnum<BatteryCapacityLevel>(), arg.batteryCapacityLevel, |
| 359 | result_listener)) { |
| 360 | *result_listener << " for batteryCapacityLevel."; |
| 361 | return false; |
| 362 | } |
| 363 | if (!ExplainMatchResult(Ge(-1), arg.batteryChargeTimeToFullNowSeconds, result_listener)) { |
| 364 | *result_listener << " for batteryChargeTimeToFullNowSeconds."; |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | if (!ExplainMatchResult( |
| 369 | AnyOf(Eq(0), AllOf(Gt(kFullChargeDesignCapMinUah), Lt(kFullChargeDesignCapMaxUah))), |
| 370 | arg.batteryFullChargeDesignCapacityUah, result_listener)) { |
| 371 | *result_listener << " for batteryFullChargeDesignCapacityUah. It should be greater than " |
| 372 | "100 mAh and less than 100,000 mAh, or 0 if unknown"; |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Tests the values returned by getHealthInfo() from interface IHealth. |
| 381 | */ |
| 382 | TEST_P(HealthAidl, getHealthInfo) { |
| 383 | HealthInfo value; |
| 384 | auto status = health->getHealthInfo(&value); |
| 385 | ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION))); |
| 386 | if (!status.isOk()) return; |
| 387 | ASSERT_THAT(value, IsValidHealthInfo()); |
| 388 | } |
| 389 | |
| 390 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HealthAidl); |
| 391 | INSTANTIATE_TEST_SUITE_P(Health, HealthAidl, |
| 392 | testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)), |
| 393 | PrintInstanceNameToString); |
| 394 | |
| 395 | // For battery current tests, value may not be stable if the battery current has fluctuated. |
| 396 | // Retry in a bit more time (with the following timeout) and consider the test successful if it |
| 397 | // has succeed once. |
| 398 | static constexpr auto gBatteryTestTimeout = 1min; |
| 399 | static constexpr double gCurrentCompareFactor = 0.50; |
| 400 | class BatteryTest : public HealthAidl {}; |
| 401 | |
| 402 | // Tuple for all IHealth::get* API return values. |
| 403 | template <typename T> |
| 404 | struct HalResult { |
| 405 | std::shared_ptr<ScopedAStatus> result = std::make_shared<ScopedAStatus>(); |
| 406 | T value; |
| 407 | }; |
| 408 | |
| 409 | // Needs to be called repeatedly within a period of time to ensure values are initialized. |
| 410 | static AssertionResult IsBatteryCurrentSignCorrect(const HalResult<BatteryStatus>& status, |
| 411 | const HalResult<int32_t>& current, |
| 412 | bool acceptZeroCurrentAsUnknown) { |
| 413 | // getChargeStatus / getCurrentNow / getCurrentAverage / getHealthInfo already tested above. |
| 414 | // Here, just skip if not ok. |
| 415 | if (!status.result->isOk()) { |
| 416 | return AssertionSuccess() << "getChargeStatus / getHealthInfo returned " |
| 417 | << status.result->getDescription() << ", skipping"; |
| 418 | } |
| 419 | |
| 420 | if (!current.result->isOk()) { |
| 421 | return AssertionSuccess() << "getCurrentNow / getCurrentAverage returned " |
| 422 | << current.result->getDescription() << ", skipping"; |
| 423 | } |
| 424 | |
| 425 | return ::android::hardware::health::test_utils::IsBatteryCurrentSignCorrect( |
| 426 | status.value, current.value, acceptZeroCurrentAsUnknown, |
| 427 | [](BatteryStatus status) { return toString(status); }); |
| 428 | } |
| 429 | |
| 430 | static AssertionResult IsBatteryCurrentSimilar(const HalResult<BatteryStatus>& status, |
| 431 | const HalResult<int32_t>& current_now, |
| 432 | const HalResult<int32_t>& current_average) { |
| 433 | if (status.result->isOk() && status.value == BatteryStatus::FULL) { |
| 434 | // No reason to test on full battery because battery current load fluctuates. |
| 435 | return AssertionSuccess() << "Battery is full, skipping"; |
| 436 | } |
| 437 | |
| 438 | // getCurrentNow / getCurrentAverage / getHealthInfo already tested above. Here, just skip if |
| 439 | // not SUCCESS or value 0. |
| 440 | if (!current_now.result->isOk() || current_now.value == 0) { |
| 441 | return AssertionSuccess() << "getCurrentNow returned " |
| 442 | << current_now.result->getDescription() << " with value " |
| 443 | << current_now.value << ", skipping"; |
| 444 | } |
| 445 | |
| 446 | if (!current_average.result->isOk() || current_average.value == 0) { |
| 447 | return AssertionSuccess() << "getCurrentAverage returned " |
| 448 | << current_average.result->getDescription() << " with value " |
| 449 | << current_average.value << ", skipping"; |
| 450 | } |
| 451 | |
| 452 | return ::android::hardware::health::test_utils::IsBatteryCurrentSimilar( |
| 453 | current_now.value, current_average.value, gCurrentCompareFactor); |
| 454 | } |
| 455 | |
| 456 | TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusInHealthInfo) { |
| 457 | auto testOnce = [&]() -> AssertionResult { |
| 458 | HalResult<HealthInfo> health_info; |
| 459 | *health_info.result = health->getHealthInfo(&health_info.value); |
| 460 | |
| 461 | return IsBatteryCurrentSignCorrect( |
| 462 | {health_info.result, health_info.value.batteryStatus}, |
| 463 | {health_info.result, health_info.value.batteryCurrentMicroamps}, |
| 464 | true /* accept zero current as unknown */); |
| 465 | }; |
| 466 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 467 | << "You may want to try again later when current_now becomes stable."; |
| 468 | } |
| 469 | |
| 470 | TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusInHealthInfo) { |
| 471 | auto testOnce = [&]() -> AssertionResult { |
| 472 | HalResult<HealthInfo> health_info; |
| 473 | *health_info.result = health->getHealthInfo(&health_info.value); |
| 474 | return IsBatteryCurrentSignCorrect( |
| 475 | {health_info.result, health_info.value.batteryStatus}, |
| 476 | {health_info.result, health_info.value.batteryCurrentAverageMicroamps}, |
| 477 | true /* accept zero current as unknown */); |
| 478 | }; |
| 479 | |
| 480 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 481 | << "You may want to try again later when current_average becomes stable."; |
| 482 | } |
| 483 | |
| 484 | TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentInHealthInfo) { |
| 485 | auto testOnce = [&]() -> AssertionResult { |
| 486 | HalResult<HealthInfo> health_info; |
| 487 | *health_info.result = health->getHealthInfo(&health_info.value); |
| 488 | return IsBatteryCurrentSimilar( |
| 489 | {health_info.result, health_info.value.batteryStatus}, |
| 490 | {health_info.result, health_info.value.batteryCurrentMicroamps}, |
| 491 | {health_info.result, health_info.value.batteryCurrentAverageMicroamps}); |
| 492 | }; |
| 493 | |
| 494 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 495 | << "You may want to try again later when current_now and current_average becomes " |
| 496 | "stable."; |
| 497 | } |
| 498 | |
| 499 | TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusFromHal) { |
| 500 | auto testOnce = [&]() -> AssertionResult { |
| 501 | HalResult<BatteryStatus> status; |
| 502 | *status.result = health->getChargeStatus(&status.value); |
| 503 | HalResult<int32_t> current_now; |
| 504 | *current_now.result = health->getCurrentNowMicroamps(¤t_now.value); |
| 505 | return IsBatteryCurrentSignCorrect(status, current_now, |
| 506 | false /* accept zero current as unknown */); |
| 507 | }; |
| 508 | |
| 509 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 510 | << "You may want to try again later when current_now becomes stable."; |
| 511 | } |
| 512 | |
| 513 | TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusFromHal) { |
| 514 | auto testOnce = [&]() -> AssertionResult { |
| 515 | HalResult<BatteryStatus> status; |
| 516 | *status.result = health->getChargeStatus(&status.value); |
| 517 | HalResult<int32_t> current_average; |
| 518 | *current_average.result = health->getCurrentAverageMicroamps(¤t_average.value); |
| 519 | return IsBatteryCurrentSignCorrect(status, current_average, |
| 520 | false /* accept zero current as unknown */); |
| 521 | }; |
| 522 | |
| 523 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 524 | << "You may want to try again later when current_average becomes stable."; |
| 525 | } |
| 526 | |
| 527 | TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentFromHal) { |
| 528 | auto testOnce = [&]() -> AssertionResult { |
| 529 | HalResult<BatteryStatus> status; |
| 530 | *status.result = health->getChargeStatus(&status.value); |
| 531 | HalResult<int32_t> current_now; |
| 532 | *current_now.result = health->getCurrentNowMicroamps(¤t_now.value); |
| 533 | HalResult<int32_t> current_average; |
| 534 | *current_average.result = health->getCurrentAverageMicroamps(¤t_average.value); |
| 535 | return IsBatteryCurrentSimilar(status, current_now, current_average); |
| 536 | }; |
| 537 | |
| 538 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 539 | << "You may want to try again later when current_average becomes stable."; |
| 540 | } |
| 541 | |
| 542 | AssertionResult IsBatteryStatusCorrect(const HalResult<BatteryStatus>& status, |
| 543 | const HalResult<HealthInfo>& health_info) { |
| 544 | // getChargetStatus / getHealthInfo is already tested above. Here, just skip if not ok. |
| 545 | if (!health_info.result->isOk()) { |
| 546 | return AssertionSuccess() << "getHealthInfo returned " |
| 547 | << health_info.result->getDescription() << ", skipping"; |
| 548 | } |
| 549 | if (!status.result->isOk()) { |
| 550 | return AssertionSuccess() << "getChargeStatus returned " << status.result->getDescription() |
| 551 | << ", skipping"; |
| 552 | } |
| 553 | return ::android::hardware::health::test_utils::IsBatteryStatusCorrect( |
| 554 | status.value, health_info.value, [](BatteryStatus status) { return toString(status); }); |
| 555 | } |
| 556 | |
| 557 | TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) { |
| 558 | auto testOnce = [&]() -> AssertionResult { |
| 559 | HalResult<BatteryStatus> status; |
| 560 | *status.result = health->getChargeStatus(&status.value); |
| 561 | HalResult<HealthInfo> health_info; |
| 562 | *health_info.result = health->getHealthInfo(&health_info.value); |
| 563 | return IsBatteryStatusCorrect(status, health_info); |
| 564 | }; |
| 565 | |
| 566 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 567 | << "You may want to try again later when battery_status becomes stable."; |
| 568 | } |
| 569 | |
| 570 | TEST_P(BatteryTest, ConnectedAgainstStatusInHealthInfo) { |
| 571 | auto testOnce = [&]() -> AssertionResult { |
| 572 | HalResult<HealthInfo> health_info; |
| 573 | *health_info.result = health->getHealthInfo(&health_info.value); |
| 574 | return IsBatteryStatusCorrect({health_info.result, health_info.value.batteryStatus}, |
| 575 | health_info); |
| 576 | }; |
| 577 | |
| 578 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 579 | << "You may want to try again later when getHealthInfo becomes stable."; |
| 580 | } |
| 581 | |
| 582 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BatteryTest); |
| 583 | INSTANTIATE_TEST_SUITE_P(Health, BatteryTest, |
| 584 | testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)), |
| 585 | PrintInstanceNameToString); |
| 586 | |
| 587 | } // namespace aidl::android::hardware::health |
| 588 | |
| 589 | int main(int argc, char** argv) { |
| 590 | ::testing::InitGoogleTest(&argc, argv); |
| 591 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 592 | ABinderProcess_startThreadPool(); |
| 593 | return RUN_ALL_TESTS(); |
| 594 | } |