Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Yifan Hong | f86271d | 2017-11-06 12:52:20 -0800 | [diff] [blame] | 17 | #define LOG_TAG "health_hidl_hal_test" |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 18 | |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 19 | #include <chrono> |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 20 | #include <mutex> |
Yifan Hong | 03b2a34 | 2019-03-08 11:37:33 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <string> |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 23 | #include <thread> |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 24 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
| 27 | #include <android/hardware/health/1.0/types.h> |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 28 | #include <android/hardware/health/2.0/IHealth.h> |
| 29 | #include <android/hardware/health/2.0/types.h> |
Yifan Hong | e6807dd | 2019-03-07 13:04:33 -0800 | [diff] [blame] | 30 | #include <gflags/gflags.h> |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
| 32 | #include <hidl/GtestPrinter.h> |
| 33 | #include <hidl/ServiceManagement.h> |
| 34 | #include <log/log.h> |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 35 | |
| 36 | using ::testing::AssertionFailure; |
| 37 | using ::testing::AssertionResult; |
| 38 | using ::testing::AssertionSuccess; |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 39 | using namespace std::chrono_literals; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 40 | |
Yifan Hong | e6807dd | 2019-03-07 13:04:33 -0800 | [diff] [blame] | 41 | DEFINE_bool(force, false, "Force test healthd even when the default instance is present."); |
| 42 | |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 43 | // Return expr if it is evaluated to false. |
| 44 | #define TEST_AND_RETURN(expr) \ |
| 45 | do { \ |
| 46 | auto res = (expr); \ |
| 47 | if (!res) return res; \ |
| 48 | } while (0) |
| 49 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | namespace hardware { |
| 52 | namespace health { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 53 | |
| 54 | using V1_0::BatteryStatus; |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 55 | using V1_0::toString; |
| 56 | |
| 57 | namespace V2_0 { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 58 | |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 59 | class HealthHidlTest : public ::testing::TestWithParam<std::string> { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 60 | public: |
| 61 | virtual void SetUp() override { |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 62 | std::string serviceName = GetParam(); |
Yifan Hong | e6807dd | 2019-03-07 13:04:33 -0800 | [diff] [blame] | 63 | |
| 64 | if (serviceName == "backup" && !FLAGS_force && |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 65 | IHealth::getService() != nullptr) { |
Yifan Hong | e6807dd | 2019-03-07 13:04:33 -0800 | [diff] [blame] | 66 | LOG(INFO) << "Skipping tests on healthd because the default instance is present. " |
| 67 | << "Use --force if you really want to test healthd."; |
| 68 | GTEST_SKIP(); |
| 69 | } |
| 70 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 71 | LOG(INFO) << "get service with name:" << serviceName; |
| 72 | ASSERT_FALSE(serviceName.empty()); |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 73 | mHealth = IHealth::getService(serviceName); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 74 | ASSERT_NE(mHealth, nullptr); |
| 75 | } |
| 76 | |
| 77 | sp<IHealth> mHealth; |
| 78 | }; |
| 79 | |
| 80 | class Callback : public IHealthInfoCallback { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 81 | public: |
Hridya Valsaraju | d31932a | 2018-01-17 23:09:24 -0800 | [diff] [blame] | 82 | Return<void> healthInfoChanged(const HealthInfo&) override { |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 83 | std::lock_guard<std::mutex> lock(mMutex); |
| 84 | mInvoked = true; |
| 85 | mInvokedNotify.notify_all(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 86 | return Void(); |
| 87 | } |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 88 | template <typename R, typename P> |
| 89 | bool waitInvoke(std::chrono::duration<R, P> duration) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 90 | std::unique_lock<std::mutex> lock(mMutex); |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 91 | bool r = mInvokedNotify.wait_for(lock, duration, [this] { return this->mInvoked; }); |
| 92 | mInvoked = false; |
| 93 | return r; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 94 | } |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 95 | private: |
| 96 | std::mutex mMutex; |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 97 | std::condition_variable mInvokedNotify; |
| 98 | bool mInvoked = false; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | #define ASSERT_OK(r) ASSERT_TRUE(isOk(r)) |
| 102 | #define EXPECT_OK(r) EXPECT_TRUE(isOk(r)) |
| 103 | template <typename T> |
| 104 | AssertionResult isOk(const Return<T>& r) { |
| 105 | return r.isOk() ? AssertionSuccess() : (AssertionFailure() << r.description()); |
| 106 | } |
| 107 | |
| 108 | #define ASSERT_ALL_OK(r) ASSERT_TRUE(isAllOk(r)) |
| 109 | // Both isOk() and Result::SUCCESS |
| 110 | AssertionResult isAllOk(const Return<Result>& r) { |
| 111 | if (!r.isOk()) { |
| 112 | return AssertionFailure() << r.description(); |
| 113 | } |
| 114 | if (static_cast<Result>(r) != Result::SUCCESS) { |
| 115 | return AssertionFailure() << toString(static_cast<Result>(r)); |
| 116 | } |
| 117 | return AssertionSuccess(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test whether callbacks work. Tested functions are IHealth::registerCallback, |
| 122 | * unregisterCallback, and update. |
| 123 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 124 | TEST_P(HealthHidlTest, Callbacks) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 125 | using namespace std::chrono_literals; |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 126 | sp<Callback> firstCallback = new Callback(); |
| 127 | sp<Callback> secondCallback = new Callback(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 128 | |
| 129 | ASSERT_ALL_OK(mHealth->registerCallback(firstCallback)); |
| 130 | ASSERT_ALL_OK(mHealth->registerCallback(secondCallback)); |
| 131 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 132 | // registerCallback may or may not invoke the callback immediately, so the test needs |
| 133 | // to wait for the invocation. If the implementation chooses not to invoke the callback |
| 134 | // immediately, just wait for some time. |
| 135 | firstCallback->waitInvoke(200ms); |
| 136 | secondCallback->waitInvoke(200ms); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 137 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 138 | // assert that the first callback is invoked when update is called. |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 139 | ASSERT_ALL_OK(mHealth->update()); |
| 140 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 141 | ASSERT_TRUE(firstCallback->waitInvoke(1s)); |
| 142 | ASSERT_TRUE(secondCallback->waitInvoke(1s)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 143 | |
| 144 | ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback)); |
| 145 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 146 | // clear any potentially pending callbacks result from wakealarm / kernel events |
| 147 | // If there is none, just wait for some time. |
| 148 | firstCallback->waitInvoke(200ms); |
| 149 | secondCallback->waitInvoke(200ms); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 150 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 151 | // assert that the second callback is still invoked even though the first is unregistered. |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 152 | ASSERT_ALL_OK(mHealth->update()); |
| 153 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 154 | ASSERT_FALSE(firstCallback->waitInvoke(200ms)); |
| 155 | ASSERT_TRUE(secondCallback->waitInvoke(1s)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 156 | |
| 157 | ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 158 | } |
| 159 | |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 160 | TEST_P(HealthHidlTest, UnregisterNonExistentCallback) { |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 161 | sp<Callback> callback = new Callback(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 162 | auto ret = mHealth->unregisterCallback(callback); |
| 163 | ASSERT_OK(ret); |
| 164 | ASSERT_EQ(Result::NOT_FOUND, static_cast<Result>(ret)) << "Actual: " << toString(ret); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Pass the test if: |
| 169 | * - Property is not supported (res == NOT_SUPPORTED) |
| 170 | * - Result is success, and predicate is true |
| 171 | * @param res the Result value. |
| 172 | * @param valueStr the string representation for actual value (for error message) |
| 173 | * @param pred a predicate that test whether the value is valid |
| 174 | */ |
| 175 | #define EXPECT_VALID_OR_UNSUPPORTED_PROP(res, valueStr, pred) \ |
| 176 | EXPECT_TRUE(isPropertyOk(res, valueStr, pred, #pred)) |
| 177 | |
| 178 | AssertionResult isPropertyOk(Result res, const std::string& valueStr, bool pred, |
| 179 | const std::string& predStr) { |
| 180 | if (res == Result::SUCCESS) { |
| 181 | if (pred) { |
| 182 | return AssertionSuccess(); |
| 183 | } |
| 184 | return AssertionFailure() << "value doesn't match.\nActual: " << valueStr |
| 185 | << "\nExpected: " << predStr; |
| 186 | } |
| 187 | if (res == Result::NOT_SUPPORTED) { |
| 188 | return AssertionSuccess(); |
| 189 | } |
| 190 | return AssertionFailure() << "Result is not SUCCESS or NOT_SUPPORTED: " << toString(res); |
| 191 | } |
| 192 | |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 193 | bool verifyStorageInfo(const hidl_vec<struct StorageInfo>& info) { |
| 194 | for (size_t i = 0; i < info.size(); i++) { |
| 195 | if (!(0 <= info[i].eol && info[i].eol <= 3 && 0 <= info[i].lifetimeA && |
| 196 | info[i].lifetimeA <= 0x0B && 0 <= info[i].lifetimeB && info[i].lifetimeB <= 0x0B)) { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 204 | template <typename T> |
| 205 | bool verifyEnum(T value) { |
Steven Moreland | c90461c | 2018-05-01 16:54:09 -0700 | [diff] [blame] | 206 | for (auto it : hidl_enum_range<T>()) { |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 207 | if (it == value) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | bool verifyHealthInfo(const HealthInfo& health_info) { |
Hridya Valsaraju | 075c182 | 2018-04-03 11:19:08 -0700 | [diff] [blame] | 216 | if (!verifyStorageInfo(health_info.storageInfos)) { |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | using V1_0::BatteryStatus; |
| 221 | using V1_0::BatteryHealth; |
| 222 | |
Robin Lee | ac5a0d3 | 2019-10-09 15:48:17 +0200 | [diff] [blame] | 223 | if (!((health_info.legacy.batteryCurrent != INT32_MIN) && |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 224 | (0 <= health_info.legacy.batteryLevel && health_info.legacy.batteryLevel <= 100) && |
| 225 | verifyEnum<BatteryHealth>(health_info.legacy.batteryHealth) && |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 226 | verifyEnum<BatteryStatus>(health_info.legacy.batteryStatus))) { |
| 227 | return false; |
| 228 | } |
| 229 | |
Robin Lee | ac5a0d3 | 2019-10-09 15:48:17 +0200 | [diff] [blame] | 230 | if (health_info.legacy.batteryPresent) { |
| 231 | // If a battery is present, the battery status must be known. |
| 232 | if (!((health_info.legacy.batteryChargeCounter > 0) && |
| 233 | (health_info.legacy.batteryStatus != BatteryStatus::UNKNOWN))) { |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | /* |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 242 | * Tests the values returned by getChargeCounter() from interface IHealth. |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 243 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 244 | TEST_P(HealthHidlTest, getChargeCounter) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 245 | EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) { |
| 246 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0); |
| 247 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Tests the values returned by getCurrentNow() from interface IHealth. |
| 252 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 253 | TEST_P(HealthHidlTest, getCurrentNow) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 254 | EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) { |
| 255 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 256 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Tests the values returned by getCurrentAverage() from interface IHealth. |
| 261 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 262 | TEST_P(HealthHidlTest, getCurrentAverage) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 263 | EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) { |
| 264 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 265 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Tests the values returned by getCapacity() from interface IHealth. |
| 270 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 271 | TEST_P(HealthHidlTest, getCapacity) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 272 | EXPECT_OK(mHealth->getCapacity([](auto result, auto value) { |
| 273 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100); |
| 274 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Tests the values returned by getEnergyCounter() from interface IHealth. |
| 279 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 280 | TEST_P(HealthHidlTest, getEnergyCounter) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 281 | EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) { |
| 282 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN); |
| 283 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Tests the values returned by getChargeStatus() from interface IHealth. |
| 288 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 289 | TEST_P(HealthHidlTest, getChargeStatus) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 290 | EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) { |
Yifan Hong | 5ac6f5b | 2020-01-27 16:20:49 -0800 | [diff] [blame] | 291 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyEnum<BatteryStatus>(value)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 292 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Tests the values returned by getStorageInfo() from interface IHealth. |
| 297 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 298 | TEST_P(HealthHidlTest, getStorageInfo) { |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 299 | EXPECT_OK(mHealth->getStorageInfo([](auto result, auto& value) { |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 300 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyStorageInfo(value)); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 301 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Tests the values returned by getDiskStats() from interface IHealth. |
| 306 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 307 | TEST_P(HealthHidlTest, getDiskStats) { |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 308 | EXPECT_OK(mHealth->getDiskStats([](auto result, auto& value) { |
Hridya Valsaraju | 075c182 | 2018-04-03 11:19:08 -0700 | [diff] [blame] | 309 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), true); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 310 | })); |
Yifan Hong | 26c1200 | 2018-10-02 14:52:02 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Tests the values returned by getHealthInfo() from interface IHealth. |
| 315 | */ |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 316 | TEST_P(HealthHidlTest, getHealthInfo) { |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 317 | EXPECT_OK(mHealth->getHealthInfo([](auto result, auto& value) { |
| 318 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyHealthInfo(value)); |
| 319 | })); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame^] | 322 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HealthHidlTest); |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 323 | INSTANTIATE_TEST_SUITE_P( |
| 324 | PerInstance, HealthHidlTest, |
| 325 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IHealth::descriptor)), |
| 326 | android::hardware::PrintInstanceNameToString); |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 327 | |
| 328 | // For battery current tests, value may not be stable if the battery current has fluctuated. |
| 329 | // Retry in a bit more time (with the following timeout) and consider the test successful if it |
| 330 | // has succeed once. |
| 331 | static constexpr auto gBatteryTestTimeout = 1min; |
| 332 | // Tests on battery current signs are only enforced on devices launching with Android 11. |
| 333 | static constexpr int64_t gBatteryTestMinShippingApiLevel = 30; |
| 334 | static constexpr double gCurrentCompareFactor = 0.50; |
| 335 | |
| 336 | // Tuple for all IHealth::get* API return values. |
| 337 | template <typename T> |
| 338 | struct HalResult { |
| 339 | Result result; |
| 340 | T value; |
| 341 | }; |
| 342 | |
| 343 | // Needs to be called repeatedly within a period of time to ensure values are initialized. |
| 344 | static AssertionResult IsBatteryCurrentSignCorrect(HalResult<BatteryStatus> status, |
| 345 | HalResult<int32_t> current, |
| 346 | bool acceptZeroCurrentAsUnknown) { |
| 347 | // getChargeStatus / getCurrentNow / getCurrentAverage / getHealthInfo already tested above. |
| 348 | // Here, just skip if not ok. |
| 349 | if (status.result != Result::SUCCESS) { |
| 350 | return AssertionSuccess() << "getChargeStatus / getHealthInfo returned " |
| 351 | << toString(status.result) << ", skipping"; |
| 352 | } |
| 353 | |
| 354 | if (current.result != Result::SUCCESS) { |
| 355 | return AssertionSuccess() << "getCurrentNow / getCurrentAverage returned " |
| 356 | << toString(current.result) << ", skipping"; |
| 357 | } |
| 358 | |
| 359 | // For IHealth.getCurrentNow/Average, if current is not available, it is expected that |
| 360 | // current.result == Result::NOT_SUPPORTED, which is checked above. Hence, zero current is |
| 361 | // not treated as unknown values. |
| 362 | // For IHealth.getHealthInfo, if current is not available, health_info.current_* == 0. |
| 363 | // Caller of this function provides current.result == Result::SUCCESS. Hence, just skip the |
| 364 | // check. |
| 365 | if (current.value == 0 && acceptZeroCurrentAsUnknown) { |
| 366 | return AssertionSuccess() |
| 367 | << "current is 0, which indicates the value may not be available. Skipping."; |
| 368 | } |
| 369 | |
| 370 | switch (status.value) { |
| 371 | case BatteryStatus::UNKNOWN: |
| 372 | if (current.value != 0) { |
| 373 | // BatteryStatus may be UNKNOWN initially with a non-zero current value, but |
| 374 | // after it is initialized, it should be known. |
| 375 | return AssertionFailure() |
| 376 | << "BatteryStatus is UNKNOWN but current is not 0. Actual: " |
| 377 | << current.value; |
| 378 | } |
| 379 | break; |
| 380 | case BatteryStatus::CHARGING: |
| 381 | if (current.value <= 0) { |
| 382 | return AssertionFailure() |
| 383 | << "BatteryStatus is CHARGING but current is not positive. Actual: " |
| 384 | << current.value; |
| 385 | } |
| 386 | break; |
| 387 | case BatteryStatus::NOT_CHARGING: |
| 388 | if (current.value > 0) { |
| 389 | return AssertionFailure() << "BatteryStatus is " << toString(status.value) |
| 390 | << " but current is positive. Actual: " << current.value; |
| 391 | } |
| 392 | break; |
| 393 | case BatteryStatus::DISCHARGING: |
| 394 | if (current.value >= 0) { |
| 395 | return AssertionFailure() |
| 396 | << "BatteryStatus is " << toString(status.value) |
| 397 | << " but current is not negative. Actual: " << current.value; |
| 398 | } |
| 399 | break; |
| 400 | case BatteryStatus::FULL: |
| 401 | // Battery current may be positive or negative depending on the load. |
| 402 | break; |
| 403 | default: |
| 404 | return AssertionFailure() << "Unknown BatteryStatus " << toString(status.value); |
| 405 | } |
| 406 | |
| 407 | return AssertionSuccess() << "BatteryStatus is " << toString(status.value) |
| 408 | << " and current has the correct sign: " << current.value; |
| 409 | } |
| 410 | |
| 411 | static AssertionResult IsValueSimilar(int32_t dividend, int32_t divisor, double factor) { |
| 412 | auto difference = abs(dividend - divisor); |
| 413 | if (difference > factor * abs(divisor)) { |
| 414 | return AssertionFailure() << dividend << " and " << divisor << " are not similar."; |
| 415 | } |
| 416 | return AssertionSuccess() << dividend << " and " << divisor << " are similar."; |
| 417 | } |
| 418 | |
| 419 | static AssertionResult IsBatteryCurrentSimilar(HalResult<BatteryStatus> status, |
| 420 | HalResult<int32_t> currentNow, |
| 421 | HalResult<int32_t> currentAverage) { |
| 422 | if (status.result == Result::SUCCESS && status.value == BatteryStatus::FULL) { |
| 423 | // No reason to test on full battery because battery current load fluctuates. |
| 424 | return AssertionSuccess() << "Battery is full, skipping"; |
| 425 | } |
| 426 | |
| 427 | // getCurrentNow / getCurrentAverage / getHealthInfo already tested above. Here, just skip if |
| 428 | // not SUCCESS or value 0. |
| 429 | if (currentNow.result != Result::SUCCESS || currentNow.value == 0) { |
| 430 | return AssertionSuccess() << "getCurrentNow returned " << toString(currentNow.result) |
| 431 | << " with value " << currentNow.value << ", skipping"; |
| 432 | } |
| 433 | |
| 434 | if (currentAverage.result != Result::SUCCESS || currentAverage.value == 0) { |
| 435 | return AssertionSuccess() << "getCurrentAverage returned " |
| 436 | << toString(currentAverage.result) << " with value " |
| 437 | << currentAverage.value << ", skipping"; |
| 438 | } |
| 439 | |
| 440 | // Check that the two values are similar. Note that the two tests uses a different |
| 441 | // divisor to ensure that they are actually pretty similar. For example, |
| 442 | // IsValueSimilar(5,10,0.4) returns true, but IsValueSimlar(10,5,0.4) returns false. |
| 443 | TEST_AND_RETURN(IsValueSimilar(currentNow.value, currentAverage.value, gCurrentCompareFactor) |
| 444 | << " for now vs. average. Check units."); |
| 445 | TEST_AND_RETURN(IsValueSimilar(currentAverage.value, currentNow.value, gCurrentCompareFactor) |
| 446 | << " for average vs. now. Check units."); |
| 447 | return AssertionSuccess() << "currentNow = " << currentNow.value |
| 448 | << " and currentAverage = " << currentAverage.value |
| 449 | << " are considered similar."; |
| 450 | } |
| 451 | |
| 452 | // Test that f() returns AssertionSuccess() once in a given period of time. |
| 453 | template <typename Duration, typename Function> |
| 454 | static AssertionResult SucceedOnce(Duration d, Function f) { |
| 455 | AssertionResult result = AssertionFailure() << "Function never evaluated."; |
| 456 | auto end = std::chrono::system_clock::now() + d; |
| 457 | while (std::chrono::system_clock::now() <= end) { |
| 458 | result = f(); |
| 459 | if (result) { |
| 460 | return result; |
| 461 | } |
| 462 | std::this_thread::sleep_for(2s); |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | uint64_t GetShippingApiLevel() { |
| 468 | uint64_t api_level = android::base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0); |
| 469 | if (api_level != 0) { |
| 470 | return api_level; |
| 471 | } |
| 472 | return android::base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0); |
| 473 | } |
| 474 | |
| 475 | class BatteryTest : public HealthHidlTest { |
| 476 | public: |
| 477 | void SetUp() override { |
| 478 | HealthHidlTest::SetUp(); |
| 479 | |
| 480 | auto shippingApiLevel = GetShippingApiLevel(); |
| 481 | if (shippingApiLevel < gBatteryTestMinShippingApiLevel) { |
| 482 | GTEST_SKIP() << "Skipping on devices with first API level " << shippingApiLevel; |
| 483 | } |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusInHealthInfo) { |
| 488 | auto testOnce = [&]() -> AssertionResult { |
| 489 | HalResult<HealthInfo> healthInfo; |
| 490 | TEST_AND_RETURN(isOk(mHealth->getHealthInfo([&](auto result, const auto& value) { |
| 491 | healthInfo = {result, value}; |
| 492 | }))); |
| 493 | |
| 494 | return IsBatteryCurrentSignCorrect( |
| 495 | {healthInfo.result, healthInfo.value.legacy.batteryStatus}, |
| 496 | {healthInfo.result, healthInfo.value.legacy.batteryCurrent}, |
| 497 | true /* accept zero current as unknown */); |
| 498 | }; |
| 499 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 500 | << "You may want to try again later when current_now becomes stable."; |
| 501 | } |
| 502 | |
| 503 | TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusInHealthInfo) { |
| 504 | auto testOnce = [&]() -> AssertionResult { |
| 505 | HalResult<HealthInfo> healthInfo; |
| 506 | TEST_AND_RETURN(isOk(mHealth->getHealthInfo([&](auto result, const auto& value) { |
| 507 | healthInfo = {result, value}; |
| 508 | }))); |
| 509 | return IsBatteryCurrentSignCorrect( |
| 510 | {healthInfo.result, healthInfo.value.legacy.batteryStatus}, |
| 511 | {healthInfo.result, healthInfo.value.batteryCurrentAverage}, |
| 512 | true /* accept zero current as unknown */); |
| 513 | }; |
| 514 | |
| 515 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 516 | << "You may want to try again later when current_average becomes stable."; |
| 517 | } |
| 518 | |
| 519 | TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentInHealthInfo) { |
| 520 | auto testOnce = [&]() -> AssertionResult { |
| 521 | HalResult<HealthInfo> healthInfo; |
| 522 | TEST_AND_RETURN(isOk(mHealth->getHealthInfo([&](auto result, const auto& value) { |
| 523 | healthInfo = {result, value}; |
| 524 | }))); |
| 525 | return IsBatteryCurrentSimilar({healthInfo.result, healthInfo.value.legacy.batteryStatus}, |
| 526 | {healthInfo.result, healthInfo.value.legacy.batteryCurrent}, |
| 527 | {healthInfo.result, healthInfo.value.batteryCurrentAverage}); |
| 528 | }; |
| 529 | |
| 530 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 531 | << "You may want to try again later when current_now and current_average becomes " |
| 532 | "stable."; |
| 533 | } |
| 534 | |
| 535 | TEST_P(BatteryTest, InstantCurrentAgainstChargeStatusFromHal) { |
| 536 | auto testOnce = [&]() -> AssertionResult { |
| 537 | HalResult<BatteryStatus> status; |
| 538 | HalResult<int32_t> currentNow; |
| 539 | TEST_AND_RETURN(isOk(mHealth->getChargeStatus([&](auto result, auto value) { |
| 540 | status = {result, value}; |
| 541 | }))); |
| 542 | TEST_AND_RETURN(isOk(mHealth->getCurrentNow([&](auto result, auto value) { |
| 543 | currentNow = {result, value}; |
| 544 | }))); |
| 545 | |
| 546 | return IsBatteryCurrentSignCorrect(status, currentNow, |
| 547 | false /* accept zero current as unknown */); |
| 548 | }; |
| 549 | |
| 550 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 551 | << "You may want to try again later when current_now becomes stable."; |
| 552 | } |
| 553 | |
| 554 | TEST_P(BatteryTest, AverageCurrentAgainstChargeStatusFromHal) { |
| 555 | auto testOnce = [&]() -> AssertionResult { |
| 556 | HalResult<BatteryStatus> status; |
| 557 | TEST_AND_RETURN(isOk(mHealth->getChargeStatus([&](auto result, auto value) { |
| 558 | status = {result, value}; |
| 559 | }))); |
| 560 | HalResult<int32_t> currentAverage; |
| 561 | TEST_AND_RETURN(isOk(mHealth->getCurrentAverage([&](auto result, auto value) { |
| 562 | currentAverage = {result, value}; |
| 563 | }))); |
| 564 | return IsBatteryCurrentSignCorrect(status, currentAverage, |
| 565 | false /* accept zero current as unknown */); |
| 566 | }; |
| 567 | |
| 568 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 569 | << "You may want to try again later when current_average becomes stable."; |
| 570 | } |
| 571 | |
| 572 | TEST_P(BatteryTest, InstantCurrentAgainstAverageCurrentFromHal) { |
| 573 | auto testOnce = [&]() -> AssertionResult { |
| 574 | HalResult<BatteryStatus> status; |
| 575 | TEST_AND_RETURN(isOk(mHealth->getChargeStatus([&](auto result, auto value) { |
| 576 | status = {result, value}; |
| 577 | }))); |
| 578 | HalResult<int32_t> currentNow; |
| 579 | TEST_AND_RETURN(isOk(mHealth->getCurrentNow([&](auto result, auto value) { |
| 580 | currentNow = {result, value}; |
| 581 | }))); |
| 582 | HalResult<int32_t> currentAverage; |
| 583 | TEST_AND_RETURN(isOk(mHealth->getCurrentAverage([&](auto result, auto value) { |
| 584 | currentAverage = {result, value}; |
| 585 | }))); |
| 586 | return IsBatteryCurrentSimilar(status, currentNow, currentAverage); |
| 587 | }; |
| 588 | |
| 589 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 590 | << "You may want to try again later when current_average becomes stable."; |
| 591 | } |
| 592 | |
| 593 | AssertionResult IsBatteryStatusCorrect(HalResult<BatteryStatus> status, |
| 594 | HalResult<HealthInfo> healthInfo) { |
| 595 | // getChargetStatus / getHealthInfo is already tested above. Here, just skip if not ok. |
| 596 | if (healthInfo.result != Result::SUCCESS) { |
| 597 | return AssertionSuccess() << "getHealthInfo returned " << toString(healthInfo.result) |
| 598 | << ", skipping"; |
| 599 | } |
| 600 | if (status.result != Result::SUCCESS) { |
| 601 | return AssertionSuccess() << "getChargeStatus returned " << toString(status.result) |
| 602 | << ", skipping"; |
| 603 | } |
| 604 | |
| 605 | const auto& batteryInfo = healthInfo.value.legacy; |
| 606 | bool isConnected = batteryInfo.chargerAcOnline || batteryInfo.chargerUsbOnline || |
| 607 | batteryInfo.chargerWirelessOnline; |
| 608 | |
| 609 | std::stringstream message; |
| 610 | message << "BatteryStatus is " << toString(status.value) << " and " |
| 611 | << (isConnected ? "" : "no ") |
| 612 | << "power source is connected: ac=" << batteryInfo.chargerAcOnline |
| 613 | << ", usb=" << batteryInfo.chargerUsbOnline |
| 614 | << ", wireless=" << batteryInfo.chargerWirelessOnline; |
| 615 | |
| 616 | switch (status.value) { |
| 617 | case BatteryStatus::UNKNOWN: { |
| 618 | // Don't enforce anything on isConnected on unknown battery status. |
| 619 | // Battery-less devices must report UNKNOWN battery status, but may report true |
| 620 | // or false on isConnected. |
| 621 | } break; |
| 622 | case BatteryStatus::CHARGING: |
| 623 | case BatteryStatus::NOT_CHARGING: |
| 624 | case BatteryStatus::FULL: { |
| 625 | if (!isConnected) { |
| 626 | return AssertionFailure() << message.str(); |
| 627 | } |
| 628 | } break; |
| 629 | case BatteryStatus::DISCHARGING: { |
| 630 | if (isConnected) { |
| 631 | return AssertionFailure() << message.str(); |
| 632 | } |
| 633 | } break; |
| 634 | default: { |
| 635 | return AssertionFailure() << "Unknown battery status value " << toString(status.value); |
| 636 | } break; |
| 637 | } |
| 638 | |
| 639 | return AssertionSuccess() << message.str(); |
| 640 | } |
| 641 | |
| 642 | TEST_P(BatteryTest, ConnectedAgainstStatusFromHal) { |
| 643 | auto testOnce = [&]() -> AssertionResult { |
| 644 | HalResult<BatteryStatus> status; |
| 645 | TEST_AND_RETURN(isOk(mHealth->getChargeStatus([&](auto result, auto value) { |
| 646 | status = {result, value}; |
| 647 | }))); |
| 648 | HalResult<HealthInfo> healthInfo; |
| 649 | TEST_AND_RETURN(isOk(mHealth->getHealthInfo([&](auto result, const auto& value) { |
| 650 | healthInfo = {result, value}; |
| 651 | }))); |
| 652 | return IsBatteryStatusCorrect(status, healthInfo); |
| 653 | }; |
| 654 | |
| 655 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 656 | << "You may want to try again later when battery_status becomes stable."; |
| 657 | } |
| 658 | |
| 659 | TEST_P(BatteryTest, ConnectedAgainstStatusInHealthInfo) { |
| 660 | auto testOnce = [&]() -> AssertionResult { |
| 661 | HalResult<HealthInfo> healthInfo; |
| 662 | TEST_AND_RETURN(isOk(mHealth->getHealthInfo([&](auto result, const auto& value) { |
| 663 | healthInfo = {result, value}; |
| 664 | }))); |
| 665 | return IsBatteryStatusCorrect({healthInfo.result, healthInfo.value.legacy.batteryStatus}, |
| 666 | healthInfo); |
| 667 | }; |
| 668 | |
| 669 | EXPECT_TRUE(SucceedOnce(gBatteryTestTimeout, testOnce)) |
| 670 | << "You may want to try again later when getHealthInfo becomes stable."; |
| 671 | } |
| 672 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame^] | 673 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BatteryTest); |
Yifan Hong | 94841c9 | 2020-01-28 16:11:39 -0800 | [diff] [blame] | 674 | INSTANTIATE_TEST_SUITE_P( |
| 675 | PerInstance, BatteryTest, |
| 676 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IHealth::descriptor)), |
| 677 | android::hardware::PrintInstanceNameToString); |
| 678 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 679 | } // namespace V2_0 |
| 680 | } // namespace health |
| 681 | } // namespace hardware |
| 682 | } // namespace android |
| 683 | |
| 684 | int main(int argc, char** argv) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 685 | ::testing::InitGoogleTest(&argc, argv); |
Yifan Hong | e6807dd | 2019-03-07 13:04:33 -0800 | [diff] [blame] | 686 | gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */); |
nelsonli | 4da3ca5 | 2019-12-10 17:17:09 +0800 | [diff] [blame] | 687 | return RUN_ALL_TESTS(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 688 | } |