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 | |
| 19 | #include <mutex> |
| 20 | |
| 21 | #include <VtsHalHidlTargetTestBase.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android/hardware/health/2.0/IHealth.h> |
| 24 | #include <android/hardware/health/2.0/types.h> |
Yifan Hong | 0d48dc8 | 2019-03-07 13:04:33 -0800 | [diff] [blame^] | 25 | #include <gflags/gflags.h> |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 26 | |
| 27 | using ::testing::AssertionFailure; |
| 28 | using ::testing::AssertionResult; |
| 29 | using ::testing::AssertionSuccess; |
| 30 | using ::testing::VtsHalHidlTargetTestBase; |
| 31 | using ::testing::VtsHalHidlTargetTestEnvBase; |
| 32 | |
Yifan Hong | 0d48dc8 | 2019-03-07 13:04:33 -0800 | [diff] [blame^] | 33 | DEFINE_bool(force, false, "Force test healthd even when the default instance is present."); |
| 34 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | namespace health { |
| 38 | namespace V2_0 { |
| 39 | |
| 40 | using V1_0::BatteryStatus; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 41 | |
| 42 | // Test environment for graphics.composer |
| 43 | class HealthHidlEnvironment : public VtsHalHidlTargetTestEnvBase { |
| 44 | public: |
| 45 | // get the test environment singleton |
| 46 | static HealthHidlEnvironment* Instance() { |
| 47 | static HealthHidlEnvironment* instance = new HealthHidlEnvironment; |
| 48 | return instance; |
| 49 | } |
| 50 | |
| 51 | virtual void registerTestServices() override { registerTestService<IHealth>(); } |
| 52 | |
| 53 | private: |
| 54 | HealthHidlEnvironment() {} |
| 55 | |
| 56 | GTEST_DISALLOW_COPY_AND_ASSIGN_(HealthHidlEnvironment); |
| 57 | }; |
| 58 | |
| 59 | class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 60 | public: |
| 61 | virtual void SetUp() override { |
| 62 | std::string serviceName = HealthHidlEnvironment::Instance()->getServiceName<IHealth>(); |
Yifan Hong | 0d48dc8 | 2019-03-07 13:04:33 -0800 | [diff] [blame^] | 63 | |
| 64 | if (serviceName == "backup" && !FLAGS_force && |
| 65 | ::testing::VtsHalHidlTargetTestBase::getService<IHealth>() != nullptr) { |
| 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()); |
| 73 | mHealth = ::testing::VtsHalHidlTargetTestBase::getService<IHealth>(serviceName); |
| 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 | */ |
| 124 | TEST_F(HealthHidlTest, Callbacks) { |
| 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 | |
| 160 | TEST_F(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) { |
| 206 | for (auto it : hidl_enum_iterator<T>()) { |
| 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 | |
| 223 | if (!((health_info.legacy.batteryChargeCounter > 0) && |
| 224 | (health_info.legacy.batteryCurrent != INT32_MIN) && |
| 225 | (0 <= health_info.legacy.batteryLevel && health_info.legacy.batteryLevel <= 100) && |
| 226 | verifyEnum<BatteryHealth>(health_info.legacy.batteryHealth) && |
| 227 | (health_info.legacy.batteryStatus != BatteryStatus::UNKNOWN) && |
| 228 | verifyEnum<BatteryStatus>(health_info.legacy.batteryStatus))) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Tests the values returned by getChargeCounter(), |
| 237 | * getCurrentNow(), getCurrentAverage(), getCapacity(), getEnergyCounter(), |
| 238 | * getChargeStatus(), getStorageInfo(), getDiskStats() and getHealthInfo() from |
| 239 | * interface IHealth. |
| 240 | */ |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 241 | TEST_F(HealthHidlTest, Properties) { |
| 242 | EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) { |
| 243 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0); |
| 244 | })); |
| 245 | EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) { |
| 246 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 247 | })); |
| 248 | EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) { |
| 249 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 250 | })); |
| 251 | EXPECT_OK(mHealth->getCapacity([](auto result, auto value) { |
| 252 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100); |
| 253 | })); |
| 254 | EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) { |
| 255 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN); |
| 256 | })); |
| 257 | EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) { |
| 258 | EXPECT_VALID_OR_UNSUPPORTED_PROP( |
| 259 | result, toString(value), |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 260 | value != BatteryStatus::UNKNOWN && verifyEnum<BatteryStatus>(value)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 261 | })); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 262 | EXPECT_OK(mHealth->getStorageInfo([](auto result, auto& value) { |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 263 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyStorageInfo(value)); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 264 | })); |
| 265 | EXPECT_OK(mHealth->getDiskStats([](auto result, auto& value) { |
Hridya Valsaraju | 075c182 | 2018-04-03 11:19:08 -0700 | [diff] [blame] | 266 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), true); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 267 | })); |
Hridya Valsaraju | 87e2960 | 2018-01-12 17:46:22 -0800 | [diff] [blame] | 268 | EXPECT_OK(mHealth->getHealthInfo([](auto result, auto& value) { |
| 269 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyHealthInfo(value)); |
| 270 | })); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | } // namespace V2_0 |
| 274 | } // namespace health |
| 275 | } // namespace hardware |
| 276 | } // namespace android |
| 277 | |
| 278 | int main(int argc, char** argv) { |
| 279 | using ::android::hardware::health::V2_0::HealthHidlEnvironment; |
| 280 | ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance()); |
| 281 | ::testing::InitGoogleTest(&argc, argv); |
| 282 | HealthHidlEnvironment::Instance()->init(&argc, argv); |
Yifan Hong | 0d48dc8 | 2019-03-07 13:04:33 -0800 | [diff] [blame^] | 283 | gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 284 | int status = RUN_ALL_TESTS(); |
| 285 | LOG(INFO) << "Test result = " << status; |
| 286 | return status; |
| 287 | } |