blob: c47ddca07fe3914e87372976ddf3a2ae0c640087 [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/*
Daniel Zhengb1553a02024-10-30 11:26:59 -0700363 * Tests the values returned by getHingeInfo() from interface IHealth.
364 */
365TEST_P(HealthAidl, getHingeInfo) {
366 std::vector<HingeInfo> value;
367 auto status = health->getHingeInfo(&value);
368 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
369 if (!status.isOk()) return;
370 for (auto& hinge : value) {
371 ASSERT_TRUE(hinge.expectedHingeLifespan > 0);
372 }
373}
374
375/*
Yifan Hong2200cff2021-10-28 12:18:35 -0700376 * Tests the values returned by getDiskStats() from interface IHealth.
377 */
378TEST_P(HealthAidl, getDiskStats) {
379 std::vector<DiskStats> value;
380 auto status = health->getDiskStats(&value);
381 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
382}
383
384MATCHER(IsValidHealthInfo, "") {
385 *result_listener << "value is " << arg.toString() << ".";
386 if (!ExplainMatchResult(Each(IsValidStorageInfo()), arg.storageInfos, result_listener)) {
387 *result_listener << " for storageInfos.";
388 return false;
389 }
390
391 if (!ExplainMatchResult(Not(INT32_MIN), arg.batteryCurrentMicroamps, result_listener)) {
392 *result_listener << " for batteryCurrentMicroamps.";
393 return false;
394 }
395
396 if (!ExplainMatchResult(InClosedRange(0, 100), arg.batteryLevel, result_listener)) {
397 *result_listener << " for batteryLevel.";
398 return false;
399 }
400
401 if (!ExplainMatchResult(IsValidEnum<BatteryHealth>(), arg.batteryHealth, result_listener)) {
402 *result_listener << " for batteryHealth.";
403 return false;
404 }
405
406 if (!ExplainMatchResult(IsValidEnum<BatteryStatus>(), arg.batteryStatus, result_listener)) {
407 *result_listener << " for batteryStatus.";
408 return false;
409 }
410
411 if (arg.batteryPresent) {
412 if (!ExplainMatchResult(Gt(0), arg.batteryChargeCounterUah, result_listener)) {
413 *result_listener << " for batteryChargeCounterUah when battery is present.";
414 return false;
415 }
416 if (!ExplainMatchResult(Not(BatteryStatus::UNKNOWN), arg.batteryStatus, result_listener)) {
417 *result_listener << " for batteryStatus when battery is present.";
418 return false;
419 }
420 }
421
422 if (!ExplainMatchResult(IsValidEnum<BatteryCapacityLevel>(), arg.batteryCapacityLevel,
423 result_listener)) {
424 *result_listener << " for batteryCapacityLevel.";
425 return false;
426 }
427 if (!ExplainMatchResult(Ge(-1), arg.batteryChargeTimeToFullNowSeconds, result_listener)) {
428 *result_listener << " for batteryChargeTimeToFullNowSeconds.";
429 return false;
430 }
431
432 if (!ExplainMatchResult(
433 AnyOf(Eq(0), AllOf(Gt(kFullChargeDesignCapMinUah), Lt(kFullChargeDesignCapMaxUah))),
434 arg.batteryFullChargeDesignCapacityUah, result_listener)) {
435 *result_listener << " for batteryFullChargeDesignCapacityUah. It should be greater than "
436 "100 mAh and less than 100,000 mAh, or 0 if unknown";
437 return false;
438 }
439
440 return true;
441}
442
443/*
444 * Tests the values returned by getHealthInfo() from interface IHealth.
445 */
446TEST_P(HealthAidl, getHealthInfo) {
447 HealthInfo value;
448 auto status = health->getHealthInfo(&value);
449 ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
450 if (!status.isOk()) return;
451 ASSERT_THAT(value, IsValidHealthInfo());
452}
453
454GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HealthAidl);
455INSTANTIATE_TEST_SUITE_P(Health, HealthAidl,
456 testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
457 PrintInstanceNameToString);
458
459// For battery current tests, value may not be stable if the battery current has fluctuated.
460// Retry in a bit more time (with the following timeout) and consider the test successful if it
461// has succeed once.
462static constexpr auto gBatteryTestTimeout = 1min;
463static constexpr double gCurrentCompareFactor = 0.50;
464class BatteryTest : public HealthAidl {};
465
466// Tuple for all IHealth::get* API return values.
467template <typename T>
468struct HalResult {
469 std::shared_ptr<ScopedAStatus> result = std::make_shared<ScopedAStatus>();
470 T value;
471};
472
473// Needs to be called repeatedly within a period of time to ensure values are initialized.
474static AssertionResult IsBatteryCurrentSignCorrect(const HalResult<BatteryStatus>& status,
475 const HalResult<int32_t>& current,
476 bool acceptZeroCurrentAsUnknown) {
477 // getChargeStatus / getCurrentNow / getCurrentAverage / getHealthInfo already tested above.
478 // Here, just skip if not ok.
479 if (!status.result->isOk()) {
480 return AssertionSuccess() << "getChargeStatus / getHealthInfo returned "
481 << status.result->getDescription() << ", skipping";
482 }
483
484 if (!current.result->isOk()) {
485 return AssertionSuccess() << "getCurrentNow / getCurrentAverage returned "
486 << current.result->getDescription() << ", skipping";
487 }
488
489 return ::android::hardware::health::test_utils::IsBatteryCurrentSignCorrect(
490 status.value, current.value, acceptZeroCurrentAsUnknown,
491 [](BatteryStatus status) { return toString(status); });
492}
493
494static AssertionResult IsBatteryCurrentSimilar(const HalResult<BatteryStatus>& status,
495 const HalResult<int32_t>& current_now,
496 const HalResult<int32_t>& current_average) {
497 if (status.result->isOk() && status.value == BatteryStatus::FULL) {
498 // No reason to test on full battery because battery current load fluctuates.
499 return AssertionSuccess() << "Battery is full, skipping";
500 }
501
502 // getCurrentNow / getCurrentAverage / getHealthInfo already tested above. Here, just skip if
503 // not SUCCESS or value 0.
504 if (!current_now.result->isOk() || current_now.value == 0) {
505 return AssertionSuccess() << "getCurrentNow returned "
506 << current_now.result->getDescription() << " with value "
507 << current_now.value << ", skipping";
508 }
509
510 if (!current_average.result->isOk() || current_average.value == 0) {
511 return AssertionSuccess() << "getCurrentAverage returned "
512 << current_average.result->getDescription() << " with value "
513 << current_average.value << ", skipping";
514 }
515
516 return ::android::hardware::health::test_utils::IsBatteryCurrentSimilar(
517 current_now.value, current_average.value, gCurrentCompareFactor);
518}
519
520TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusInHealthInfo) {
521 auto testOnce = [&]() -> AssertionResult {
522 HalResult<HealthInfo> health_info;
523 *health_info.result = health->getHealthInfo(&health_info.value);
524
525 return IsBatteryCurrentSignCorrect(
526 {health_info.result, health_info.value.batteryStatus},
527 {health_info.result, health_info.value.batteryCurrentMicroamps},
528 true /* accept zero current as unknown */);
529 };
530 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
531 << "You may want to try again later when current_now becomes stable.";
532}
533
534TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusInHealthInfo) {
535 auto testOnce = [&]() -> AssertionResult {
536 HalResult<HealthInfo> health_info;
537 *health_info.result = health->getHealthInfo(&health_info.value);
538 return IsBatteryCurrentSignCorrect(
539 {health_info.result, health_info.value.batteryStatus},
540 {health_info.result, health_info.value.batteryCurrentAverageMicroamps},
541 true /* accept zero current as unknown */);
542 };
543
544 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
545 << "You may want to try again later when current_average becomes stable.";
546}
547
548TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentInHealthInfo) {
549 auto testOnce = [&]() -> AssertionResult {
550 HalResult<HealthInfo> health_info;
551 *health_info.result = health->getHealthInfo(&health_info.value);
552 return IsBatteryCurrentSimilar(
553 {health_info.result, health_info.value.batteryStatus},
554 {health_info.result, health_info.value.batteryCurrentMicroamps},
555 {health_info.result, health_info.value.batteryCurrentAverageMicroamps});
556 };
557
558 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
559 << "You may want to try again later when current_now and current_average becomes "
560 "stable.";
561}
562
563TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusFromHal) {
564 auto testOnce = [&]() -> AssertionResult {
565 HalResult<BatteryStatus> status;
566 *status.result = health->getChargeStatus(&status.value);
567 HalResult<int32_t> current_now;
568 *current_now.result = health->getCurrentNowMicroamps(&current_now.value);
569 return IsBatteryCurrentSignCorrect(status, current_now,
570 false /* accept zero current as unknown */);
571 };
572
573 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
574 << "You may want to try again later when current_now becomes stable.";
575}
576
577TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusFromHal) {
578 auto testOnce = [&]() -> AssertionResult {
579 HalResult<BatteryStatus> status;
580 *status.result = health->getChargeStatus(&status.value);
581 HalResult<int32_t> current_average;
582 *current_average.result = health->getCurrentAverageMicroamps(&current_average.value);
583 return IsBatteryCurrentSignCorrect(status, current_average,
584 false /* accept zero current as unknown */);
585 };
586
587 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
588 << "You may want to try again later when current_average becomes stable.";
589}
590
591TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentFromHal) {
592 auto testOnce = [&]() -> AssertionResult {
593 HalResult<BatteryStatus> status;
594 *status.result = health->getChargeStatus(&status.value);
595 HalResult<int32_t> current_now;
596 *current_now.result = health->getCurrentNowMicroamps(&current_now.value);
597 HalResult<int32_t> current_average;
598 *current_average.result = health->getCurrentAverageMicroamps(&current_average.value);
599 return IsBatteryCurrentSimilar(status, current_now, current_average);
600 };
601
602 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
603 << "You may want to try again later when current_average becomes stable.";
604}
605
606AssertionResult IsBatteryStatusCorrect(const HalResult<BatteryStatus>& status,
607 const HalResult<HealthInfo>& health_info) {
608 // getChargetStatus / getHealthInfo is already tested above. Here, just skip if not ok.
609 if (!health_info.result->isOk()) {
610 return AssertionSuccess() << "getHealthInfo returned "
611 << health_info.result->getDescription() << ", skipping";
612 }
613 if (!status.result->isOk()) {
614 return AssertionSuccess() << "getChargeStatus returned " << status.result->getDescription()
615 << ", skipping";
616 }
617 return ::android::hardware::health::test_utils::IsBatteryStatusCorrect(
618 status.value, health_info.value, [](BatteryStatus status) { return toString(status); });
619}
620
621TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) {
622 auto testOnce = [&]() -> AssertionResult {
623 HalResult<BatteryStatus> status;
624 *status.result = health->getChargeStatus(&status.value);
625 HalResult<HealthInfo> health_info;
626 *health_info.result = health->getHealthInfo(&health_info.value);
627 return IsBatteryStatusCorrect(status, health_info);
628 };
629
630 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
631 << "You may want to try again later when battery_status becomes stable.";
632}
633
634TEST_P(BatteryTest, ConnectedAgainstStatusInHealthInfo) {
635 auto testOnce = [&]() -> AssertionResult {
636 HalResult<HealthInfo> health_info;
637 *health_info.result = health->getHealthInfo(&health_info.value);
638 return IsBatteryStatusCorrect({health_info.result, health_info.value.batteryStatus},
639 health_info);
640 };
641
642 EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
643 << "You may want to try again later when getHealthInfo becomes stable.";
644}
645
646GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BatteryTest);
647INSTANTIATE_TEST_SUITE_P(Health, BatteryTest,
648 testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
649 PrintInstanceNameToString);
650
651} // namespace aidl::android::hardware::health
652
653int main(int argc, char** argv) {
654 ::testing::InitGoogleTest(&argc, argv);
655 ABinderProcess_setThreadPoolMaxThreadCount(1);
656 ABinderProcess_startThreadPool();
657 return RUN_ALL_TESTS();
658}