• 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.Context;
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 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class AutoRestrictionPreferenceControllerTest {
40     private static final int ON = 1;
41     private static final int OFF = 0;
42 
43     private AutoRestrictionPreferenceController mController;
44     private SwitchPreference mPreference;
45     private Context mContext;
46     private FakeFeatureFactory mFeatureFactory;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51 
52         mFeatureFactory = FakeFeatureFactory.setupForTest();
53         mContext = RuntimeEnvironment.application;
54         mController = new AutoRestrictionPreferenceController(mContext);
55         mPreference = new SwitchPreference(mContext);
56     }
57 
58     @Test
testUpdateState_AutoRestrictionOn_preferenceChecked()59     public void testUpdateState_AutoRestrictionOn_preferenceChecked() {
60         putAutoRestrictionValue(ON);
61 
62         mController.updateState(mPreference);
63 
64         assertThat(mPreference.isChecked()).isTrue();
65     }
66 
67     @Test
testUpdateState_AutoRestrictionOff_preferenceUnchecked()68     public void testUpdateState_AutoRestrictionOff_preferenceUnchecked() {
69         putAutoRestrictionValue(OFF);
70 
71         mController.updateState(mPreference);
72 
73         assertThat(mPreference.isChecked()).isFalse();
74     }
75 
76     @Test
testUpdateState_checkPreference_autoRestrictionOn()77     public void testUpdateState_checkPreference_autoRestrictionOn() {
78         mController.onPreferenceChange(mPreference, true);
79 
80         assertThat(getAutoRestrictionValue()).isEqualTo(ON);
81     }
82 
83     @Test
testUpdateState_unCheckPreference_autoRestrictionOff()84     public void testUpdateState_unCheckPreference_autoRestrictionOff() {
85         mController.onPreferenceChange(mPreference, false);
86 
87         assertThat(getAutoRestrictionValue()).isEqualTo(OFF);
88     }
89 
90     @Test
testGetAvailabilityStatus_smartBatterySupported_returnDisabled()91     public void testGetAvailabilityStatus_smartBatterySupported_returnDisabled() {
92         doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported();
93 
94         assertThat(mController.getAvailabilityStatus()).isEqualTo(
95                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
96     }
97 
98     @Test
testGetAvailabilityStatus_smartBatteryUnSupported_returnAvailable()99     public void testGetAvailabilityStatus_smartBatteryUnSupported_returnAvailable() {
100         doReturn(false).when(mFeatureFactory.powerUsageFeatureProvider).isSmartBatterySupported();
101 
102         assertThat(mController.getAvailabilityStatus()).isEqualTo(
103                 BasePreferenceController.AVAILABLE);
104     }
105 
putAutoRestrictionValue(int value)106     private void putAutoRestrictionValue(int value) {
107         Settings.Global.putInt(mContext.getContentResolver(),
108                 Settings.Global.APP_AUTO_RESTRICTION_ENABLED,
109                 value);
110     }
111 
getAutoRestrictionValue()112     private int getAutoRestrictionValue() {
113         return Settings.Global.getInt(mContext.getContentResolver(),
114                 Settings.Global.APP_AUTO_RESTRICTION_ENABLED, ON);
115     }
116 }
117