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