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> |
| 25 | |
| 26 | using ::testing::AssertionFailure; |
| 27 | using ::testing::AssertionResult; |
| 28 | using ::testing::AssertionSuccess; |
| 29 | using ::testing::VtsHalHidlTargetTestBase; |
| 30 | using ::testing::VtsHalHidlTargetTestEnvBase; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace health { |
| 35 | namespace V2_0 { |
| 36 | |
| 37 | using V1_0::BatteryStatus; |
Yifan Hong | f86271d | 2017-11-06 12:52:20 -0800 | [diff] [blame] | 38 | using V1_0::HealthInfo; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 39 | |
| 40 | // Test environment for graphics.composer |
| 41 | class HealthHidlEnvironment : public VtsHalHidlTargetTestEnvBase { |
| 42 | public: |
| 43 | // get the test environment singleton |
| 44 | static HealthHidlEnvironment* Instance() { |
| 45 | static HealthHidlEnvironment* instance = new HealthHidlEnvironment; |
| 46 | return instance; |
| 47 | } |
| 48 | |
| 49 | virtual void registerTestServices() override { registerTestService<IHealth>(); } |
| 50 | |
| 51 | private: |
| 52 | HealthHidlEnvironment() {} |
| 53 | |
| 54 | GTEST_DISALLOW_COPY_AND_ASSIGN_(HealthHidlEnvironment); |
| 55 | }; |
| 56 | |
| 57 | class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 58 | public: |
| 59 | virtual void SetUp() override { |
| 60 | std::string serviceName = HealthHidlEnvironment::Instance()->getServiceName<IHealth>(); |
| 61 | LOG(INFO) << "get service with name:" << serviceName; |
| 62 | ASSERT_FALSE(serviceName.empty()); |
| 63 | mHealth = ::testing::VtsHalHidlTargetTestBase::getService<IHealth>(serviceName); |
| 64 | ASSERT_NE(mHealth, nullptr); |
| 65 | } |
| 66 | |
| 67 | sp<IHealth> mHealth; |
| 68 | }; |
| 69 | |
| 70 | class Callback : public IHealthInfoCallback { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 71 | public: |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 72 | Return<void> healthInfoChanged(const HealthInfo&) override { |
| 73 | std::lock_guard<std::mutex> lock(mMutex); |
| 74 | mInvoked = true; |
| 75 | mInvokedNotify.notify_all(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 76 | return Void(); |
| 77 | } |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 78 | template <typename R, typename P> |
| 79 | bool waitInvoke(std::chrono::duration<R, P> duration) { |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 80 | std::unique_lock<std::mutex> lock(mMutex); |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 81 | bool r = mInvokedNotify.wait_for(lock, duration, [this] { return this->mInvoked; }); |
| 82 | mInvoked = false; |
| 83 | return r; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 84 | } |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 85 | private: |
| 86 | std::mutex mMutex; |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 87 | std::condition_variable mInvokedNotify; |
| 88 | bool mInvoked = false; |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #define ASSERT_OK(r) ASSERT_TRUE(isOk(r)) |
| 92 | #define EXPECT_OK(r) EXPECT_TRUE(isOk(r)) |
| 93 | template <typename T> |
| 94 | AssertionResult isOk(const Return<T>& r) { |
| 95 | return r.isOk() ? AssertionSuccess() : (AssertionFailure() << r.description()); |
| 96 | } |
| 97 | |
| 98 | #define ASSERT_ALL_OK(r) ASSERT_TRUE(isAllOk(r)) |
| 99 | // Both isOk() and Result::SUCCESS |
| 100 | AssertionResult isAllOk(const Return<Result>& r) { |
| 101 | if (!r.isOk()) { |
| 102 | return AssertionFailure() << r.description(); |
| 103 | } |
| 104 | if (static_cast<Result>(r) != Result::SUCCESS) { |
| 105 | return AssertionFailure() << toString(static_cast<Result>(r)); |
| 106 | } |
| 107 | return AssertionSuccess(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Test whether callbacks work. Tested functions are IHealth::registerCallback, |
| 112 | * unregisterCallback, and update. |
| 113 | */ |
| 114 | TEST_F(HealthHidlTest, Callbacks) { |
| 115 | using namespace std::chrono_literals; |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 116 | sp<Callback> firstCallback = new Callback(); |
| 117 | sp<Callback> secondCallback = new Callback(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 118 | |
| 119 | ASSERT_ALL_OK(mHealth->registerCallback(firstCallback)); |
| 120 | ASSERT_ALL_OK(mHealth->registerCallback(secondCallback)); |
| 121 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 122 | // registerCallback may or may not invoke the callback immediately, so the test needs |
| 123 | // to wait for the invocation. If the implementation chooses not to invoke the callback |
| 124 | // immediately, just wait for some time. |
| 125 | firstCallback->waitInvoke(200ms); |
| 126 | secondCallback->waitInvoke(200ms); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 127 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 128 | // assert that the first callback is invoked when update is called. |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 129 | ASSERT_ALL_OK(mHealth->update()); |
| 130 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 131 | ASSERT_TRUE(firstCallback->waitInvoke(1s)); |
| 132 | ASSERT_TRUE(secondCallback->waitInvoke(1s)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 133 | |
| 134 | ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback)); |
| 135 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 136 | // clear any potentially pending callbacks result from wakealarm / kernel events |
| 137 | // If there is none, just wait for some time. |
| 138 | firstCallback->waitInvoke(200ms); |
| 139 | secondCallback->waitInvoke(200ms); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 140 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 141 | // 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] | 142 | ASSERT_ALL_OK(mHealth->update()); |
| 143 | |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 144 | ASSERT_FALSE(firstCallback->waitInvoke(200ms)); |
| 145 | ASSERT_TRUE(secondCallback->waitInvoke(1s)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 146 | |
| 147 | ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback)); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST_F(HealthHidlTest, UnregisterNonExistentCallback) { |
Yifan Hong | d6ea57e | 2017-11-20 14:41:09 -0800 | [diff] [blame] | 151 | sp<Callback> callback = new Callback(); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 152 | auto ret = mHealth->unregisterCallback(callback); |
| 153 | ASSERT_OK(ret); |
| 154 | ASSERT_EQ(Result::NOT_FOUND, static_cast<Result>(ret)) << "Actual: " << toString(ret); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Pass the test if: |
| 159 | * - Property is not supported (res == NOT_SUPPORTED) |
| 160 | * - Result is success, and predicate is true |
| 161 | * @param res the Result value. |
| 162 | * @param valueStr the string representation for actual value (for error message) |
| 163 | * @param pred a predicate that test whether the value is valid |
| 164 | */ |
| 165 | #define EXPECT_VALID_OR_UNSUPPORTED_PROP(res, valueStr, pred) \ |
| 166 | EXPECT_TRUE(isPropertyOk(res, valueStr, pred, #pred)) |
| 167 | |
| 168 | AssertionResult isPropertyOk(Result res, const std::string& valueStr, bool pred, |
| 169 | const std::string& predStr) { |
| 170 | if (res == Result::SUCCESS) { |
| 171 | if (pred) { |
| 172 | return AssertionSuccess(); |
| 173 | } |
| 174 | return AssertionFailure() << "value doesn't match.\nActual: " << valueStr |
| 175 | << "\nExpected: " << predStr; |
| 176 | } |
| 177 | if (res == Result::NOT_SUPPORTED) { |
| 178 | return AssertionSuccess(); |
| 179 | } |
| 180 | return AssertionFailure() << "Result is not SUCCESS or NOT_SUPPORTED: " << toString(res); |
| 181 | } |
| 182 | |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 183 | bool verifyStorageInfo(const hidl_vec<struct StorageInfo>& info) { |
| 184 | for (size_t i = 0; i < info.size(); i++) { |
| 185 | if (!(0 <= info[i].eol && info[i].eol <= 3 && 0 <= info[i].lifetimeA && |
| 186 | info[i].lifetimeA <= 0x0B && 0 <= info[i].lifetimeB && info[i].lifetimeB <= 0x0B)) { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool verifyDiskStats(const hidl_vec<struct DiskStats>& stats) { |
| 195 | for (size_t i = 0; i < stats.size(); i++) { |
| 196 | if (!(stats[i].reads > 0 && stats[i].readMerges > 0 && stats[i].readSectors > 0 && |
| 197 | stats[i].readTicks > 0 && stats[i].writes > 0 && stats[i].writeMerges > 0 && |
| 198 | stats[i].writeSectors > 0 && stats[i].writeTicks > 0 && stats[i].ioTicks > 0)) { |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 206 | TEST_F(HealthHidlTest, Properties) { |
| 207 | EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) { |
| 208 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0); |
| 209 | })); |
| 210 | EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) { |
| 211 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 212 | })); |
| 213 | EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) { |
| 214 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 215 | })); |
| 216 | EXPECT_OK(mHealth->getCapacity([](auto result, auto value) { |
| 217 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100); |
| 218 | })); |
| 219 | EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) { |
| 220 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN); |
| 221 | })); |
| 222 | EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) { |
| 223 | EXPECT_VALID_OR_UNSUPPORTED_PROP( |
| 224 | result, toString(value), |
| 225 | value == BatteryStatus::CHARGING || value == BatteryStatus::DISCHARGING || |
| 226 | value == BatteryStatus::NOT_CHARGING || value == BatteryStatus::FULL); |
| 227 | })); |
Hridya Valsaraju | 2120ecc | 2017-12-20 13:27:52 -0800 | [diff] [blame] | 228 | EXPECT_OK(mHealth->getStorageInfo([](auto result, auto& value) { |
| 229 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), (verifyStorageInfo(value))); |
| 230 | })); |
| 231 | EXPECT_OK(mHealth->getDiskStats([](auto result, auto& value) { |
| 232 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyDiskStats(value)); |
| 233 | })); |
Yifan Hong | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | } // namespace V2_0 |
| 237 | } // namespace health |
| 238 | } // namespace hardware |
| 239 | } // namespace android |
| 240 | |
| 241 | int main(int argc, char** argv) { |
| 242 | using ::android::hardware::health::V2_0::HealthHidlEnvironment; |
| 243 | ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance()); |
| 244 | ::testing::InitGoogleTest(&argc, argv); |
| 245 | HealthHidlEnvironment::Instance()->init(&argc, argv); |
| 246 | int status = RUN_ALL_TESTS(); |
| 247 | LOG(INFO) << "Test result = " << status; |
| 248 | return status; |
| 249 | } |