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