Home | History | Annotate | Download | only in development
      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.settingslib.development;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
     22 import static org.mockito.ArgumentMatchers.any;
     23 import static org.mockito.ArgumentMatchers.anyString;
     24 import static org.mockito.Mockito.never;
     25 import static org.mockito.Mockito.spy;
     26 import static org.mockito.Mockito.verify;
     27 import static org.mockito.Mockito.when;
     28 
     29 import android.content.Context;
     30 import android.content.pm.PackageManager;
     31 import android.os.UserManager;
     32 import android.provider.Settings;
     33 import android.support.v14.preference.SwitchPreference;
     34 import android.support.v7.preference.Preference;
     35 import android.support.v7.preference.PreferenceScreen;
     36 
     37 import com.android.settingslib.SettingLibRobolectricTestRunner;
     38 import com.android.settingslib.TestConfig;
     39 
     40 import org.junit.Before;
     41 import org.junit.Test;
     42 import org.junit.runner.RunWith;
     43 import org.mockito.Mock;
     44 import org.mockito.MockitoAnnotations;
     45 import org.robolectric.annotation.Config;
     46 import org.robolectric.shadows.ShadowApplication;
     47 
     48 @RunWith(SettingLibRobolectricTestRunner.class)
     49 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     50 public class EnableAdbPreferenceControllerTest {
     51     @Mock(answer = RETURNS_DEEP_STUBS)
     52     private PreferenceScreen mScreen;
     53     @Mock
     54     private UserManager mUserManager;
     55     @Mock
     56     private PackageManager mPackageManager;
     57 
     58     private Context mContext;
     59     private SwitchPreference mPreference;
     60     private ConcreteEnableAdbPreferenceController mController;
     61 
     62     @Before
     63     public void setUp() {
     64         MockitoAnnotations.initMocks(this);
     65         ShadowApplication shadowContext = ShadowApplication.getInstance();
     66         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
     67         mContext = spy(shadowContext.getApplicationContext());
     68         when(mContext.getPackageManager()).thenReturn(mPackageManager);
     69         mPreference = new SwitchPreference(mContext);
     70         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
     71         mController = new ConcreteEnableAdbPreferenceController(mContext);
     72         mPreference.setKey(mController.getPreferenceKey());
     73     }
     74 
     75     @Test
     76     public void displayPreference_isNotAdmin_shouldRemovePreference() {
     77         when(mUserManager.isAdminUser()).thenReturn(false);
     78         when(mScreen.getPreferenceCount()).thenReturn(1);
     79         when(mScreen.getPreference(0)).thenReturn(mPreference);
     80 
     81         mController.displayPreference(mScreen);
     82 
     83         verify(mScreen).removePreference(any(Preference.class));
     84     }
     85 
     86     @Test
     87     public void displayPreference_isAdmin_shouldNotRemovePreference() {
     88         when(mUserManager.isAdminUser()).thenReturn(true);
     89 
     90         mController.displayPreference(mScreen);
     91 
     92         verify(mScreen, never()).removePreference(any(Preference.class));
     93     }
     94 
     95 
     96     @Test
     97     public void resetPreference_shouldUncheck() {
     98         when(mUserManager.isAdminUser()).thenReturn(true);
     99         mController.displayPreference(mScreen);
    100         mPreference.setChecked(true);
    101 
    102         mController.resetPreference();
    103 
    104         assertThat(mPreference.isChecked()).isFalse();
    105     }
    106 
    107     @Test
    108     public void handlePreferenceTreeClick_shouldUpdateSettings() {
    109         when(mUserManager.isAdminUser()).thenReturn(true);
    110         Settings.Secure.putInt(mContext.getContentResolver(),
    111                 Settings.Global.ADB_ENABLED, 1);
    112         mPreference.setChecked(true);
    113         mController.displayPreference(mScreen);
    114 
    115         mController.handlePreferenceTreeClick(mPreference);
    116 
    117         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
    118                 Settings.Global.ADB_ENABLED, 0)).isEqualTo(0);
    119     }
    120 
    121     @Test
    122     public void updateState_settingsOn_shouldCheck() {
    123         when(mUserManager.isAdminUser()).thenReturn(true);
    124         Settings.Secure.putInt(mContext.getContentResolver(),
    125                 Settings.Global.ADB_ENABLED, 1);
    126         mPreference.setChecked(false);
    127         mController.displayPreference(mScreen);
    128 
    129         mController.updateState(mPreference);
    130 
    131         assertThat(mPreference.isChecked()).isTrue();
    132     }
    133 
    134     @Test
    135     public void updateState_settingsOff_shouldUncheck() {
    136         when(mUserManager.isAdminUser()).thenReturn(true);
    137         Settings.Secure.putInt(mContext.getContentResolver(),
    138                 Settings.Global.ADB_ENABLED, 0);
    139         mPreference.setChecked(true);
    140         mController.displayPreference(mScreen);
    141 
    142         mController.updateState(mPreference);
    143 
    144         assertThat(mPreference.isChecked()).isFalse();
    145     }
    146 
    147     class ConcreteEnableAdbPreferenceController extends AbstractEnableAdbPreferenceController {
    148         public ConcreteEnableAdbPreferenceController(Context context) {
    149             super(context);
    150         }
    151 
    152         @Override
    153         public void showConfirmationDialog(SwitchPreference preference) {
    154             // Don't show a dialog, just set setting.
    155             writeAdbSetting(true);
    156         }
    157     }
    158 }
    159