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 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.provider.Settings; 28 29 import androidx.preference.PreferenceScreen; 30 import androidx.preference.SwitchPreference; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class AdbPreferenceControllerTest { 42 43 @Mock 44 private SwitchPreference mPreference; 45 @Mock 46 private PreferenceScreen mPreferenceScreen; 47 @Mock 48 private DevelopmentSettingsDashboardFragment mFragment; 49 50 private Context mContext; 51 private AdbPreferenceController mController; 52 53 @Before setup()54 public void setup() { 55 MockitoAnnotations.initMocks(this); 56 mContext = RuntimeEnvironment.application; 57 mController = spy(new AdbPreferenceController(mContext, mFragment)); 58 doReturn(true).when(mController).isAvailable(); 59 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 60 .thenReturn(mPreference); 61 mController.displayPreference(mPreferenceScreen); 62 } 63 64 @Test onDeveloperOptionsDisabled_shouldDisablePreference()65 public void onDeveloperOptionsDisabled_shouldDisablePreference() { 66 mController.onDeveloperOptionsDisabled(); 67 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 68 Settings.Global.ADB_ENABLED, -1); 69 70 assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_OFF); 71 verify(mPreference).setEnabled(false); 72 verify(mPreference).setChecked(false); 73 } 74 75 @Test onAdbDialogConfirmed_shouldEnableAdbSetting()76 public void onAdbDialogConfirmed_shouldEnableAdbSetting() { 77 mController.onAdbDialogConfirmed(); 78 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 79 Settings.Global.ADB_ENABLED, -1); 80 81 assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_ON); 82 } 83 84 @Test onAdbDialogDismissed_preferenceShouldNotBeChecked()85 public void onAdbDialogDismissed_preferenceShouldNotBeChecked() { 86 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.ADB_ENABLED, 87 AdbPreferenceController.ADB_SETTING_OFF); 88 mController.onAdbDialogDismissed(); 89 90 verify(mPreference).setChecked(false); 91 } 92 } 93