blob: 55b48b86ffcb6ef974d4277fd8e80d62e4847eee [file] [log] [blame]
Yifan Hong69c22542017-10-03 17:40:24 -07001/*
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 Hongf86271d2017-11-06 12:52:20 -080017#define LOG_TAG "health_hidl_hal_test"
Yifan Hong69c22542017-10-03 17:40:24 -070018
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 Hong0d48dc82019-03-07 13:04:33 -080025#include <gflags/gflags.h>
Yifan Hong69c22542017-10-03 17:40:24 -070026
27using ::testing::AssertionFailure;
28using ::testing::AssertionResult;
29using ::testing::AssertionSuccess;
30using ::testing::VtsHalHidlTargetTestBase;
31using ::testing::VtsHalHidlTargetTestEnvBase;
32
Yifan Hong0d48dc82019-03-07 13:04:33 -080033DEFINE_bool(force, false, "Force test healthd even when the default instance is present.");
34
Yifan Hong69c22542017-10-03 17:40:24 -070035namespace android {
36namespace hardware {
37namespace health {
38namespace V2_0 {
39
40using V1_0::BatteryStatus;
Yifan Hong69c22542017-10-03 17:40:24 -070041
42// Test environment for graphics.composer
43class 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
59class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase {
60 public:
61 virtual void SetUp() override {
62 std::string serviceName = HealthHidlEnvironment::Instance()->getServiceName<IHealth>();
Yifan Hong0d48dc82019-03-07 13:04:33 -080063
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 Hong69c22542017-10-03 17:40:24 -070071 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
80class Callback : public IHealthInfoCallback {
Yifan Hong69c22542017-10-03 17:40:24 -070081 public:
Hridya Valsarajud31932a2018-01-17 23:09:24 -080082 Return<void> healthInfoChanged(const HealthInfo&) override {
Yifan Hongd6ea57e2017-11-20 14:41:09 -080083 std::lock_guard<std::mutex> lock(mMutex);
84 mInvoked = true;
85 mInvokedNotify.notify_all();
Yifan Hong69c22542017-10-03 17:40:24 -070086 return Void();
87 }
Yifan Hongd6ea57e2017-11-20 14:41:09 -080088 template <typename R, typename P>
89 bool waitInvoke(std::chrono::duration<R, P> duration) {
Yifan Hong69c22542017-10-03 17:40:24 -070090 std::unique_lock<std::mutex> lock(mMutex);
Yifan Hongd6ea57e2017-11-20 14:41:09 -080091 bool r = mInvokedNotify.wait_for(lock, duration, [this] { return this->mInvoked; });
92 mInvoked = false;
93 return r;
Yifan Hong69c22542017-10-03 17:40:24 -070094 }
Yifan Hong69c22542017-10-03 17:40:24 -070095 private:
96 std::mutex mMutex;
Yifan Hongd6ea57e2017-11-20 14:41:09 -080097 std::condition_variable mInvokedNotify;
98 bool mInvoked = false;
Yifan Hong69c22542017-10-03 17:40:24 -070099};
100
101#define ASSERT_OK(r) ASSERT_TRUE(isOk(r))
102#define EXPECT_OK(r) EXPECT_TRUE(isOk(r))
103template <typename T>
104AssertionResult 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
110AssertionResult 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 */
124TEST_F(HealthHidlTest, Callbacks) {
125 using namespace std::chrono_literals;
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800126 sp<Callback> firstCallback = new Callback();
127 sp<Callback> secondCallback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700128
129 ASSERT_ALL_OK(mHealth->registerCallback(firstCallback));
130 ASSERT_ALL_OK(mHealth->registerCallback(secondCallback));
131
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800132 // 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 Hong69c22542017-10-03 17:40:24 -0700137
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800138 // assert that the first callback is invoked when update is called.
Yifan Hong69c22542017-10-03 17:40:24 -0700139 ASSERT_ALL_OK(mHealth->update());
140
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800141 ASSERT_TRUE(firstCallback->waitInvoke(1s));
142 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700143
144 ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback));
145
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800146 // 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 Hong69c22542017-10-03 17:40:24 -0700150
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800151 // assert that the second callback is still invoked even though the first is unregistered.
Yifan Hong69c22542017-10-03 17:40:24 -0700152 ASSERT_ALL_OK(mHealth->update());
153
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800154 ASSERT_FALSE(firstCallback->waitInvoke(200ms));
155 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700156
157 ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback));
Yifan Hong69c22542017-10-03 17:40:24 -0700158}
159
160TEST_F(HealthHidlTest, UnregisterNonExistentCallback) {
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800161 sp<Callback> callback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700162 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
178AssertionResult 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 Valsaraju2120ecc2017-12-20 13:27:52 -0800193bool 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 Valsaraju87e29602018-01-12 17:46:22 -0800204template <typename T>
205bool 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
215bool verifyHealthInfo(const HealthInfo& health_info) {
Hridya Valsaraju075c1822018-04-03 11:19:08 -0700216 if (!verifyStorageInfo(health_info.storageInfos)) {
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800217 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 Hong69c22542017-10-03 17:40:24 -0700241TEST_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 Valsaraju87e29602018-01-12 17:46:22 -0800260 value != BatteryStatus::UNKNOWN && verifyEnum<BatteryStatus>(value));
Yifan Hong69c22542017-10-03 17:40:24 -0700261 }));
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800262 EXPECT_OK(mHealth->getStorageInfo([](auto result, auto& value) {
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800263 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyStorageInfo(value));
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800264 }));
265 EXPECT_OK(mHealth->getDiskStats([](auto result, auto& value) {
Hridya Valsaraju075c1822018-04-03 11:19:08 -0700266 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), true);
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800267 }));
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800268 EXPECT_OK(mHealth->getHealthInfo([](auto result, auto& value) {
269 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyHealthInfo(value));
270 }));
Yifan Hong69c22542017-10-03 17:40:24 -0700271}
272
273} // namespace V2_0
274} // namespace health
275} // namespace hardware
276} // namespace android
277
278int 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 Hong0d48dc82019-03-07 13:04:33 -0800283 gflags::ParseCommandLineFlags(&argc, &argv, true /* remove flags */);
Yifan Hong69c22542017-10-03 17:40:24 -0700284 int status = RUN_ALL_TESTS();
285 LOG(INFO) << "Test result = " << status;
286 return status;
287}