blob: 783ce11d694dd713639e61813ede90606f216f88 [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
87class 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
97class 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
121TEST_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
156TEST_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 */
165TEST_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 */
176TEST_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 */
187TEST_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 */
198TEST_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 */
209TEST_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 */
220TEST_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 Wu33561612022-11-24 12:10:55 +0800228/*
229 * Tests the values returned by getChargingPolicy() from interface IHealth.
230 */
231TEST_P(HealthAidl, getChargingPolicy) {
Jack Wu9beec7e2023-02-02 11:27:58 +0800232 int32_t version = 0;
233 auto status = health->getInterfaceVersion(&version);
234 ASSERT_TRUE(status.isOk()) << status;
235 if (version < 2) {
236 GTEST_SKIP() << "Support in health hal v2 for EU Ecodesign";
237 }
Jack Wu33561612022-11-24 12:10:55 +0800238 BatteryChargingPolicy value;
Jack Wu9beec7e2023-02-02 11:27:58 +0800239 status = health->getChargingPolicy(&value);
Jack Wu33561612022-11-24 12:10:55 +0800240 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
241 if (!status.isOk()) return;
242 ASSERT_THAT(value, IsValidEnum<BatteryChargingPolicy>());
243}
244
245/*
246 * Tests that setChargingPolicy() writes the value and compared the returned
247 * value by getChargingPolicy() from interface IHealth.
248 */
249TEST_P(HealthAidl, setChargingPolicy) {
Jack Wu9beec7e2023-02-02 11:27:58 +0800250 int32_t version = 0;
251 auto status = health->getInterfaceVersion(&version);
252 ASSERT_TRUE(status.isOk()) << status;
253 if (version < 2) {
254 GTEST_SKIP() << "Support in health hal v2 for EU Ecodesign";
255 }
256
Jack Wu33561612022-11-24 12:10:55 +0800257 BatteryChargingPolicy value;
258
259 /* set ChargingPolicy*/
Jack Wu2d6557c2023-03-21 20:17:01 +0800260 status = health->setChargingPolicy(BatteryChargingPolicy::LONG_LIFE);
Jack Wu33561612022-11-24 12:10:55 +0800261 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
262 if (!status.isOk()) return;
263
264 /* get ChargingPolicy*/
265 status = health->getChargingPolicy(&value);
266 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
267 if (!status.isOk()) return;
Jack Wu2d6557c2023-03-21 20:17:01 +0800268 // the result of getChargingPolicy will be one of default(1), ADAPTIVE_AON(2)
269 // ADAPTIVE_AC(3) or LONG_LIFE(4). default(1) means NOT_SUPPORT
270 ASSERT_THAT(static_cast<int>(value), AnyOf(Eq(1), Eq(4)));
Jack Wu33561612022-11-24 12:10:55 +0800271}
272
273MATCHER(IsValidHealthData, "") {
274 *result_listener << "value is " << arg.toString() << ".";
275 if (!ExplainMatchResult(Ge(-1), arg.batteryManufacturingDateSeconds, result_listener)) {
276 *result_listener << " for batteryManufacturingDateSeconds.";
277 return false;
278 }
279 if (!ExplainMatchResult(Ge(-1), arg.batteryFirstUsageSeconds, result_listener)) {
280 *result_listener << " for batteryFirstUsageSeconds.";
281 return false;
282 }
AleX Pelosi39c56412023-02-17 00:15:59 +0000283 if (!ExplainMatchResult(Ge(-1), arg.batteryStateOfHealth, result_listener)) {
284 *result_listener << " for batteryStateOfHealth.";
285 return false;
286 }
Jack Wu33561612022-11-24 12:10:55 +0800287
288 return true;
289}
290
291/*
292 * Tests the values returned by getBatteryHealthData() from interface IHealth.
293 */
294TEST_P(HealthAidl, getBatteryHealthData) {
Jack Wu9beec7e2023-02-02 11:27:58 +0800295 int32_t version = 0;
296 auto status = health->getInterfaceVersion(&version);
297 ASSERT_TRUE(status.isOk()) << status;
298 if (version < 2) {
299 GTEST_SKIP() << "Support in health hal v2 for EU Ecodesign";
300 }
301
Jack Wu33561612022-11-24 12:10:55 +0800302 BatteryHealthData value;
Jack Wu9beec7e2023-02-02 11:27:58 +0800303 status = health->getBatteryHealthData(&value);
Jack Wu33561612022-11-24 12:10:55 +0800304 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
305 if (!status.isOk()) return;
306 ASSERT_THAT(value, IsValidHealthData());
307}
308
Yifan Hong2200cff2021-10-28 12:18:35 -0700309MATCHER(IsValidStorageInfo, "") {
310 *result_listener << "value is " << arg.toString() << ".";
311 if (!ExplainMatchResult(InClosedRange(0, 3), arg.eol, result_listener)) {
312 *result_listener << " for eol.";
313 return false;
314 }
315 if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeA, result_listener)) {
316 *result_listener << " for lifetimeA.";
317 return false;
318 }
319 if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeB, result_listener)) {
320 *result_listener << " for lifetimeB.";
321 return false;
322 }
323 return true;
324}
325
326/*
327 * Tests the values returned by getStorageInfo() from interface IHealth.
328 */
329TEST_P(HealthAidl, getStorageInfo) {
330 std::vector<StorageInfo> value;
331 auto status = health->getStorageInfo(&value);
332 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
333 if (!status.isOk()) return;
334 ASSERT_THAT(value, Each(IsValidStorageInfo()));
335}
336
337/*
338 * Tests the values returned by getDiskStats() from interface IHealth.
339 */
340TEST_P(HealthAidl, getDiskStats) {
341 std::vector<DiskStats> value;
342 auto status = health->getDiskStats(&value);
343 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
344}
345
346MATCHER(IsValidHealthInfo, "") {
347 *result_listener << "value is " << arg.toString() << ".";
348 if (!ExplainMatchResult(Each(IsValidStorageInfo()), arg.storageInfos, result_listener)) {
349 *result_listener << " for storageInfos.";
350 return false;
351 }
352
353 if (!ExplainMatchResult(Not(INT32_MIN), arg.batteryCurrentMicroamps, result_listener)) {
354 *result_listener << " for batteryCurrentMicroamps.";
355 return false;
356 }
357
358 if (!ExplainMatchResult(InClosedRange(0, 100), arg.batteryLevel, result_listener)) {
359 *result_listener << " for batteryLevel.";
360 return false;
361 }
362
363 if (!ExplainMatchResult(IsValidEnum<BatteryHealth>(), arg.batteryHealth, result_listener)) {
364 *result_listener << " for batteryHealth.";
365 return false;
366 }
367
368 if (!ExplainMatchResult(IsValidEnum<BatteryStatus>(), arg.batteryStatus, result_listener)) {
369 *result_listener << " for batteryStatus.";
370 return false;
371 }
372
373 if (arg.batteryPresent) {
374 if (!ExplainMatchResult(Gt(0), arg.batteryChargeCounterUah, result_listener)) {
375 *result_listener << " for batteryChargeCounterUah when battery is present.";
376 return false;
377 }
378 if (!ExplainMatchResult(Not(BatteryStatus::UNKNOWN), arg.batteryStatus, result_listener)) {
379 *result_listener << " for batteryStatus when battery is present.";
380 return false;
381 }
382 }
383
384 if (!ExplainMatchResult(IsValidEnum<BatteryCapacityLevel>(), arg.batteryCapacityLevel,
385 result_listener)) {
386 *result_listener << " for batteryCapacityLevel.";
387 return false;
388 }
389 if (!ExplainMatchResult(Ge(-1), arg.batteryChargeTimeToFullNowSeconds, result_listener)) {
390 *result_listener << " for batteryChargeTimeToFullNowSeconds.";
391 return false;
392 }
393
394 if (!ExplainMatchResult(
395 AnyOf(Eq(0), AllOf(Gt(kFullChargeDesignCapMinUah), Lt(kFullChargeDesignCapMaxUah))),
396 arg.batteryFullChargeDesignCapacityUah, result_listener)) {
397 *result_listener << " for batteryFullChargeDesignCapacityUah. It should be greater than "
398 "100 mAh and less than 100,000 mAh, or 0 if unknown";
399 return false;
400 }
401
402 return true;
403}
404
405/*
406 * Tests the values returned by getHealthInfo() from interface IHealth.
407 */
408TEST_P(HealthAidl, getHealthInfo) {
409 HealthInfo value;
410 auto status = health->getHealthInfo(&value);
411 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
412 if (!status.isOk()) return;
413 ASSERT_THAT(value, IsValidHealthInfo());
414}
415
416GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HealthAidl);
417INSTANTIATE_TEST_SUITE_P(Health, HealthAidl,
418 testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
419 PrintInstanceNameToString);
420
421// For battery current tests, value may not be stable if the battery current has fluctuated.
422// Retry in a bit more time (with the following timeout) and consider the test successful if it
423// has succeed once.
424static constexpr auto gBatteryTestTimeout = 1min;
425static constexpr double gCurrentCompareFactor = 0.50;
426class BatteryTest : public HealthAidl {};
427
428// Tuple for all IHealth::get* API return values.
429template <typename T>
430struct HalResult {
431 std::shared_ptr<ScopedAStatus> result = std::make_shared<ScopedAStatus>();
432 T value;
433};
434
435// Needs to be called repeatedly within a period of time to ensure values are initialized.
436static AssertionResult IsBatteryCurrentSignCorrect(const HalResult<BatteryStatus>& status,
437 const HalResult<int32_t>& current,
438 bool acceptZeroCurrentAsUnknown) {
439 // getChargeStatus / getCurrentNow / getCurrentAverage / getHealthInfo already tested above.
440 // Here, just skip if not ok.
441 if (!status.result->isOk()) {
442 return AssertionSuccess() << "getChargeStatus / getHealthInfo returned "
443 << status.result->getDescription() << ", skipping";
444 }
445
446 if (!current.result->isOk()) {
447 return AssertionSuccess() << "getCurrentNow / getCurrentAverage returned "
448 << current.result->getDescription() << ", skipping";
449 }
450
451 return ::android::hardware::health::test_utils::IsBatteryCurrentSignCorrect(
452 status.value, current.value, acceptZeroCurrentAsUnknown,
453 [](BatteryStatus status) { return toString(status); });
454}
455
456static AssertionResult IsBatteryCurrentSimilar(const HalResult<BatteryStatus>& status,
457 const HalResult<int32_t>& current_now,
458 const HalResult<int32_t>& current_average) {
459 if (status.result->isOk() && status.value == BatteryStatus::FULL) {
460 // No reason to test on full battery because battery current load fluctuates.
461 return AssertionSuccess() << "Battery is full, skipping";
462 }
463
464 // getCurrentNow / getCurrentAverage / getHealthInfo already tested above. Here, just skip if
465 // not SUCCESS or value 0.
466 if (!current_now.result->isOk() || current_now.value == 0) {
467 return AssertionSuccess() << "getCurrentNow returned "
468 << current_now.result->getDescription() << " with value "
469 << current_now.value << ", skipping";
470 }
471
472 if (!current_average.result->isOk() || current_average.value == 0) {
473 return AssertionSuccess() << "getCurrentAverage returned "
474 << current_average.result->getDescription() << " with value "
475 << current_average.value << ", skipping";
476 }
477
478 return ::android::hardware::health::test_utils::IsBatteryCurrentSimilar(
479 current_now.value, current_average.value, gCurrentCompareFactor);
480}
481
482TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusInHealthInfo) {
483 auto testOnce = [&]() -> AssertionResult {
484 HalResult<HealthInfo> health_info;
485 *health_info.result = health->getHealthInfo(&health_info.value);
486
487 return IsBatteryCurrentSignCorrect(
488 {health_info.result, health_info.value.batteryStatus},
489 {health_info.result, health_info.value.batteryCurrentMicroamps},
490 true /* accept zero current as unknown */);
491 };
492 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
493 << "You may want to try again later when current_now becomes stable.";
494}
495
496TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusInHealthInfo) {
497 auto testOnce = [&]() -> AssertionResult {
498 HalResult<HealthInfo> health_info;
499 *health_info.result = health->getHealthInfo(&health_info.value);
500 return IsBatteryCurrentSignCorrect(
501 {health_info.result, health_info.value.batteryStatus},
502 {health_info.result, health_info.value.batteryCurrentAverageMicroamps},
503 true /* accept zero current as unknown */);
504 };
505
506 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
507 << "You may want to try again later when current_average becomes stable.";
508}
509
510TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentInHealthInfo) {
511 auto testOnce = [&]() -> AssertionResult {
512 HalResult<HealthInfo> health_info;
513 *health_info.result = health->getHealthInfo(&health_info.value);
514 return IsBatteryCurrentSimilar(
515 {health_info.result, health_info.value.batteryStatus},
516 {health_info.result, health_info.value.batteryCurrentMicroamps},
517 {health_info.result, health_info.value.batteryCurrentAverageMicroamps});
518 };
519
520 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
521 << "You may want to try again later when current_now and current_average becomes "
522 "stable.";
523}
524
525TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusFromHal) {
526 auto testOnce = [&]() -> AssertionResult {
527 HalResult<BatteryStatus> status;
528 *status.result = health->getChargeStatus(&status.value);
529 HalResult<int32_t> current_now;
530 *current_now.result = health->getCurrentNowMicroamps(&current_now.value);
531 return IsBatteryCurrentSignCorrect(status, current_now,
532 false /* accept zero current as unknown */);
533 };
534
535 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
536 << "You may want to try again later when current_now becomes stable.";
537}
538
539TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusFromHal) {
540 auto testOnce = [&]() -> AssertionResult {
541 HalResult<BatteryStatus> status;
542 *status.result = health->getChargeStatus(&status.value);
543 HalResult<int32_t> current_average;
544 *current_average.result = health->getCurrentAverageMicroamps(&current_average.value);
545 return IsBatteryCurrentSignCorrect(status, current_average,
546 false /* accept zero current as unknown */);
547 };
548
549 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
550 << "You may want to try again later when current_average becomes stable.";
551}
552
553TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentFromHal) {
554 auto testOnce = [&]() -> AssertionResult {
555 HalResult<BatteryStatus> status;
556 *status.result = health->getChargeStatus(&status.value);
557 HalResult<int32_t> current_now;
558 *current_now.result = health->getCurrentNowMicroamps(&current_now.value);
559 HalResult<int32_t> current_average;
560 *current_average.result = health->getCurrentAverageMicroamps(&current_average.value);
561 return IsBatteryCurrentSimilar(status, current_now, current_average);
562 };
563
564 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
565 << "You may want to try again later when current_average becomes stable.";
566}
567
568AssertionResult IsBatteryStatusCorrect(const HalResult<BatteryStatus>& status,
569 const HalResult<HealthInfo>& health_info) {
570 // getChargetStatus / getHealthInfo is already tested above. Here, just skip if not ok.
571 if (!health_info.result->isOk()) {
572 return AssertionSuccess() << "getHealthInfo returned "
573 << health_info.result->getDescription() << ", skipping";
574 }
575 if (!status.result->isOk()) {
576 return AssertionSuccess() << "getChargeStatus returned " << status.result->getDescription()
577 << ", skipping";
578 }
579 return ::android::hardware::health::test_utils::IsBatteryStatusCorrect(
580 status.value, health_info.value, [](BatteryStatus status) { return toString(status); });
581}
582
583TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) {
584 auto testOnce = [&]() -> AssertionResult {
585 HalResult<BatteryStatus> status;
586 *status.result = health->getChargeStatus(&status.value);
587 HalResult<HealthInfo> health_info;
588 *health_info.result = health->getHealthInfo(&health_info.value);
589 return IsBatteryStatusCorrect(status, health_info);
590 };
591
592 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
593 << "You may want to try again later when battery_status becomes stable.";
594}
595
596TEST_P(BatteryTest, ConnectedAgainstStatusInHealthInfo) {
597 auto testOnce = [&]() -> AssertionResult {
598 HalResult<HealthInfo> health_info;
599 *health_info.result = health->getHealthInfo(&health_info.value);
600 return IsBatteryStatusCorrect({health_info.result, health_info.value.batteryStatus},
601 health_info);
602 };
603
604 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
605 << "You may want to try again later when getHealthInfo becomes stable.";
606}
607
608GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BatteryTest);
609INSTANTIATE_TEST_SUITE_P(Health, BatteryTest,
610 testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
611 PrintInstanceNameToString);
612
613} // namespace aidl::android::hardware::health
614
615int main(int argc, char** argv) {
616 ::testing::InitGoogleTest(&argc, argv);
617 ABinderProcess_setThreadPoolMaxThreadCount(1);
618 ABinderProcess_startThreadPool();
619 return RUN_ALL_TESTS();
620}