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 ::testing::UnorderedElementsAreArray;
33
34 namespace {
35
createPolicy(const std::string & policyId,const std::vector<PowerComponent> & enabledComponents,const std::vector<PowerComponent> & disabledComponents)36 CarPowerPolicyPtr createPolicy(const std::string& policyId,
37 const std::vector<PowerComponent>& enabledComponents,
38 const std::vector<PowerComponent>& disabledComponents) {
39 CarPowerPolicyPtr policy = std::make_shared<CarPowerPolicy>();
40 policy->policyId = policyId;
41 policy->enabledComponents = enabledComponents;
42 policy->disabledComponents = disabledComponents;
43 return policy;
44 }
45
assertEqual(const CarPowerPolicyPtr & left,const CarPowerPolicyPtr & right)46 void assertEqual(const CarPowerPolicyPtr& left, const CarPowerPolicyPtr& right) {
47 ASSERT_EQ(left->policyId, right->policyId);
48 EXPECT_THAT(left->enabledComponents, UnorderedElementsAreArray(right->enabledComponents));
49 EXPECT_THAT(left->disabledComponents, UnorderedElementsAreArray(right->disabledComponents));
50 }
51
52 } // namespace
53
54 class PowerComponentHandlerTest : public ::testing::Test {
55 public:
PowerComponentHandlerTest()56 PowerComponentHandlerTest() { handler.init(); }
57
58 PowerComponentHandler handler;
59 };
60
TEST_F(PowerComponentHandlerTest,TestInitialPowerComponentStates)61 TEST_F(PowerComponentHandlerTest, TestInitialPowerComponentStates) {
62 CarPowerPolicyPtr policy = handler.getAccumulatedPolicy();
63 std::vector<PowerComponent> allComponents;
64 for (auto componentId : enum_range<PowerComponent>()) {
65 allComponents.push_back(componentId);
66 }
67
68 EXPECT_THAT(allComponents, UnorderedElementsAreArray(policy->disabledComponents));
69 }
70
TEST_F(PowerComponentHandlerTest,TestGetPowerComponentState)71 TEST_F(PowerComponentHandlerTest, TestGetPowerComponentState) {
72 CarPowerPolicyPtr policy =
73 createPolicy("test_policy", {PowerComponent::WIFI, PowerComponent::NFC},
74 {PowerComponent::AUDIO, PowerComponent::DISPLAY});
75 handler.applyPowerPolicy(policy);
76
77 ASSERT_TRUE(*handler.getPowerComponentState(PowerComponent::WIFI));
78 ASSERT_TRUE(*handler.getPowerComponentState(PowerComponent::NFC));
79 ASSERT_FALSE(*handler.getPowerComponentState(PowerComponent::AUDIO));
80 ASSERT_FALSE(*handler.getPowerComponentState(PowerComponent::DISPLAY));
81 }
82
TEST_F(PowerComponentHandlerTest,TestApplyPowerPolicy_multipleTimes)83 TEST_F(PowerComponentHandlerTest, TestApplyPowerPolicy_multipleTimes) {
84 std::vector<std::tuple<std::string, std::vector<PowerComponent>, std::vector<PowerComponent>>>
85 testCases = {
86 {"test_policy1", {PowerComponent::WIFI}, {PowerComponent::AUDIO}},
87 {"test_policy2",
88 {PowerComponent::WIFI, PowerComponent::DISPLAY},
89 {PowerComponent::NFC}},
90 {"test_policy3",
91 {PowerComponent::CPU, PowerComponent::INPUT},
92 {PowerComponent::WIFI}},
93 {"test_policy4", {PowerComponent::MEDIA, PowerComponent::AUDIO}, {}},
94 };
95 CarPowerPolicyPtr expectedPolicy =
96 createPolicy("test_policy4",
97 {PowerComponent::AUDIO, PowerComponent::MEDIA, PowerComponent::DISPLAY,
98 PowerComponent::INPUT, PowerComponent::CPU},
99 {PowerComponent::BLUETOOTH, PowerComponent::WIFI, PowerComponent::CELLULAR,
100 PowerComponent::ETHERNET, PowerComponent::PROJECTION, PowerComponent::NFC,
101 PowerComponent::VOICE_INTERACTION, PowerComponent::VISUAL_INTERACTION,
102 PowerComponent::TRUSTED_DEVICE_DETECTION, PowerComponent::LOCATION,
103 PowerComponent::MICROPHONE});
104
105 for (const auto& tc : testCases) {
106 auto [policyId, enabledComponents, disabledComponents] = tc;
107 CarPowerPolicyPtr policy = createPolicy(policyId, enabledComponents, disabledComponents);
108 handler.applyPowerPolicy(policy);
109 }
110
111 ASSERT_NO_FATAL_FAILURE(assertEqual(expectedPolicy, handler.getAccumulatedPolicy()));
112 }
113
114 } // namespace powerpolicy
115 } // namespace automotive
116 } // namespace frameworks
117 } // namespace android
118