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