summaryrefslogtreecommitdiffstats
blob: 8abed0c50354be0e26a9007e25b3474552606e83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "mHealthhidl_hal_test"

#include <mutex>

#include <VtsHalHidlTargetTestBase.h>
#include <android-base/logging.h>
#include <android/hardware/health/2.0/IHealth.h>
#include <android/hardware/health/2.0/types.h>

using ::testing::AssertionFailure;
using ::testing::AssertionResult;
using ::testing::AssertionSuccess;
using ::testing::VtsHalHidlTargetTestBase;
using ::testing::VtsHalHidlTargetTestEnvBase;

namespace android {
namespace hardware {
namespace health {
namespace V2_0 {

using V1_0::BatteryStatus;
using V1_0::HealthInfo;

// Test environment for graphics.composer
class HealthHidlEnvironment : public VtsHalHidlTargetTestEnvBase {
   public:
    // get the test environment singleton
    static HealthHidlEnvironment* Instance() {
        static HealthHidlEnvironment* instance = new HealthHidlEnvironment;
        return instance;
    }

    virtual void registerTestServices() override { registerTestService<IHealth>(); }

   private:
    HealthHidlEnvironment() {}

    GTEST_DISALLOW_COPY_AND_ASSIGN_(HealthHidlEnvironment);
};

class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase {
   public:
    virtual void SetUp() override {
        std::string serviceName = HealthHidlEnvironment::Instance()->getServiceName<IHealth>();
        LOG(INFO) << "get service with name:" << serviceName;
        ASSERT_FALSE(serviceName.empty());
        mHealth = ::testing::VtsHalHidlTargetTestBase::getService<IHealth>(serviceName);
        ASSERT_NE(mHealth, nullptr);
    }

    sp<IHealth> mHealth;
};

class Callback : public IHealthInfoCallback {
    using Function = std::function<void(const HealthInfo&)>;

   public:
    Callback(const Function& f) : mInternal(f) {}
    Return<void> healthInfoChanged(const HealthInfo& info) override {
        std::unique_lock<std::mutex> lock(mMutex);
        if (mInternal) mInternal(info);
        return Void();
    }
    void clear() {
        std::unique_lock<std::mutex> lock(mMutex);
        mInternal = nullptr;
    }

