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.KeepActivitiesPreferenceController.SETTING_VALUE_OFF; 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.app.IActivityManager; 27 import android.content.Context; 28 import android.os.RemoteException; 29 import android.provider.Settings; 30 31 import androidx.preference.PreferenceScreen; 32 import androidx.preference.SwitchPreference; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class KeepActivitiesPreferenceControllerTest { 44 45 private static final int SETTING_VALUE_ON = 1; 46 47 @Mock 48 private SwitchPreference mPreference; 49 @Mock 50 private PreferenceScreen mPreferenceScreen; 51 @Mock 52 private IActivityManager mActivityManager; 53 54 private Context mContext; 55 private KeepActivitiesPreferenceController mController; 56 57 @Before setup()58 public void setup() { 59 MockitoAnnotations.initMocks(this); 60 mContext = RuntimeEnvironment.application; 61 mController = spy(new KeepActivitiesPreferenceController(mContext)); 62 doReturn(mActivityManager).when(mController).getActivityManager(); 63 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 64 .thenReturn(mPreference); 65 mController.displayPreference(mPreferenceScreen); 66 } 67 68 @Test onPreferenceChanged_settingEnabled_turnOnDestroyActivities()69 public void onPreferenceChanged_settingEnabled_turnOnDestroyActivities() 70 throws RemoteException { 71 mController.onPreferenceChange(mPreference, true /* new value */); 72 73 verify(mActivityManager).setAlwaysFinish(true); 74 } 75 76 @Test onPreferenceChanged_settingDisabled_turnOffDestroyActivities()77 public void onPreferenceChanged_settingDisabled_turnOffDestroyActivities() 78 throws RemoteException { 79 mController.onPreferenceChange(mPreference, false /* new value */); 80 81 verify(mActivityManager).setAlwaysFinish(false); 82 } 83 84 @Test updateState_settingEnabled_preferenceShouldBeChecked()85 public void updateState_settingEnabled_preferenceShouldBeChecked() { 86 Settings.Global.putInt(mContext.getContentResolver(), 87 Settings.Global.ALWAYS_FINISH_ACTIVITIES, SETTING_VALUE_ON); 88 mController.updateState(mPreference); 89 90 verify(mPreference).setChecked(true); 91 } 92 93 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()94 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 95 Settings.Global.putInt(mContext.getContentResolver(), 96 Settings.Global.ALWAYS_FINISH_ACTIVITIES, SETTING_VALUE_OFF); 97 mController.updateState(mPreference); 98 99 verify(mPreference).setChecked(false); 100 } 101 102 @Test onDeveloperOptionsDisabled_shouldDisablePreference()103 public void onDeveloperOptionsDisabled_shouldDisablePreference() throws RemoteException { 104 mController.onDeveloperOptionsSwitchDisabled(); 105 106 verify(mActivityManager).setAlwaysFinish(false); 107 verify(mPreference).setEnabled(false); 108 verify(mPreference).setChecked(false); 109 } 110 } 111