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.app.settings.SettingsEnums;
25 import android.content.Context;
26 import android.os.PowerManager;
27 import android.provider.Settings;
28 import android.util.Pair;
29 
30 import androidx.preference.Preference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.util.ReflectionHelpers;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class BatterySaverControllerTest {
43 
44     @Mock
45     private Preference mBatterySaverPref;
46     @Mock
47     private PowerManager mPowerManager;
48 
49     private BatterySaverController mBatterySaverController;
50     private Context mContext;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55 
56         mContext = RuntimeEnvironment.application;
57         mBatterySaverController = spy(new BatterySaverController(mContext));
58         ReflectionHelpers.setField(mBatterySaverController, "mPowerManager", mPowerManager);
59         ReflectionHelpers.setField(mBatterySaverController, "mBatterySaverPref", mBatterySaverPref);
60 
61         Settings.Global.putInt(mContext.getContentResolver(),
62                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
63     }
64 
65     @Test
onPreferenceChange_onStart()66     public void onPreferenceChange_onStart() {
67         mBatterySaverController.onStart();
68         verify(mBatterySaverPref).setSummary("Off");
69     }
70 
71     @Test
onPreferenceChange_onPowerSaveModeChanged()72     public void onPreferenceChange_onPowerSaveModeChanged() {
73         mBatterySaverController.onPowerSaveModeChanged();
74         verify(mBatterySaverPref).setSummary("Off");
75     }
76 
77     @Test
getSummary_batterySaverOn_showSummaryOn()78     public void getSummary_batterySaverOn_showSummaryOn() {
79         when(mPowerManager.isPowerSaveMode()).thenReturn(true);
80 
81         assertThat(mBatterySaverController.getSummary()).isEqualTo("On");
82     }
83 
84     @Test
getSummary_batterySaverOffButScheduled_showSummaryScheduled()85     public void getSummary_batterySaverOffButScheduled_showSummaryScheduled() {
86         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
87         Settings.Global.putInt(mContext.getContentResolver(),
88                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15);
89 
90         assertThat(mBatterySaverController.getSummary()).isEqualTo("Will turn on at 15%");
91     }
92 
93     @Test
getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff()94     public void getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff() {
95         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
96         Settings.Global.putInt(mContext.getContentResolver(),
97                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
98 
99         assertThat(mBatterySaverController.getSummary()).isEqualTo("Off");
100     }
101 
102     @Test
getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine()103     public void getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine() {
104         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
105         Settings.Global.putInt(
106                 mContext.getContentResolver(),
107                 Settings.Global.AUTOMATIC_POWER_SAVE_MODE,
108                 PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
109 
110         assertThat(mBatterySaverController.getSummary()).
111                 isEqualTo("Will turn on based on your routine");
112     }
113 
114     @Test
getSummary_batterySaverOff_showSummaryOff()115     public void getSummary_batterySaverOff_showSummaryOff() {
116         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
117 
118         assertThat(mBatterySaverController.getSummary()).isEqualTo("Off");
119     }
120 
121     @Test
getAvailabilityStatus_returnAvailable()122     public void getAvailabilityStatus_returnAvailable() {
123         assertThat(mBatterySaverController.getAvailabilityStatus())
124                 .isEqualTo(BatterySaverController.AVAILABLE);
125     }
126 }
127