blob: c5431e45c2057067e789d75fafcc410920ed4ec6 [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 Hong69c22542017-10-03 17:40:24 -070038
39// Test environment for graphics.composer
40class 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
56class 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
69class Callback : public IHealthInfoCallback {
Yifan Hong69c22542017-10-03 17:40:24 -070070 public:
Hridya Valsarajud31932a2018-01-17 23:09:24 -080071 Return<void> healthInfoChanged(const HealthInfo&) override {
Yifan Hongd6ea57e2017-11-20 14:41:09 -080072 std::lock_guard<std::mutex> lock(mMutex);
73 mInvoked = true;
74 mInvokedNotify.notify_all();
Yifan Hong69c22542017-10-03 17:40:24 -070075 return Void();
76 }
Yifan Hongd6ea57e2017-11-20 14:41:09 -080077 template <typename R, typename P>
78 bool waitInvoke(std::chrono::duration<R, P> duration) {
Yifan Hong69c22542017-10-03 17:40:24 -070079 std::unique_lock<std::mutex> lock(mMutex);
Yifan Hongd6ea57e2017-11-20 14:41:09 -080080 bool r = mInvokedNotify.wait_for(lock, duration, [this] { return this->mInvoked; });
81 mInvoked = false;
82 return r;
Yifan Hong69c22542017-10-03 17:40:24 -070083 }
Yifan Hong69c22542017-10-03 17:40:24 -070084 private:
85 std::mutex mMutex;
Yifan Hongd6ea57e2017-11-20 14:41:09 -080086 std::condition_variable mInvokedNotify;
87 bool mInvoked = false;
Yifan Hong69c22542017-10-03 17:40:24 -070088};
89
90#define ASSERT_OK(r) ASSERT_TRUE(isOk(r))
91#define EXPECT_OK(r) EXPECT_TRUE(isOk(r))
92template <typename T>
93AssertionResult isOk(const Return<T>& r) {
94 return r.isOk() ? AssertionSuccess() : (AssertionFailure() << r.description());
95}
96
97#define ASSERT_ALL_OK(r) ASSERT_TRUE(isAllOk(r))
98// Both isOk() and Result::SUCCESS
99AssertionResult isAllOk(const Return<Result>& r) {
100 if (!r.isOk()) {
101 return AssertionFailure() << r.description();
102 }
103 if (static_cast<Result>(r) != Result::SUCCESS) {
104 return AssertionFailure() << toString(static_cast<Result>(r));
105 }
106 return AssertionSuccess();
107}
108
109/**
110 * Test whether callbacks work. Tested functions are IHealth::registerCallback,
111 * unregisterCallback, and update.
112 */
113TEST_F(HealthHidlTest, Callbacks) {
114 using namespace std::chrono_literals;
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800115 sp<Callback> firstCallback = new Callback();
116 sp<Callback> secondCallback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700117
118 ASSERT_ALL_OK(mHealth->registerCallback(firstCallback));
119 ASSERT_ALL_OK(mHealth->registerCallback(secondCallback));
120
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800121 // registerCallback may or may not invoke the callback immediately, so the test needs
122 // to wait for the invocation. If the implementation chooses not to invoke the callback
123 // immediately, just wait for some time.
124 firstCallback->waitInvoke(200ms);
125 secondCallback->waitInvoke(200ms);
Yifan Hong69c22542017-10-03 17:40:24 -0700126
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800127 // assert that the first callback is invoked when update is called.
Yifan Hong69c22542017-10-03 17:40:24 -0700128 ASSERT_ALL_OK(mHealth->update());
129
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800130 ASSERT_TRUE(firstCallback->waitInvoke(1s));
131 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700132
133 ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback));
134
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800135 // clear any potentially pending callbacks result from wakealarm / kernel events
136 // If there is none, just wait for some time.
137 firstCallback->waitInvoke(200ms);
138 secondCallback->waitInvoke(200ms);
Yifan Hong69c22542017-10-03 17:40:24 -0700139
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800140 // assert that the second callback is still invoked even though the first is unregistered.
Yifan Hong69c22542017-10-03 17:40:24 -0700141 ASSERT_ALL_OK(mHealth->update());
142
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800143 ASSERT_FALSE(firstCallback->waitInvoke(200ms));
144 ASSERT_TRUE(secondCallback->waitInvoke(1s));
Yifan Hong69c22542017-10-03 17:40:24 -0700145
146 ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback));
Yifan Hong69c22542017-10-03 17:40:24 -0700147}
148
149TEST_F(HealthHidlTest, UnregisterNonExistentCallback) {
Yifan Hongd6ea57e2017-11-20 14:41:09 -0800150 sp<Callback> callback = new Callback();
Yifan Hong69c22542017-10-03 17:40:24 -0700151 auto ret = mHealth->unregisterCallback(callback);
152 ASSERT_OK(ret);
153 ASSERT_EQ(Result::NOT_FOUND, static_cast<Result>(ret)) << "Actual: " << toString(ret);
154}
155
156/**
157 * Pass the test if:
158 * - Property is not supported (res == NOT_SUPPORTED)
159 * - Result is success, and predicate is true
160 * @param res the Result value.
161 * @param valueStr the string representation for actual value (for error message)
162 * @param pred a predicate that test whether the value is valid
163 */
164#define EXPECT_VALID_OR_UNSUPPORTED_PROP(res, valueStr, pred) \
165 EXPECT_TRUE(isPropertyOk(res, valueStr, pred, #pred))
166
167AssertionResult isPropertyOk(Result res, const std::string& valueStr, bool pred,
168 const std::string& predStr) {
169 if (res == Result::SUCCESS) {
170 if (pred) {
171 return AssertionSuccess();
172 }
173 return AssertionFailure() << "value doesn't match.\nActual: " << valueStr
174 << "\nExpected: " << predStr;
175 }
176 if (res == Result::NOT_SUPPORTED) {
177 return AssertionSuccess();
178 }
179 return AssertionFailure() << "Result is not SUCCESS or NOT_SUPPORTED: " << toString(res);
180}
181
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800182bool verifyStorageInfo(const hidl_vec<struct StorageInfo>& info) {
183 for (size_t i = 0; i < info.size(); i++) {
184 if (!(0 <= info[i].eol && info[i].eol <= 3 && 0 <= info[i].lifetimeA &&
185 info[i].lifetimeA <= 0x0B && 0 <= info[i].lifetimeB && info[i].lifetimeB <= 0x0B)) {
186 return false;
187 }
188 }
189
190 return true;
191}
192
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800193template <typename T>
194bool verifyEnum(T value) {
195 for (auto it : hidl_enum_iterator<T>()) {
196 if (it == value) {
197 return true;
198 }
199 }
200
201 return false;
202}
203
204bool verifyHealthInfo(const HealthInfo& health_info) {
Hridya Valsaraju075c1822018-04-03 11:19:08 -0700205 if (!verifyStorageInfo(health_info.storageInfos)) {
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800206 return false;
207 }
208
209 using V1_0::BatteryStatus;
210 using V1_0::BatteryHealth;
211
212 if (!((health_info.legacy.batteryChargeCounter > 0) &&
213 (health_info.legacy.batteryCurrent != INT32_MIN) &&
214 (0 <= health_info.legacy.batteryLevel && health_info.legacy.batteryLevel <= 100) &&
215 verifyEnum<BatteryHealth>(health_info.legacy.batteryHealth) &&
216 (health_info.legacy.batteryStatus != BatteryStatus::UNKNOWN) &&
217 verifyEnum<BatteryStatus>(health_info.legacy.batteryStatus))) {
218 return false;
219 }
220
221 return true;
222}
223
224/*
225 * Tests the values returned by getChargeCounter(),
226 * getCurrentNow(), getCurrentAverage(), getCapacity(), getEnergyCounter(),
227 * getChargeStatus(), getStorageInfo(), getDiskStats() and getHealthInfo() from
228 * interface IHealth.
229 */
Yifan Hong69c22542017-10-03 17:40:24 -0700230TEST_F(HealthHidlTest, Properties) {
231 EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) {
232 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0);
233 }));
234 EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) {
235 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
236 }));
237 EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) {
238 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
239 }));
240 EXPECT_OK(mHealth->getCapacity([](auto result, auto value) {
241 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100);
242 }));
243 EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) {
244 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN);
245 }));
246 EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) {
247 EXPECT_VALID_OR_UNSUPPORTED_PROP(
248 result, toString(value),
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800249 value != BatteryStatus::UNKNOWN && verifyEnum<BatteryStatus>(value));
Yifan Hong69c22542017-10-03 17:40:24 -0700250 }));
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800251 EXPECT_OK(mHealth->getStorageInfo([](auto result, auto& value) {
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800252 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyStorageInfo(value));
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800253 }));
254 EXPECT_OK(mHealth->getDiskStats([](auto result, auto& value) {
Hridya Valsaraju075c1822018-04-03 11:19:08 -0700255 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), true);
Hridya Valsaraju2120ecc2017-12-20 13:27:52 -0800256 }));
Hridya Valsaraju87e29602018-01-12 17:46:22 -0800257 EXPECT_OK(mHealth->getHealthInfo([](auto result, auto& value) {
258 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, toString(value), verifyHealthInfo(value));
259 }));
Yifan Hong69c22542017-10-03 17:40:24 -0700260}
261
262} // namespace V2_0
263} // namespace health
264} // namespace hardware
265} // namespace android
266
267int main(int argc, char** argv) {
268 using ::android::hardware::health::V2_0::HealthHidlEnvironment;
269 ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance());
270 ::testing::InitGoogleTest(&argc, argv);
271 HealthHidlEnvironment::Instance()->init(&argc, argv);
272 int status = RUN_ALL_TESTS();
273 LOG(INFO) << "Test result = " << status;
274 return status;
275}