• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.fuelgauge;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.os.PowerManager;
26 import android.provider.Settings;
27 
28 import androidx.preference.Preference;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.util.ReflectionHelpers;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class BatterySaverControllerTest {
41 
42     @Mock
43     private Preference mBatterySaverPref;
44     @Mock
45     private PowerManager mPowerManager;
46 
47     private BatterySaverController mBatterySaverController;
48     private Context mContext;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53 
54         mContext = RuntimeEnvironment.application;
55         mBatterySaverController = spy(new BatterySaverController(mContext));
56         ReflectionHelpers.setField(mBatterySaverController, "mPowerManager", mPowerManager);
57         ReflectionHelpers.setField(mBatterySaverController, "mBatterySaverPref", mBatterySaverPref);
58 
59         Settings.Global.putInt(mContext.getContentResolver(),
60                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
61     }
62 
63     @Test
onPreferenceChange_onStart()64     public void onPreferenceChange_onStart() {
65         mBatterySaverController.onStart();
66         verify(mBatterySaverPref).setSummary("Off");
67     }
68 
69     @Test
onPreferenceChange_onPowerSaveModeChanged()70     public void onPreferenceChange_onPowerSaveModeChanged() {
71         mBatterySaverController.onPowerSaveModeChanged();
72         verify(mBatterySaverPref).setSummary("Off");
73     }
74 
75     @Test
getSummary_batterySaverOn_showSummaryOn()76     public void getSummary_batterySaverOn_showSummaryOn() {
77         when(mPowerManager.isPowerSaveMode()).thenReturn(true);
78 
79         assertThat(mBatterySaverController.getSummary()).isEqualTo("On");
80     }
81 
82     @Test
getSummary_batterySaverOffButScheduled_showSummaryScheduled()83     public void getSummary_batterySaverOffButScheduled_showSummaryScheduled() {
84         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
85         Settings.Global.putInt(mContext.getContentResolver(),
86                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15);
87 
88         assertThat(mBatterySaverController.getSummary()).isEqualTo("Will turn on at 15%");
89     }
90 
91     @Test
getSummary_batterySaverOff_showSummaryOff()92     public void getSummary_batterySaverOff_showSummaryOff() {
93         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
94 
95         assertThat(mBatterySaverController.getSummary()).isEqualTo("Off");
96     }
97 }
98