blob: b2c6160d775cc23b3b09ad5e217a1776e9d89c32 [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>
25
26using ::testing::AssertionFailure;
27using ::testing::AssertionResult;
28using ::testing::AssertionSuccess;
29using ::testing::VtsHalHidlTargetTestBase;
30using ::testing::VtsHalHidlTargetTestEnvBase;
31
32namespace android {
33namespace hardware {
34namespace health {
35namespace V2_0 {
36
37using V1_0::BatteryStatus;
Yifan Hongf86271d2017-11-06 12:52:20 -080038using V1_0::HealthInfo;
Yifan Hong69c22542017-10-03 17:40:24 -070039
40// Test environment for graphics.composer
41class 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
57class 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
70class Callback : public IHealthInfoCallback {
Yifan Hong69c22542017-10-03 17:40:24 -070071 public:
Yifan Hongd6ea57e2017-11-20 14:41:09 -080072 Return<void> healthInfoChanged(const HealthInfo&) override {
73 std::lock_guard<std::mutex> lock(mMutex);
74 mInvoked = true;
75 mInvokedNotify.notify_all();
Yifan Hong69c22542017-10-03 17:40:24 -070076 return Void();
77 }
Yifan Hongd6ea57e2017-11-20 14:41:09 -080078 template <typename R, typename P>
79 bool waitInvoke(std::chrono::duration<R, P> duration) {
Yifan Hong69c22542017-10-03 17:40:24 -070080 std::unique_lock<std::mutex> lock(mMutex);
Yifan Hongd6ea57e2017-11-20 14:41:09 -080081 bool r = mInvokedNotify.wait_for(lock, duration, [this] { return this->mInvoked; });
82 mInvoked = false;
83 return r;
Yifan Hong69c22542017-10-03 17:40:24 -070084 }
Yifan Hong69c22542017-10-03 17:40:24 -070085 private:
86 std::mutex mMutex;
Yifan Hongd6ea57e2017-11-20 14:41:09 -080087 std::condition_variable mInvokedNotify;
88 bool mInvoked = false;
Yifan Hong69c22542017-10-03 17:40:24 -070089};
90
91#define ASSERT_OK(r) ASSERT_TRUE(isOk(r))
92#define EXPECT_OK(r) EXPECT_TRUE(isOk(r))
93template <typename T>
94AssertionResult 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
100AssertionResult 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 */
114TEST_F(HealthHidlTest, Callbacks) {
115 using namespace std::chrono_literals;
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800116 sp<Callback> firstCallback = new Callback();
117 sp<Callback> secondCallback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700118
119 ASSERT_ALL_OK(mHealth->registerCallback(firstCallback));
120 ASSERT_ALL_OK(mHealth->registerCallback(secondCallback));
121
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800122 // 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 Hong69c22542017-10-03 17:40:24 -0700127
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800128 // assert that the first callback is invoked when update is called.
Yifan Hong69c22542017-10-03 17:40:24 -0700129 ASSERT_ALL_OK(mHealth->update());
130
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800131 ASSERT_TRUE(firstCallback->waitInvoke(1s));
132 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700133
134 ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback));
135
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800136 // 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 Hong69c22542017-10-03 17:40:24 -0700140
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800141 // assert that the second callback is still invoked even though the first is unregistered.
Yifan Hong69c22542017-10-03 17:40:24 -0700142 ASSERT_ALL_OK(mHealth->update());
143
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800144 ASSERT_FALSE(firstCallback->waitInvoke(200ms));
145 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700146
147 ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback));
Yifan Hong69c22542017-10-03 17:40:24 -0700148}
149
150TEST_F(HealthHidlTest, UnregisterNonExistentCallback) {
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800151 sp<Callback> callback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700152 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
168AssertionResult 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 Valsaraju2120ecc2017-12-20 13:27:52 -0800183bool 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
194bool 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 Hong69c22542017-10-03 17:40:24 -0700206TEST_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 Valsaraju2120ecc2017-12-20 13:27:52 -0800228 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 Hong69c22542017-10-03 17:40:24 -0700234}
235
236} // namespace V2_0
237} // namespace health
238} // namespace hardware
239} // namespace android
240
241int 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}