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 private Preference mBatterySaverPref;
43     @Mock private PowerManager mPowerManager;
44 
45     private BatterySaverController mBatterySaverController;
46     private Context mContext;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51 
52         mContext = RuntimeEnvironment.application;
53         mBatterySaverController = spy(new BatterySaverController(mContext));
54         ReflectionHelpers.setField(mBatterySaverController, "mPowerManager", mPowerManager);
55         ReflectionHelpers.setField(mBatterySaverController, "mBatterySaverPref", mBatterySaverPref);
56 
57         Settings.Global.putInt(
58                 mContext.getContentResolver(), Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
59     }
60 
61     @Test
onPreferenceChange_onStart()62     public void onPreferenceChange_onStart() {
63         mBatterySaverController.onStart();
64         verify(mBatterySaverPref).setSummary("Off");
65     }
66 
67     @Test
onPreferenceChange_onPowerSaveModeChanged()68     public void onPreferenceChange_onPowerSaveModeChanged() {
69         mBatterySaverController.onPowerSaveModeChanged();
70         verify(mBatterySaverPref).setSummary("Off");
71     }
72 
73     @Test
getSummary_batterySaverOn_showSummaryOn()74     public void getSummary_batterySaverOn_showSummaryOn() {
75         when(mPowerManager.isPowerSaveMode()).thenReturn(true);
76 
77         assertThat(mBatterySaverController.getSummary()).isEqualTo("On");
78     }
79 
80     @Test
getSummary_batterySaverOffButScheduled_showSummaryScheduled()81     public void getSummary_batterySaverOffButScheduled_showSummaryScheduled() {
82         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
83         Settings.Global.putInt(
84                 mContext.getContentResolver(), Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15);
85 
86         assertThat(mBatterySaverController.getSummary()).isEqualTo("Will turn on at 15%");
87     }
88 
89     @Test
getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff()90     public void getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff() {
91         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
92         Settings.Global.putInt(
93                 mContext.getContentResolver(), Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
94 
95         assertThat(mBatterySaverController.getSummary()).isEqualTo("Off");
96     }
97 
98     @Test
getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine()99     public void getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine() {
100         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
101         Settings.Global.putInt(
102                 mContext.getContentResolver(),
103                 Settings.Global.AUTOMATIC_POWER_SAVE_MODE,
104                 PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC);
105 
106         assertThat(mBatterySaverController.getSummary())
107                 .isEqualTo("Will turn on based on your routine");
108     }
109 
110     @Test
getSummary_batterySaverOff_showSummaryOff()111     public void getSummary_batterySaverOff_showSummaryOff() {
112         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
113 
114         assertThat(mBatterySaverController.getSummary()).isEqualTo("Off");
115     }
116 
117     @Test
getAvailabilityStatus_returnAvailable()118     public void getAvailabilityStatus_returnAvailable() {
119         assertThat(mBatterySaverController.getAvailabilityStatus())
120                 .isEqualTo(BatterySaverController.AVAILABLE);
121     }
122 }
123