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 com.android.car.power; 18 19 import static android.car.hardware.power.PowerComponent.AUDIO; 20 import static android.car.hardware.power.PowerComponent.BLUETOOTH; 21 import static android.car.hardware.power.PowerComponent.CELLULAR; 22 import static android.car.hardware.power.PowerComponent.CPU; 23 import static android.car.hardware.power.PowerComponent.DISPLAY; 24 import static android.car.hardware.power.PowerComponent.ETHERNET; 25 import static android.car.hardware.power.PowerComponent.INPUT; 26 import static android.car.hardware.power.PowerComponent.LOCATION; 27 import static android.car.hardware.power.PowerComponent.MEDIA; 28 import static android.car.hardware.power.PowerComponent.MICROPHONE; 29 import static android.car.hardware.power.PowerComponent.NFC; 30 import static android.car.hardware.power.PowerComponent.PROJECTION; 31 import static android.car.hardware.power.PowerComponent.TRUSTED_DEVICE_DETECTION; 32 import static android.car.hardware.power.PowerComponent.VISUAL_INTERACTION; 33 import static android.car.hardware.power.PowerComponent.VOICE_INTERACTION; 34 import static android.car.hardware.power.PowerComponent.WIFI; 35 import static android.car.hardware.power.PowerComponentUtil.INVALID_POWER_COMPONENT; 36 37 import static com.android.car.test.power.CarPowerPolicyUtil.assertPolicyIdentical; 38 39 import static com.google.common.truth.Truth.assertWithMessage; 40 41 import static org.mockito.Mockito.when; 42 43 import android.app.AppOpsManager; 44 import android.car.hardware.power.CarPowerPolicy; 45 import android.car.test.util.TemporaryFile; 46 import android.content.Context; 47 import android.content.pm.PackageManager; 48 import android.util.AtomicFile; 49 50 import com.android.car.systeminterface.SystemInterface; 51 52 import org.junit.Before; 53 import org.junit.Test; 54 import org.junit.runner.RunWith; 55 import org.mockito.Mock; 56 import org.mockito.junit.MockitoJUnitRunner; 57 58 /** 59 * Tests for {@link PowerComponentHandler}. 60 */ 61 @RunWith(MockitoJUnitRunner.class) 62 public final class PowerComponentHandlerUnitTest { 63 @Mock 64 private SystemInterface mSystemInterface; 65 @Mock 66 private Context mContext; 67 @Mock 68 private AppOpsManager mAppOpsManager; 69 @Mock 70 private PackageManager mPackageManager; 71 private PowerComponentHandler mHandler; 72 private TemporaryFile mComponentStateFile; 73 74 @Before setUp()75 public void setUp() throws Exception { 76 mComponentStateFile = new TemporaryFile("COMPONENT_STATE_FILE"); 77 when(mContext.getSystemService(AppOpsManager.class)).thenReturn(mAppOpsManager); 78 when(mContext.getPackageManager()).thenReturn(mPackageManager); 79 mHandler = new PowerComponentHandler(mContext, mSystemInterface, 80 new AtomicFile(mComponentStateFile.getFile())); 81 mHandler.init(/* customComponents = */ null); 82 } 83 84 @Test testGetAccumulatedPolicy_firstTime()85 public void testGetAccumulatedPolicy_firstTime() throws Exception { 86 CarPowerPolicy policy = mHandler.getAccumulatedPolicy(); 87 CarPowerPolicy expected = new CarPowerPolicy("", new int[]{VOICE_INTERACTION}, 88 new int[]{AUDIO, BLUETOOTH, CELLULAR, CPU, DISPLAY, ETHERNET, INPUT, LOCATION, 89 MEDIA, MICROPHONE, NFC, PROJECTION, TRUSTED_DEVICE_DETECTION, 90 VISUAL_INTERACTION, WIFI}); 91 92 assertPolicyIdentical(expected, policy); 93 } 94 95 @Test testApplyPowerPolicy_oneTime()96 public void testApplyPowerPolicy_oneTime() throws Exception { 97 CarPowerPolicy policy = new CarPowerPolicy("test_policy1", new int[]{WIFI, BLUETOOTH, 98 DISPLAY, VOICE_INTERACTION}, new int[]{AUDIO}); 99 CarPowerPolicy expected = new CarPowerPolicy("test_policy1", new int[]{WIFI, BLUETOOTH, 100 DISPLAY, VOICE_INTERACTION}, new int[]{AUDIO, CELLULAR, CPU, ETHERNET, INPUT, 101 LOCATION, MEDIA, MICROPHONE, NFC, PROJECTION, TRUSTED_DEVICE_DETECTION, 102 VISUAL_INTERACTION}); 103 104 mHandler.applyPowerPolicy(policy); 105 106 assertPolicyIdentical(expected, mHandler.getAccumulatedPolicy()); 107 } 108 109 @Test testApplyPowerPolicy_multipleTimes()110 public void testApplyPowerPolicy_multipleTimes() throws Exception { 111 CarPowerPolicy[] policies = new CarPowerPolicy[]{ 112 new CarPowerPolicy("test_policy1", new int[]{WIFI}, new int[]{AUDIO}), 113 new CarPowerPolicy("test_policy2", new int[]{WIFI, DISPLAY}, new int[]{NFC}), 114 new CarPowerPolicy("test_policy3", new int[]{CPU, INPUT}, new int[]{WIFI}), 115 new CarPowerPolicy("test_policy4", new int[]{MEDIA, AUDIO}, new int[]{})}; 116 CarPowerPolicy expected = new CarPowerPolicy("test_policy4", 117 new int[]{AUDIO, MEDIA, DISPLAY, INPUT, CPU, VOICE_INTERACTION}, 118 new int[]{BLUETOOTH, CELLULAR, ETHERNET, LOCATION, MICROPHONE, NFC, PROJECTION, 119 TRUSTED_DEVICE_DETECTION, VISUAL_INTERACTION, WIFI}); 120 121 for (CarPowerPolicy policy : policies) { 122 mHandler.applyPowerPolicy(policy); 123 } 124 125 assertPolicyIdentical(expected, mHandler.getAccumulatedPolicy()); 126 } 127 128 @Test testApplyPowerPolicy_withCustomComponents()129 public void testApplyPowerPolicy_withCustomComponents() throws Exception { 130 int customComponentId = 1001; 131 mHandler.registerCustomComponents(new Integer[]{customComponentId}); 132 CarPowerPolicy policy1 = new CarPowerPolicy("test_policy1", 133 new int[]{WIFI, customComponentId}, new int[]{AUDIO}); 134 CarPowerPolicy policy2 = new CarPowerPolicy("test_policy2", new int[]{WIFI, DISPLAY}, 135 new int[]{NFC}); 136 137 mHandler.applyPowerPolicy(policy1); 138 mHandler.applyPowerPolicy(policy2); 139 140 CarPowerPolicy expected = new CarPowerPolicy("test_policy2", 141 new int[]{DISPLAY, WIFI, VOICE_INTERACTION, customComponentId}, 142 new int[]{INPUT, MEDIA, AUDIO, BLUETOOTH, CELLULAR, ETHERNET, LOCATION, MICROPHONE, 143 NFC, PROJECTION, 144 TRUSTED_DEVICE_DETECTION, VISUAL_INTERACTION, CPU}); 145 assertPolicyIdentical(expected, mHandler.getAccumulatedPolicy()); 146 147 CarPowerPolicy policy3 = new CarPowerPolicy("test_policy3", new int[]{WIFI, AUDIO}, 148 new int[]{customComponentId}); 149 CarPowerPolicy policy4 = new CarPowerPolicy("test_policy4", new int[]{WIFI}, 150 new int[]{NFC, DISPLAY}); 151 152 mHandler.applyPowerPolicy(policy3); 153 mHandler.applyPowerPolicy(policy4); 154 155 expected = new CarPowerPolicy("test_policy4", 156 new int[]{WIFI, AUDIO, VOICE_INTERACTION}, 157 new int[]{DISPLAY, INPUT, MEDIA, BLUETOOTH, CELLULAR, ETHERNET, LOCATION, 158 MICROPHONE, NFC, PROJECTION, TRUSTED_DEVICE_DETECTION, VISUAL_INTERACTION, 159 CPU, customComponentId}); 160 assertPolicyIdentical(expected, mHandler.getAccumulatedPolicy()); 161 } 162 163 @Test testPowerComponentMediator()164 public void testPowerComponentMediator() { 165 PowerComponentHandler.PowerComponentMediator mediator = 166 new PowerComponentMediatorDefault(); 167 168 assertWithMessage("Default value for isComponentAvailable()") 169 .that(mediator.isComponentAvailable()).isFalse(); 170 assertWithMessage("Default value for isEnabled()").that(mediator.isEnabled()).isFalse(); 171 172 mediator.setEnabled(true); 173 174 // Default setEnabled() does nothing. 175 assertWithMessage("Value after setEnabled(true)").that(mediator.isEnabled()).isFalse(); 176 } 177 178 private static final class PowerComponentMediatorDefault extends 179 PowerComponentHandler.PowerComponentMediator { PowerComponentMediatorDefault()180 PowerComponentMediatorDefault() { 181 super(INVALID_POWER_COMPONENT); 182 } 183 } 184 } 185