Benjamin Schwartz | 40492c2 | 2020-08-31 11:55:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include <aidl/android/hardware/powerstats/IPowerStats.h> |
| 20 | #include <android-base/properties.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
| 23 | |
| 24 | using aidl::android::hardware::powerstats::EnergyData; |
| 25 | using aidl::android::hardware::powerstats::IPowerStats; |
| 26 | using aidl::android::hardware::powerstats::PowerEntityInfo; |
| 27 | using aidl::android::hardware::powerstats::PowerEntityStateResidencyResult; |
Benjamin Schwartz | 40492c2 | 2020-08-31 11:55:55 -0700 | [diff] [blame] | 28 | using aidl::android::hardware::powerstats::RailInfo; |
| 29 | using ndk::SpAIBinder; |
| 30 | |
| 31 | class PowerStatsAidl : public testing::TestWithParam<std::string> { |
| 32 | public: |
| 33 | virtual void SetUp() override { |
| 34 | powerstats = IPowerStats::fromBinder( |
| 35 | SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 36 | ASSERT_NE(nullptr, powerstats.get()); |
| 37 | } |
| 38 | |
| 39 | std::shared_ptr<IPowerStats> powerstats; |
| 40 | }; |
| 41 | |
| 42 | TEST_P(PowerStatsAidl, TestGetEnergyData) { |
| 43 | std::vector<EnergyData> data; |
| 44 | ASSERT_TRUE(powerstats->getEnergyData({}, &data).isOk()); |
| 45 | } |
| 46 | |
Benjamin Schwartz | c4529fb | 2020-09-01 11:57:53 -0700 | [diff] [blame^] | 47 | // Each PowerEntity must have a valid name |
| 48 | TEST_P(PowerStatsAidl, ValidatePowerEntityNames) { |
| 49 | std::vector<PowerEntityInfo> infos; |
| 50 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 51 | |
| 52 | for (auto info : infos) { |
| 53 | EXPECT_NE(info.powerEntityName, ""); |
| 54 | } |
Benjamin Schwartz | 40492c2 | 2020-08-31 11:55:55 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Benjamin Schwartz | c4529fb | 2020-09-01 11:57:53 -0700 | [diff] [blame^] | 57 | // Each power entity must have a unique name |
| 58 | TEST_P(PowerStatsAidl, ValidatePowerEntityUniqueNames) { |
| 59 | std::vector<PowerEntityInfo> infos; |
| 60 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 61 | |
| 62 | std::set<std::string> names; |
| 63 | for (auto info : infos) { |
| 64 | EXPECT_TRUE(names.insert(info.powerEntityName).second); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Each PowerEntity must have a unique ID |
| 69 | TEST_P(PowerStatsAidl, ValidatePowerEntityIds) { |
| 70 | std::vector<PowerEntityInfo> infos; |
| 71 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 72 | |
| 73 | std::set<int32_t> ids; |
| 74 | for (auto info : infos) { |
| 75 | EXPECT_TRUE(ids.insert(info.powerEntityId).second); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Each state must have a valid name |
| 80 | TEST_P(PowerStatsAidl, ValidateStateNames) { |
| 81 | std::vector<PowerEntityInfo> infos; |
| 82 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 83 | |
| 84 | for (auto info : infos) { |
| 85 | for (auto state : info.states) { |
| 86 | EXPECT_NE(state.powerEntityStateName, ""); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Each state must have a name that is unique to the given PowerEntity |
| 92 | TEST_P(PowerStatsAidl, ValidateStateUniqueNames) { |
| 93 | std::vector<PowerEntityInfo> infos; |
| 94 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 95 | |
| 96 | for (auto info : infos) { |
| 97 | std::set<std::string> stateNames; |
| 98 | for (auto state : info.states) { |
| 99 | EXPECT_TRUE(stateNames.insert(state.powerEntityStateName).second); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Each state must have an ID that is unique to the given PowerEntity |
| 105 | TEST_P(PowerStatsAidl, ValidateStateUniqueIds) { |
| 106 | std::vector<PowerEntityInfo> infos; |
| 107 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 108 | |
| 109 | for (auto info : infos) { |
| 110 | std::set<uint32_t> stateIds; |
| 111 | for (auto state : info.states) { |
| 112 | EXPECT_TRUE(stateIds.insert(state.powerEntityStateId).second); |
| 113 | } |
| 114 | } |
Benjamin Schwartz | 40492c2 | 2020-08-31 11:55:55 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_P(PowerStatsAidl, TestGetPowerEntityStateResidencyData) { |
| 118 | std::vector<PowerEntityStateResidencyResult> data; |
| 119 | ASSERT_TRUE(powerstats->getPowerEntityStateResidencyData({}, &data).isOk()); |
| 120 | } |
| 121 | |
| 122 | TEST_P(PowerStatsAidl, TestGetRailInfo) { |
| 123 | std::vector<RailInfo> info; |
| 124 | ASSERT_TRUE(powerstats->getRailInfo(&info).isOk()); |
| 125 | } |
| 126 | |
| 127 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerStatsAidl); |
| 128 | INSTANTIATE_TEST_SUITE_P( |
| 129 | PowerStats, PowerStatsAidl, |
| 130 | testing::ValuesIn(android::getAidlHalInstanceNames(IPowerStats::descriptor)), |
| 131 | android::PrintInstanceNameToString); |
| 132 | |
| 133 | int main(int argc, char** argv) { |
| 134 | ::testing::InitGoogleTest(&argc, argv); |
| 135 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 136 | ABinderProcess_startThreadPool(); |
| 137 | return RUN_ALL_TESTS(); |
| 138 | } |