   private:
    std::mutex mMutex;
    Function mInternal;
};

#define ASSERT_OK(r) ASSERT_TRUE(isOk(r))
#define EXPECT_OK(r) EXPECT_TRUE(isOk(r))
template <typename T>
AssertionResult isOk(const Return<T>& r) {
    return r.isOk() ? AssertionSuccess() : (AssertionFailure() << r.description());
}

#define ASSERT_ALL_OK(r) ASSERT_TRUE(isAllOk(r))
// Both isOk() and Result::SUCCESS
AssertionResult isAllOk(const Return<Result>& r) {
    if (!r.isOk()) {
        return AssertionFailure() << r.description();
    }
    if (static_cast<Result>(r) != Result::SUCCESS) {
        return AssertionFailure() << toString(static_cast<Result>(r));
    }
    return AssertionSuccess();
}

/**
 * Test whether callbacks work. Tested functions are IHealth::registerCallback,
 * unregisterCallback, and update.
 */
TEST_F(HealthHidlTest, Callbacks) {
    using namespace std::chrono_literals;

    std::mutex mutex;
    std::condition_variable cv;
    bool firstCallbackInvoked = false;
    bool secondCallbackInvoked = false;

    sp<Callback> firstCallback = new Callback([&](const auto&) {
        std::unique_lock<std::mutex> lk(mutex);
        firstCallbackInvoked = true;
    });

    sp<Callback> secondCallback = new Callback([&](const auto&) {
        std::unique_lock<std::mutex> lk(mutex);
        secondCallbackInvoked = true;
        cv.notify_all();
    });

    ASSERT_ALL_OK(mHealth->registerCallback(firstCallback));
    ASSERT_ALL_OK(mHealth->registerCallback(secondCallback));

    // assert that the first callback is invoked when update is called.
    {
        std::unique_lock<std::mutex> lk(mutex);
        firstCallbackInvoked = false;
        secondCallbackInvoked = false;
    }

    ASSERT_ALL_OK(mHealth->update());

    {
        std::unique_lock<std::mutex> lk(mutex);
        EXPECT_TRUE(cv.wait_for(lk, 1s, [&] {
            return firstCallbackInvoked && secondCallbackInvoked;
        })) << "Timeout.";
        ASSERT_TRUE(firstCallbackInvoked);
        ASSERT_TRUE(secondCallbackInvoked);
    }

    ASSERT_ALL_OK(mHealth->unregisterCallback(firstCallback));

    // assert that the second callback is still invoked even though the first is unregistered.
    {
        std::unique_lock<std::mutex> lk(mutex);
        firstCallbackInvoked = false;
        secondCallbackInvoked = false;
    }

    ASSERT_ALL_OK(mHealth->update());

    {
        std::unique_lock<std::mutex> lk(mutex);
        EXPECT_TRUE(cv.wait_for(lk, 1s, [&] { return secondCallbackInvoked; })) << "Timeout.";
        ASSERT_FALSE(firstCallbackInvoked);
        ASSERT_TRUE(secondCallbackInvoked);
    }

    ASSERT_ALL_OK(mHealth->unregisterCallback(secondCallback));

    // avoid reference to lambda function that goes out of scope.
    firstCallback->clear();
    secondCallback->clear();
}

TEST_F(HealthHidlTest, UnregisterNonExistentCallback) {
    sp<Callback> callback = new Callback([](const auto&) {});
    auto ret = mHealth->unregisterCallback(callback);
    ASSERT_OK(ret);
    ASSERT_EQ(Result::NOT_FOUND, static_cast<Result>(ret)) << "Actual: " << toString(ret);
}

/**
 * Pass the test if:
 *  - Property is not supported (res == NOT_SUPPORTED)
 *  - Result is success, and predicate is true
 * @param res the Result value.
 * @param valueStr the string representation for actual value (for error message)
 * @param pred a predicate that test whether the value is valid
 */
#define EXPECT_VALID_OR_UNSUPPORTED_PROP(res, valueStr, pred) \
    EXPECT_TRUE(isPropertyOk(res, valueStr, pred, #pred))

AssertionResult isPropertyOk(Result res, const std::string& valueStr, bool pred,
                             const std::string& predStr) {
    if (res == Result::SUCCESS) {
        if (pred) {
            return AssertionSuccess();
        }
        return AssertionFailure() << "value doesn't match.\nActual: " << valueStr
                                  << "\nExpected: " << predStr;
    }
    if (res == Result::NOT_SUPPORTED) {
        return AssertionSuccess();
    }
    return AssertionFailure() << "Result is not SUCCESS or NOT_SUPPORTED: " << toString(res);
}

TEST_F(HealthHidlTest, Properties) {
    EXPECT_OK(mHealth->getChargeCounter([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value > 0);
    }));
    EXPECT_OK(mHealth->getCurrentNow([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
    }));
    EXPECT_OK(mHealth->getCurrentAverage([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT32_MIN);
    }));
    EXPECT_OK(mHealth->getCapacity([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), 0 <= value && value <= 100);
    }));
    EXPECT_OK(mHealth->getEnergyCounter([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(result, std::to_string(value), value != INT64_MIN);
    }));
    EXPECT_OK(mHealth->getChargeStatus([](auto result, auto value) {
        EXPECT_VALID_OR_UNSUPPORTED_PROP(
            result, toString(value),
            value == BatteryStatus::CHARGING || value == BatteryStatus::DISCHARGING ||
                value == BatteryStatus::NOT_CHARGING || value == BatteryStatus::FULL);
    }));
}

}  // namespace V2_0
}  // namespace health
}  // namespace hardware
}  // namespace android

int main(int argc, char** argv) {
    using ::android::hardware::health::V2_0::HealthHidlEnvironment;
    ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance());
    ::testing::InitGoogleTest(&argc, argv);
    HealthHidlEnvironment::Instance()->init(&argc, argv);
    int status = RUN_ALL_TESTS();
    LOG(INFO) << "Test result = " << status;
    return status;
}