1 /*
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 
17 #define LOG_TAG "health_hidl_hal_test"
18 
19 #include <android/hardware/health/1.0/IHealth.h>
20 #include <android/hardware/health/1.0/types.h>
21 #include <log/log.h>
22 
23 #include <VtsHalHidlTargetTestBase.h>
24 #include <VtsHalHidlTargetTestEnvBase.h>
25 
26 using HealthConfig = ::android::hardware::health::V1_0::HealthConfig;
27 using HealthInfo = ::android::hardware::health::V1_0::HealthInfo;
28 using IHealth = ::android::hardware::health::V1_0::IHealth;
29 using Result = ::android::hardware::health::V1_0::Result;
30 
31 using ::android::sp;
32 
33 // Test environment for Health HIDL HAL.
34 class HealthHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
35    public:
36     // get the test environment singleton
Instance()37     static HealthHidlEnvironment* Instance() {
38         static HealthHidlEnvironment* instance = new HealthHidlEnvironment;
39         return instance;
40     }
41 
registerTestServices()42     virtual void registerTestServices() override { registerTestService<IHealth>(); }
43    private:
HealthHidlEnvironment()44     HealthHidlEnvironment() {}
45 };
46 
47 class HealthHidlTest : public ::testing::VtsHalHidlTargetTestBase {
48    public:
SetUp()49     virtual void SetUp() override {
50         health = ::testing::VtsHalHidlTargetTestBase::getService<IHealth>(
51             HealthHidlEnvironment::Instance()->getServiceName<IHealth>());
52         ASSERT_NE(health, nullptr);
53         health->init(config,
54                      [&](const auto& halConfigOut) { config = halConfigOut; });
55     }
56 
57     sp<IHealth> health;
58     HealthConfig config;
59 };
60 
61 /**
62  * Ensure EnergyCounter call returns positive energy counter or NOT_SUPPORTED
63  */
TEST_F(HealthHidlTest,TestEnergyCounter)64 TEST_F(HealthHidlTest, TestEnergyCounter) {
65     Result result;
66     int64_t energy = 0;
67     health->energyCounter([&](Result ret, int64_t energyOut) {
68         result = ret;
69         energy = energyOut;
70     });
71 
72     ASSERT_TRUE(result == Result::SUCCESS || result == Result::NOT_SUPPORTED);
73     ASSERT_TRUE(result != Result::SUCCESS || energy > 0);
74 }
75 
main(int argc,char ** argv)76 int main(int argc, char **argv) {
77     ::testing::AddGlobalTestEnvironment(HealthHidlEnvironment::Instance());
78     ::testing::InitGoogleTest(&argc, argv);
79     HealthHidlEnvironment::Instance()->init(&argc, argv);
80     int status = RUN_ALL_TESTS();
81     ALOGI("Test result = %d", status);
82     return status;
83 }
84