1 /* 2 * Copyright (C) 2018 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.server; 18 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.BatteryManager; 27 import android.os.BatteryManagerInternal; 28 import android.os.IPowerManager; 29 import android.os.IThermalService; 30 import android.os.OsProtoEnums; 31 import android.os.PowerManager; 32 import android.os.RemoteException; 33 34 import androidx.test.InstrumentationRegistry; 35 import androidx.test.filters.SmallTest; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import com.android.internal.os.CachedDeviceState; 39 import com.android.internal.util.test.BroadcastInterceptingContext; 40 41 import org.junit.After; 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 48 /** 49 * Tests for {@link CachedDeviceStateService}. 50 */ 51 @SmallTest 52 @RunWith(AndroidJUnit4.class) 53 public class CachedDeviceStateServiceTest { 54 @Mock private BatteryManagerInternal mBatteryManager; 55 @Mock private IPowerManager mPowerManager; 56 @Mock private IThermalService mThermalService; 57 private BroadcastInterceptingContext mContext; 58 59 @Before setUp()60 public void setUp() throws RemoteException { 61 MockitoAnnotations.initMocks(this); 62 Context context = InstrumentationRegistry.getContext(); 63 PowerManager powerManager = new PowerManager(context, mPowerManager, mThermalService, null); 64 mContext = new BroadcastInterceptingContext(context) { 65 @Override 66 public Object getSystemService(String name) { 67 switch (name) { 68 case Context.POWER_SERVICE: 69 return powerManager; 70 default: 71 return super.getSystemService(name); 72 } 73 } 74 }; 75 76 LocalServices.addService(BatteryManagerInternal.class, mBatteryManager); 77 78 when(mBatteryManager.getPlugType()).thenReturn(OsProtoEnums.BATTERY_PLUGGED_NONE); 79 when(mPowerManager.isInteractive()).thenReturn(true); 80 } 81 82 @After tearDown()83 public void tearDown() { 84 // Added by the CachedDeviceStateService.onStart(). 85 LocalServices.removeServiceForTest(CachedDeviceState.Readonly.class); 86 87 // Added in @Before. 88 LocalServices.removeServiceForTest(BatteryManagerInternal.class); 89 } 90 91 @Test correctlyReportsScreenInteractive()92 public void correctlyReportsScreenInteractive() throws RemoteException { 93 CachedDeviceStateService service = new CachedDeviceStateService(mContext); 94 when(mPowerManager.isInteractive()).thenReturn(true); // Screen on. 95 96 service.onStart(); 97 CachedDeviceState.Readonly deviceState = 98 LocalServices.getService(CachedDeviceState.Readonly.class); 99 100 // State can be initialized correctly only after PHASE_SYSTEM_SERVICES_READY. 101 assertThat(deviceState.isScreenInteractive()).isFalse(); 102 103 service.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); 104 105 assertThat(deviceState.isScreenInteractive()).isTrue(); 106 107 mContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF)); 108 assertThat(deviceState.isScreenInteractive()).isFalse(); 109 110 mContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON)); 111 assertThat(deviceState.isScreenInteractive()).isTrue(); 112 } 113 114 @Test correctlyReportsCharging()115 public void correctlyReportsCharging() { 116 CachedDeviceStateService service = new CachedDeviceStateService(mContext); 117 when(mBatteryManager.getPlugType()).thenReturn(OsProtoEnums.BATTERY_PLUGGED_NONE); 118 119 service.onStart(); 120 CachedDeviceState.Readonly deviceState = 121 LocalServices.getService(CachedDeviceState.Readonly.class); 122 123 // State can be initialized correctly only after PHASE_SYSTEM_SERVICES_READY. 124 assertThat(deviceState.isCharging()).isTrue(); 125 126 service.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); 127 128 assertThat(deviceState.isCharging()).isFalse(); 129 130 Intent intentPluggedIn = new Intent(Intent.ACTION_BATTERY_CHANGED); 131 intentPluggedIn.putExtra(BatteryManager.EXTRA_PLUGGED, OsProtoEnums.BATTERY_PLUGGED_AC); 132 mContext.sendBroadcast(intentPluggedIn); 133 assertThat(deviceState.isCharging()).isTrue(); 134 135 Intent intentUnplugged = new Intent(Intent.ACTION_BATTERY_CHANGED); 136 intentUnplugged.putExtra(BatteryManager.EXTRA_PLUGGED, OsProtoEnums.BATTERY_PLUGGED_NONE); 137 mContext.sendBroadcast(intentUnplugged); 138 assertThat(deviceState.isCharging()).isFalse(); 139 } 140 141 @Test correctlyTracksTimeOnBattery()142 public void correctlyTracksTimeOnBattery() throws Exception { 143 CachedDeviceStateService service = new CachedDeviceStateService(mContext); 144 when(mBatteryManager.getPlugType()).thenReturn(OsProtoEnums.BATTERY_PLUGGED_NONE); 145 146 service.onStart(); 147 CachedDeviceState.Readonly deviceState = 148 LocalServices.getService(CachedDeviceState.Readonly.class); 149 150 CachedDeviceState.TimeInStateStopwatch stopwatch = 151 deviceState.createTimeOnBatteryStopwatch(); 152 153 // State can be initialized correctly only after PHASE_SYSTEM_SERVICES_READY. 154 assertThat(stopwatch.isRunning()).isFalse(); 155 service.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); 156 157 assertThat(stopwatch.isRunning()).isTrue(); 158 stopwatch.reset(); 159 160 Thread.sleep(100); 161 assertThat(stopwatch.isRunning()).isTrue(); 162 assertThat(stopwatch.getMillis()).isAtLeast(100L); 163 164 long timeOnBatteryBeforePluggedIn = stopwatch.getMillis(); 165 Intent intentPluggedIn = new Intent(Intent.ACTION_BATTERY_CHANGED); 166 intentPluggedIn.putExtra(BatteryManager.EXTRA_PLUGGED, OsProtoEnums.BATTERY_PLUGGED_AC); 167 mContext.sendBroadcast(intentPluggedIn); 168 169 assertThat(stopwatch.getMillis()).isAtLeast(timeOnBatteryBeforePluggedIn); 170 assertThat(stopwatch.isRunning()).isFalse(); 171 172 long timeOnBatteryAfterPluggedIn = stopwatch.getMillis(); 173 Thread.sleep(20); 174 assertThat(stopwatch.getMillis()).isEqualTo(timeOnBatteryAfterPluggedIn); 175 176 stopwatch.reset(); 177 assertThat(stopwatch.getMillis()).isEqualTo(0L); 178 assertThat(stopwatch.isRunning()).isFalse(); 179 } 180 } 181