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 | |
| 17 | #define LOG_TAG "mHealthhidl_hal_test" |
| 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 | 69c2254 | 2017-10-03 17:40:24 -0700 | [diff] [blame] | 38 | |
| 39 | // Test environment for graphics.composer |
| 40 | class HealthHidlEnvironment : public VtsHalHidlTargetTestEnvBase { |
| 41 | public: |
| 42 | // get the test environment singleton |
| 43 | static HealthHidlEnvironment* Instance() { |
| 44 | static HealthHidlEnvironment* instance = new HealthHidlEnvironment; |
| 45 | return instance; |
| 46 | } |
| 47 | |
| 48 | virtual void registerTestServices() override { registerTestService<IHealth>(); } |
| 49 | |
| 50 | private: |
| 51 | HealthHidlEnvironment() {} |
| 52 | |
| 53 | GTEST_DISALLOW_COPY_AND_ASSIGN_(HealthHidlEnvironment); |
| 54 | }; |
| 55 | |
| 56 | class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 57 | public: |
| 58 | virtual void SetUp() override { |
| 59 | std::string serviceName = HealthHidlEnvironment::Instance()->getServiceName<IHealth>(); |
| 60 | LOG(INFO) << "get service with name:" << serviceName; |
| 61 | ASSERT_FALSE(serviceName.empty()); |
| 62 | mHealth = ::testing::VtsHalHidlTargetTestBase::getService<IHealth>(serviceName); |
| 63 | ASSERT_NE(mHealth, nullptr); |
| 64 | } |
| 65 | |
| 66 | sp<IHealth> mHealth; |
| 67 | }; |
| 68 | |
| 69 | class Callback : public IHealthInfoCallback { |
| 70 | using Function = std::function<void(const HealthInfo&)>; |
| 71 | |
| 72 | public: |
| 73 | Callback(const Function& f) : mInternal(f) {} |
| 74 | Return<void> healthInfoChanged(const HealthInfo& info) override { |
| 75 | std::unique_lock<std::mutex> lock(mMutex); |
| 76 | if (mInternal) mInternal(info); |
| 77 | return Void(); |
| 78 | } |
| 79 | void clear() { |
| 80 | std::unique_lock<std::mutex> lock(mMutex); |
| 81 | mInternal = nullptr; |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | std::mutex mMutex; |
| 86 | Function mInternal; |
| 87 | }; |
| 88 | |
| 89 | #define ASSERT_OK(r) ASSERT_TRUE(isOk(r)) |
| 90 | #define EXPECT_OK(r) EXPECT_TRUE(isOk(r)) |
| 91 | template <typename T> |
| 92 | AssertionResult isOk(const Return<T>& r) { |
| 93 | return r.isOk() ? AssertionSuccess() : (AssertionFailure() << r.description()); |
| 94 | } |
| 95 | |
| 96 | #define ASSERT_ALL_OK(r) ASSERT_TRUE(isAllOk(r)) |
| 97 | // Both isOk() and Result::SUCCESS |
| 98 | AssertionResult isAllOk(const Return<Result>& r) { |
| 99 | if (!r.isOk()) { |
| 100 | return AssertionFailure() << r.description(); |
| 101 | } |
| 102 | if (static_cast<Result>(r) != Result::SUCCESS) { |
| 103 | return AssertionFailure() << toString(static_cast<Result>(r)); |
| 104 | } |
| 105 | return AssertionSuccess(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Test whether callbacks work. Tested functions are IHealth::registerCallback, |
| 110 | * unregisterCallback, and update. |
| 111 | */ |
| 112 | TEST_F(HealthHidlTest, Callbacks) { |
| 113 | using namespace std::chrono_literals; |
| 114 | |
| 115 | std::mutex mutex; |
| 116 | std::condition_variable cv; |
| 117 | bool firstCallbackInvoked = false; |
| 118 | bool secondCallbackInvoked = false; |
| 119 | |
| 120 | sp<Callback> firstCallback = new Callback([&](const auto&) { |
| 121 | std::unique_lock<std::mutex> lk(mutex); |
| 122 | firstCallbackInvoked = true; |
| 123 | }); |
| 124 | |
| 125 | sp<Callback> secondCallback = new Callback([&](const auto&) { |
| 126 | std::unique_lock<std::mutex> lk(mutex); |
| 127 | secondCallbackInvoked = true; |
| 128 | cv.notify_all(); |
| 129 | }); |
| 130 | |
| 131 | ASSERT_ALL_OK(mHealth->registerCallback(firstCallback)); |
| 132 | ASSERT_ALL_OK(mHealth->registerCallback(secondCallback)); |
| 133 | |
| 134 | // assert that the first callback is invoked when update is called. |
| 135 | { |
| 136 | std::unique_lock<std::mutex> lk(mutex); |
| 137 | firstCallbackInvoked = false; |
| 138 | secondCallbackInvoked = false; |
| 139 | } |
| 140 | |
| 141 | ASSERT_ALL_OK(mHealth->update()); |
| 142 | |
| 143 | { |
| 144 | std::unique_lock<std::mutex> lk(mutex); |
| 145 | EXPECT_TRUE(cv.wait_for(lk, 1s, [&] { |
| 146 | return firstCallbackInvoked && secondCallbackInvoked; |
| 147 | })) << "Timeout."; |
| 148 | ASSERT_TRUE(firstCallbackInvoked); |
| 149 | ASSERT_TRUE(secondCallbackInvoked); |
| 150 | } |
| 151 | |
| 152 | ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback)); |
| 153 | |
| 154 | // assert that the second callback is still invoked even though the first is unregistered. |
| 155 | { |
| 156 | std::unique_lock<std::mutex> lk(mutex); |
| 157 | firstCallbackInvoked = false; |
| 158 | secondCallbackInvoked = false; |
| 159 | } |
| 160 | |
| 161 | ASSERT_ALL_OK(mHealth->update()); |
| 162 | |
| 163 | { |
| 164 | std::unique_lock<std::mutex> lk(mutex); |
| 165 | EXPECT_TRUE(cv.wait_for(lk, 1s, [&] { return secondCallbackInvoked; })) << "Timeout."; |
| 166 | ASSERT_FALSE(firstCallbackInvoked); |
| 167 | ASSERT_TRUE(secondCallbackInvoked); |
| 168 | } |
| 169 | |
| 170 | ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback)); |
| 171 | |
| 172 | // avoid reference to lambda function that goes out of scope. |
| 173 | firstCallback->clear(); |
| 174 | secondCallback->clear(); |
| 175 | } |
| 176 | |
| 177 | TEST_F(HealthHidlTest, UnregisterNonExistentCallback) { |
| 178 | sp<Callback> callback = new Callback([](const auto&) {}); |
| 179 | auto ret = mHealth->unregisterCallback(callback); |
| 180 | ASSERT_OK(ret); |
| 181 | ASSERT_EQ(Result::NOT_FOUND, static_cast<Result>(ret)) << "Actual: " << toString(ret); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Pass the test if: |
| 186 | * - Property is not supported (res == NOT_SUPPORTED) |
| 187 | * - Result is success, and predicate is true |
| 188 | * @param res the Result value. |
| 189 | * @param valueStr the string representation for actual value (for error message) |
| 190 | * @param pred a predicate that test whether the value is valid |
| 191 | */ |
| 192 | #define EXPECT_VALID_OR_UNSUPPORTED_PROP(res, valueStr, pred) \ |
| 193 | EXPECT_TRUE(isPropertyOk(res, valueStr, pred, #pred)) |
| 194 | |
| 195 | AssertionResult isPropertyOk(Result res, const std::string& valueStr, bool pred, |
| 196 | const std::string& predStr) { |
| 197 | if (res == Result::SUCCESS) { |
| 198 | if (pred) { |
| 199 | return AssertionSuccess(); |
| 200 | } |
| 201 | return AssertionFailure() << "value doesn't match.\nActual: " << valueStr |
| 202 | << "\nExpected: " << predStr; |
| 203 | } |
| 204 | if (res == Result::NOT_SUPPORTED) { |
| 205 | return AssertionSuccess(); |
| 206 | } |
| 207 | return AssertionFailure() << "Result is not SUCCESS or NOT_SUPPORTED: " << toString(res); |
| 208 | } |
| 209 | |
| 210 | TEST_F(HealthHidlTest, Properties) { |
| 211 | EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) { |
| 212 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0); |
| 213 | })); |
| 214 | EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) { |
| 215 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 216 | })); |
| 217 | EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) { |
| 218 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN); |
| 219 | })); |
| 220 | EXPECT_OK(mHealth->getCapacity([](auto result, auto value) { |
| 221 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100); |
| 222 | })); |
| 223 | EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) { |
| 224 | EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN); |
| 225 | })); |
| 226 | EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) { |
| 227 | EXPECT_VALID_OR_UNSUPPORTED_PROP( |
| 228 | result, toString(value), |
| 229 | value == BatteryStatus::CHARGING || value == BatteryStatus::DISCHARGING || |
| 230 | value == BatteryStatus::NOT_CHARGING || value == BatteryStatus::FULL); |
| 231 | })); |
| 232 | } |
| 233 | |
| 234 | } // namespace V2_0 |
| 235 | } // namespace health |
| 236 | } // namespace hardware |
| 237 | } // namespace android |
| 238 | |
| 239 | int main(int argc, char** argv) { |
| 240 | using ::android::hardware::health::V2_0::HealthHidlEnvironment; |
| 241 | ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance()); |
| 242 | ::testing::InitGoogleTest(&argc, argv); |
| 243 | HealthHidlEnvironment::Instance()->init(&argc, argv); |
| 244 | int status = RUN_ALL_TESTS(); |
| 245 | LOG(INFO) << "Test result = " << status; |
| 246 | return status; |
| 247 | } |