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 "stats_hidl_hal_test"
18 #include <android-base/logging.h>
19 #include <android/frameworks/stats/1.0/IStats.h>
20 
21 #include <gtest/gtest.h>
22 #include <hidl/GtestPrinter.h>
23 #include <hidl/ServiceManagement.h>
24 #include <utils/StrongPointer.h>
25 
26 using android::sp;
27 using android::frameworks::stats::V1_0::BatteryCausedShutdown;
28 using android::frameworks::stats::V1_0::BatteryHealthSnapshotArgs;
29 using android::frameworks::stats::V1_0::ChargeCycles;
30 using android::frameworks::stats::V1_0::HardwareFailed;
31 using android::frameworks::stats::V1_0::IStats;
32 using android::frameworks::stats::V1_0::SlowIo;
33 using android::frameworks::stats::V1_0::SpeakerImpedance;
34 using android::frameworks::stats::V1_0::UsbPortOverheatEvent;
35 using android::frameworks::stats::V1_0::VendorAtom;
36 using Value = android::frameworks::stats::V1_0::VendorAtom::Value;
37 using android::hardware::Return;
38 
39 class StatsHidlTest : public ::testing::TestWithParam<std::string> {
40    public:
SetUp()41     virtual void SetUp() override {
42         client = IStats::getService(GetParam());
43         ASSERT_NE(client, nullptr);
44     }
45 
TearDown()46     virtual void TearDown() override {}
47 
48     sp<IStats> client;
49 };
50 
51 // Sanity check IStats::reportSpeakerImpedance.
TEST_P(StatsHidlTest,reportSpeakerImpedance)52 TEST_P(StatsHidlTest, reportSpeakerImpedance) {
53     SpeakerImpedance impedance = {.speakerLocation = 0,
54                                   .milliOhms = static_cast<int32_t>(1234 * 1000)};
55     Return<void> ret;
56     ret = client->reportSpeakerImpedance(impedance);
57     ASSERT_TRUE(ret.isOk());
58 }
59 
60 // Sanity check IStats::reportHardwareFailed.
TEST_P(StatsHidlTest,reportHardwareFailed)61 TEST_P(StatsHidlTest, reportHardwareFailed) {
62     HardwareFailed failed = {.hardwareType = HardwareFailed::HardwareType::CODEC,
63                              .hardwareLocation = 0,
64                              .errorCode = HardwareFailed::HardwareErrorCode::COMPLETE};
65     Return<void> ret;
66 
67     ret = client->reportHardwareFailed(failed);
68     ASSERT_TRUE(ret.isOk());
69 }
70 
71 // Sanity check IStats::reportChargeCycles.
TEST_P(StatsHidlTest,reportChargeCycles)72 TEST_P(StatsHidlTest, reportChargeCycles) {
73     std::vector<int> charge_cycles = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
74     ChargeCycles cycles;
75     cycles.cycleBucket = charge_cycles;
76 
77     Return<void> ret;
78     ret = client->reportChargeCycles(cycles);
79     ASSERT_TRUE(ret.isOk());
80 }
81 
82 // Sanity check IStats::reportBatteryHealthSnapshot.
TEST_P(StatsHidlTest,reportBatteryHealthSnapshot)83 TEST_P(StatsHidlTest, reportBatteryHealthSnapshot) {
84     BatteryHealthSnapshotArgs args{.temperatureDeciC = 3000,
85                                    .voltageMicroV = 1,
86                                    .currentMicroA = 2,
87                                    .openCircuitVoltageMicroV = 3,
88                                    .resistanceMicroOhm = 5,
89                                    .levelPercent = 101};
90 
91     Return<void> ret;
92     ret = client->reportBatteryHealthSnapshot(args);
93     /* TODO: Test all enum values and/or a bad enum value */
94     ASSERT_TRUE(ret.isOk());
95 }
96 
97 // Sanity check IStats::reportSlowIo.
TEST_P(StatsHidlTest,reportSlowIo)98 TEST_P(StatsHidlTest, reportSlowIo) {
99     SlowIo slowio = {.operation = SlowIo::IoOperation::READ, .count = 5};
100 
101     Return<void> ret;
102     ret = client->reportSlowIo(slowio);
103     /* TODO: Test all enum values and/or a bad enum value */
104     ASSERT_TRUE(ret.isOk());
105 }
106 
107 // Sanity check IStats::reportBatteryCausedShutdown.
TEST_P(StatsHidlTest,reportBatteryCausedShutdown)108 TEST_P(StatsHidlTest, reportBatteryCausedShutdown) {
109     BatteryCausedShutdown shutdown = {.voltageMicroV = 3};
110 
111     Return<void> ret;
112     ret = client->reportBatteryCausedShutdown(shutdown);
113     ASSERT_TRUE(ret.isOk());
114 }
115 
116 // Sanity check IStats::reportUsbPortOverheatEvent.
TEST_P(StatsHidlTest,reportUsbPortOverheatEvent)117 TEST_P(StatsHidlTest, reportUsbPortOverheatEvent) {
118     UsbPortOverheatEvent event = {.plugTemperatureDeciC = 210,
119                                   .maxTemperatureDeciC = 220,
120                                   .timeToOverheat = 1,
121                                   .timeToHysteresis = 2,
122                                   .timeToInactive = 3};
123 
124     Return<void> ret;
125     ret = client->reportUsbPortOverheatEvent(event);
126     ASSERT_TRUE(ret.isOk());
127 }
128 
129 // Sanity check IStats::reportVendorAtom.
TEST_P(StatsHidlTest,reportVendorAtom)130 TEST_P(StatsHidlTest, reportVendorAtom) {
131     std::vector<Value> values;
132     Value tmp;
133     tmp.longValue(70000);
134     values.push_back(tmp);
135     tmp.intValue(7);
136     values.push_back(tmp);
137     tmp.floatValue(8.5);
138     values.push_back(tmp);
139     tmp.stringValue("test");
140     values.push_back(tmp);
141     tmp.intValue(3);
142     values.push_back(tmp);
143     VendorAtom atom = {.reverseDomainName = "com.google.pixel", .atomId = 100001, .values = values};
144 
145     Return<void> ret;
146     client->reportVendorAtom(atom);
147     ASSERT_TRUE(ret.isOk());
148 }
149 
150 INSTANTIATE_TEST_SUITE_P(
151         PerInstance, StatsHidlTest,
152         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IStats::descriptor)),
153         android::hardware::PrintInstanceNameToString);
154