1 /*
2 * Copyright (c) 2021, 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 #include "PowerComponentHandler.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <memory>
23 #include <string>
24 #include <tuple>
25 #include <vector>
26
27 namespace android {
28 namespace frameworks {
29 namespace automotive {
30 namespace powerpolicy {
31
32 using ::aidl::android::frameworks::automotive::powerpolicy::CarPowerPolicy;
33 using ::aidl::android::frameworks::automotive::powerpolicy::PowerComponent;
34
35 using ::testing::UnorderedElementsAreArray;
36
37 namespace {
38
39 static constexpr int32_t CUSTOM_COMPONENT_ID_1000 = 1000;
40 static constexpr int32_t CUSTOM_COMPONENT_ID_1002 = 1002;
41
createPolicy(const std::string & policyId,const std::vector<PowerComponent> & enabledComponents,const std::vector<PowerComponent> & disabledComponents,const std::vector<int> & enabledCustomComponents,const std::vector<int> & disabledCustomComponents)42 CarPowerPolicyPtr createPolicy(const std::string& policyId,
43 const std::vector<PowerComponent>& enabledComponents,
44 const std::vector<PowerComponent>& disabledComponents,
45 const std::vector<int>& enabledCustomComponents,
46 const std::vector<int>& disabledCustomComponents) {
47 CarPowerPolicyPtr policy = std::make_shared<CarPowerPolicy>();
48 policy->policyId = policyId;
49 policy->enabledComponents = enabledComponents;
50 policy->disabledComponents = disabledComponents;
51 policy->enabledCustomComponents = enabledCustomComponents;
52 policy->disabledCustomComponents = disabledCustomComponents;
53 return policy;
54 }
55
assertEqual(const CarPowerPolicyPtr & left,const CarPowerPolicyPtr & right)56 void assertEqual(const CarPowerPolicyPtr& left, const CarPowerPolicyPtr& right) {
57 ASSERT_EQ(left->policyId, right->policyId);
58 EXPECT_THAT(left->enabledComponents, UnorderedElementsAreArray(right->enabledComponents));
59 EXPECT_THAT(left->disabledComponents, UnorderedElementsAreArray(right->disabledComponents));
60 EXPECT_THAT(left->enabledCustomComponents,
61 UnorderedElementsAreArray(right->enabledCustomComponents));
62 EXPECT_THAT(left->disabledCustomComponents,
63 UnorderedElementsAreArray(right->disabledCustomComponents));
64 }
65
66 } // namespace
67
68 class PowerComponentHandlerTest : public ::testing::Test {
69 public:
PowerComponentHandlerTest()70 PowerComponentHandlerTest() { handler.init(); }
71
72 PowerComponentHandler handler;
73 };
74
TEST_F(PowerComponentHandlerTest,TestInitialPowerComponentStates)75 TEST_F(PowerComponentHandlerTest, TestInitialPowerComponentStates) {
76 CarPowerPolicyPtr policy = handler.getAccumulatedPolicy();
77 std::vector<PowerComponent> allComponents;
78 for (auto componentId : ::ndk::enum_range<PowerComponent>()) {
79 if (componentId >= PowerComponent::MINIMUM_CUSTOM_COMPONENT_VALUE) {
80 continue;
81 }
82 allComponents.push_back(componentId);
83 }
84
85 EXPECT_THAT(allComponents, UnorderedElementsAreArray(policy->disabledComponents));
86 }
87
TEST_F(PowerComponentHandlerTest,TestGetPowerComponentState)88 TEST_F(PowerComponentHandlerTest, TestGetPowerComponentState) {
89 CarPowerPolicyPtr policy =
90 createPolicy("test_policy", {PowerComponent::WIFI, PowerComponent::NFC},
91 {PowerComponent::AUDIO, PowerComponent::DISPLAY}, {}, {});
92 handler.applyPowerPolicy(policy);
93
94 ASSERT_TRUE(*handler.getPowerComponentState(PowerComponent::WIFI));
95 ASSERT_TRUE(*handler.getPowerComponentState(PowerComponent::NFC));
96 ASSERT_FALSE(*handler.getPowerComponentState(PowerComponent::AUDIO));
97 ASSERT_FALSE(*handler.getPowerComponentState(PowerComponent::DISPLAY));
98 }
99
TEST_F(PowerComponentHandlerTest,TestGetCustomPowerComponentState)100 TEST_F(PowerComponentHandlerTest, TestGetCustomPowerComponentState) {
101 CarPowerPolicyPtr policy =
102 createPolicy("test_policy", {PowerComponent::WIFI, PowerComponent::NFC},
103 {PowerComponent::AUDIO, PowerComponent::DISPLAY},
104 {CUSTOM_COMPONENT_ID_1000}, {CUSTOM_COMPONENT_ID_1002});
105 handler.applyPowerPolicy(policy);
106
107 ASSERT_TRUE(*handler.getCustomPowerComponentState(CUSTOM_COMPONENT_ID_1000));
108 ASSERT_FALSE(*handler.getCustomPowerComponentState(CUSTOM_COMPONENT_ID_1002));
109 }
110
TEST_F(PowerComponentHandlerTest,TestApplyPowerPolicy_multipleTimes)111 TEST_F(PowerComponentHandlerTest, TestApplyPowerPolicy_multipleTimes) {
112 std::vector<std::tuple<std::string, std::vector<PowerComponent>, std::vector<PowerComponent>>>
113 testCases = {
114 {"test_policy1", {PowerComponent::WIFI}, {PowerComponent::AUDIO}},
115 {"test_policy2",
116 {PowerComponent::WIFI, PowerComponent::DISPLAY},
117 {PowerComponent::NFC}},
118 {"test_policy3",
119 {PowerComponent::CPU, PowerComponent::INPUT},
120 {PowerComponent::WIFI}},
121 {"test_policy4", {PowerComponent::MEDIA, PowerComponent::AUDIO}, {}},
122 };
123 CarPowerPolicyPtr expectedPolicy =
124 createPolicy("test_policy4",
125 {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
126 PowerComponent::INPUT, PowerComponent::CPU},
127 {PowerComponent::BLUETOOTH, PowerComponent::WIFI, PowerComponent::CELLULAR,
128 PowerComponent::ETHERNET, PowerComponent::PROJECTION, PowerComponent::NFC,
129 PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
130 PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
131 PowerComponent::MICROPHONE},
132 {}, {});
133
134 for (const auto& tc : testCases) {
135 auto [policyId, enabledComponents, disabledComponents] = tc;
136 CarPowerPolicyPtr policy =
137 createPolicy(policyId, enabledComponents, disabledComponents, {}, {});
138 handler.applyPowerPolicy(policy);
139 }
140
141 ASSERT_NO_FATAL_FAILURE(assertEqual(expectedPolicy, handler.getAccumulatedPolicy()));
142 }
143
TEST_F(PowerComponentHandlerTest,TestApplyPowerPolicy_change_policies_with_custom_components)144 TEST_F(PowerComponentHandlerTest, TestApplyPowerPolicy_change_policies_with_custom_components) {
145 CarPowerPolicyPtr policy =
146 createPolicy("test_policy1", {PowerComponent::WIFI, PowerComponent::AUDIO}, {}, {}, {});
147
148 handler.applyPowerPolicy(policy);
149
150 CarPowerPolicyPtr expectedPolicy =
151 createPolicy("test_policy1", {PowerComponent::WIFI, PowerComponent::AUDIO},
152 {PowerComponent::MEDIA, PowerComponent::DISPLAY, PowerComponent::INPUT,
153 PowerComponent::CPU, PowerComponent::BLUETOOTH, PowerComponent::CELLULAR,
154 PowerComponent::ETHERNET, PowerComponent::PROJECTION, PowerComponent::NFC,
155 PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
156 PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
157 PowerComponent::MICROPHONE},
158 {}, {});
159 ASSERT_NO_FATAL_FAILURE(assertEqual(expectedPolicy, handler.getAccumulatedPolicy()));
160
161 policy = createPolicy("test_policy2", {}, {}, {CUSTOM_COMPONENT_ID_1002}, {});
162
163 handler.applyPowerPolicy(policy);
164
165 expectedPolicy.get()->policyId = "test_policy2";
166 expectedPolicy.get()->enabledCustomComponents = {CUSTOM_COMPONENT_ID_1002};
167
168 ASSERT_NO_FATAL_FAILURE(assertEqual(handler.getAccumulatedPolicy(), expectedPolicy));
169
170 policy = createPolicy("test_policy3", {}, {},
171 {CUSTOM_COMPONENT_ID_1002, CUSTOM_COMPONENT_ID_1000}, {});
172
173 handler.applyPowerPolicy(policy);
174
175 expectedPolicy.get()->policyId = "test_policy3";
176 expectedPolicy.get()->enabledCustomComponents = {CUSTOM_COMPONENT_ID_1002,
177 CUSTOM_COMPONENT_ID_1000};
178
179 ASSERT_NO_FATAL_FAILURE(assertEqual(handler.getAccumulatedPolicy(), expectedPolicy));
180
181 policy = createPolicy("test_policy4", {}, {}, {CUSTOM_COMPONENT_ID_1000},
182 {CUSTOM_COMPONENT_ID_1002});
183
184 handler.applyPowerPolicy(policy);
185
186 expectedPolicy.get()->policyId = "test_policy4";
187 expectedPolicy.get()->enabledCustomComponents = {CUSTOM_COMPONENT_ID_1000};
188 expectedPolicy.get()->disabledCustomComponents = {CUSTOM_COMPONENT_ID_1002};
189
190 ASSERT_NO_FATAL_FAILURE(assertEqual(handler.getAccumulatedPolicy(), expectedPolicy));
191 }
192
193 } // namespace powerpolicy
194 } // namespace automotive
195 } // namespace frameworks
196 } // namespace android
197