1 /* 2 * Copyright (C) 2017 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 package com.android.server.power; 17 18 import android.os.PowerSaveState; 19 import android.os.Handler; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 import com.android.server.power.BatterySaverPolicy.ServiceType; 24 25 import org.mockito.Mock; 26 import org.mockito.MockitoAnnotations; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 /** 31 * Tests for {@link com.android.server.power.BatterySaverPolicy} 32 */ 33 public class BatterySaverPolicyTest extends AndroidTestCase { 34 private static final boolean BATTERY_SAVER_ON = true; 35 private static final boolean BATTERY_SAVER_OFF = false; 36 private static final float BRIGHTNESS_FACTOR = 0.7f; 37 private static final float DEFAULT_BRIGHTNESS_FACTOR = 0.5f; 38 private static final float PRECISION = 0.001f; 39 private static final int GPS_MODE = 0; 40 private static final int DEFAULT_GPS_MODE = 1; 41 private static final String BATTERY_SAVER_CONSTANTS = "vibration_disabled=true," 42 + "animation_disabled=false," 43 + "soundtrigger_disabled=true," 44 + "firewall_disabled=false," 45 + "datasaver_disabled=false," 46 + "adjust_brightness_disabled=true," 47 + "adjust_brightness_factor=0.7," 48 + "fullbackup_deferred=true," 49 + "keyvaluebackup_deferred=false," 50 + "gps_mode=0"; 51 private static final String BATTERY_SAVER_INCORRECT_CONSTANTS = "vi*,!=,,true"; 52 53 @Mock 54 Handler mHandler; 55 private BatterySaverPolicy mBatterySaverPolicy; 56 57 public void setUp() throws Exception { 58 super.setUp(); 59 MockitoAnnotations.initMocks(this); 60 mBatterySaverPolicy = new BatterySaverPolicy(mHandler); 61 mBatterySaverPolicy.start(getContext().getContentResolver()); 62 } 63 64 @SmallTest 65 public void testGetBatterySaverPolicy_PolicyNull_DefaultValueCorrect() { 66 testServiceDefaultValue(ServiceType.NULL); 67 } 68 69 @SmallTest 70 public void testGetBatterySaverPolicy_PolicyVibration_DefaultValueCorrect() { 71 testServiceDefaultValue(ServiceType.VIBRATION); 72 } 73 74 @SmallTest 75 public void testGetBatterySaverPolicy_PolicySound_DefaultValueCorrect() { 76 testServiceDefaultValue(ServiceType.SOUND); 77 } 78 79 @SmallTest 80 public void testGetBatterySaverPolicy_PolicyFullBackup_DefaultValueCorrect() { 81 testServiceDefaultValue(ServiceType.FULL_BACKUP); 82 } 83 84 @SmallTest 85 public void testGetBatterySaverPolicy_PolicyKeyValueBackup_DefaultValueCorrect() { 86 testServiceDefaultValue(ServiceType.KEYVALUE_BACKUP); 87 } 88 89 @SmallTest 90 public void testGetBatterySaverPolicy_PolicyAnimation_DefaultValueCorrect() { 91 testServiceDefaultValue(ServiceType.ANIMATION); 92 } 93 94 @SmallTest 95 public void testGetBatterySaverPolicy_PolicyBatteryStats_DefaultValueCorrect() { 96 testServiceDefaultValue(ServiceType.BATTERY_STATS); 97 } 98 99 @SmallTest 100 public void testGetBatterySaverPolicy_PolicyNetworkFirewall_DefaultValueCorrect() { 101 testServiceDefaultValue(ServiceType.NETWORK_FIREWALL); 102 } 103 104 @SmallTest 105 public void testGetBatterySaverPolicy_PolicyDataSaver_DefaultValueCorrect() { 106 mBatterySaverPolicy.updateConstants(""); 107 final PowerSaveState batterySaverStateOn = 108 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.DATA_SAVER, BATTERY_SAVER_ON); 109 assertThat(batterySaverStateOn.batterySaverEnabled).isFalse(); 110 111 final PowerSaveState batterySaverStateOff = mBatterySaverPolicy.getBatterySaverPolicy( 112 ServiceType.DATA_SAVER, BATTERY_SAVER_OFF); 113 assertThat(batterySaverStateOff.batterySaverEnabled).isFalse(); 114 } 115 116 @SmallTest 117 public void testGetBatterySaverPolicy_PolicyScreenBrightness_DefaultValueCorrect() { 118 testServiceDefaultValue(ServiceType.SCREEN_BRIGHTNESS); 119 120 PowerSaveState stateOn = 121 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS, true); 122 assertThat(stateOn.brightnessFactor).isWithin(PRECISION).of(DEFAULT_BRIGHTNESS_FACTOR); 123 } 124 125 @SmallTest 126 public void testGetBatterySaverPolicy_PolicyGps_DefaultValueCorrect() { 127 testServiceDefaultValue(ServiceType.GPS); 128 129 PowerSaveState stateOn = 130 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS, true); 131 assertThat(stateOn.gpsMode).isEqualTo(DEFAULT_GPS_MODE); 132 } 133 134 @SmallTest 135 public void testUpdateConstants_getCorrectData() { 136 mBatterySaverPolicy.updateConstants(BATTERY_SAVER_CONSTANTS); 137 138 final PowerSaveState vibrationState = 139 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.VIBRATION, BATTERY_SAVER_ON); 140 assertThat(vibrationState.batterySaverEnabled).isTrue(); 141 142 final PowerSaveState animationState = 143 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.ANIMATION, BATTERY_SAVER_ON); 144 assertThat(animationState.batterySaverEnabled).isFalse(); 145 146 final PowerSaveState soundState = 147 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SOUND, BATTERY_SAVER_ON); 148 assertThat(soundState.batterySaverEnabled).isTrue(); 149 150 final PowerSaveState networkState = mBatterySaverPolicy.getBatterySaverPolicy( 151 ServiceType.NETWORK_FIREWALL, BATTERY_SAVER_ON); 152 assertThat(networkState.batterySaverEnabled).isTrue(); 153 154 final PowerSaveState screenState = 155 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS, 156 BATTERY_SAVER_ON); 157 assertThat(screenState.batterySaverEnabled).isFalse(); 158 assertThat(screenState.brightnessFactor).isWithin(PRECISION).of(BRIGHTNESS_FACTOR); 159 160 final PowerSaveState fullBackupState = mBatterySaverPolicy.getBatterySaverPolicy( 161 ServiceType.FULL_BACKUP, BATTERY_SAVER_ON); 162 assertThat(fullBackupState.batterySaverEnabled).isTrue(); 163 164 final PowerSaveState keyValueBackupState = mBatterySaverPolicy.getBatterySaverPolicy( 165 ServiceType.KEYVALUE_BACKUP, BATTERY_SAVER_ON); 166 assertThat(keyValueBackupState.batterySaverEnabled).isFalse(); 167 168 final PowerSaveState dataSaverState = mBatterySaverPolicy.getBatterySaverPolicy( 169 ServiceType.DATA_SAVER, BATTERY_SAVER_ON); 170 assertThat(dataSaverState.batterySaverEnabled).isTrue(); 171 172 final PowerSaveState gpsState = 173 mBatterySaverPolicy.getBatterySaverPolicy(ServiceType.GPS, BATTERY_SAVER_ON); 174 assertThat(gpsState.batterySaverEnabled).isTrue(); 175 assertThat(gpsState.gpsMode).isEqualTo(GPS_MODE); 176 } 177 178 @SmallTest 179 public void testUpdateConstants_IncorrectData_NotCrash() { 180 //Should not crash 181 mBatterySaverPolicy.updateConstants(BATTERY_SAVER_INCORRECT_CONSTANTS); 182 mBatterySaverPolicy.updateConstants(null); 183 } 184 185 private void testServiceDefaultValue(@ServiceType int type) { 186 mBatterySaverPolicy.updateConstants(""); 187 final PowerSaveState batterySaverStateOn = 188 mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_ON); 189 assertThat(batterySaverStateOn.batterySaverEnabled).isTrue(); 190 191 final PowerSaveState batterySaverStateOff = 192 mBatterySaverPolicy.getBatterySaverPolicy(type, BATTERY_SAVER_OFF); 193 assertThat(batterySaverStateOff.batterySaverEnabled).isFalse(); 194 } 195 } 196