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 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 22 | import static org.mockito.ArgumentMatchers.eq; |
| 23 | import static org.mockito.Mockito.times; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.os.PowerManager; |
| 31 | import android.provider.Settings.Secure; |
| 32 | |
| 33 | import com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
| 41 | |
| 42 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 43 | public class BatterySaverUtilsTest { |
| 44 | @Mock |
| 45 | Context mMockContext; |
| 46 | |
| 47 | @Mock |
| 48 | ContentResolver mMockResolver; |
| 49 | |
| 50 | @Mock |
| 51 | PowerManager mMockPowerManager; |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
| 55 | MockitoAnnotations.initMocks(this); |
| 56 | |
| 57 | when(mMockContext.getContentResolver()).thenReturn(mMockResolver); |
| 58 | when(mMockContext.getSystemService(eq(PowerManager.class))).thenReturn(mMockPowerManager); |
| 59 | when(mMockPowerManager.setPowerSaveMode(anyBoolean())).thenReturn(true); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testSetPowerSaveMode_enable_firstCall_needWarning() throws Exception { |
| 64 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 65 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 66 | |
| 67 | assertEquals(false, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 68 | |
| 69 | verify(mMockContext, times(1)).sendBroadcast(any(Intent.class)); |
| 70 | verify(mMockPowerManager, times(0)).setPowerSaveMode(anyBoolean()); |
| 71 | |
| 72 | // They shouldn't have changed. |
| 73 | assertEquals(-1, |
| 74 | Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 75 | assertEquals(-2, |
| 76 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testSetPowerSaveMode_enable_secondCall_needWarning() throws Exception { |
| 81 | Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked. |
| 82 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 83 | |
| 84 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 85 | |
| 86 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 87 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 88 | |
| 89 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 90 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testSetPowerSaveMode_enable_thridCall_needWarning() throws Exception { |
| 95 | Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); // Already acked. |
| 96 | Secure.putInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 1); |
| 97 | |
| 98 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, true)); |
| 99 | |
| 100 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 101 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 102 | |
| 103 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 104 | assertEquals(2, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testSetPowerSaveMode_enable_firstCall_noWarning() throws Exception { |
| 109 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 110 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 111 | |
| 112 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, true, false)); |
| 113 | |
| 114 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 115 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(true)); |
| 116 | |
| 117 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 118 | assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testSetPowerSaveMode_disable_firstCall_noWarning() throws Exception { |
| 123 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 124 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 125 | |
| 126 | // When disabling, needFirstTimeWarning doesn't matter. |
| 127 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, false)); |
| 128 | |
| 129 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 130 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false)); |
| 131 | |
| 132 | assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 133 | assertEquals(-2, |
| 134 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testSetPowerSaveMode_disable_firstCall_needWarning() throws Exception { |
| 139 | Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null"); |
| 140 | Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null"); |
| 141 | |
| 142 | // When disabling, needFirstTimeWarning doesn't matter. |
| 143 | assertEquals(true, BatterySaverUtils.setPowerSaveMode(mMockContext, false, true)); |
| 144 | |
| 145 | verify(mMockContext, times(0)).sendBroadcast(any(Intent.class)); |
| 146 | verify(mMockPowerManager, times(1)).setPowerSaveMode(eq(false)); |
| 147 | |
| 148 | assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1)); |
| 149 | assertEquals(-2, |
| 150 | Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2)); |
| 151 | } |
| 152 | } |