blob: bf8548c94b633b6cc1cd202bbd2d074bf84432be [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
183TEST_F(HealthHidlTest, Properties) {
184 EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) {
185 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0);
186 }));
187 EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) {
188 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
189 }));
190 EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) {
191 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
192 }));
193 EXPECT_OK(mHealth->getCapacity([](auto result, auto value) {
194 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100);
195 }));
196 EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) {
197 EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN);
198 }));
199 EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) {
200 EXPECT_VALID_OR_UNSUPPORTED_PROP(
201 result, toString(value),
202 value == BatteryStatus::CHARGING || value == BatteryStatus::DISCHARGING ||
203 value == BatteryStatus::NOT_CHARGING || value == BatteryStatus::FULL);
204 }));
205}
206
207} // namespace V2_0
208} // namespace health
209} // namespace hardware
210} // namespace android
211
212int main(int argc, char** argv) {
213 using ::android::hardware::health::V2_0::HealthHidlEnvironment;
214 ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance());
215 ::testing::InitGoogleTest(&argc, argv);
216 HealthHidlEnvironment::Instance()->init(&argc, argv);
217 int status = RUN_ALL_TESTS();
218 LOG(INFO) << "Test result = " << status;
219 return status;
220}