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.Set; 25 26 public final class PowerPolicyTestHelper { 27 private final CpmsFrameworkLayerStateInfo mFrameCpms; 28 private final CpmsSystemLayerStateInfo mSystemCpms; 29 private final SilentModeInfo mSilentMode; 30 private final String mStep; 31 private final String mTestcase; 32 33 public static final String CURRENT_STATE_ASSERT_MSG = "current state"; 34 public static final String CURRENT_POLICY_ASSERT_MSG = "current policy"; 35 public static final String CURRENT_POWER_COMPONENT_ASSERT_MSG = "current power components"; 36 public static final String REGISTERED_POLICY_ASSERT_MSG = "registered policy"; 37 public static final String SILENT_MODE_FULL_ASSERT_MSG = "silent mode in full"; 38 public static final String SILENT_MODE_STATUS_ASSERT_MSG = "silent mode status"; 39 public static final String PENDING_POLICY_ASSERT_MSG = "pending policy id"; 40 public static final String TOTAL_REGISTERED_POLICIES_ASSERT_MSG = 41 "the total number of registered policies"; 42 PowerPolicyTestHelper(String testcase, String step, CpmsFrameworkLayerStateInfo frameCpms, CpmsSystemLayerStateInfo sysCpms, SilentModeInfo silentMode)43 public PowerPolicyTestHelper(String testcase, String step, 44 CpmsFrameworkLayerStateInfo frameCpms, CpmsSystemLayerStateInfo sysCpms, 45 SilentModeInfo silentMode) { 46 mStep = step; 47 mTestcase = testcase; 48 mFrameCpms = frameCpms; 49 mSystemCpms = sysCpms; 50 mSilentMode = silentMode; 51 } 52 checkCurrentState(int expected)53 public void checkCurrentState(int expected) { 54 assertWithMessage(CURRENT_STATE_ASSERT_MSG) 55 .that(expected == mFrameCpms.getCurrentState()).isTrue(); 56 } 57 checkCurrentPolicy(String expectedPolicyId)58 public void checkCurrentPolicy(String expectedPolicyId) { 59 boolean expected = expectedPolicyId.equals(mFrameCpms.getCurrentPolicyId()); 60 if (!expected) { 61 CLog.d("expectedPolicyId: " + expectedPolicyId); 62 CLog.d("currentPolicyId: " + mFrameCpms.getCurrentPolicyId()); 63 } 64 assertWithMessage(CURRENT_POLICY_ASSERT_MSG).that(expected).isTrue(); 65 } 66 checkSilentModeStatus(boolean expected)67 public void checkSilentModeStatus(boolean expected) { 68 assertWithMessage(SILENT_MODE_STATUS_ASSERT_MSG) 69 .that(mFrameCpms.getForcedSilentMode() == expected).isTrue(); 70 } 71 checkSilentModeFull(SilentModeInfo expected)72 public void checkSilentModeFull(SilentModeInfo expected) { 73 boolean status = expected.equals(mSilentMode); 74 if (!status) { 75 CLog.e("PowerPolicyTestHelper expected silent mode: %s", expected.toString()); 76 CLog.e("PowerPolicyTestHelper got tested silent mode: %s", mSilentMode.toString()); 77 } 78 assertWithMessage(SILENT_MODE_FULL_ASSERT_MSG).that(status).isTrue(); 79 } 80 checkRegisteredPolicy(PowerPolicyDef expectedPolicy)81 public void checkRegisteredPolicy(PowerPolicyDef expectedPolicy) { 82 boolean status = false; 83 for (PowerPolicyDef def : mSystemCpms.getRegisteredPolicies()) { 84 if (def.getPolicyId().equals(expectedPolicy.getPolicyId())) { 85 status = expectedPolicy.equals(def); 86 if (!status) { 87 CLog.e("PowerPolicyTestHelper expected policy: %s", expectedPolicy.toString()); 88 CLog.e("PowerPolicyTestHelper got result policy: %s", def.toString()); 89 } 90 break; 91 } 92 } 93 assertWithMessage(REGISTERED_POLICY_ASSERT_MSG).that(status).isTrue(); 94 } 95 checkTotalRegisteredPolicies(int totalNum)96 public void checkTotalRegisteredPolicies(int totalNum) { 97 assertWithMessage(TOTAL_REGISTERED_POLICIES_ASSERT_MSG) 98 .that(mSystemCpms.getRegisteredPolicies().size()).isEqualTo(totalNum); 99 } 100 checkCurrentPowerComponents(PowerPolicyDef expected)101 public void checkCurrentPowerComponents(PowerPolicyDef expected) throws Exception { 102 assertThat(mFrameCpms.getCurrentEnabledComponents()).asList() 103 .containsExactlyElementsIn(expected.getEnables()); 104 assertThat(mFrameCpms.getCurrentDisabledComponents()).asList() 105 .containsExactlyElementsIn(expected.getDisables()); 106 } 107 checkCurrentPolicyGroupId(String expected)108 public void checkCurrentPolicyGroupId(String expected) { 109 if (expected == null) { 110 expected = "null"; 111 } 112 assertWithMessage("checkCurrentPolicyGroupId") 113 .that(expected.equals(mFrameCpms.getCurrentPolicyGroupId())).isTrue(); 114 } 115 getNumberOfRegisteredPolicies()116 public int getNumberOfRegisteredPolicies() { 117 return mSystemCpms.getTotalRegisteredPolicies(); 118 } 119 checkPowerPolicyGroupsDefined(PowerPolicyGroups policyGroups)120 public void checkPowerPolicyGroupsDefined(PowerPolicyGroups policyGroups) { 121 assertWithMessage("Groups cannot be null").that(policyGroups).isNotNull(); 122 Set<String> groupIds = policyGroups.getGroupIds(); 123 for (String groupId : groupIds) { 124 PowerPolicyGroups.PowerPolicyGroupDef groupDef = policyGroups.getGroup(groupId); 125 assertWithMessage("Group definition cannot be null").that(groupDef).isNotNull(); 126 assertWithMessage("Group is not defined").that( 127 mFrameCpms.getPowerPolicyGroups().containsGroup(groupId, groupDef)).isTrue(); 128 } 129 } 130 getCurrentPowerState()131 public int getCurrentPowerState() { 132 return mFrameCpms.getCurrentState(); 133 } 134 } 135