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