blob: f293773383207ac839d21fedc46cb797f5a20a88 [file] [log] [blame]
Benjamin Schwartz817521b2020-10-05 09:20:16 -07001/*
2 * Copyright (C) 2020 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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
Benjamin Schwartz18a3fb52020-10-05 15:44:19 -070019#include <aidl/android/hardware/power/stats/IPowerStats.h>
Benjamin Schwartz817521b2020-10-05 09:20:16 -070020#include <android-base/properties.h>
21#include <android/binder_manager.h>
22#include <android/binder_process.h>
23
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080024using aidl::android::hardware::power::stats::Channel;
Benjamin Schwartz18a3fb52020-10-05 15:44:19 -070025using aidl::android::hardware::power::stats::EnergyMeasurement;
26using aidl::android::hardware::power::stats::IPowerStats;
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080027using aidl::android::hardware::power::stats::PowerEntity;
Benjamin Schwartz18a3fb52020-10-05 15:44:19 -070028using aidl::android::hardware::power::stats::StateResidencyResult;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070029
30using ndk::SpAIBinder;
31
32class PowerStatsAidl : public testing::TestWithParam<std::string> {
33 public:
34 virtual void SetUp() override {
35 powerstats = IPowerStats::fromBinder(
36 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
37 ASSERT_NE(nullptr, powerstats.get());
38 }
39
40 std::shared_ptr<IPowerStats> powerstats;
41};
42
43TEST_P(PowerStatsAidl, TestReadEnergyMeter) {
44 std::vector<EnergyMeasurement> data;
45 ASSERT_TRUE(powerstats->readEnergyMeters({}, &data).isOk());
46}
47
48// Each PowerEntity must have a valid name
49TEST_P(PowerStatsAidl, ValidatePowerEntityNames) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080050 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070051 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
52
53 for (auto info : infos) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080054 EXPECT_NE(info.name, "");
Benjamin Schwartz817521b2020-10-05 09:20:16 -070055 }
56}
57
58// Each power entity must have a unique name
59TEST_P(PowerStatsAidl, ValidatePowerEntityUniqueNames) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080060 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070061 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
62
63 std::set<std::string> names;
64 for (auto info : infos) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080065 EXPECT_TRUE(names.insert(info.name).second);
Benjamin Schwartz817521b2020-10-05 09:20:16 -070066 }
67}
68
69// Each PowerEntity must have a unique ID
70TEST_P(PowerStatsAidl, ValidatePowerEntityIds) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080071 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070072 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
73
74 std::set<int32_t> ids;
75 for (auto info : infos) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080076 EXPECT_TRUE(ids.insert(info.id).second);
Benjamin Schwartz817521b2020-10-05 09:20:16 -070077 }
78}
79
80// Each state must have a valid name
81TEST_P(PowerStatsAidl, ValidateStateNames) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080082 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070083 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
84
85 for (auto info : infos) {
86 for (auto state : info.states) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080087 EXPECT_NE(state.name, "");
Benjamin Schwartz817521b2020-10-05 09:20:16 -070088 }
89 }
90}
91
92// Each state must have a name that is unique to the given PowerEntity
93TEST_P(PowerStatsAidl, ValidateStateUniqueNames) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -080094 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -070095 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
96
97 for (auto info : infos) {
98 std::set<std::string> stateNames;
99 for (auto state : info.states) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -0800100 EXPECT_TRUE(stateNames.insert(state.name).second);
Benjamin Schwartz817521b2020-10-05 09:20:16 -0700101 }
102 }
103}
104
105// Each state must have an ID that is unique to the given PowerEntity
106TEST_P(PowerStatsAidl, ValidateStateUniqueIds) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -0800107 std::vector<PowerEntity> infos;
Benjamin Schwartz817521b2020-10-05 09:20:16 -0700108 ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk());
109
110 for (auto info : infos) {
111 std::set<int32_t> stateIds;
112 for (auto state : info.states) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -0800113 EXPECT_TRUE(stateIds.insert(state.id).second);
Benjamin Schwartz817521b2020-10-05 09:20:16 -0700114 }
115 }
116}
117
118TEST_P(PowerStatsAidl, TestGetStateResidency) {
119 std::vector<StateResidencyResult> results;
120 ASSERT_TRUE(powerstats->getStateResidency({}, &results).isOk());
121}
122
123TEST_P(PowerStatsAidl, TestGetEnergyMeterInfo) {
Benjamin Schwartz74e09c72020-12-21 14:57:45 -0800124 std::vector<Channel> info;
Benjamin Schwartz817521b2020-10-05 09:20:16 -0700125 ASSERT_TRUE(powerstats->getEnergyMeterInfo(&info).isOk());
126}
127
128GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerStatsAidl);
129INSTANTIATE_TEST_SUITE_P(
130 PowerStats, PowerStatsAidl,
131 testing::ValuesIn(android::getAidlHalInstanceNames(IPowerStats::descriptor)),
132 android::PrintInstanceNameToString);
133
134int main(int argc, char** argv) {
135 ::testing::InitGoogleTest(&argc, argv);
136 ABinderProcess_setThreadPoolMaxThreadCount(1);
137 ABinderProcess_startThreadPool();
138 return RUN_ALL_TESTS();
139}