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 <VtsHalHidlTargetTestBase.h>
22 #include <VtsHalHidlTargetTestEnvBase.h>
23 
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 // Test environment for Power HIDL HAL.
40 class StatsHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
41    public:
42     // get the test environment singleton
Instance()43     static StatsHidlEnvironment* Instance() {
44         static StatsHidlEnvironment* instance = new StatsHidlEnvironment;
45         return instance;
46     }
47 
registerTestServices()48     virtual void registerTestServices() override { registerTestService<IStats>(); }
49 };
50 
51 class StatsHidlTest : public ::testing::VtsHalHidlTargetTestBase {
52    public:
SetUp()53     virtual void SetUp() override {
54         client = ::testing::VtsHalHidlTargetTestBase::getService<IStats>(
55             StatsHidlEnvironment::Instance()->getServiceName<IStats>());
56         ASSERT_NE(client, nullptr);
57     }
58 
TearDown()59     virtual void TearDown() override {}
60 
61     sp<IStats> client;
62 };
63 
64 // Sanity check IStats::reportSpeakerImpedance.
TEST_F(StatsHidlTest,reportSpeakerImpedance)65 TEST_F(StatsHidlTest, reportSpeakerImpedance) {
66     SpeakerImpedance impedance = {.speakerLocation = 0,
67                                   .milliOhms = static_cast<int32_t>(1234 * 1000)};
68     Return<void> ret;
69     ret = client->reportSpeakerImpedance(impedance);
70     ASSERT_TRUE(ret.isOk());
71 }
72 
73 // Sanity check IStats::reportHardwareFailed.
TEST_F(StatsHidlTest,reportHardwareFailed)74 TEST_F(StatsHidlTest, reportHardwareFailed) {
75     HardwareFailed failed = {.hardwareType = HardwareFailed::HardwareType::CODEC,
76                              .hardwareLocation = 0,
77                              .errorCode = HardwareFailed::HardwareErrorCode::COMPLETE};
78     Return<void> ret;
79 
80     ret = client->reportHardwareFailed(failed);
81     ASSERT_TRUE(ret.isOk());
82 }
83 
84 // Sanity check IStats::reportChargeCycles.
TEST_F(StatsHidlTest,reportChargeCycles)85 TEST_F(StatsHidlTest, reportChargeCycles) {
86     std::vector<int> charge_cycles = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
87     ChargeCycles cycles;
88     cycles.cycleBucket = charge_cycles;
89 
90     Return<void> ret;
91     ret = client->reportChargeCycles(cycles);
92     ASSERT_TRUE(ret.isOk());
93 }
94 
95 // Sanity check IStats::reportBatteryHealthSnapshot.
TEST_F(StatsHidlTest,reportBatteryHealthSnapshot)96 TEST_F(StatsHidlTest, reportBatteryHealthSnapshot) {
97     BatteryHealthSnapshotArgs args{.temperatureDeciC = 3000,
98                                    .voltageMicroV = 1,
99                                    .currentMicroA = 2,
100                                    .openCircuitVoltageMicroV = 3,
101                                    .resistanceMicroOhm = 5,
102                                    .levelPercent = 101};
103 
104     Return<void> ret;
105     ret = client->reportBatteryHealthSnapshot(args);
106     /* TODO: Test all enum values and/or a bad enum value */
107     ASSERT_TRUE(ret.isOk());
108 }
109 
110 // Sanity check IStats::reportSlowIo.
TEST_F(StatsHidlTest,reportSlowIo)111 TEST_F(StatsHidlTest, reportSlowIo) {
112     SlowIo slowio = {.operation = SlowIo::IoOperation::READ, .count = 5};
113 
114     Return<void> ret;
115     ret = client->reportSlowIo(slowio);
116     /* TODO: Test all enum values and/or a bad enum value */
117     ASSERT_TRUE(ret.isOk());
118 }
119 
120 // Sanity check IStats::reportBatteryCausedShutdown.
TEST_F(StatsHidlTest,reportBatteryCausedShutdown)121 TEST_F(StatsHidlTest, reportBatteryCausedShutdown) {
122     BatteryCausedShutdown shutdown = {.voltageMicroV = 3};
123 
124     Return<void> ret;
125     ret = client->reportBatteryCausedShutdown(shutdown);
126     ASSERT_TRUE(ret.isOk());
127 }
128 
129 // Sanity check IStats::reportUsbPortOverheatEvent.
TEST_F(StatsHidlTest,reportUsbPortOverheatEvent)130 TEST_F(StatsHidlTest, reportUsbPortOverheatEvent) {
131     UsbPortOverheatEvent event = {.maxTemperatureDeciC = 220,
132                                   .plugTemperatureDeciC = 210,
133                                   .timeToOverheat = 1,
134                                   .timeToHysteresis = 2,
135                                   .timeToInactive = 3};
136 
137     Return<void> ret;
138     ret = client->reportUsbPortOverheatEvent(event);
139     ASSERT_TRUE(ret.isOk());
140 }
141 
142 // Sanity check IStats::reportVendorAtom.
TEST_F(StatsHidlTest,reportVendorAtom)143 TEST_F(StatsHidlTest, reportVendorAtom) {
144     std::vector<Value> values;
145     Value tmp;
146     tmp.longValue(70000);
147     values.push_back(tmp);
148     tmp.intValue(7);
149     values.push_back(tmp);
150     tmp.floatValue(8.5);
151     values.push_back(tmp);
152     tmp.stringValue("test");
153     values.push_back(tmp);
154     tmp.intValue(3);
155     values.push_back(tmp);
156     VendorAtom atom = {.reverseDomainName = "com.google.pixel", .atomId = 100001, .values = values};
157 
158     Return<void> ret;
159     client->reportVendorAtom(atom);
160     ASSERT_TRUE(ret.isOk());
161 }
162 
main(int argc,char ** argv)163 int main(int argc, char** argv) {
164     ::testing::AddGlobalTestEnvironment(StatsHidlEnvironment::Instance());
165     ::testing::InitGoogleTest(&argc, argv);
166     StatsHidlEnvironment::Instance()->init(&argc, argv);
167     int status = RUN_ALL_TESTS();
168     LOG(INFO) << "Test result = " << status;
169     return status;
170 }
171