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