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