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 package android.car.cts.powerpolicy;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import com.android.tradefed.log.LogUtil.CLog;
23 
24 import java.util.ArrayList;
25 import java.util.Set;
26 
27 public final class PowerPolicyTestHelper {
28     private final CpmsFrameworkLayerStateInfo mFrameCpms;
29     private final CpmsSystemLayerStateInfo mSystemCpms;
30     private final SilentModeInfo mSilentMode;
31     private final String mStep;
32     private final String mTestcase;
33 
34     public static final String CURRENT_STATE_ASSERT_MSG = "current state";
35     public static final String CURRENT_POLICY_ASSERT_MSG = "current policy";
36     public static final String CURRENT_POWER_COMPONENT_ASSERT_MSG = "current power components";
37     public static final String REGISTERED_POLICY_ASSERT_MSG = "registered policy";
38     public static final String SILENT_MODE_FULL_ASSERT_MSG = "silent mode in full";
39     public static final String SILENT_MODE_STATUS_ASSERT_MSG = "silent mode status";
40     public static final String PENDING_POLICY_ASSERT_MSG = "pending policy id";
41     public static final String TOTAL_REGISTERED_POLICIES_ASSERT_MSG =
42             "the total number of registered policies";
43 
PowerPolicyTestHelper(String testcase, String step, CpmsFrameworkLayerStateInfo frameCpms, CpmsSystemLayerStateInfo sysCpms, SilentModeInfo silentMode)44     public PowerPolicyTestHelper(String testcase, String step,
45             CpmsFrameworkLayerStateInfo frameCpms, CpmsSystemLayerStateInfo sysCpms,
46             SilentModeInfo silentMode) {
47         mStep = step;
48         mTestcase = testcase;
49         mFrameCpms = frameCpms;
50         mSystemCpms = sysCpms;
51         mSilentMode = silentMode;
52     }
53 
checkCurrentState(int expected)54     public void checkCurrentState(int expected) {
55         String msg = CURRENT_STATE_ASSERT_MSG + "\nmFrameCpms:\n" + mFrameCpms;
56         assertWithMessage(msg)
57                 .that(mFrameCpms.getCurrentState()).isEqualTo(expected);
58     }
59 
checkCurrentPolicy(String expectedPolicyId)60     public void checkCurrentPolicy(String expectedPolicyId) {
61         boolean expected = expectedPolicyId.equals(mFrameCpms.getCurrentPolicyId());
62         if (!expected) {
63             CLog.d("expectedPolicyId: " + expectedPolicyId);
64             CLog.d("currentPolicyId: " + mFrameCpms.getCurrentPolicyId());
65         }
66         assertWithMessage(CURRENT_POLICY_ASSERT_MSG).that(expected).isTrue();
67     }
68 
checkSilentModeStatus(boolean expected)69     public void checkSilentModeStatus(boolean expected) {
70         assertWithMessage(SILENT_MODE_STATUS_ASSERT_MSG)
71                 .that(mFrameCpms.getForcedSilentMode() == expected).isTrue();
72     }
73 
checkSilentModeFull(SilentModeInfo expected)74     public void checkSilentModeFull(SilentModeInfo expected) {
75         boolean status = expected.equals(mSilentMode);
76         if (!status) {
77             CLog.e("PowerPolicyTestHelper expected silent mode: %s", expected.toString());
78             CLog.e("PowerPolicyTestHelper got tested silent mode: %s", mSilentMode.toString());
79         }
80         assertWithMessage(SILENT_MODE_FULL_ASSERT_MSG).that(status).isTrue();
81     }
82 
checkRegisteredPolicy(PowerPolicyDef expectedPolicy)83     public void checkRegisteredPolicy(PowerPolicyDef expectedPolicy) {
84         boolean status = false;
85         for (PowerPolicyDef def : mSystemCpms.getRegisteredPolicies()) {
86             if (def.getPolicyId().equals(expectedPolicy.getPolicyId())) {
87                 status = expectedPolicy.equals(def);
88                 if (!status) {
89                     CLog.e("PowerPolicyTestHelper expected policy: %s", expectedPolicy.toString());
90                     CLog.e("PowerPolicyTestHelper got result policy: %s", def.toString());
91                 }
92                 break;
93             }
94         }
95         assertWithMessage(REGISTERED_POLICY_ASSERT_MSG).that(status).isTrue();
96     }
97 
checkTotalRegisteredPolicies(int totalNum)98     public void checkTotalRegisteredPolicies(int totalNum) {
99         ArrayList<PowerPolicyDef> policies = mSystemCpms.getRegisteredPolicies();
100         String assertMsg = "registered policies: \n";
101         for (int i = 0; i < policies.size(); i++) {
102             assertMsg += policies.get(i).toString() + "\n";
103         }
104         assertWithMessage(assertMsg)
105                 .that(mSystemCpms.getRegisteredPolicies().size()).isEqualTo(totalNum);
106     }
107 
108     /**
109      * Checks if the given power policy is already defined.
110      *
111      * @param policyDef The definition of a power policy.
112      * @return Whether the given power policy is defined.
113      */
isPowerPolicyIdDefined(PowerPolicyDef policyDef)114     public boolean isPowerPolicyIdDefined(PowerPolicyDef policyDef) {
115         for (PowerPolicyDef def : mSystemCpms.getRegisteredPolicies()) {
116             if (def.getPolicyId().equals(policyDef.getPolicyId())) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 
checkCurrentPowerComponents(PowerPolicyDef expected)123     public void checkCurrentPowerComponents(PowerPolicyDef expected) throws Exception {
124         assertThat(mFrameCpms.getCurrentEnabledComponents()).asList()
125                 .containsExactlyElementsIn(expected.getEnables());
126         assertThat(mFrameCpms.getCurrentDisabledComponents()).asList()
127                 .containsExactlyElementsIn(expected.getDisables());
128     }
129 
130     /**
131      * Check to see if the current power policy group is the expected one
132      *
133      * <p> If {@code useProtoDump} is true, a null expected policy group ID will be treated as an
134      * empty string, since that's what proto parsing turns null policy group IDs into. If it is
135      * false, meaning text dump is used, the expected policy group ID is "null" as a string.
136      *
137      * @param expected power policy group ID that is expected to be the current one
138      * @param useProtoDump whether the method used to parse the policy group information was proto
139      *                     dump or not
140      */
checkCurrentPolicyGroupId(String expected, boolean useProtoDump)141     public void checkCurrentPolicyGroupId(String expected, boolean useProtoDump) {
142         if (expected == null) {
143             // differential treatment of null policy by text and proto parsing
144             if (useProtoDump) {
145                 expected = "";
146             } else {
147                 expected = "null";
148             }
149         }
150         assertWithMessage(/* messageToPrepend = */ "Current policy group ID").that(
151                 mFrameCpms.getCurrentPolicyGroupId()).isEqualTo(expected);
152     }
153 
checkPowerPolicyGroups(PowerPolicyGroups expected)154     public void checkPowerPolicyGroups(PowerPolicyGroups expected) {
155         assertWithMessage(/* messageToPrepend = */ "Power policy groups").that(
156                 mFrameCpms.getPowerPolicyGroups()).isEqualTo(expected);
157     }
158 
getNumberOfRegisteredPolicies()159     public int getNumberOfRegisteredPolicies() {
160         return mSystemCpms.getTotalRegisteredPolicies();
161     }
162 
checkPowerPolicyGroupsDefined(PowerPolicyGroups policyGroups)163     public void checkPowerPolicyGroupsDefined(PowerPolicyGroups policyGroups) {
164         assertWithMessage("Groups cannot be null").that(policyGroups).isNotNull();
165         Set<String> groupIds = policyGroups.getGroupIds();
166         for (String groupId : groupIds) {
167             PowerPolicyGroups.PowerPolicyGroupDef groupDef = policyGroups.getGroup(groupId);
168             assertWithMessage("Group definition cannot be null").that(groupDef).isNotNull();
169             assertWithMessage("Group is not defined").that(
170                     mFrameCpms.getPowerPolicyGroups().containsGroup(groupId, groupDef)).isTrue();
171         }
172     }
173 
getCurrentPowerState()174     public int getCurrentPowerState() {
175         return mFrameCpms.getCurrentState();
176     }
177 }
178