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