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