Merge "health AIDL: add vts tests" am: 9b965ac6a5
am: 0b1b6a0258
am: a2f7571af3
Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1875176 Change-Id: If28cdf405daa8ecc00c1c45e84f3c77a82d6fa93
This commit is contained in:
commit
5874237a8c
6 changed files with 781 additions and 118 deletions
|
@ -32,5 +32,11 @@ cc_test {
|
|||
"android.hardware.health@1.0",
|
||||
"android.hardware.health@2.0",
|
||||
],
|
||||
test_suites: ["general-tests", "vts"],
|
||||
header_libs: [
|
||||
"libhealthtest_headers",
|
||||
],
|
||||
test_suites: [
|
||||
"general-tests",
|
||||
"vts",
|
||||
],
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <android/hardware/health/2.0/types.h>
|
||||
#include <gflags/gflags.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <health-test/TestUtils.h>
|
||||
#include <hidl/GtestPrinter.h>
|
||||
#include <hidl/ServiceManagement.h>
|
||||
#include <log/log.h>
|
||||
|
@ -51,6 +52,7 @@ namespace android {
|
|||
namespace hardware {
|
||||
namespace health {
|
||||
|
||||
using test_utils::SucceedOnce;
|
||||
using V1_0::BatteryStatus;
|
||||
using V1_0::toString;
|
||||
|
||||
|
@ -356,64 +358,9 @@ static AssertionResult IsBatteryCurrentSignCorrect(HalResult<BatteryStatus> stat
|
|||
<< toString(current.result) << ", skipping";
|
||||
}
|
||||
|
||||
// For IHealth.getCurrentNow/Average, if current is not available, it is expected that
|
||||
// current.result == Result::NOT_SUPPORTED, which is checked above. Hence, zero current is
|
||||
// not treated as unknown values.
|
||||
// For IHealth.getHealthInfo, if current is not available, health_info.current_* == 0.
|
||||
// Caller of this function provides current.result == Result::SUCCESS. Hence, just skip the
|
||||
// check.
|
||||
if (current.value == 0 && acceptZeroCurrentAsUnknown) {
|
||||
return AssertionSuccess()
|
||||
<< "current is 0, which indicates the value may not be available. Skipping.";
|
||||
}
|
||||
|
||||
switch (status.value) {
|
||||
case BatteryStatus::UNKNOWN:
|
||||
if (current.value != 0) {
|
||||
// BatteryStatus may be UNKNOWN initially with a non-zero current value, but
|
||||
// after it is initialized, it should be known.
|
||||
return AssertionFailure()
|
||||
<< "BatteryStatus is UNKNOWN but current is not 0. Actual: "
|
||||
<< current.value;
|
||||
}
|
||||
break;
|
||||
case BatteryStatus::CHARGING:
|
||||
if (current.value <= 0) {
|
||||
return AssertionFailure()
|
||||
<< "BatteryStatus is CHARGING but current is not positive. Actual: "
|
||||
<< current.value;
|
||||
}
|
||||
break;
|
||||
case BatteryStatus::NOT_CHARGING:
|
||||
if (current.value > 0) {
|
||||
return AssertionFailure() << "BatteryStatus is " << toString(status.value)
|
||||
<< " but current is positive. Actual: " << current.value;
|
||||
}
|
||||
break;
|
||||
case BatteryStatus::DISCHARGING:
|
||||
if (current.value >= 0) {
|
||||
return AssertionFailure()
|
||||
<< "BatteryStatus is " << toString(status.value)
|
||||
<< " but current is not negative. Actual: " << current.value;
|
||||
}
|
||||
break;
|
||||
case BatteryStatus::FULL:
|
||||
// Battery current may be positive or negative depending on the load.
|
||||
break;
|
||||
default:
|
||||
return AssertionFailure() << "Unknown BatteryStatus " << toString(status.value);
|
||||
}
|
||||
|
||||
return AssertionSuccess() << "BatteryStatus is " << toString(status.value)
|
||||
<< " and current has the correct sign: " << current.value;
|
||||
}
|
||||
|
||||
static AssertionResult IsValueSimilar(int32_t dividend, int32_t divisor, double factor) {
|
||||
auto difference = abs(dividend - divisor);
|
||||
if (difference > factor * abs(divisor)) {
|
||||
return AssertionFailure() << dividend << " and " << divisor << " are not similar.";
|
||||
}
|
||||
return AssertionSuccess() << dividend << " and " << divisor << " are similar.";
|
||||
return test_utils::IsBatteryCurrentSignCorrect(
|
||||
status.value, current.value, acceptZeroCurrentAsUnknown,
|
||||
[](BatteryStatus status) { return toString(status); });
|
||||
}
|
||||
|
||||
static AssertionResult IsBatteryCurrentSimilar(HalResult<BatteryStatus> status,
|
||||
|
@ -437,31 +384,8 @@ static AssertionResult IsBatteryCurrentSimilar(HalResult<BatteryStatus> status,
|
|||
<< currentAverage.value << ", skipping";
|
||||
}
|
||||
|
||||
// Check that the two values are similar. Note that the two tests uses a different
|
||||
// divisor to ensure that they are actually pretty similar. For example,
|
||||
// IsValueSimilar(5,10,0.4) returns true, but IsValueSimlar(10,5,0.4) returns false.
|
||||
TEST_AND_RETURN(IsValueSimilar(currentNow.value, currentAverage.value, gCurrentCompareFactor)
|
||||
<< " for now vs. average. Check units.");
|
||||
TEST_AND_RETURN(IsValueSimilar(currentAverage.value, currentNow.value, gCurrentCompareFactor)
|
||||
<< " for average vs. now. Check units.");
|
||||
return AssertionSuccess() << "currentNow = " << currentNow.value
|
||||
<< " and currentAverage = " << currentAverage.value
|
||||
<< " are considered similar.";
|
||||
}
|
||||
|
||||
// Test that f() returns AssertionSuccess() once in a given period of time.
|
||||
template <typename Duration, typename Function>
|
||||
static AssertionResult SucceedOnce(Duration d, Function f) {
|
||||
AssertionResult result = AssertionFailure() << "Function never evaluated.";
|
||||
auto end = std::chrono::system_clock::now() + d;
|
||||
while (std::chrono::system_clock::now() <= end) {
|
||||
result = f();
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
std::this_thread::sleep_for(2s);
|
||||
}
|
||||
return result;
|
||||
return test_utils::IsBatteryCurrentSimilar(currentNow.value, currentAverage.value,
|
||||
gCurrentCompareFactor);
|
||||
}
|
||||
|
||||
uint64_t GetShippingApiLevel() {
|
||||
|
@ -603,40 +527,8 @@ AssertionResult IsBatteryStatusCorrect(HalResult<BatteryStatus> status,
|
|||
}
|
||||
|
||||
const auto& batteryInfo = healthInfo.value.legacy;
|
||||
bool isConnected = batteryInfo.chargerAcOnline || batteryInfo.chargerUsbOnline ||
|
||||
batteryInfo.chargerWirelessOnline;
|
||||
|
||||
std::stringstream message;
|
||||
message << "BatteryStatus is " << toString(status.value) << " and "
|
||||
<< (isConnected ? "" : "no ")
|
||||
<< "power source is connected: ac=" << batteryInfo.chargerAcOnline
|
||||
<< ", usb=" << batteryInfo.chargerUsbOnline
|
||||
<< ", wireless=" << batteryInfo.chargerWirelessOnline;
|
||||
|
||||
switch (status.value) {
|
||||
case BatteryStatus::UNKNOWN: {
|
||||
// Don't enforce anything on isConnected on unknown battery status.
|
||||
// Battery-less devices must report UNKNOWN battery status, but may report true
|
||||
// or false on isConnected.
|
||||
} break;
|
||||
case BatteryStatus::CHARGING:
|
||||
case BatteryStatus::NOT_CHARGING:
|
||||
case BatteryStatus::FULL: {
|
||||
if (!isConnected) {
|
||||
return AssertionFailure() << message.str();
|
||||
}
|
||||
} break;
|
||||
case BatteryStatus::DISCHARGING: {
|
||||
if (isConnected) {
|
||||
return AssertionFailure() << message.str();
|
||||
}
|
||||
} break;
|
||||
default: {
|
||||
return AssertionFailure() << "Unknown battery status value " << toString(status.value);
|
||||
} break;
|
||||
}
|
||||
|
||||
return AssertionSuccess() << message.str();
|
||||
return test_utils::IsBatteryStatusCorrect(
|
||||
status.value, batteryInfo, [](BatteryStatus status) { return toString(status); });
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) {
|
||||
|
|
48
health/aidl/vts/functional/Android.bp
Normal file
48
health/aidl/vts/functional/Android.bp
Normal file
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// Copyright (C) 2021 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
package {
|
||||
// See: http://go/android-license-faq
|
||||
// A large-scale-change added 'default_applicable_licenses' to import
|
||||
// all of the 'license_kinds' from "hardware_interfaces_license"
|
||||
// to get the below license kinds:
|
||||
// SPDX-license-identifier-Apache-2.0
|
||||
default_applicable_licenses: ["hardware_interfaces_license"],
|
||||
}
|
||||
|
||||
cc_test {
|
||||
name: "VtsHalHealthTargetTest",
|
||||
defaults: [
|
||||
"VtsHalTargetTestDefaults",
|
||||
"use_libaidlvintf_gtest_helper_static",
|
||||
],
|
||||
srcs: [
|
||||
"VtsHalHealthTargetTest.cpp",
|
||||
],
|
||||
shared_libs: [
|
||||
"libbinder_ndk",
|
||||
],
|
||||
static_libs: [
|
||||
"android.hardware.health-V1-ndk",
|
||||
"libgmock",
|
||||
],
|
||||
header_libs: [
|
||||
"libhealthtest_headers",
|
||||
],
|
||||
test_suites: [
|
||||
"vts",
|
||||
],
|
||||
}
|
539
health/aidl/vts/functional/VtsHalHealthTargetTest.cpp
Normal file
539
health/aidl/vts/functional/VtsHalHealthTargetTest.cpp
Normal file
|
@ -0,0 +1,539 @@
|
|||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#define LOG_TAG "health_aidl_hal_test"
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include <aidl/Gtest.h>
|
||||
#include <aidl/Vintf.h>
|
||||
#include <aidl/android/hardware/health/BnHealthInfoCallback.h>
|
||||
#include <aidl/android/hardware/health/IHealth.h>
|
||||
#include <android/binder_auto_utils.h>
|
||||
#include <android/binder_enums.h>
|
||||
#include <android/binder_interface_utils.h>
|
||||
#include <android/binder_manager.h>
|
||||
#include <android/binder_process.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <health-test/TestUtils.h>
|
||||
|
||||
using android::getAidlHalInstanceNames;
|
||||
using android::PrintInstanceNameToString;
|
||||
using android::hardware::health::test_utils::SucceedOnce;
|
||||
using ndk::enum_range;
|
||||
using ndk::ScopedAStatus;
|
||||
using ndk::SharedRefBase;
|
||||
using ndk::SpAIBinder;
|
||||
using testing::AllOf;
|
||||
using testing::AnyOf;
|
||||
using testing::AnyOfArray;
|
||||
using testing::AssertionFailure;
|
||||
using testing::AssertionResult;
|
||||
using testing::AssertionSuccess;
|
||||
using testing::Contains;
|
||||
using testing::Each;
|
||||
using testing::Eq;
|
||||
using testing::ExplainMatchResult;
|
||||
using testing::Ge;
|
||||
using testing::Gt;
|
||||
using testing::Le;
|
||||
using testing::Lt;
|
||||
using testing::Matcher;
|
||||
using testing::Not;
|
||||
using namespace std::string_literals;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
namespace aidl::android::hardware::health {
|
||||
|
||||
static constexpr int32_t kFullChargeDesignCapMinUah = 100 * 1000;
|
||||
static constexpr int32_t kFullChargeDesignCapMaxUah = 100 * 1000 * 1000;
|
||||
|
||||
MATCHER(IsOk, "") {
|
||||
*result_listener << "status is " << arg.getDescription();
|
||||
return arg.isOk();
|
||||
}
|
||||
|
||||
MATCHER_P(ExceptionIs, exception_code, "") {
|
||||
*result_listener << "status is " << arg.getDescription();
|
||||
return arg.getExceptionCode() == exception_code;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Matcher<T> InClosedRange(const T& lo, const T& hi) {
|
||||
return AllOf(Ge(lo), Le(hi));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Matcher<T> IsValidEnum() {
|
||||
return AnyOfArray(enum_range<T>().begin(), enum_range<T>().end());
|
||||
}
|
||||
|
||||
class HealthAidl : public testing::TestWithParam<std::string> {
|
||||
public:
|
||||
void SetUp() override {
|
||||
SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str()));
|
||||
health = IHealth::fromBinder(binder);
|
||||
ASSERT_NE(health, nullptr);
|
||||
}
|
||||
std::shared_ptr<IHealth> health;
|
||||
};
|
||||
|
||||
class Callback : public BnHealthInfoCallback {
|
||||
public:
|
||||
ScopedAStatus healthInfoChanged(const HealthInfo&) override {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
invoked_ = true;
|
||||
}
|
||||
invoked_notify_.notify_all();
|
||||
return ScopedAStatus::ok();
|
||||
}
|
||||
template <typename R, typename P>
|
||||
[[nodiscard]] bool waitInvoke(std::chrono::duration<R, P> duration) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
bool r = invoked_notify_.wait_for(lock, duration, [this] { return this->invoked_; });
|
||||
invoked_ = false;
|
||||
return r;
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable invoked_notify_;
|
||||
bool invoked_ = false;
|
||||
};
|
||||
|
||||
TEST_P(HealthAidl, Callbacks) {
|
||||
auto first_callback = SharedRefBase::make<Callback>();
|
||||
auto second_callback = SharedRefBase::make<Callback>();
|
||||
|
||||
ASSERT_THAT(health->registerCallback(first_callback), IsOk());
|
||||
ASSERT_THAT(health->registerCallback(second_callback), IsOk());
|
||||
|
||||
// registerCallback may or may not invoke the callback immediately, so the test needs
|
||||
// to wait for the invocation. If the implementation chooses not to invoke the callback
|
||||
// immediately, just wait for some time.
|
||||
(void)first_callback->waitInvoke(200ms);
|
||||
(void)second_callback->waitInvoke(200ms);
|
||||
|
||||
// assert that the first callback is invoked when update is called.
|
||||
ASSERT_THAT(health->update(), IsOk());
|
||||
|
||||
ASSERT_TRUE(first_callback->waitInvoke(1s));
|
||||
ASSERT_TRUE(second_callback->waitInvoke(1s));
|
||||
|
||||
ASSERT_THAT(health->unregisterCallback(first_callback), IsOk());
|
||||
|
||||
// clear any potentially pending callbacks result from wakealarm / kernel events
|
||||
// If there is none, just wait for some time.
|
||||
(void)first_callback->waitInvoke(200ms);
|
||||
(void)second_callback->waitInvoke(200ms);
|
||||
|
||||
// assert that the second callback is still invoked even though the first is unregistered.
|
||||
ASSERT_THAT(health->update(), IsOk());
|
||||
|
||||
ASSERT_FALSE(first_callback->waitInvoke(200ms));
|
||||
ASSERT_TRUE(second_callback->waitInvoke(1s));
|
||||
|
||||
ASSERT_THAT(health->unregisterCallback(second_callback), IsOk());
|
||||
}
|
||||
|
||||
TEST_P(HealthAidl, UnregisterNonExistentCallback) {
|
||||
auto callback = SharedRefBase::make<Callback>();
|
||||
auto ret = health->unregisterCallback(callback);
|
||||
ASSERT_THAT(ret, ExceptionIs(EX_ILLEGAL_ARGUMENT));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getChargeCounterUah() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getChargeCounterUah) {
|
||||
int32_t value;
|
||||
auto status = health->getChargeCounterUah(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, Ge(0));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getCurrentNowMicroamps() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getCurrentNowMicroamps) {
|
||||
int32_t value;
|
||||
auto status = health->getCurrentNowMicroamps(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, Not(INT32_MIN));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getCurrentAverageMicroamps() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getCurrentAverageMicroamps) {
|
||||
int32_t value;
|
||||
auto status = health->getCurrentAverageMicroamps(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, Not(INT32_MIN));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getCapacity() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getCapacity) {
|
||||
int32_t value;
|
||||
auto status = health->getCapacity(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, InClosedRange(0, 100));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getEnergyCounterNwh() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getEnergyCounterNwh) {
|
||||
int64_t value;
|
||||
auto status = health->getEnergyCounterNwh(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, Not(INT64_MIN));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getChargeStatus() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getChargeStatus) {
|
||||
BatteryStatus value;
|
||||
auto status = health->getChargeStatus(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, IsValidEnum<BatteryStatus>());
|
||||
}
|
||||
|
||||
MATCHER(IsValidStorageInfo, "") {
|
||||
*result_listener << "value is " << arg.toString() << ".";
|
||||
if (!ExplainMatchResult(InClosedRange(0, 3), arg.eol, result_listener)) {
|
||||
*result_listener << " for eol.";
|
||||
return false;
|
||||
}
|
||||
if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeA, result_listener)) {
|
||||
*result_listener << " for lifetimeA.";
|
||||
return false;
|
||||
}
|
||||
if (!ExplainMatchResult(InClosedRange(0, 0x0B), arg.lifetimeB, result_listener)) {
|
||||
*result_listener << " for lifetimeB.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getStorageInfo() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getStorageInfo) {
|
||||
std::vector<StorageInfo> value;
|
||||
auto status = health->getStorageInfo(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, Each(IsValidStorageInfo()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getDiskStats() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getDiskStats) {
|
||||
std::vector<DiskStats> value;
|
||||
auto status = health->getDiskStats(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
}
|
||||
|
||||
MATCHER(IsValidHealthInfo, "") {
|
||||
*result_listener << "value is " << arg.toString() << ".";
|
||||
if (!ExplainMatchResult(Each(IsValidStorageInfo()), arg.storageInfos, result_listener)) {
|
||||
*result_listener << " for storageInfos.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(Not(INT32_MIN), arg.batteryCurrentMicroamps, result_listener)) {
|
||||
*result_listener << " for batteryCurrentMicroamps.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(InClosedRange(0, 100), arg.batteryLevel, result_listener)) {
|
||||
*result_listener << " for batteryLevel.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(IsValidEnum<BatteryHealth>(), arg.batteryHealth, result_listener)) {
|
||||
*result_listener << " for batteryHealth.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(IsValidEnum<BatteryStatus>(), arg.batteryStatus, result_listener)) {
|
||||
*result_listener << " for batteryStatus.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (arg.batteryPresent) {
|
||||
if (!ExplainMatchResult(Gt(0), arg.batteryChargeCounterUah, result_listener)) {
|
||||
*result_listener << " for batteryChargeCounterUah when battery is present.";
|
||||
return false;
|
||||
}
|
||||
if (!ExplainMatchResult(Not(BatteryStatus::UNKNOWN), arg.batteryStatus, result_listener)) {
|
||||
*result_listener << " for batteryStatus when battery is present.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(IsValidEnum<BatteryCapacityLevel>(), arg.batteryCapacityLevel,
|
||||
result_listener)) {
|
||||
*result_listener << " for batteryCapacityLevel.";
|
||||
return false;
|
||||
}
|
||||
if (!ExplainMatchResult(Ge(-1), arg.batteryChargeTimeToFullNowSeconds, result_listener)) {
|
||||
*result_listener << " for batteryChargeTimeToFullNowSeconds.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ExplainMatchResult(
|
||||
AnyOf(Eq(0), AllOf(Gt(kFullChargeDesignCapMinUah), Lt(kFullChargeDesignCapMaxUah))),
|
||||
arg.batteryFullChargeDesignCapacityUah, result_listener)) {
|
||||
*result_listener << " for batteryFullChargeDesignCapacityUah. It should be greater than "
|
||||
"100 mAh and less than 100,000 mAh, or 0 if unknown";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests the values returned by getHealthInfo() from interface IHealth.
|
||||
*/
|
||||
TEST_P(HealthAidl, getHealthInfo) {
|
||||
HealthInfo value;
|
||||
auto status = health->getHealthInfo(&value);
|
||||
ASSERT_THAT(status, AnyOf(IsOk(), ExceptionIs(EX_UNSUPPORTED_OPERATION)));
|
||||
if (!status.isOk()) return;
|
||||
ASSERT_THAT(value, IsValidHealthInfo());
|
||||
}
|
||||
|
||||
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HealthAidl);
|
||||
INSTANTIATE_TEST_SUITE_P(Health, HealthAidl,
|
||||
testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
|
||||
PrintInstanceNameToString);
|
||||
|
||||
// For battery current tests, value may not be stable if the battery current has fluctuated.
|
||||
// Retry in a bit more time (with the following timeout) and consider the test successful if it
|
||||
// has succeed once.
|
||||
static constexpr auto gBatteryTestTimeout = 1min;
|
||||
static constexpr double gCurrentCompareFactor = 0.50;
|
||||
class BatteryTest : public HealthAidl {};
|
||||
|
||||
// Tuple for all IHealth::get* API return values.
|
||||
template <typename T>
|
||||
struct HalResult {
|
||||
std::shared_ptr<ScopedAStatus> result = std::make_shared<ScopedAStatus>();
|
||||
T value;
|
||||
};
|
||||
|
||||
// Needs to be called repeatedly within a period of time to ensure values are initialized.
|
||||
static AssertionResult IsBatteryCurrentSignCorrect(const HalResult<BatteryStatus>& status,
|
||||
const HalResult<int32_t>& current,
|
||||
bool acceptZeroCurrentAsUnknown) {
|
||||
// getChargeStatus / getCurrentNow / getCurrentAverage / getHealthInfo already tested above.
|
||||
// Here, just skip if not ok.
|
||||
if (!status.result->isOk()) {
|
||||
return AssertionSuccess() << "getChargeStatus / getHealthInfo returned "
|
||||
<< status.result->getDescription() << ", skipping";
|
||||
}
|
||||
|
||||
if (!current.result->isOk()) {
|
||||
return AssertionSuccess() << "getCurrentNow / getCurrentAverage returned "
|
||||
<< current.result->getDescription() << ", skipping";
|
||||
}
|
||||
|
||||
return ::android::hardware::health::test_utils::IsBatteryCurrentSignCorrect(
|
||||
status.value, current.value, acceptZeroCurrentAsUnknown,
|
||||
[](BatteryStatus status) { return toString(status); });
|
||||
}
|
||||
|
||||
static AssertionResult IsBatteryCurrentSimilar(const HalResult<BatteryStatus>& status,
|
||||
const HalResult<int32_t>& current_now,
|
||||
const HalResult<int32_t>& current_average) {
|
||||
if (status.result->isOk() && status.value == BatteryStatus::FULL) {
|
||||
// No reason to test on full battery because battery current load fluctuates.
|
||||
return AssertionSuccess() << "Battery is full, skipping";
|
||||
}
|
||||
|
||||
// getCurrentNow / getCurrentAverage / getHealthInfo already tested above. Here, just skip if
|
||||
// not SUCCESS or value 0.
|
||||
if (!current_now.result->isOk() || current_now.value == 0) {
|
||||
return AssertionSuccess() << "getCurrentNow returned "
|
||||
<< current_now.result->getDescription() << " with value "
|
||||
<< current_now.value << ", skipping";
|
||||
}
|
||||
|
||||
if (!current_average.result->isOk() || current_average.value == 0) {
|
||||
return AssertionSuccess() << "getCurrentAverage returned "
|
||||
<< current_average.result->getDescription() << " with value "
|
||||
<< current_average.value << ", skipping";
|
||||
}
|
||||
|
||||
return ::android::hardware::health::test_utils::IsBatteryCurrentSimilar(
|
||||
current_now.value, current_average.value, gCurrentCompareFactor);
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusInHealthInfo) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<HealthInfo> health_info;
|
||||
*health_info.result = health->getHealthInfo(&health_info.value);
|
||||
|
||||
return IsBatteryCurrentSignCorrect(
|
||||
{health_info.result, health_info.value.batteryStatus},
|
||||
{health_info.result, health_info.value.batteryCurrentMicroamps},
|
||||
true /* accept zero current as unknown */);
|
||||
};
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_now becomes stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusInHealthInfo) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<HealthInfo> health_info;
|
||||
*health_info.result = health->getHealthInfo(&health_info.value);
|
||||
return IsBatteryCurrentSignCorrect(
|
||||
{health_info.result, health_info.value.batteryStatus},
|
||||
{health_info.result, health_info.value.batteryCurrentAverageMicroamps},
|
||||
true /* accept zero current as unknown */);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_average becomes stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentInHealthInfo) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<HealthInfo> health_info;
|
||||
*health_info.result = health->getHealthInfo(&health_info.value);
|
||||
return IsBatteryCurrentSimilar(
|
||||
{health_info.result, health_info.value.batteryStatus},
|
||||
{health_info.result, health_info.value.batteryCurrentMicroamps},
|
||||
{health_info.result, health_info.value.batteryCurrentAverageMicroamps});
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_now and current_average becomes "
|
||||
"stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusFromHal) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<BatteryStatus> status;
|
||||
*status.result = health->getChargeStatus(&status.value);
|
||||
HalResult<int32_t> current_now;
|
||||
*current_now.result = health->getCurrentNowMicroamps(¤t_now.value);
|
||||
return IsBatteryCurrentSignCorrect(status, current_now,
|
||||
false /* accept zero current as unknown */);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_now becomes stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusFromHal) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<BatteryStatus> status;
|
||||
*status.result = health->getChargeStatus(&status.value);
|
||||
HalResult<int32_t> current_average;
|
||||
*current_average.result = health->getCurrentAverageMicroamps(¤t_average.value);
|
||||
return IsBatteryCurrentSignCorrect(status, current_average,
|
||||
false /* accept zero current as unknown */);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_average becomes stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentFromHal) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<BatteryStatus> status;
|
||||
*status.result = health->getChargeStatus(&status.value);
|
||||
HalResult<int32_t> current_now;
|
||||
*current_now.result = health->getCurrentNowMicroamps(¤t_now.value);
|
||||
HalResult<int32_t> current_average;
|
||||
*current_average.result = health->getCurrentAverageMicroamps(¤t_average.value);
|
||||
return IsBatteryCurrentSimilar(status, current_now, current_average);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when current_average becomes stable.";
|
||||
}
|
||||
|
||||
AssertionResult IsBatteryStatusCorrect(const HalResult<BatteryStatus>& status,
|
||||
const HalResult<HealthInfo>& health_info) {
|
||||
// getChargetStatus / getHealthInfo is already tested above. Here, just skip if not ok.
|
||||
if (!health_info.result->isOk()) {
|
||||
return AssertionSuccess() << "getHealthInfo returned "
|
||||
<< health_info.result->getDescription() << ", skipping";
|
||||
}
|
||||
if (!status.result->isOk()) {
|
||||
return AssertionSuccess() << "getChargeStatus returned " << status.result->getDescription()
|
||||
<< ", skipping";
|
||||
}
|
||||
return ::android::hardware::health::test_utils::IsBatteryStatusCorrect(
|
||||
status.value, health_info.value, [](BatteryStatus status) { return toString(status); });
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<BatteryStatus> status;
|
||||
*status.result = health->getChargeStatus(&status.value);
|
||||
HalResult<HealthInfo> health_info;
|
||||
*health_info.result = health->getHealthInfo(&health_info.value);
|
||||
return IsBatteryStatusCorrect(status, health_info);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when battery_status becomes stable.";
|
||||
}
|
||||
|
||||
TEST_P(BatteryTest, ConnectedAgainstStatusInHealthInfo) {
|
||||
auto testOnce = [&]() -> AssertionResult {
|
||||
HalResult<HealthInfo> health_info;
|
||||
*health_info.result = health->getHealthInfo(&health_info.value);
|
||||
return IsBatteryStatusCorrect({health_info.result, health_info.value.batteryStatus},
|
||||
health_info);
|
||||
};
|
||||
|
||||
EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce))
|
||||
<< "You may want to try again later when getHealthInfo becomes stable.";
|
||||
}
|
||||
|
||||
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BatteryTest);
|
||||
INSTANTIATE_TEST_SUITE_P(Health, BatteryTest,
|
||||
testing::ValuesIn(getAidlHalInstanceNames(IHealth::descriptor)),
|
||||
PrintInstanceNameToString);
|
||||
|
||||
} // namespace aidl::android::hardware::health
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
ABinderProcess_setThreadPoolMaxThreadCount(1);
|
||||
ABinderProcess_startThreadPool();
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
29
health/utils/libhealthtest/Android.bp
Normal file
29
health/utils/libhealthtest/Android.bp
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Copyright (C) 2021 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Utils library for VTS tests.
|
||||
cc_library_headers {
|
||||
name: "libhealthtest_headers",
|
||||
static_libs: [
|
||||
"libgmock",
|
||||
"libgtest",
|
||||
],
|
||||
export_static_lib_headers: [
|
||||
"libgmock",
|
||||
"libgtest",
|
||||
],
|
||||
export_include_dirs: [
|
||||
"include",
|
||||
],
|
||||
}
|
149
health/utils/libhealthtest/include/health-test/TestUtils.h
Normal file
149
health/utils/libhealthtest/include/health-test/TestUtils.h
Normal file
|
@ -0,0 +1,149 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace android::hardware::health::test_utils {
|
||||
|
||||
using testing::AssertionFailure;
|
||||
using testing::AssertionResult;
|
||||
using testing::AssertionSuccess;
|
||||
using std::chrono_literals::operator""s;
|
||||
|
||||
// Needs to be called repeatedly within a period of time to ensure values are initialized.
|
||||
template <typename BatteryStatusType, typename BatteryStatusToStringFn>
|
||||
inline AssertionResult IsBatteryCurrentSignCorrect(const BatteryStatusType& status, int32_t current,
|
||||
bool acceptZeroCurrentAsUnknown,
|
||||
const BatteryStatusToStringFn& toString) {
|
||||
// For IHealth.getCurrentNow/Average, if current is not available, it is expected that
|
||||
// the error code is NOT_SUPPORTED, which is checked above. Hence, zero current is
|
||||
// not treated as unknown values.
|
||||
// For IHealth.getHealthInfo, if current is not available, health_info.current_* == 0.
|
||||
// Caller of this function provides current.result == Result::SUCCESS. Hence, just skip the
|
||||
// check.
|
||||
if (current == 0 && acceptZeroCurrentAsUnknown) {
|
||||
return AssertionSuccess()
|
||||
<< "current is 0, which indicates the value may not be available. Skipping.";
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
case BatteryStatusType::UNKNOWN:
|
||||
if (current != 0) {
|
||||
// BatteryStatus may be UNKNOWN initially with a non-zero current value, but
|
||||
// after it is initialized, it should be known.
|
||||
return AssertionFailure()
|
||||
<< "BatteryStatus is UNKNOWN but current is not 0. Actual: " << current;
|
||||
}
|
||||
break;
|
||||
case BatteryStatusType::CHARGING:
|
||||
if (current <= 0) {
|
||||
return AssertionFailure()
|
||||
<< "BatteryStatus is CHARGING but current is not positive. Actual: "
|
||||
<< current;
|
||||
}
|
||||
break;
|
||||
case BatteryStatusType::NOT_CHARGING:
|
||||
if (current > 0) {
|
||||
return AssertionFailure() << "BatteryStatus is " << toString(status)
|
||||
<< " but current is positive. Actual: " << current;
|
||||
}
|
||||
break;
|
||||
case BatteryStatusType::DISCHARGING:
|
||||
if (current >= 0) {
|
||||
return AssertionFailure() << "BatteryStatus is " << toString(status)
|
||||
<< " but current is not negative. Actual: " << current;
|
||||
}
|
||||
break;
|
||||
case BatteryStatusType::FULL:
|
||||
// Battery current may be positive or negative depending on the load.
|
||||
break;
|
||||
default:
|
||||
return AssertionFailure() << "Unknown BatteryStatus " << toString(status);
|
||||
}
|
||||
|
||||
return AssertionSuccess() << "BatteryStatus is " << toString(status)
|
||||
<< " and current has the correct sign: " << current;
|
||||
}
|
||||
|
||||
inline AssertionResult IsValueSimilar(int32_t dividend, int32_t divisor, double factor) {
|
||||
auto difference = abs(dividend - divisor);
|
||||
if (difference > factor * abs(divisor)) {
|
||||
return AssertionFailure() << dividend << " and " << divisor
|
||||
<< " are not similar (factor = " << factor << ")";
|
||||
}
|
||||
return AssertionSuccess() << dividend << " and " << divisor
|
||||
<< " are similar (factor = " << factor << ")";
|
||||
}
|
||||
|
||||
inline AssertionResult IsBatteryCurrentSimilar(int32_t currentNow, int32_t currentAverage,
|
||||
double currentCompareFactor) {
|
||||
// Check that the two values are similar. Note that the two tests uses a different
|
||||
// divisor to ensure that they are actually pretty similar. For example,
|
||||
// IsValueSimilar(5,10,0.4) returns true, but IsValueSimlar(10,5,0.4) returns false.
|
||||
auto res = IsValueSimilar(currentNow, currentAverage, currentCompareFactor)
|
||||
<< " for now vs. average. Check units.";
|
||||
if (!res) return res;
|
||||
res = IsValueSimilar(currentAverage, currentNow, currentCompareFactor)
|
||||
<< " for average vs. now. Check units.";
|
||||
if (!res) return res;
|
||||
return AssertionSuccess() << "currentNow = " << currentNow
|
||||
<< " and currentAverage = " << currentAverage
|
||||
<< " are considered similar.";
|
||||
}
|
||||
|
||||
// Test that f() returns AssertionSuccess() once in a given period of time.
|
||||
template <typename Duration, typename Function>
|
||||
inline AssertionResult SucceedOnce(Duration d, Function f) {
|
||||
AssertionResult result = AssertionFailure() << "Function is never evaluated.";
|
||||
auto end = std::chrono::system_clock::now() + d;
|
||||
while (std::chrono::system_clock::now() <= end) {
|
||||
result = f();
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
std::this_thread::sleep_for(2s);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename BatteryStatusType, typename BatteryInfoType, typename BatteryStatusToStringFn>
|
||||
AssertionResult IsBatteryStatusCorrect(const BatteryStatusType& status,
|
||||
const BatteryInfoType& batteryInfo,
|
||||
const BatteryStatusToStringFn& toString) {
|
||||
bool isConnected = batteryInfo.chargerAcOnline || batteryInfo.chargerUsbOnline ||
|
||||
batteryInfo.chargerWirelessOnline;
|
||||
|
||||
std::stringstream message;
|
||||
message << "BatteryStatus is " << toString(status) << " and " << (isConnected ? "" : "no ")
|
||||
<< "power source is connected: ac=" << batteryInfo.chargerAcOnline
|
||||
<< ", usb=" << batteryInfo.chargerUsbOnline
|
||||
<< ", wireless=" << batteryInfo.chargerWirelessOnline;
|
||||
|
||||
switch (status) {
|
||||
case BatteryStatusType::UNKNOWN: {
|
||||
// Don't enforce anything on isConnected on unknown battery status.
|
||||
// Battery-less devices must report UNKNOWN battery status, but may report true
|
||||
// or false on isConnected.
|
||||
} break;
|
||||
case BatteryStatusType::CHARGING:
|
||||
case BatteryStatusType::NOT_CHARGING:
|
||||
case BatteryStatusType::FULL: {
|
||||
if (!isConnected) {
|
||||
return AssertionFailure() << message.str();
|
||||
}
|
||||
} break;
|
||||
case BatteryStatusType::DISCHARGING: {
|
||||
if (isConnected) {
|
||||
return AssertionFailure() << message.str();
|
||||
}
|
||||
} break;
|
||||
default: {
|
||||
return AssertionFailure() << "Unknown battery status value " << toString(status);
|
||||
} break;
|
||||
}
|
||||
|
||||
return AssertionSuccess() << message.str();
|
||||
}
|
||||
|
||||
} // namespace android::hardware::health::test_utils
|
Loading…
Reference in a new issue