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