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