Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 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.settingslib.fuelgauge; |
| 18 | |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 20 | |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 24 | import static org.mockito.ArgumentMatchers.eq; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.content.ContentResolver; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
| 32 | import android.os.PowerManager; |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 33 | import android.provider.Settings.Global; |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 34 | import android.provider.Settings.Secure; |
| 35 | |
| 36 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | |
| 45 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 46 | public class BatterySaverUtilsTest { |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 47 | final int BATTERY_SAVER_THRESHOLD_1 = 15; |
| 48 | final int BATTERY_SAVER_THRESHOLD_2 = 20; |
| 49 | |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 50 | @Mock |
| 51 | Context mMockContext; |
| 52 | |
| 53 | @Mock |
| 54 | ContentResolver mMockResolver; |
| 55 | |
| 56 | @Mock |
| 57 | PowerManager mMockPowerManager; |
| 58 | |
| 59 | @Before |
| 60 | public void setUp() throws Exception { |
| 61 | MockitoAnnotations.initMocks(this); |
| 62 | |
| 63 | when(mMockContext.getContentResolver()).thenReturn(mMockResolver); |
| 64 | when(mMockContext.getSystemService(eq(PowerManager.class))).thenReturn(mMockPowerManager); |
| 65 | when(mMockPowerManager.setPowerSaveMode(anyBoolean())).thenReturn(true); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testSetPowerSaveMode_enable_firstCall_needWarning() throws Exception { |
| 70 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 71 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 72 | |
| 73 | assertEquals(false, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 74 | |
| 75 | verify(mMockContext, times(1)).sendBroadcast(any(Intent.class)); |
| 76 | verify(mMockPowerManager, times(0)).setPowerSaveMode(anyBoolean()); |
| 77 | |
| 78 | // They shouldn't have changed. |
| 79 | assertEquals(-1, |
| 80 | Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 81 | assertEquals(-2, |
| 82 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testSetPowerSaveMode_enable_secondCall_needWarning() throws Exception { |
| 87 | Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked. |
| 88 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 89 | |
| 90 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 91 | |
| 92 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 93 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 94 | |
| 95 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 96 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testSetPowerSaveMode_enable_thridCall_needWarning() throws Exception { |
| 101 | Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked. |
| 102 | Secure.putInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 1); |
| 103 | |
| 104 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 105 | |
| 106 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 107 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 108 | |
| 109 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 110 | assertEquals(2, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testSetPowerSaveMode_enable_firstCall_noWarning() throws Exception { |
| 115 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 116 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 117 | |
| 118 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, false)); |
| 119 | |
| 120 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 121 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 122 | |
| 123 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 124 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testSetPowerSaveMode_disable_firstCall_noWarning() throws Exception { |
| 129 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 130 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 131 | |
| 132 | // When disabling, needFirstTimeWarning doesn't matter. |
| 133 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, false)); |
| 134 | |
| 135 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 136 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false)); |
| 137 | |
| 138 | assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 139 | assertEquals(-2, |
| 140 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testSetPowerSaveMode_disable_firstCall_needWarning() throws Exception { |
| 145 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 146 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 147 | |
| 148 | // When disabling, needFirstTimeWarning doesn't matter. |
| 149 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, true)); |
| 150 | |
| 151 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 152 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false)); |
| 153 | |
| 154 | assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 155 | assertEquals(-2, |
| 156 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 157 | } |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 158 | |
| 159 | @Test |
| 160 | public void testEnsureAutoBatterysaver_setNewPositiveValue_doNotOverwrite() throws Exception { |
Fan Zhang | afa3693 | 2018-10-11 14:44:48 -0700 | [diff] [blame^] | 161 | Global.putInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 162 | |
| 163 | BatterySaverUtils.ensureAutoBatterySaver(mMockContext, BATTERY_SAVER_THRESHOLD_1); |
| 164 | |
Fan Zhang | afa3693 | 2018-10-11 14:44:48 -0700 | [diff] [blame^] | 165 | assertThat(Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1)) |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 166 | .isEqualTo(BATTERY_SAVER_THRESHOLD_1); |
| 167 | |
| 168 | // Once a positive number is set, ensureAutoBatterySaver() won't overwrite it. |
| 169 | BatterySaverUtils.ensureAutoBatterySaver(mMockContext, BATTERY_SAVER_THRESHOLD_2); |
Fan Zhang | afa3693 | 2018-10-11 14:44:48 -0700 | [diff] [blame^] | 170 | assertThat(Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1)) |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 171 | .isEqualTo(BATTERY_SAVER_THRESHOLD_1); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testSetAutoBatterySaverTriggerLevel_setSuppressSuggestion() throws Exception { |
| 176 | Global.putString(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, "null"); |
| 177 | Secure.putString(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, "null"); |
| 178 | |
| 179 | BatterySaverUtils.setAutoBatterySaverTriggerLevel(mMockContext, 0); |
| 180 | assertThat(Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1)) |
| 181 | .isEqualTo(0); |
| 182 | assertThat(Secure.getInt(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, -1)) |
| 183 | .isEqualTo(-1); // not set. |
| 184 | |
Fan Zhang | afa3693 | 2018-10-11 14:44:48 -0700 | [diff] [blame^] | 185 | BatterySaverUtils.setAutoBatterySaverTriggerLevel(mMockContext, BATTERY_SAVER_THRESHOLD_1); |
| 186 | assertThat(Global.getInt(mMockResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1)) |
Makoto Onuki | 5972773 | 2018-04-04 12:44:05 -0700 | [diff] [blame] | 187 | .isEqualTo(BATTERY_SAVER_THRESHOLD_1); |
| 188 | assertThat(Secure.getInt(mMockResolver, Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, -1)) |
| 189 | .isEqualTo(1); |
| 190 | } |
Makoto Onuki | 16a0dd2 | 2018-03-20 10:40:37 -0700 | [diff] [blame] | 191 | } |