• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.provider.Settings;
25 import android.support.v14.preference.SwitchPreference;
26 import android.support.v7.preference.PreferenceScreen;
27 
28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
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.RuntimeEnvironment;
36 
37 @RunWith(SettingsRobolectricTestRunner.class)
38 public class TetheringHardwareAccelPreferenceControllerTest {
39 
40     @Mock
41     private SwitchPreference mPreference;
42     @Mock
43     private PreferenceScreen mPreferenceScreen;
44 
45     private Context mContext;
46     private TetheringHardwareAccelPreferenceController mController;
47 
48     @Before
setup()49     public void setup() {
50         MockitoAnnotations.initMocks(this);
51         mContext = RuntimeEnvironment.application;
52         mController = new TetheringHardwareAccelPreferenceController(mContext);
53         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
54             .thenReturn(mPreference);
55         mController.displayPreference(mPreferenceScreen);
56     }
57 
58     @Test
onPreferenceChanged_settingEnabled_turnOnTetheringAccel()59     public void onPreferenceChanged_settingEnabled_turnOnTetheringAccel() {
60         mController.onPreferenceChange(mPreference, true /* new value */);
61 
62         final int mode = Settings.System.getInt(mContext.getContentResolver(),
63                 Settings.Global.TETHER_OFFLOAD_DISABLED, -1 /* default */);
64 
65         assertThat(mode).isEqualTo(TetheringHardwareAccelPreferenceController.SETTING_VALUE_ON);
66     }
67 
68     @Test
onPreferenceChanged_settingDisabled_turnOffTetheringAccel()69     public void onPreferenceChanged_settingDisabled_turnOffTetheringAccel() {
70         mController.onPreferenceChange(mPreference, false /* new value */);
71 
72         final int mode = Settings.System.getInt(mContext.getContentResolver(),
73                 Settings.Global.TETHER_OFFLOAD_DISABLED, -1 /* default */);
74 
75         assertThat(mode).isEqualTo(TetheringHardwareAccelPreferenceController.SETTING_VALUE_OFF);
76     }
77 
78     @Test
updateState_settingEnabled_preferenceShouldBeChecked()79     public void updateState_settingEnabled_preferenceShouldBeChecked() {
80         Settings.System.putInt(mContext.getContentResolver(),
81                 Settings.Global.TETHER_OFFLOAD_DISABLED,
82                 TetheringHardwareAccelPreferenceController.SETTING_VALUE_ON);
83         mController.updateState(mPreference);
84 
85         verify(mPreference).setChecked(true);
86     }
87 
88     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()89     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
90         Settings.System.putInt(mContext.getContentResolver(),
91                 Settings.Global.TETHER_OFFLOAD_DISABLED,
92                 TetheringHardwareAccelPreferenceController.SETTING_VALUE_OFF);
93         mController.updateState(mPreference);
94 
95         verify(mPreference).setChecked(false);
96     }
97 
98     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()99     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
100         mController.onDeveloperOptionsSwitchDisabled();
101         final int mode = Settings.System.getInt(mContext.getContentResolver(),
102                 Settings.Global.TETHER_OFFLOAD_DISABLED, -1 /* default */);
103 
104         assertThat(mode).isEqualTo(TetheringHardwareAccelPreferenceController.SETTING_VALUE_OFF);
105         verify(mPreference).setEnabled(false);
106         verify(mPreference).setChecked(false);
107     }
108 }
109