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.android.settings.development.LocalTerminalPreferenceController 20 .TERMINAL_APP_PACKAGE; 21 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.os.UserManager; 31 32 import androidx.preference.PreferenceScreen; 33 import androidx.preference.SwitchPreference; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class LocalTerminalPreferenceControllerTest { 44 45 @Mock 46 private Context mContext; 47 @Mock 48 private SwitchPreference mPreference; 49 @Mock 50 private PreferenceScreen mPreferenceScreen; 51 @Mock 52 private UserManager mUserManager; 53 @Mock 54 private PackageManager mPackageManager; 55 56 private LocalTerminalPreferenceController mController; 57 58 @Before setup()59 public void setup() { 60 MockitoAnnotations.initMocks(this); 61 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 62 when(mUserManager.isAdminUser()).thenReturn(true); 63 mController = spy(new LocalTerminalPreferenceController(mContext)); 64 doReturn(true).when(mController).isAvailable(); 65 doReturn(mPackageManager).when(mController).getPackageManager(); 66 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 67 .thenReturn(mPreference); 68 mController.displayPreference(mPreferenceScreen); 69 } 70 71 @Test displayPreference_shouldDisablePreferenceWhenNotAdmin()72 public void displayPreference_shouldDisablePreferenceWhenNotAdmin() { 73 when(mUserManager.isAdminUser()).thenReturn(false); 74 mController.displayPreference(mPreferenceScreen); 75 76 verify(mPreference).setEnabled(false); 77 } 78 79 @Test onPreferenceChanged_turnOnTerminal()80 public void onPreferenceChanged_turnOnTerminal() { 81 mController.onPreferenceChange(null, true); 82 83 verify(mPackageManager).setApplicationEnabledSetting(TERMINAL_APP_PACKAGE, 84 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); 85 } 86 87 @Test onPreferenceChanged_turnOffTerminal()88 public void onPreferenceChanged_turnOffTerminal() { 89 mController.onPreferenceChange(null, false); 90 91 verify(mPackageManager).setApplicationEnabledSetting(TERMINAL_APP_PACKAGE, 92 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0); 93 } 94 95 @Test updateState_preferenceShouldBeChecked()96 public void updateState_preferenceShouldBeChecked() { 97 when(mPackageManager.getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)).thenReturn( 98 PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 99 mController.updateState(mPreference); 100 101 verify(mPreference).setChecked(true); 102 } 103 104 @Test updateState_preferenceShouldNotBeChecked()105 public void updateState_preferenceShouldNotBeChecked() { 106 when(mPackageManager.getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)).thenReturn( 107 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); 108 mController.updateState(mPreference); 109 110 verify(mPreference).setChecked(false); 111 } 112 113 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()114 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 115 mController.onDeveloperOptionsSwitchDisabled(); 116 117 verify(mPackageManager).setApplicationEnabledSetting(TERMINAL_APP_PACKAGE, 118 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0); 119 verify(mPreference).setChecked(false); 120 verify(mPreference).setEnabled(false); 121 } 122 123 @Test onDeveloperOptionsSwitchEnabled_shouldDoNothingWhenNotAdmin()124 public void onDeveloperOptionsSwitchEnabled_shouldDoNothingWhenNotAdmin() { 125 when(mUserManager.isAdminUser()).thenReturn(false); 126 mController.onDeveloperOptionsSwitchDisabled(); 127 128 verify(mPreference, never()).setEnabled(true); 129 } 130 131 @Test onDeveloperOptionsSwitchEnabled_preferenceShouldBeEnabledWhenAdmin()132 public void onDeveloperOptionsSwitchEnabled_preferenceShouldBeEnabledWhenAdmin() { 133 mController.onDeveloperOptionsSwitchEnabled(); 134 135 verify(mPreference).setEnabled(true); 136 } 137 } 138