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 org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.spy; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.app.IActivityManager; 25 import android.content.Context; 26 import android.os.RemoteException; 27 import android.support.v7.preference.ListPreference; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 public class BackgroundProcessLimitPreferenceControllerTest { 42 43 @Mock 44 private IActivityManager mActivityManager; 45 @Mock 46 private ListPreference mPreference; 47 @Mock 48 private PreferenceScreen mScreen; 49 50 /** 51 * 0: Standard limit 52 * 1: No Background processes 53 * 2: At most 1 process 54 * 3: At most 2 processes 55 * 4: At most 3 processes 56 * 5: At most 4 processes 57 */ 58 private String[] mListValues; 59 private String[] mListSummaries; 60 private Context mContext; 61 private BackgroundProcessLimitPreferenceController mController; 62 63 @Before setup()64 public void setup() { 65 MockitoAnnotations.initMocks(this); 66 mContext = RuntimeEnvironment.application; 67 mListValues = mContext.getResources().getStringArray(R.array.app_process_limit_values); 68 mListSummaries = mContext.getResources().getStringArray(R.array.app_process_limit_entries); 69 mController = spy(new BackgroundProcessLimitPreferenceController(mContext)); 70 doReturn(mActivityManager).when(mController).getActivityManagerService(); 71 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 72 mController.displayPreference(mScreen); 73 } 74 75 @Test onPreferenceChange_noBackgroundProcessSet_shouldSetToNoBackgroundProcess()76 public void onPreferenceChange_noBackgroundProcessSet_shouldSetToNoBackgroundProcess() 77 throws RemoteException { 78 mController.onPreferenceChange(mPreference, mListValues[1]); 79 80 verify(mActivityManager).setProcessLimit(Integer.valueOf(mListValues[1])); 81 } 82 83 @Test onPreferenceChange_1ProcessSet_shouldSetTo1BackgroundProcess()84 public void onPreferenceChange_1ProcessSet_shouldSetTo1BackgroundProcess() 85 throws RemoteException { 86 mController.onPreferenceChange(mPreference, mListValues[2]); 87 88 verify(mActivityManager).setProcessLimit(Integer.valueOf(mListValues[2])); 89 } 90 91 @Test updateState_noBackgroundProcessSet_shouldSetPreferenceToNoBackgroundProcess()92 public void updateState_noBackgroundProcessSet_shouldSetPreferenceToNoBackgroundProcess() 93 throws RemoteException { 94 when(mActivityManager.getProcessLimit()).thenReturn(Integer.valueOf(mListValues[1])); 95 96 mController.updateState(mPreference); 97 98 verify(mPreference).setValue(mListValues[1]); 99 verify(mPreference).setSummary(mListSummaries[1]); 100 } 101 102 @Test updateState_1ProcessSet_shouldSetPreference1BackgroundProcess()103 public void updateState_1ProcessSet_shouldSetPreference1BackgroundProcess() 104 throws RemoteException { 105 when(mActivityManager.getProcessLimit()).thenReturn(Integer.valueOf(mListValues[2])); 106 107 mController.updateState(mPreference); 108 109 verify(mPreference).setValue(mListValues[2]); 110 verify(mPreference).setSummary(mListSummaries[2]); 111 } 112 113 @Test updateState_veryHighLimit_shouldDefaultToStandardLimit()114 public void updateState_veryHighLimit_shouldDefaultToStandardLimit() throws RemoteException { 115 when(mActivityManager.getProcessLimit()).thenReturn(Integer.MAX_VALUE); 116 117 mController.updateState(mPreference); 118 119 verify(mPreference).setValue(mListValues[0]); 120 verify(mPreference).setSummary(mListSummaries[0]); 121 } 122 123 @Test onDeveloperOptionsSwitchDisabled_shouldDisableAndResetPreference()124 public void onDeveloperOptionsSwitchDisabled_shouldDisableAndResetPreference() 125 throws RemoteException { 126 mController.onDeveloperOptionsSwitchDisabled(); 127 128 verify(mPreference).setEnabled(false); 129 verify(mActivityManager).setProcessLimit(-1); 130 } 131 } 132