2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settingslib.fuelgauge;
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.ArgumentMatchers.anyString;
21 import static org.mockito.Mockito.atLeastOnce;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.pm.PackageManager;
31 import android.os.IDeviceIdleController;
33 import com.android.settingslib.SettingsLibRobolectricTestRunner;
34 import com.android.settingslib.testutils.shadow.ShadowDefaultDialerManager;
35 import com.android.settingslib.testutils.shadow.ShadowSmsApplication;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 import org.robolectric.shadow.api.Shadow;
45 import org.robolectric.shadows.ShadowPackageManager;
47 @RunWith(SettingsLibRobolectricTestRunner.class)
48 @Config(shadows = {ShadowDefaultDialerManager.class, ShadowSmsApplication.class})
49 public class PowerWhitelistBackendTest {
51 private static final String PACKAGE_ONE = "com.example.packageone";
52 private static final String PACKAGE_TWO = "com.example.packagetwo";
55 private IDeviceIdleController mDeviceIdleService;
57 private DevicePolicyManager mDevicePolicyManager;
58 private PowerWhitelistBackend mPowerWhitelistBackend;
59 private ShadowPackageManager mPackageManager;
60 private Context mContext;
63 public void setUp() throws Exception {
64 MockitoAnnotations.initMocks(this);
65 mContext = spy(RuntimeEnvironment.application);
66 doReturn(mContext).when(mContext).getApplicationContext();
67 doReturn(mDevicePolicyManager).when(mContext).getSystemService(DevicePolicyManager.class);
68 doReturn(new String[] {}).when(mDeviceIdleService).getFullPowerWhitelist();
69 doReturn(new String[] {}).when(mDeviceIdleService).getSystemPowerWhitelist();
70 doReturn(new String[] {}).when(mDeviceIdleService).getSystemPowerWhitelistExceptIdle();
71 doNothing().when(mDeviceIdleService).addPowerSaveWhitelistApp(anyString());
72 doNothing().when(mDeviceIdleService).removePowerSaveWhitelistApp(anyString());
73 mPackageManager = Shadow.extract(mContext.getPackageManager());
74 mPackageManager.setSystemFeature(PackageManager.FEATURE_TELEPHONY, true);
77 mPowerWhitelistBackend = new PowerWhitelistBackend(mContext, mDeviceIdleService);
81 public void testIsWhitelisted() throws Exception {
82 doReturn(new String[] {PACKAGE_ONE}).when(mDeviceIdleService).getFullPowerWhitelist();
83 mPowerWhitelistBackend.refreshList();
85 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isTrue();
86 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_TWO)).isFalse();
87 assertThat(mPowerWhitelistBackend.isWhitelisted(new String[] {PACKAGE_ONE})).isTrue();
88 assertThat(mPowerWhitelistBackend.isWhitelisted(new String[] {PACKAGE_TWO})).isFalse();
90 mPowerWhitelistBackend.addApp(PACKAGE_TWO);
92 verify(mDeviceIdleService, atLeastOnce()).addPowerSaveWhitelistApp(PACKAGE_TWO);
93 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isTrue();
94 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_TWO)).isTrue();
95 assertThat(mPowerWhitelistBackend.isWhitelisted(
96 new String[] {PACKAGE_ONE, PACKAGE_TWO})).isTrue();
98 mPowerWhitelistBackend.removeApp(PACKAGE_TWO);
100 verify(mDeviceIdleService, atLeastOnce()).removePowerSaveWhitelistApp(PACKAGE_TWO);
101 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isTrue();
102 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_TWO)).isFalse();
103 assertThat(mPowerWhitelistBackend.isWhitelisted(new String[] {PACKAGE_ONE})).isTrue();
104 assertThat(mPowerWhitelistBackend.isWhitelisted(new String[] {PACKAGE_TWO})).isFalse();
106 mPowerWhitelistBackend.removeApp(PACKAGE_ONE);
108 verify(mDeviceIdleService, atLeastOnce()).removePowerSaveWhitelistApp(PACKAGE_ONE);
109 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isFalse();
110 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_TWO)).isFalse();
111 assertThat(mPowerWhitelistBackend.isWhitelisted(
112 new String[] {PACKAGE_ONE, PACKAGE_TWO})).isFalse();
116 public void isWhitelisted_shouldWhitelistDefaultSms() {
117 final String testSms = "com.android.test.defaultsms";
118 ShadowSmsApplication.setDefaultSmsApplication(new ComponentName(testSms, "receiver"));
120 assertThat(mPowerWhitelistBackend.isWhitelisted(testSms)).isTrue();
124 public void isWhitelisted_shouldWhitelistDefaultDialer() {
125 final String testDialer = "com.android.test.defaultdialer";
126 ShadowDefaultDialerManager.setDefaultDialerApplication(testDialer);
128 assertThat(mPowerWhitelistBackend.isWhitelisted(testDialer)).isTrue();
132 public void isWhitelisted_shouldWhitelistActiveDeviceAdminApp() {
133 doReturn(true).when(mDevicePolicyManager).packageHasActiveAdmins(PACKAGE_ONE);
135 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isTrue();
139 public void testIsSystemWhitelisted() throws Exception {
140 doReturn(new String[] {PACKAGE_ONE}).when(mDeviceIdleService).getSystemPowerWhitelist();
141 mPowerWhitelistBackend.refreshList();
143 assertThat(mPowerWhitelistBackend.isSysWhitelisted(PACKAGE_ONE)).isTrue();
144 assertThat(mPowerWhitelistBackend.isSysWhitelisted(PACKAGE_TWO)).isFalse();
145 assertThat(mPowerWhitelistBackend.isWhitelisted(PACKAGE_ONE)).isFalse();
149 public void testIsSystemWhitelistedExceptIdle_onePackage() throws Exception {
150 doReturn(new String[] {PACKAGE_TWO}).when(
151 mDeviceIdleService).getSystemPowerWhitelistExceptIdle();
152 mPowerWhitelistBackend.refreshList();
154 assertThat(mPowerWhitelistBackend.isSysWhitelistedExceptIdle(PACKAGE_ONE)).isFalse();
155 assertThat(mPowerWhitelistBackend.isSysWhitelistedExceptIdle(PACKAGE_TWO)).isTrue();
159 public void testIsSystemWhitelistedExceptIdle_packageArray() throws Exception {
160 doReturn(new String[] {PACKAGE_TWO}).when(
161 mDeviceIdleService).getSystemPowerWhitelistExceptIdle();
162 mPowerWhitelistBackend.refreshList();
164 final String[] idlePackages = {PACKAGE_ONE, PACKAGE_TWO};
165 final String[] normalPackages = {PACKAGE_ONE};
167 assertThat(mPowerWhitelistBackend.isSysWhitelistedExceptIdle(normalPackages)).isFalse();
168 assertThat(mPowerWhitelistBackend.isSysWhitelistedExceptIdle(idlePackages)).isTrue();