• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.fuelgauge;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 
23 import android.content.ContentResolver;
24 import android.provider.Settings;
25 
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import com.android.settingslib.widget.MainSwitchPreference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class SmartBatteryPreferenceControllerTest {
41 
42     private static final int ON = 1;
43     private static final int OFF = 0;
44 
45     private SmartBatteryPreferenceController mController;
46     private SwitchPreference mPreference;
47     private ContentResolver mContentResolver;
48     private FakeFeatureFactory mFeatureFactory;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53 
54         mFeatureFactory = FakeFeatureFactory.setupForTest();
55         mContentResolver = RuntimeEnvironment.application.getContentResolver();
56         mController = new SmartBatteryPreferenceController(RuntimeEnvironment.application);
57         mPreference = new SwitchPreference(RuntimeEnvironment.application);
58     }
59 
60     @Test
testUpdateState_smartBatteryOn_preferenceChecked()61     public void testUpdateState_smartBatteryOn_preferenceChecked() {
62         putSmartBatteryValue(ON);
63 
64         mController.updateState(mPreference);
65 
66         assertThat(mPreference.isChecked()).isTrue();
67     }
68 
69     @Test
testUpdateState_smartBatteryOff_preferenceUnchecked()70     public void testUpdateState_smartBatteryOff_preferenceUnchecked() {
71         putSmartBatteryValue(OFF);
72 
73         mController.updateState(mPreference);
74 
75         assertThat(mPreference.isChecked()).isFalse();
76     }
77 
78     @Test
testUpdateState_checkPreference_smartBatteryOn()79     public void testUpdateState_checkPreference_smartBatteryOn() {
80         mController.onPreferenceChange(mPreference, true);
81 
82         assertThat(getSmartBatteryValue()).isEqualTo(ON);
83     }
84 
85     @Test
testUpdateState_unCheckPreference_smartBatteryOff()86     public void testUpdateState_unCheckPreference_smartBatteryOff() {
87         mController.onPreferenceChange(mPreference, false);
88 
89         assertThat(getSmartBatteryValue()).isEqualTo(OFF);
90     }
91 
92     @Test
testGetAvailabilityStatus_smartBatterySupported_returnAvailable()93     public void testGetAvailabilityStatus_smartBatterySupported_returnAvailable() {
94         doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported();
95 
96         assertThat(mController.getAvailabilityStatus()).isEqualTo(
97                 BasePreferenceController.AVAILABLE);
98     }
99 
100     @Test
testGetAvailabilityStatus_smartBatteryUnSupported_returnDisabled()101     public void testGetAvailabilityStatus_smartBatteryUnSupported_returnDisabled() {
102         doReturn(false).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported();
103 
104         assertThat(mController.getAvailabilityStatus()).isEqualTo(
105                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
106     }
107 
putSmartBatteryValue(int value)108     private void putSmartBatteryValue(int value) {
109         Settings.Global.putInt(mContentResolver,
110                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, value);
111     }
112 
getSmartBatteryValue()113     private int getSmartBatteryValue() {
114         return Settings.Global.getInt(mContentResolver,
115                 Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, ON);
116     }
117 
118     @Test
isSliceableCorrectKey_returnsTrue()119     public void isSliceableCorrectKey_returnsTrue() {
120         final SmartBatteryPreferenceController controller =
121                 new SmartBatteryPreferenceController(null);
122         assertThat(controller.isSliceable()).isTrue();
123     }
124 
125     @Test
isPublicSlice_returnsTrue()126     public void isPublicSlice_returnsTrue() {
127         assertThat(mController.isPublicSlice()).isTrue();
128     }
129 }
130