Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -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 | |
Benjamin Schwartz | 18a3fb5 | 2020-10-05 15:44:19 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/stats/IPowerStats.h> |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
| 23 | |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 24 | using aidl::android::hardware::power::stats::Channel; |
Benjamin Schwartz | 18a3fb5 | 2020-10-05 15:44:19 -0700 | [diff] [blame] | 25 | using aidl::android::hardware::power::stats::EnergyMeasurement; |
| 26 | using aidl::android::hardware::power::stats::IPowerStats; |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 27 | using aidl::android::hardware::power::stats::PowerEntity; |
Benjamin Schwartz | 18a3fb5 | 2020-10-05 15:44:19 -0700 | [diff] [blame] | 28 | using aidl::android::hardware::power::stats::StateResidencyResult; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 29 | |
| 30 | using ndk::SpAIBinder; |
| 31 | |
| 32 | class 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 | |
| 43 | TEST_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 |
| 49 | TEST_P(PowerStatsAidl, ValidatePowerEntityNames) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 50 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 52 | |
| 53 | for (auto info : infos) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 54 | EXPECT_NE(info.name, ""); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | // Each power entity must have a unique name |
| 59 | TEST_P(PowerStatsAidl, ValidatePowerEntityUniqueNames) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 60 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 62 | |
| 63 | std::set<std::string> names; |
| 64 | for (auto info : infos) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 65 | EXPECT_TRUE(names.insert(info.name).second); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | // Each PowerEntity must have a unique ID |
| 70 | TEST_P(PowerStatsAidl, ValidatePowerEntityIds) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 71 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 73 | |
| 74 | std::set<int32_t> ids; |
| 75 | for (auto info : infos) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 76 | EXPECT_TRUE(ids.insert(info.id).second); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | // Each state must have a valid name |
| 81 | TEST_P(PowerStatsAidl, ValidateStateNames) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 82 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 83 | ASSERT_TRUE(powerstats->getPowerEntityInfo(&infos).isOk()); |
| 84 | |
| 85 | for (auto info : infos) { |
| 86 | for (auto state : info.states) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 87 | EXPECT_NE(state.name, ""); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Each state must have a name that is unique to the given PowerEntity |
| 93 | TEST_P(PowerStatsAidl, ValidateStateUniqueNames) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 94 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 95 | 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 Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 100 | EXPECT_TRUE(stateNames.insert(state.name).second); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Each state must have an ID that is unique to the given PowerEntity |
| 106 | TEST_P(PowerStatsAidl, ValidateStateUniqueIds) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 107 | std::vector<PowerEntity> infos; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 108 | 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 Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 113 | EXPECT_TRUE(stateIds.insert(state.id).second); |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | TEST_P(PowerStatsAidl, TestGetStateResidency) { |
| 119 | std::vector<StateResidencyResult> results; |
| 120 | ASSERT_TRUE(powerstats->getStateResidency({}, &results).isOk()); |
| 121 | } |
| 122 | |
| 123 | TEST_P(PowerStatsAidl, TestGetEnergyMeterInfo) { |
Benjamin Schwartz | 74e09c7 | 2020-12-21 14:57:45 -0800 | [diff] [blame] | 124 | std::vector<Channel> info; |
Benjamin Schwartz | 817521b | 2020-10-05 09:20:16 -0700 | [diff] [blame] | 125 | ASSERT_TRUE(powerstats->getEnergyMeterInfo(&info).isOk()); |
| 126 | } |
| 127 | |
| 128 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PowerStatsAidl); |
| 129 | INSTANTIATE_TEST_SUITE_P( |
| 130 | PowerStats, PowerStatsAidl, |
| 131 | testing::ValuesIn(android::getAidlHalInstanceNames(IPowerStats::descriptor)), |
| 132 | android::PrintInstanceNameToString); |
| 133 | |
| 134 | int main(int argc, char** argv) { |
| 135 | ::testing::InitGoogleTest(&argc, argv); |
| 136 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 137 | ABinderProcess_startThreadPool(); |
| 138 | return RUN_ALL_TESTS(); |
| 139 | } |