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