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;
18 
19 import android.car.cts.app.PowerPolicyTestCommandStatus;
20 import android.car.cts.app.PowerPolicyTestCommandType;
21 import android.car.cts.powerpolicy.CpmsFrameworkLayerStateInfo;
22 import android.car.cts.powerpolicy.CpmsSystemLayerStateInfo;
23 import android.car.cts.powerpolicy.LocationInfo;
24 import android.car.cts.powerpolicy.PowerPolicyConstants;
25 import android.car.cts.powerpolicy.PowerPolicyDef;
26 import android.car.cts.powerpolicy.PowerPolicyGroups;
27 import android.car.cts.powerpolicy.PowerPolicyTestAnalyzer;
28 import android.car.cts.powerpolicy.PowerPolicyTestHelper;
29 import android.car.cts.powerpolicy.PowerPolicyTestResult;
30 import android.car.cts.powerpolicy.SilentModeInfo;
31 import android.car.cts.powerpolicy.SystemInfoParser;
32 
33 import com.android.compatibility.common.util.CommonTestUtils;
34 import com.android.tradefed.log.LogUtil.CLog;
35 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
36 
37 import org.junit.After;
38 import org.junit.Assume;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 import java.lang.reflect.Method;
44 
45 @RunWith(DeviceJUnit4ClassRunner.class)
46 public final class PowerPolicyHostTest extends CarHostJUnit4TestCase {
47     private static final String ANDROID_CLIENT_PKG = "android.car.cts.app";
48     private static final String ANDROID_CLIENT_ACTIVITY = ANDROID_CLIENT_PKG
49             + "/.PowerPolicyTestActivity";
50     private static final String TEST_COMMAND_HEADER =
51             "am start -n " + ANDROID_CLIENT_ACTIVITY + " --es powerpolicy ";
52 
53     private static final int DEFAULT_TIMEOUT_SEC = 20;
54 
55     private final PowerPolicyTestAnalyzer mTestAnalyzer = new PowerPolicyTestAnalyzer(this);
56 
57     @Before
checkPrecondition()58     public void checkPrecondition() throws Exception {
59         checkDefaultPowerPolicySet("pre-condition");
60     }
61 
62     @After
restoreInitCondition()63     public void restoreInitCondition() throws Exception {
64         applyPowerPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
65     }
66 
67     @Test
testPowerPolicySilentMode()68     public void testPowerPolicySilentMode() throws Exception {
69         String testcase = "testPowerPolicySilentModeFull:";
70         String teststep;
71         PowerPolicyTestHelper testHelper;
72 
73         SilentModeInfo smInfo = getSilentModeInfo();
74         Assume.assumeTrue("HW does not support silent mode. Skip the test",
75                 smInfo.getMonitoringHWStateSignal());
76 
77         teststep = "switch to forced silent";
78         enterForcedSilentMode();
79         testHelper = getTestHelper(testcase, 1, teststep);
80         testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
81         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.NO_USER_INTERACTION);
82         testHelper.checkSilentModeStatus(true);
83         testHelper.checkSilentModeFull(SilentModeInfo.FORCED_SILENT);
84         testHelper.checkCurrentPowerComponents(PowerPolicyDef.PolicySet.NO_USER_INTERACT);
85 
86         teststep = "restore to normal mode";
87         leaveForcedSilentMode();
88         testHelper = getTestHelper(testcase, 2, teststep);
89         testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
90         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
91         testHelper.checkSilentModeStatus(false);
92         testHelper.checkSilentModeFull(SilentModeInfo.NO_SILENT);
93         testHelper.checkCurrentPowerComponents(PowerPolicyDef.PolicySet.DEFAULT_ALL_ON);
94     }
95 
96     /**
97      * Tests the error conditions for CPMS at the ON state.
98      *
99      * <p>All other VHAL events but {@code SHUTDOWN_PREPARE} shall not have any impact
100      * to CPMS power state.
101      */
102     @Test
testDefaultStateMachineAtONState()103     public void testDefaultStateMachineAtONState() throws Exception {
104         String testcase = "testDefaultStateMachineAtONState:";
105         String[] stepNames = {
106             "trigger VHAL ON event",
107             "trigger VHAL CANCEL_SHUTDOWN",
108             "trigger VHAL FINISHED"
109         };
110         int[] vhalReqs = {
111             PowerPolicyConstants.VhalPowerStateReq.ON,
112             PowerPolicyConstants.VhalPowerStateReq.CANCEL_SHUTDOWN,
113             PowerPolicyConstants.VhalPowerStateReq.FINISHED
114         };
115 
116         for (int i = 0; i < stepNames.length; i++) {
117             triggerVhalPowerStateReq(vhalReqs[i], PowerPolicyConstants.ShutdownParam.NOT_USED);
118             PowerPolicyTestHelper testHelper = getTestHelper(testcase, i + 1, stepNames[i]);
119             testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
120             testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
121         }
122     }
123 
124     @Test
testPowerPolicyChange()125     public void testPowerPolicyChange() throws Exception {
126         String testcase = "testPowerPolicyChange:";
127         int expectedTotalPolicies = PowerPolicyDef.PolicySet.TOTAL_DEFAULT_REGISTERED_POLICIES;
128         int stepNo = 0;
129         String teststep;
130         PowerPolicyTestHelper testHelper;
131 
132         teststep = "check the inital power policies";
133         testHelper = getTestHelper(testcase, stepNo++, teststep);
134         testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
135         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
136         testHelper.checkTotalRegisteredPolicies(expectedTotalPolicies);
137 
138         // create two power policies, test1 and test2, for power policy change test
139         defineAndCheckPolicyTest1(testcase, stepNo++, ++expectedTotalPolicies);
140         defineAndCheckPolicyTest2(testcase, stepNo++, ++expectedTotalPolicies);
141 
142         teststep = "apply power policy test1";
143         applyPowerPolicy(PowerPolicyDef.IdSet.TEST1);
144         testHelper = getTestHelper(testcase, stepNo++, teststep);
145         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.TEST1);
146 
147         teststep = "apply power policy test2";
148         applyPowerPolicy(PowerPolicyDef.IdSet.TEST2);
149         testHelper = getTestHelper(testcase, stepNo++, teststep);
150         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.TEST2);
151 
152         teststep = "revert power policy back to the default";
153         applyPowerPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
154         testHelper = getTestHelper(testcase, stepNo++, teststep);
155         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
156 
157         // add "test power policy listener" here so that one reboot clears all
158         defineAndCheckPolicyListenerTest(testcase, stepNo++, ++expectedTotalPolicies);
159         String clientTestcase = "PowerPolicyListenerTest";
160         PowerPolicyTestResult testResult = new PowerPolicyTestResult(mTestAnalyzer);
161         String clientAction = PowerPolicyTestCommandType.DUMP_LISTENER.name();
162         String component = "AUDIO";
163 
164         setClientTestcase(clientTestcase);
165         int currentNumberListeners = getNumberPolicyListeners();
166         registerPowerPolicyListener(component);
167         waitUntilNumberPolicyListenersEquals(++currentNumberListeners);
168         resetPowerPolicyListeners();
169         waitResetPowerPolicyListenersComplete(testResult, clientTestcase,
170                 PowerPolicyTestCommandType.RESET_LISTENERS.name(), component);
171         applyPowerPolicy(PowerPolicyDef.IdSet.LISTENER_TEST);
172         waitPowerPolicyListenersUpdated(testResult, clientTestcase,
173                 PowerPolicyTestCommandType.CHECK_LISTENERS.name(), component);
174 
175         dumpPowerPolicyListener(component);
176         testResult.checkLastTestResultEntry(clientTestcase, clientAction,
177                 component, PowerPolicyDef.PolicySet.LISTENER_TEST);
178 
179         unregisterPowerPolicyListener(component);
180         applyPowerPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
181         waitPowerPolicyListenersUpdated(testResult, clientTestcase,
182                 PowerPolicyTestCommandType.CHECK_LISTENERS.name(), component);
183 
184         dumpPowerPolicyListener(component);
185         testResult.checkLastTestResultEntry(clientTestcase, clientAction,
186                 component, "not_registered");
187         clearClientTestcase();
188 
189         // add respect to user setting test case here to utilize a single device reboot
190         testPowerPolicyAndComponentUserSetting();
191 
192         // add power policy group test here to utilize added test1 and test2 policies
193         teststep = "check default power policy group";
194         PowerPolicyGroups emptyGroups = new PowerPolicyGroups();
195         testHelper = getTestHelper(testcase, stepNo++, teststep);
196         testHelper.checkCurrentPolicyGroupId(null);
197         testHelper.checkPowerPolicyGroups(emptyGroups);
198 
199         teststep = "define power policy group";
200         definePowerPolicyGroup(PowerPolicyGroups.TestSet.POLICY_GROUP_DEF1.toShellCommandString());
201         definePowerPolicyGroup(PowerPolicyGroups.TestSet.POLICY_GROUP_DEF2.toShellCommandString());
202         testHelper = getTestHelper(testcase, stepNo++, teststep);
203         testHelper.checkPowerPolicyGroups(PowerPolicyGroups.TestSet.POLICY_GROUPS1);
204 
205         teststep = "set power policy group";
206         setPowerPolicyGroup(PowerPolicyGroups.TestSet.GROUP_ID1);
207         testHelper = getTestHelper(testcase, stepNo++, teststep);
208         testHelper.checkCurrentPolicyGroupId(PowerPolicyGroups.TestSet.GROUP_ID1);
209 
210         // reboot device to clear created TEST1 and TEST2 test cases.
211         // need to find a way to move reboot device into AfterAll
212         rebootDevice();
213         teststep = "reboot to clear added test power policies";
214         testHelper = getTestHelper(testcase, stepNo++, teststep);
215         expectedTotalPolicies = PowerPolicyDef.PolicySet.TOTAL_DEFAULT_REGISTERED_POLICIES;
216         testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
217         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
218         testHelper.checkTotalRegisteredPolicies(expectedTotalPolicies);
219     }
220 
fetchActivityDumpsys()221     public String fetchActivityDumpsys() throws Exception {
222         return executeCommand("dumpsys activity %s", ANDROID_CLIENT_ACTIVITY);
223     }
224 
getTestHelper(String testcase, int stepNo, String stepName)225     private PowerPolicyTestHelper getTestHelper(String testcase, int stepNo, String stepName)
226             throws Exception {
227         CpmsSystemLayerStateInfo cpmsSystemInfo = getCpmsSystemLayerStateInfo();
228         CpmsFrameworkLayerStateInfo cpmsFrameworkInfo = getCpmsFrameworkLayerStateInfo();
229         String normalizedStepName = String.format("%d. %s", stepNo, stepName);
230         return new PowerPolicyTestHelper(testcase, normalizedStepName,
231                 cpmsFrameworkInfo, cpmsSystemInfo, getSilentModeInfo());
232     }
233 
triggerVhalPowerStateReq(int reqNo, int param)234     private void triggerVhalPowerStateReq(int reqNo, int param) throws Exception {
235         executeCommand("cmd car_service inject-vhal-event %d %d,%d",
236                 PowerPolicyConstants.VHAL_POWER_STATE_REQ_PROPERTY_ID, reqNo, param);
237     }
238 
getSilentModeInfo()239     private SilentModeInfo getSilentModeInfo() throws Exception {
240         return executeAndParseCommand(
241                 new SystemInfoParser<SilentModeInfo>(SilentModeInfo.class),
242                 SilentModeInfo.COMMAND);
243     }
244 
getCpmsFrameworkLayerStateInfo()245     private CpmsFrameworkLayerStateInfo getCpmsFrameworkLayerStateInfo() throws Exception {
246         return executeAndParseCommand(new SystemInfoParser<CpmsFrameworkLayerStateInfo>(
247                 CpmsFrameworkLayerStateInfo.class), CpmsFrameworkLayerStateInfo.COMMAND);
248     }
249 
getCpmsSystemLayerStateInfo()250     private CpmsSystemLayerStateInfo getCpmsSystemLayerStateInfo() throws Exception {
251         return executeAndParseCommand(new SystemInfoParser<CpmsSystemLayerStateInfo>(
252                 CpmsSystemLayerStateInfo.class), CpmsSystemLayerStateInfo.COMMAND);
253     }
254 
rebootDevice()255     private void rebootDevice() throws Exception {
256         executeCommand("svc power reboot");
257         waitForDeviceAvailable();
258     }
259 
enterForcedSilentMode()260     private void enterForcedSilentMode() throws Exception {
261         executeCommand("cmd car_service silent-mode forced-silent");
262         waitUntilForcedSilentModeChangeTo(true);
263     }
264 
leaveForcedSilentMode()265     private void leaveForcedSilentMode() throws Exception {
266         executeCommand("cmd car_service silent-mode forced-non-silent");
267         executeCommand("cmd car_service silent-mode non-forced-silent-mode");
268         waitUntilForcedSilentModeChangeTo(false);
269     }
270 
definePowerPolicy(String policyStr)271     private void definePowerPolicy(String policyStr) throws Exception {
272         CLog.d("definePowerPolicy: %s", policyStr);
273         executeCommand("cmd car_service define-power-policy %s", policyStr);
274     }
275 
applyPowerPolicy(String policyId)276     private void applyPowerPolicy(String policyId) throws Exception {
277         executeCommand("cmd car_service apply-power-policy %s", policyId);
278     }
279 
definePowerPolicyGroup(String policyGroupStr)280     private void definePowerPolicyGroup(String policyGroupStr) throws Exception {
281         executeCommand("cmd car_service define-power-policy-group %s", policyGroupStr);
282     }
283 
setPowerPolicyGroup(String policyGroupId)284     private void setPowerPolicyGroup(String policyGroupId) throws Exception {
285         executeCommand("cmd car_service set-power-policy-group %s", policyGroupId);
286     }
287 
setClientTestcase(String testcase)288     private void setClientTestcase(String testcase) throws Exception {
289         executeCommand("%s settest,%s", TEST_COMMAND_HEADER, testcase);
290     }
291 
clearClientTestcase()292     private void clearClientTestcase() throws Exception {
293         executeCommand("%s cleartest", TEST_COMMAND_HEADER);
294     }
295 
registerPowerPolicyListener(String componentName)296     private void registerPowerPolicyListener(String componentName) throws Exception {
297         executeCommand("%s addlistener,%s", TEST_COMMAND_HEADER, componentName);
298     }
299 
unregisterPowerPolicyListener(String componentName)300     private void unregisterPowerPolicyListener(String componentName) throws Exception {
301         executeCommand("%s removelistener,%s", TEST_COMMAND_HEADER, componentName);
302     }
303 
dumpPowerPolicyListener(String componentName)304     private void dumpPowerPolicyListener(String componentName) throws Exception {
305         executeCommand("%s dumplistener,%s", TEST_COMMAND_HEADER, componentName);
306     }
307 
waitPowerPolicyListenersUpdated(PowerPolicyTestResult testResult, String clientTestcase, String clientAction, String component)308     private void waitPowerPolicyListenersUpdated(PowerPolicyTestResult testResult,
309             String clientTestcase, String clientAction, String component) throws Exception {
310         CommonTestUtils.waitUntil("timed out (" + DEFAULT_TIMEOUT_SEC
311                 + "s) waiting  policy listeners updated", DEFAULT_TIMEOUT_SEC,
312                 () -> {
313                     return checkPowerPolicyListenersUpdated(testResult, clientTestcase,
314                             clientAction, component);
315                 });
316     }
317 
checkPowerPolicyListenersUpdated(PowerPolicyTestResult testResult, String clientTestcase, String clientAction, String component)318     private boolean checkPowerPolicyListenersUpdated(PowerPolicyTestResult testResult,
319             String clientTestcase, String clientAction, String component) throws Exception {
320         executeCommand("%s checklisteners", TEST_COMMAND_HEADER);
321         return testResult.checkLastTestResultEntryData(clientTestcase, clientAction,
322                 component, PowerPolicyTestCommandStatus.PROPAGATED);
323     }
324 
resetPowerPolicyListeners()325     private void resetPowerPolicyListeners() throws Exception {
326         executeCommand("%s resetlisteners", TEST_COMMAND_HEADER);
327     }
328 
waitResetPowerPolicyListenersComplete(PowerPolicyTestResult testResult, String clientTestcase, String clientAction, String component)329     private void waitResetPowerPolicyListenersComplete(PowerPolicyTestResult testResult,
330             String clientTestcase, String clientAction, String component) throws Exception {
331         CommonTestUtils.waitUntil("timed out (" + DEFAULT_TIMEOUT_SEC
332                 + "s) waiting resetPowerPolicyListenersComplete", DEFAULT_TIMEOUT_SEC,
333                 () -> {
334                     return testResult.checkLastTestResultEntryData(clientTestcase, clientAction,
335                             component, PowerPolicyTestCommandStatus.SUCCEED);
336                 });
337     }
338 
getNumberPolicyListeners()339     private int getNumberPolicyListeners() throws Exception {
340         return getCpmsFrameworkLayerStateInfo().getNumberPolicyListeners();
341     }
342 
waitUntilNumberPolicyListenersEquals(int numListeners)343     private void waitUntilNumberPolicyListenersEquals(int numListeners) throws Exception {
344         CommonTestUtils.waitUntil("timed out (" + DEFAULT_TIMEOUT_SEC
345                 + "s) getting number policy listeners", DEFAULT_TIMEOUT_SEC,
346                 () -> (getNumberPolicyListeners() == numListeners));
347     }
348 
waitUntilForcedSilentModeChangeTo(boolean expected)349     private void waitUntilForcedSilentModeChangeTo(boolean expected) throws Exception {
350         String timeoutMsg = String.format("timed out (%ds) waiting for forced silent mode "
351                 + "to be %b", DEFAULT_TIMEOUT_SEC, expected);
352         CommonTestUtils.waitUntil(timeoutMsg, DEFAULT_TIMEOUT_SEC,
353                 () -> {
354                     SilentModeInfo silentInfo = getSilentModeInfo();
355                     CpmsFrameworkLayerStateInfo cpmsInfo = getCpmsFrameworkLayerStateInfo();
356                     return (silentInfo.getForcedSilentMode() == expected)
357                             && (cpmsInfo.getForcedSilentMode() == expected);
358                 });
359     }
360 
waitForDeviceAvailable()361     private void waitForDeviceAvailable() throws Exception {
362         try {
363             getDevice().waitForDeviceAvailable();
364         } catch (Exception e) {
365             CLog.w("device is not available, trying one more time");
366             getDevice().waitForDeviceAvailable();
367         }
368     }
369 
checkDefaultPowerPolicySet(String testcase)370     private void checkDefaultPowerPolicySet(String testcase) throws Exception {
371         String teststep = "check if the car power is on the ON state";
372         PowerPolicyTestHelper testHelper = new PowerPolicyTestHelper(testcase, teststep,
373                 getCpmsFrameworkLayerStateInfo(), getCpmsSystemLayerStateInfo(), null);
374         testHelper.checkCurrentState(PowerPolicyConstants.CarPowerState.ON);
375         testHelper.checkRegisteredPolicy(PowerPolicyDef.PolicySet.INITIAL_ALL_ON);
376         testHelper.checkRegisteredPolicy(PowerPolicyDef.PolicySet.DEFAULT_ALL_ON);
377         testHelper.checkCurrentPolicy(PowerPolicyDef.IdSet.DEFAULT_ALL_ON);
378     }
379 
defineAndCheckPolicyTest1(String testcase, int stepNo, int expectedTotalPolicies)380     private void defineAndCheckPolicyTest1(String testcase, int stepNo,
381             int expectedTotalPolicies) throws Exception {
382         String teststep = stepNo + ". define a new power policy with id test1";
383         definePowerPolicy(PowerPolicyDef.PolicySet.TEST1.toString());
384         PowerPolicyTestHelper testHelper = getTestHelper(testcase, stepNo, teststep);
385         testHelper.checkRegisteredPolicy(PowerPolicyDef.PolicySet.TEST1);
386         testHelper.checkTotalRegisteredPolicies(expectedTotalPolicies);
387     }
388 
defineAndCheckPolicyTest2(String testcase, int stepNo, int expectedTotalPolicies)389     private void defineAndCheckPolicyTest2(String testcase, int stepNo,
390             int expectedTotalPolicies) throws Exception {
391         String teststep = stepNo + ". define a new power policy with id test2";
392         definePowerPolicy(PowerPolicyDef.PolicySet.TEST2.toString());
393         PowerPolicyTestHelper testHelper = getTestHelper(testcase, stepNo, teststep);
394         testHelper.checkRegisteredPolicy(PowerPolicyDef.PolicySet.TEST2);
395         testHelper.checkTotalRegisteredPolicies(expectedTotalPolicies);
396     }
397 
defineAndCheckPolicyListenerTest(String testcase, int stepNo, int expectedTotalPolicies)398     private void defineAndCheckPolicyListenerTest(String testcase, int stepNo,
399             int expectedTotalPolicies) throws Exception {
400         String teststep = stepNo + ". define a new power policy with id listener_test";
401         definePowerPolicy(PowerPolicyDef.PolicySet.LISTENER_TEST.toString());
402         PowerPolicyTestHelper testHelper = getTestHelper(testcase, stepNo, teststep);
403         testHelper.checkRegisteredPolicy(PowerPolicyDef.PolicySet.LISTENER_TEST);
404         testHelper.checkTotalRegisteredPolicies(expectedTotalPolicies);
405     }
406 
testPowerPolicyAndComponentUserSetting()407     private void testPowerPolicyAndComponentUserSetting() throws Exception {
408         ComponentTestHelper[] testHelpers = {
409             new ComponentTestHelper<LocationInfo>(this, "LOCATION", LocationInfo.class),
410         };
411 
412         for (int i = 0; i < testHelpers.length; i++) {
413             testComponent(testHelpers[i]);
414         }
415     }
416 
417     private static final class ComponentTestHelper<T> {
418         private final PowerPolicyHostTest mHostTest;
419         private final String mComponentName;
420         private final String mInfoRetrieveCommand;
421         private final String[] mEnableCommands;
422         private final String[] mDisableCommands;
423         private final Class mTypeClass;
424 
ComponentTestHelper(PowerPolicyHostTest hostTest, String componentName, Class typeClass)425         ComponentTestHelper(PowerPolicyHostTest hostTest, String componentName, Class typeClass)
426                 throws Exception {
427             mHostTest = hostTest;
428             mComponentName = componentName;
429             mTypeClass = typeClass;
430             mInfoRetrieveCommand = (String) mTypeClass.getField("COMMAND").get(null);
431             mEnableCommands = (String[]) mTypeClass.getField("ENABLE_COMMANDS").get(null);
432             mDisableCommands = (String[]) mTypeClass.getField("DISABLE_COMMANDS").get(null);
433         }
434 
turnOnComponent()435         private void turnOnComponent() throws Exception {
436             for (int i = 0; i < mEnableCommands.length; i++) {
437                 mHostTest.executeCommand(mEnableCommands[i]);
438             }
439         }
440 
turnOffComponent()441         private void turnOffComponent() throws Exception {
442             for (int i = 0; i < mDisableCommands.length; i++) {
443                 mHostTest.executeCommand(mDisableCommands[i]);
444             }
445         }
446 
tryGetComponentInfo()447         private T tryGetComponentInfo() throws Exception {
448             return mHostTest.executeAndParseCommand(new SystemInfoParser<T>(mTypeClass),
449                     mInfoRetrieveCommand);
450         }
451 
waitUntilPowerStateChangeTo(boolean expected)452         private void waitUntilPowerStateChangeTo(boolean expected) throws Exception {
453             CommonTestUtils.waitUntil("timed out with " + mComponentName
454                     + "enabled expected: " + expected, DEFAULT_TIMEOUT_SEC,
455                     () -> {
456                         T info = tryGetComponentInfo();
457                         if (info != null) {
458                             Method isPowerOn = mTypeClass.getMethod("isPowerOn");
459                             Boolean result = (Boolean) isPowerOn.invoke(info);
460                             return result.booleanValue() == expected;
461                         }
462                         return false;
463                     });
464         }
465     }
466 
testComponent(ComponentTestHelper testHelper)467     private void testComponent(ComponentTestHelper testHelper)
468             throws Exception {
469         PowerPolicyDef powerOffPolicy =
470                 PowerPolicyDef.createWithComponentOff(testHelper.mComponentName);
471         definePowerPolicy(powerOffPolicy.toString());
472 
473         testHelper.turnOffComponent();
474         testHelper.waitUntilPowerStateChangeTo(false);
475 
476         testHelper.turnOnComponent();
477         testHelper.waitUntilPowerStateChangeTo(true);
478 
479         applyPowerPolicy(powerOffPolicy.getPolicyId());
480         testHelper.turnOnComponent();
481         testHelper.waitUntilPowerStateChangeTo(true);
482 
483         testHelper.turnOffComponent();
484         testHelper.waitUntilPowerStateChangeTo(false);
485     }
486 }
487