Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.server.am; |
| 18 | |
| 19 | import static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; |
| 20 | import static android.app.ActivityManager.PROCESS_STATE_BOUND_TOP; |
| 21 | import static android.app.ActivityManager.PROCESS_STATE_CACHED_ACTIVITY; |
| 22 | import static android.app.ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT; |
| 23 | import static android.app.ActivityManager.PROCESS_STATE_CACHED_EMPTY; |
| 24 | import static android.app.ActivityManager.PROCESS_STATE_CACHED_RECENT; |
| 25 | import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 26 | import static android.app.ActivityManager.PROCESS_STATE_HEAVY_WEIGHT; |
| 27 | import static android.app.ActivityManager.PROCESS_STATE_HOME; |
| 28 | import static android.app.ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND; |
| 29 | import static android.app.ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND; |
| 30 | import static android.app.ActivityManager.PROCESS_STATE_LAST_ACTIVITY; |
| 31 | import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT; |
| 32 | import static android.app.ActivityManager.PROCESS_STATE_PERSISTENT_UI; |
| 33 | import static android.app.ActivityManager.PROCESS_STATE_RECEIVER; |
| 34 | import static android.app.ActivityManager.PROCESS_STATE_SERVICE; |
| 35 | import static android.app.ActivityManager.PROCESS_STATE_TOP; |
| 36 | import static android.app.ActivityManager.PROCESS_STATE_TOP_SLEEPING; |
| 37 | import static android.app.ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND; |
| 38 | |
| 39 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 40 | |
| 41 | import static com.android.server.am.ProcessList.BACKUP_APP_ADJ; |
| 42 | import static com.android.server.am.ProcessList.CACHED_APP_MAX_ADJ; |
| 43 | import static com.android.server.am.ProcessList.CACHED_APP_MIN_ADJ; |
| 44 | import static com.android.server.am.ProcessList.FOREGROUND_APP_ADJ; |
| 45 | import static com.android.server.am.ProcessList.HEAVY_WEIGHT_APP_ADJ; |
| 46 | import static com.android.server.am.ProcessList.HOME_APP_ADJ; |
| 47 | import static com.android.server.am.ProcessList.PERCEPTIBLE_APP_ADJ; |
| 48 | import static com.android.server.am.ProcessList.PERCEPTIBLE_LOW_APP_ADJ; |
| 49 | import static com.android.server.am.ProcessList.PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ; |
| 50 | import static com.android.server.am.ProcessList.PERSISTENT_PROC_ADJ; |
| 51 | import static com.android.server.am.ProcessList.PERSISTENT_SERVICE_ADJ; |
| 52 | import static com.android.server.am.ProcessList.PREVIOUS_APP_ADJ; |
| 53 | import static com.android.server.am.ProcessList.SCHED_GROUP_BACKGROUND; |
| 54 | import static com.android.server.am.ProcessList.SCHED_GROUP_DEFAULT; |
| 55 | import static com.android.server.am.ProcessList.SCHED_GROUP_RESTRICTED; |
| 56 | import static com.android.server.am.ProcessList.SCHED_GROUP_TOP_APP; |
| 57 | import static com.android.server.am.ProcessList.SCHED_GROUP_TOP_APP_BOUND; |
| 58 | import static com.android.server.am.ProcessList.SERVICE_ADJ; |
| 59 | import static com.android.server.am.ProcessList.SERVICE_B_ADJ; |
| 60 | import static com.android.server.am.ProcessList.UNKNOWN_ADJ; |
| 61 | import static com.android.server.am.ProcessList.VISIBLE_APP_ADJ; |
| 62 | |
| 63 | import static org.junit.Assert.assertEquals; |
Jing Ji | 057f4cb | 2019-11-01 13:23:11 -0700 | [diff] [blame] | 64 | import static org.junit.Assert.assertTrue; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 65 | import static org.mockito.AdditionalAnswers.answer; |
Tim Murray | d33c6f8 | 2020-06-19 16:40:47 +0000 | [diff] [blame] | 66 | import static org.mockito.ArgumentMatchers.eq; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 67 | import static org.mockito.Mockito.any; |
| 68 | import static org.mockito.Mockito.anyInt; |
| 69 | import static org.mockito.Mockito.anyLong; |
| 70 | import static org.mockito.Mockito.doAnswer; |
| 71 | import static org.mockito.Mockito.doCallRealMethod; |
| 72 | import static org.mockito.Mockito.doNothing; |
| 73 | import static org.mockito.Mockito.doReturn; |
| 74 | import static org.mockito.Mockito.mock; |
| 75 | import static org.mockito.Mockito.spy; |
| 76 | |
| 77 | import android.app.IApplicationThread; |
| 78 | import android.app.IServiceConnection; |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 79 | import android.content.ComponentName; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 80 | import android.content.Context; |
| 81 | import android.content.pm.ApplicationInfo; |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 82 | import android.content.pm.PackageManagerInternal; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 83 | import android.content.pm.ServiceInfo; |
| 84 | import android.os.Build; |
| 85 | import android.os.IBinder; |
| 86 | import android.os.PowerManagerInternal; |
| 87 | import android.os.SystemClock; |
Sudheer Shanka | da0ee7f | 2019-10-01 17:05:31 -0700 | [diff] [blame] | 88 | import android.platform.test.annotations.Presubmit; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 89 | import android.util.ArrayMap; |
| 90 | import android.util.ArraySet; |
| 91 | import android.util.SparseArray; |
| 92 | |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 93 | import com.android.server.LocalServices; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 94 | import com.android.server.wm.ActivityServiceConnectionsHolder; |
| 95 | import com.android.server.wm.ActivityTaskManagerService; |
| 96 | import com.android.server.wm.WindowProcessController; |
| 97 | |
Jing Ji | 04a3cfb | 2020-03-14 00:40:49 -0700 | [diff] [blame] | 98 | import org.junit.AfterClass; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 99 | import org.junit.BeforeClass; |
| 100 | import org.junit.Test; |
| 101 | |
| 102 | import java.lang.reflect.Field; |
| 103 | import java.lang.reflect.Modifier; |
| 104 | import java.util.ArrayList; |
| 105 | |
| 106 | /** |
| 107 | * Test class for {@link OomAdjuster}. |
| 108 | * |
| 109 | * Build/Install/Run: |
| 110 | * atest MockingOomAdjusterTests |
| 111 | */ |
Sudheer Shanka | da0ee7f | 2019-10-01 17:05:31 -0700 | [diff] [blame] | 112 | @Presubmit |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 113 | public class MockingOomAdjusterTests { |
| 114 | private static final int MOCKAPP_PID = 12345; |
| 115 | private static final int MOCKAPP_UID = 12345; |
| 116 | private static final String MOCKAPP_PROCESSNAME = "test #1"; |
| 117 | private static final String MOCKAPP_PACKAGENAME = "com.android.test.test1"; |
| 118 | private static final int MOCKAPP2_PID = MOCKAPP_PID + 1; |
| 119 | private static final int MOCKAPP2_UID = MOCKAPP_UID + 1; |
| 120 | private static final String MOCKAPP2_PROCESSNAME = "test #2"; |
| 121 | private static final String MOCKAPP2_PACKAGENAME = "com.android.test.test2"; |
| 122 | private static final int MOCKAPP3_PID = MOCKAPP_PID + 2; |
| 123 | private static final int MOCKAPP3_UID = MOCKAPP_UID + 2; |
| 124 | private static final String MOCKAPP3_PROCESSNAME = "test #3"; |
| 125 | private static final String MOCKAPP3_PACKAGENAME = "com.android.test.test3"; |
| 126 | private static final int MOCKAPP4_PID = MOCKAPP_PID + 3; |
| 127 | private static final int MOCKAPP4_UID = MOCKAPP_UID + 3; |
| 128 | private static final String MOCKAPP4_PROCESSNAME = "test #4"; |
| 129 | private static final String MOCKAPP4_PACKAGENAME = "com.android.test.test4"; |
| 130 | private static final int MOCKAPP5_PID = MOCKAPP_PID + 4; |
| 131 | private static final int MOCKAPP5_UID = MOCKAPP_UID + 4; |
| 132 | private static final String MOCKAPP5_PROCESSNAME = "test #5"; |
| 133 | private static final String MOCKAPP5_PACKAGENAME = "com.android.test.test5"; |
| 134 | private static Context sContext; |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 135 | private static PackageManagerInternal sPackageManagerInternal; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 136 | private static ActivityManagerService sService; |
| 137 | |
| 138 | @BeforeClass |
| 139 | public static void setUpOnce() { |
| 140 | sContext = getInstrumentation().getTargetContext(); |
| 141 | System.setProperty("dexmaker.share_classloader", "true"); |
| 142 | |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 143 | sPackageManagerInternal = mock(PackageManagerInternal.class); |
| 144 | doReturn(new ComponentName("", "")).when(sPackageManagerInternal) |
| 145 | .getSystemUiServiceComponent(); |
Jing Ji | 04a3cfb | 2020-03-14 00:40:49 -0700 | [diff] [blame] | 146 | // Remove stale instance of PackageManagerInternal if there is any |
| 147 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 148 | LocalServices.addService(PackageManagerInternal.class, sPackageManagerInternal); |
| 149 | |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 150 | sService = mock(ActivityManagerService.class); |
| 151 | sService.mActivityTaskManager = new ActivityTaskManagerService(sContext); |
| 152 | sService.mActivityTaskManager.initialize(null, null, sContext.getMainLooper()); |
Darryl L Johnson | a098222 | 2020-02-18 18:21:51 -0800 | [diff] [blame] | 153 | sService.mPackageManagerInt = sPackageManagerInternal; |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 154 | sService.mAtmInternal = spy(sService.mActivityTaskManager.getAtmInternal()); |
| 155 | |
| 156 | sService.mConstants = new ActivityManagerConstants(sContext, sService, |
| 157 | sContext.getMainThreadHandler()); |
Jing Ji | 9b20436 | 2020-03-12 11:42:34 -0700 | [diff] [blame] | 158 | setFieldValue(ActivityManagerService.class, sService, "mContext", |
| 159 | sContext); |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 160 | ProcessList pr = new ProcessList(); |
atrost | 5ae996f | 2019-12-11 18:32:48 +0000 | [diff] [blame] | 161 | pr.init(sService, new ActiveUids(sService, false), null); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 162 | setFieldValue(ActivityManagerService.class, sService, "mProcessList", |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 163 | pr); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 164 | setFieldValue(ActivityManagerService.class, sService, "mHandler", |
| 165 | mock(ActivityManagerService.MainHandler.class)); |
| 166 | setFieldValue(ActivityManagerService.class, sService, "mProcessStats", |
| 167 | mock(ProcessStatsService.class)); |
| 168 | setFieldValue(ActivityManagerService.class, sService, "mBackupTargets", |
| 169 | mock(SparseArray.class)); |
| 170 | setFieldValue(ActivityManagerService.class, sService, "mOomAdjProfiler", |
| 171 | mock(OomAdjProfiler.class)); |
| 172 | doReturn(new ActivityManagerService.ProcessChangeItem()).when(sService) |
| 173 | .enqueueProcessChangeItemLocked(anyInt(), anyInt()); |
Tim Murray | d33c6f8 | 2020-06-19 16:40:47 +0000 | [diff] [blame] | 174 | doReturn(true).when(sService).containsTopUiOrRunningRemoteAnimOrEmptyLocked(any()); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 175 | sService.mOomAdjuster = new OomAdjuster(sService, sService.mProcessList, |
| 176 | mock(ActiveUids.class)); |
| 177 | sService.mOomAdjuster.mAdjSeq = 10000; |
| 178 | } |
| 179 | |
Jing Ji | 04a3cfb | 2020-03-14 00:40:49 -0700 | [diff] [blame] | 180 | @AfterClass |
| 181 | public static void tearDownOnce() { |
| 182 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 183 | } |
| 184 | |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 185 | private static <T> void setFieldValue(Class clazz, Object obj, String fieldName, T val) { |
| 186 | try { |
| 187 | Field field = clazz.getDeclaredField(fieldName); |
| 188 | field.setAccessible(true); |
| 189 | Field mfield = Field.class.getDeclaredField("accessFlags"); |
| 190 | mfield.setAccessible(true); |
| 191 | mfield.setInt(field, mfield.getInt(field) & ~(Modifier.FINAL | Modifier.PRIVATE)); |
| 192 | field.set(obj, val); |
| 193 | } catch (NoSuchFieldException | IllegalAccessException e) { |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | @SuppressWarnings("GuardedBy") |
| 198 | @Test |
| 199 | public void testUpdateOomAdj_DoOne_Persistent_TopUi_Sleeping() { |
| 200 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 201 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 202 | app.maxAdj = PERSISTENT_PROC_ADJ; |
| 203 | app.setHasTopUi(true); |
| 204 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_ASLEEP; |
| 205 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 206 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 207 | |
| 208 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERSISTENT_PROC_ADJ, |
| 209 | SCHED_GROUP_RESTRICTED); |
| 210 | } |
| 211 | |
| 212 | @SuppressWarnings("GuardedBy") |
| 213 | @Test |
| 214 | public void testUpdateOomAdj_DoOne_Persistent_TopUi_Awake() { |
| 215 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 216 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 217 | app.maxAdj = PERSISTENT_PROC_ADJ; |
| 218 | app.setHasTopUi(true); |
| 219 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 220 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 221 | |
| 222 | assertProcStates(app, PROCESS_STATE_PERSISTENT_UI, PERSISTENT_PROC_ADJ, |
| 223 | SCHED_GROUP_TOP_APP); |
| 224 | } |
| 225 | |
| 226 | @SuppressWarnings("GuardedBy") |
| 227 | @Test |
| 228 | public void testUpdateOomAdj_DoOne_Persistent_TopApp() { |
| 229 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 230 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 231 | app.maxAdj = PERSISTENT_PROC_ADJ; |
| 232 | doReturn(app).when(sService).getTopAppLocked(); |
| 233 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 234 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 235 | doReturn(null).when(sService).getTopAppLocked(); |
| 236 | |
| 237 | assertProcStates(app, PROCESS_STATE_PERSISTENT_UI, PERSISTENT_PROC_ADJ, |
| 238 | SCHED_GROUP_TOP_APP); |
| 239 | } |
| 240 | |
| 241 | @SuppressWarnings("GuardedBy") |
| 242 | @Test |
| 243 | public void testUpdateOomAdj_DoOne_TopApp_Awake() { |
| 244 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 245 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 246 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 247 | doReturn(app).when(sService).getTopAppLocked(); |
| 248 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 249 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 250 | doReturn(null).when(sService).getTopAppLocked(); |
| 251 | |
| 252 | assertProcStates(app, PROCESS_STATE_TOP, FOREGROUND_APP_ADJ, SCHED_GROUP_TOP_APP); |
| 253 | } |
| 254 | |
| 255 | @SuppressWarnings("GuardedBy") |
| 256 | @Test |
| 257 | public void testUpdateOomAdj_DoOne_RunningAnimations() { |
| 258 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 259 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 260 | doReturn(PROCESS_STATE_TOP_SLEEPING).when(sService.mAtmInternal).getTopProcessState(); |
| 261 | app.runningRemoteAnimation = true; |
| 262 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 263 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 264 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 265 | |
| 266 | assertProcStates(app, PROCESS_STATE_TOP_SLEEPING, VISIBLE_APP_ADJ, SCHED_GROUP_TOP_APP); |
| 267 | } |
| 268 | |
| 269 | @SuppressWarnings("GuardedBy") |
| 270 | @Test |
Tim Murray | d33c6f8 | 2020-06-19 16:40:47 +0000 | [diff] [blame] | 271 | public void testUpdateOomAdj_DoOne_TopApp_PreemptedByTopUi() { |
| 272 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 273 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 274 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 275 | doReturn(app).when(sService).getTopAppLocked(); |
| 276 | doReturn(false).when(sService).containsTopUiOrRunningRemoteAnimOrEmptyLocked(eq(app)); |
| 277 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 278 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 279 | doReturn(null).when(sService).getTopAppLocked(); |
| 280 | |
| 281 | assertProcStates(app, PROCESS_STATE_TOP, FOREGROUND_APP_ADJ, SCHED_GROUP_DEFAULT); |
| 282 | } |
| 283 | |
| 284 | @SuppressWarnings("GuardedBy") |
| 285 | @Test |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 286 | public void testUpdateOomAdj_DoOne_RunningInstrumentation() { |
| 287 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 288 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 289 | doReturn(mock(ActiveInstrumentation.class)).when(app).getActiveInstrumentation(); |
| 290 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 291 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 292 | doCallRealMethod().when(app).getActiveInstrumentation(); |
| 293 | |
| 294 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, FOREGROUND_APP_ADJ, |
| 295 | SCHED_GROUP_DEFAULT); |
| 296 | } |
| 297 | |
| 298 | @SuppressWarnings("GuardedBy") |
| 299 | @Test |
| 300 | public void testUpdateOomAdj_DoOne_ReceivingBroadcast() { |
| 301 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 302 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 303 | doReturn(true).when(sService).isReceivingBroadcastLocked(any(ProcessRecord.class), |
| 304 | any(ArraySet.class)); |
| 305 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 306 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 307 | doReturn(false).when(sService).isReceivingBroadcastLocked(any(ProcessRecord.class), |
| 308 | any(ArraySet.class)); |
| 309 | |
| 310 | assertProcStates(app, PROCESS_STATE_RECEIVER, FOREGROUND_APP_ADJ, SCHED_GROUP_BACKGROUND); |
| 311 | } |
| 312 | |
| 313 | @SuppressWarnings("GuardedBy") |
| 314 | @Test |
| 315 | public void testUpdateOomAdj_DoOne_ExecutingService() { |
| 316 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 317 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 318 | app.executingServices.add(mock(ServiceRecord.class)); |
| 319 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 320 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 321 | |
| 322 | assertProcStates(app, PROCESS_STATE_SERVICE, FOREGROUND_APP_ADJ, SCHED_GROUP_BACKGROUND); |
| 323 | } |
| 324 | |
| 325 | @SuppressWarnings("GuardedBy") |
| 326 | @Test |
| 327 | public void testUpdateOomAdj_DoOne_TopApp_Sleeping() { |
| 328 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 329 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 330 | doReturn(PROCESS_STATE_TOP_SLEEPING).when(sService.mAtmInternal).getTopProcessState(); |
| 331 | doReturn(app).when(sService).getTopAppLocked(); |
| 332 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_ASLEEP; |
| 333 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 334 | doReturn(null).when(sService).getTopAppLocked(); |
| 335 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 336 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 337 | |
| 338 | assertProcStates(app, PROCESS_STATE_TOP_SLEEPING, FOREGROUND_APP_ADJ, |
| 339 | SCHED_GROUP_BACKGROUND); |
| 340 | } |
| 341 | |
| 342 | @SuppressWarnings("GuardedBy") |
| 343 | @Test |
| 344 | public void testUpdateOomAdj_DoOne_CachedEmpty() { |
| 345 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 346 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 347 | app.setCurRawAdj(CACHED_APP_MIN_ADJ); |
| 348 | doReturn(null).when(sService).getTopAppLocked(); |
| 349 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 350 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 351 | |
| 352 | assertProcStates(app, PROCESS_STATE_CACHED_EMPTY, CACHED_APP_MIN_ADJ, |
| 353 | SCHED_GROUP_BACKGROUND); |
| 354 | } |
| 355 | |
| 356 | @SuppressWarnings("GuardedBy") |
| 357 | @Test |
| 358 | public void testUpdateOomAdj_DoOne_VisibleActivities() { |
| 359 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 360 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 361 | doReturn(mock(WindowProcessController.class)).when(app).getWindowProcessController(); |
| 362 | WindowProcessController wpc = app.getWindowProcessController(); |
| 363 | doReturn(true).when(wpc).hasActivities(); |
| 364 | doAnswer(answer((minTaskLayer, callback) -> { |
| 365 | Field field = callback.getClass().getDeclaredField("adj"); |
| 366 | field.set(callback, VISIBLE_APP_ADJ); |
| 367 | field = callback.getClass().getDeclaredField("foregroundActivities"); |
| 368 | field.set(callback, true); |
| 369 | field = callback.getClass().getDeclaredField("procState"); |
| 370 | field.set(callback, PROCESS_STATE_TOP); |
| 371 | field = callback.getClass().getDeclaredField("schedGroup"); |
| 372 | field.set(callback, SCHED_GROUP_TOP_APP); |
| 373 | return 0; |
| 374 | })).when(wpc).computeOomAdjFromActivities(anyInt(), |
| 375 | any(WindowProcessController.ComputeOomAdjCallback.class)); |
| 376 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 377 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 378 | doCallRealMethod().when(app).getWindowProcessController(); |
| 379 | |
| 380 | assertProcStates(app, PROCESS_STATE_TOP, VISIBLE_APP_ADJ, SCHED_GROUP_TOP_APP); |
| 381 | } |
| 382 | |
| 383 | @SuppressWarnings("GuardedBy") |
| 384 | @Test |
| 385 | public void testUpdateOomAdj_DoOne_RecentTasks() { |
| 386 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 387 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 388 | doReturn(mock(WindowProcessController.class)).when(app).getWindowProcessController(); |
| 389 | WindowProcessController wpc = app.getWindowProcessController(); |
| 390 | doReturn(true).when(wpc).hasRecentTasks(); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 391 | app.lastTopTime = SystemClock.uptimeMillis(); |
| 392 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 393 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 394 | doCallRealMethod().when(wpc).hasRecentTasks(); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 395 | |
| 396 | assertEquals(PROCESS_STATE_CACHED_RECENT, app.setProcState); |
| 397 | } |
| 398 | |
| 399 | @SuppressWarnings("GuardedBy") |
| 400 | @Test |
| 401 | public void testUpdateOomAdj_DoOne_FgServiceLocation() { |
| 402 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 403 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 404 | app.setHasForegroundServices(true, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); |
| 405 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 406 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 407 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 408 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 409 | SCHED_GROUP_DEFAULT); |
| 410 | } |
| 411 | |
| 412 | @SuppressWarnings("GuardedBy") |
| 413 | @Test |
| 414 | public void testUpdateOomAdj_DoOne_FgService() { |
| 415 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 416 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 417 | app.setHasForegroundServices(true, 0); |
| 418 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 419 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 420 | |
| 421 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 422 | SCHED_GROUP_DEFAULT); |
| 423 | } |
| 424 | |
| 425 | @SuppressWarnings("GuardedBy") |
| 426 | @Test |
| 427 | public void testUpdateOomAdj_DoOne_OverlayUi() { |
| 428 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 429 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 430 | app.setHasOverlayUi(true); |
| 431 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 432 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 433 | |
| 434 | assertProcStates(app, PROCESS_STATE_IMPORTANT_FOREGROUND, PERCEPTIBLE_APP_ADJ, |
| 435 | SCHED_GROUP_DEFAULT); |
| 436 | } |
| 437 | |
| 438 | @SuppressWarnings("GuardedBy") |
| 439 | @Test |
| 440 | public void testUpdateOomAdj_DoOne_PerceptibleRecent() { |
| 441 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 442 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 443 | app.setHasForegroundServices(true, 0); |
| 444 | app.lastTopTime = SystemClock.uptimeMillis(); |
| 445 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 446 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 447 | |
| 448 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, |
| 449 | PERCEPTIBLE_RECENT_FOREGROUND_APP_ADJ, SCHED_GROUP_DEFAULT); |
| 450 | } |
| 451 | |
| 452 | @SuppressWarnings("GuardedBy") |
| 453 | @Test |
| 454 | public void testUpdateOomAdj_DoOne_Toast() { |
| 455 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 456 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 457 | app.forcingToImportant = new Object(); |
| 458 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 459 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 460 | |
| 461 | assertProcStates(app, PROCESS_STATE_TRANSIENT_BACKGROUND, PERCEPTIBLE_APP_ADJ, |
| 462 | SCHED_GROUP_DEFAULT); |
| 463 | } |
| 464 | |
| 465 | @SuppressWarnings("GuardedBy") |
| 466 | @Test |
| 467 | public void testUpdateOomAdj_DoOne_HeavyWeight() { |
| 468 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 469 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 470 | doReturn(true).when(sService.mAtmInternal).isHeavyWeightProcess(any( |
| 471 | WindowProcessController.class)); |
| 472 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 473 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 474 | doReturn(false).when(sService.mAtmInternal).isHeavyWeightProcess(any( |
| 475 | WindowProcessController.class)); |
| 476 | |
| 477 | assertProcStates(app, PROCESS_STATE_HEAVY_WEIGHT, HEAVY_WEIGHT_APP_ADJ, |
| 478 | SCHED_GROUP_BACKGROUND); |
| 479 | } |
| 480 | |
| 481 | @SuppressWarnings("GuardedBy") |
| 482 | @Test |
| 483 | public void testUpdateOomAdj_DoOne_HomeApp() { |
| 484 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 485 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 486 | doReturn(mock(WindowProcessController.class)).when(app).getWindowProcessController(); |
| 487 | WindowProcessController wpc = app.getWindowProcessController(); |
| 488 | doReturn(true).when(wpc).isHomeProcess(); |
| 489 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 490 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 491 | |
| 492 | assertProcStates(app, PROCESS_STATE_HOME, HOME_APP_ADJ, SCHED_GROUP_BACKGROUND); |
| 493 | } |
| 494 | |
| 495 | @SuppressWarnings("GuardedBy") |
| 496 | @Test |
| 497 | public void testUpdateOomAdj_DoOne_PreviousApp() { |
| 498 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 499 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 500 | doReturn(mock(WindowProcessController.class)).when(app).getWindowProcessController(); |
| 501 | WindowProcessController wpc = app.getWindowProcessController(); |
| 502 | doReturn(true).when(wpc).isPreviousProcess(); |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 503 | doReturn(true).when(wpc).hasActivities(); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 504 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 505 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 506 | |
| 507 | assertProcStates(app, PROCESS_STATE_LAST_ACTIVITY, PREVIOUS_APP_ADJ, |
| 508 | SCHED_GROUP_BACKGROUND); |
| 509 | } |
| 510 | |
| 511 | @SuppressWarnings("GuardedBy") |
| 512 | @Test |
| 513 | public void testUpdateOomAdj_DoOne_Backup() { |
| 514 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 515 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 516 | BackupRecord backupTarget = new BackupRecord(null, 0, 0); |
| 517 | backupTarget.app = app; |
| 518 | doReturn(backupTarget).when(sService.mBackupTargets).get(anyInt()); |
| 519 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 520 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 521 | doReturn(null).when(sService.mBackupTargets).get(anyInt()); |
| 522 | |
| 523 | assertProcStates(app, PROCESS_STATE_TRANSIENT_BACKGROUND, BACKUP_APP_ADJ, |
| 524 | SCHED_GROUP_BACKGROUND); |
| 525 | } |
| 526 | |
| 527 | @SuppressWarnings("GuardedBy") |
| 528 | @Test |
| 529 | public void testUpdateOomAdj_DoOne_ClientActivities() { |
| 530 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 531 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 532 | doReturn(true).when(app).hasClientActivities(); |
| 533 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 534 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 535 | |
| 536 | assertEquals(PROCESS_STATE_CACHED_ACTIVITY_CLIENT, app.setProcState); |
| 537 | } |
| 538 | |
| 539 | @SuppressWarnings("GuardedBy") |
| 540 | @Test |
| 541 | public void testUpdateOomAdj_DoOne_TreatLikeActivity() { |
| 542 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 543 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 544 | app.treatLikeActivity = true; |
| 545 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 546 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 547 | |
| 548 | assertEquals(PROCESS_STATE_CACHED_ACTIVITY, app.setProcState); |
| 549 | } |
| 550 | |
| 551 | @SuppressWarnings("GuardedBy") |
| 552 | @Test |
| 553 | public void testUpdateOomAdj_DoOne_ServiceB() { |
| 554 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 555 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 556 | app.serviceb = true; |
| 557 | ServiceRecord s = mock(ServiceRecord.class); |
| 558 | doReturn(new ArrayMap<IBinder, ArrayList<ConnectionRecord>>()).when(s).getConnections(); |
| 559 | s.startRequested = true; |
| 560 | s.lastActivity = SystemClock.uptimeMillis(); |
Darryl L Johnson | d02044c | 2020-04-17 11:52:50 -0700 | [diff] [blame] | 561 | app.startService(s); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 562 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 563 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 564 | |
| 565 | assertProcStates(app, PROCESS_STATE_SERVICE, SERVICE_B_ADJ, SCHED_GROUP_BACKGROUND); |
| 566 | } |
| 567 | |
| 568 | @SuppressWarnings("GuardedBy") |
| 569 | @Test |
| 570 | public void testUpdateOomAdj_DoOne_MaxAdj() { |
| 571 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 572 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 573 | app.maxAdj = PERCEPTIBLE_LOW_APP_ADJ; |
| 574 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 575 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 576 | |
| 577 | assertProcStates(app, PROCESS_STATE_CACHED_EMPTY, PERCEPTIBLE_LOW_APP_ADJ, |
| 578 | SCHED_GROUP_DEFAULT); |
| 579 | } |
| 580 | |
| 581 | @SuppressWarnings("GuardedBy") |
| 582 | @Test |
Jing Ji | 057f4cb | 2019-11-01 13:23:11 -0700 | [diff] [blame] | 583 | public void testUpdateOomAdj_DoOne_NonCachedToCached() { |
| 584 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 585 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
Darryl L Johnson | 72aac10 | 2020-01-15 15:16:36 -0800 | [diff] [blame] | 586 | app.setCached(false); |
Jing Ji | 057f4cb | 2019-11-01 13:23:11 -0700 | [diff] [blame] | 587 | app.setCurRawAdj(SERVICE_ADJ); |
| 588 | doReturn(null).when(sService).getTopAppLocked(); |
| 589 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 590 | sService.mOomAdjuster.updateOomAdjLocked(app, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 591 | |
| 592 | assertTrue(ProcessList.CACHED_APP_MIN_ADJ <= app.setAdj); |
| 593 | assertTrue(ProcessList.CACHED_APP_MAX_ADJ >= app.setAdj); |
| 594 | } |
| 595 | |
| 596 | @SuppressWarnings("GuardedBy") |
| 597 | @Test |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 598 | public void testUpdateOomAdj_DoOne_Service_Started() { |
| 599 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 600 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 601 | ServiceRecord s = mock(ServiceRecord.class); |
| 602 | doReturn(new ArrayMap<IBinder, ArrayList<ConnectionRecord>>()).when(s).getConnections(); |
| 603 | s.startRequested = true; |
| 604 | s.lastActivity = SystemClock.uptimeMillis(); |
Darryl L Johnson | d02044c | 2020-04-17 11:52:50 -0700 | [diff] [blame] | 605 | app.startService(s); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 606 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 607 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 608 | |
| 609 | assertProcStates(app, PROCESS_STATE_SERVICE, SERVICE_ADJ, SCHED_GROUP_BACKGROUND); |
| 610 | } |
| 611 | |
| 612 | @SuppressWarnings("GuardedBy") |
| 613 | @Test |
| 614 | public void testUpdateOomAdj_DoOne_Service_Started_WaivePriority() { |
| 615 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 616 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 617 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 618 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 619 | ServiceRecord s = bindService(app, client, null, Context.BIND_WAIVE_PRIORITY, |
| 620 | mock(IBinder.class)); |
| 621 | s.startRequested = true; |
| 622 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 623 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 624 | doReturn(client).when(sService).getTopAppLocked(); |
| 625 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 626 | doReturn(null).when(sService).getTopAppLocked(); |
| 627 | |
| 628 | assertProcStates(app, PROCESS_STATE_SERVICE, UNKNOWN_ADJ, SCHED_GROUP_BACKGROUND); |
| 629 | } |
| 630 | |
| 631 | @SuppressWarnings("GuardedBy") |
| 632 | @Test |
| 633 | public void testUpdateOomAdj_DoOne_Service_Started_WaivePriority_TreatLikeActivity() { |
| 634 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 635 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 636 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 637 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 638 | bindService(app, client, null, Context.BIND_WAIVE_PRIORITY |
| 639 | | Context.BIND_TREAT_LIKE_ACTIVITY, mock(IBinder.class)); |
| 640 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 641 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 642 | |
| 643 | assertEquals(PROCESS_STATE_CACHED_ACTIVITY, app.setProcState); |
| 644 | } |
| 645 | |
| 646 | @SuppressWarnings("GuardedBy") |
| 647 | @Test |
| 648 | public void testUpdateOomAdj_DoOne_Service_Started_WaivePriority_AdjustWithActivity() { |
| 649 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 650 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 651 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 652 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 653 | IBinder binder = mock(IBinder.class); |
| 654 | ServiceRecord s = bindService(app, client, null, Context.BIND_WAIVE_PRIORITY |
| 655 | | Context.BIND_ADJUST_WITH_ACTIVITY | Context.BIND_IMPORTANT, binder); |
| 656 | ConnectionRecord cr = s.getConnections().get(binder).get(0); |
| 657 | setFieldValue(ConnectionRecord.class, cr, "activity", |
| 658 | mock(ActivityServiceConnectionsHolder.class)); |
| 659 | doReturn(true).when(cr.activity).isActivityVisible(); |
| 660 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 661 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 662 | |
| 663 | assertEquals(FOREGROUND_APP_ADJ, app.setAdj); |
| 664 | assertEquals(SCHED_GROUP_TOP_APP_BOUND, app.setSchedGroup); |
| 665 | } |
| 666 | |
| 667 | @SuppressWarnings("GuardedBy") |
| 668 | @Test |
| 669 | public void testUpdateOomAdj_DoOne_Service_Self() { |
| 670 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 671 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 672 | bindService(app, app, null, 0, mock(IBinder.class)); |
| 673 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 674 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 675 | |
| 676 | assertProcStates(app, PROCESS_STATE_CACHED_EMPTY, UNKNOWN_ADJ, SCHED_GROUP_BACKGROUND); |
| 677 | } |
| 678 | |
| 679 | @SuppressWarnings("GuardedBy") |
| 680 | @Test |
| 681 | public void testUpdateOomAdj_DoOne_Service_CachedActivity() { |
| 682 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 683 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 684 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 685 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 686 | client.treatLikeActivity = true; |
| 687 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 688 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 689 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 690 | |
| 691 | assertEquals(PROCESS_STATE_CACHED_EMPTY, app.setProcState); |
| 692 | } |
| 693 | |
| 694 | @SuppressWarnings("GuardedBy") |
| 695 | @Test |
| 696 | public void testUpdateOomAdj_DoOne_Service_AllowOomManagement() { |
| 697 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 698 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true)); |
| 699 | doReturn(mock(WindowProcessController.class)).when(app).getWindowProcessController(); |
| 700 | WindowProcessController wpc = app.getWindowProcessController(); |
| 701 | doReturn(false).when(wpc).isHomeProcess(); |
| 702 | doReturn(true).when(wpc).isPreviousProcess(); |
Jing Ji | 094c3ef | 2019-08-27 17:02:09 -0700 | [diff] [blame] | 703 | doReturn(true).when(wpc).hasActivities(); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 704 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 705 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 706 | bindService(app, client, null, Context.BIND_ALLOW_OOM_MANAGEMENT, mock(IBinder.class)); |
| 707 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 708 | doReturn(client).when(sService).getTopAppLocked(); |
| 709 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 710 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 711 | doReturn(null).when(sService).getTopAppLocked(); |
| 712 | |
| 713 | assertEquals(PREVIOUS_APP_ADJ, app.setAdj); |
| 714 | } |
| 715 | |
| 716 | @SuppressWarnings("GuardedBy") |
| 717 | @Test |
| 718 | public void testUpdateOomAdj_DoOne_Service_BoundByPersistentService() { |
| 719 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 720 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 721 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 722 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 723 | bindService(app, client, null, Context.BIND_FOREGROUND_SERVICE, mock(IBinder.class)); |
| 724 | client.maxAdj = PERSISTENT_PROC_ADJ; |
| 725 | client.setHasTopUi(true); |
| 726 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 727 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 728 | |
| 729 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, VISIBLE_APP_ADJ, |
| 730 | SCHED_GROUP_DEFAULT); |
| 731 | } |
| 732 | |
| 733 | @SuppressWarnings("GuardedBy") |
| 734 | @Test |
| 735 | public void testUpdateOomAdj_DoOne_Service_Bound_ImportantFg() { |
| 736 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 737 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 738 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 739 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 740 | bindService(app, client, null, Context.BIND_IMPORTANT, mock(IBinder.class)); |
| 741 | client.executingServices.add(mock(ServiceRecord.class)); |
| 742 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 743 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 744 | |
| 745 | assertEquals(FOREGROUND_APP_ADJ, app.setAdj); |
| 746 | } |
| 747 | |
| 748 | @SuppressWarnings("GuardedBy") |
| 749 | @Test |
| 750 | public void testUpdateOomAdj_DoOne_Service_BoundByTop() { |
| 751 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 752 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 753 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 754 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 755 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 756 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 757 | doReturn(client).when(sService).getTopAppLocked(); |
| 758 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 759 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 760 | doReturn(null).when(sService).getTopAppLocked(); |
| 761 | |
| 762 | assertProcStates(app, PROCESS_STATE_BOUND_TOP, VISIBLE_APP_ADJ, SCHED_GROUP_DEFAULT); |
| 763 | } |
| 764 | |
| 765 | @SuppressWarnings("GuardedBy") |
| 766 | @Test |
| 767 | public void testUpdateOomAdj_DoOne_Service_BoundFgService() { |
| 768 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 769 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 770 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 771 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 772 | bindService(app, client, null, Context.BIND_FOREGROUND_SERVICE, mock(IBinder.class)); |
| 773 | client.maxAdj = PERSISTENT_PROC_ADJ; |
| 774 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 775 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 776 | |
| 777 | assertEquals(PROCESS_STATE_BOUND_FOREGROUND_SERVICE, app.setProcState); |
| 778 | } |
| 779 | |
| 780 | @SuppressWarnings("GuardedBy") |
| 781 | @Test |
| 782 | public void testUpdateOomAdj_DoOne_Service_BoundNotForeground() { |
| 783 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 784 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 785 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 786 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 787 | bindService(app, client, null, Context.BIND_NOT_FOREGROUND, mock(IBinder.class)); |
| 788 | client.maxAdj = PERSISTENT_PROC_ADJ; |
| 789 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 790 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 791 | |
| 792 | assertEquals(PROCESS_STATE_TRANSIENT_BACKGROUND, app.setProcState); |
| 793 | } |
| 794 | |
| 795 | @SuppressWarnings("GuardedBy") |
| 796 | @Test |
| 797 | public void testUpdateOomAdj_DoOne_Service_ImportantFgService() { |
| 798 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 799 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 800 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 801 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 802 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 803 | client.setHasForegroundServices(true, 0); |
| 804 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 805 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 806 | |
| 807 | assertEquals(PROCESS_STATE_FOREGROUND_SERVICE, app.setProcState); |
| 808 | } |
| 809 | |
| 810 | @SuppressWarnings("GuardedBy") |
| 811 | @Test |
| 812 | public void testUpdateOomAdj_DoOne_Service_BoundByBackup_AboveClient() { |
| 813 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 814 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 815 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 816 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 817 | bindService(app, client, null, Context.BIND_ABOVE_CLIENT, mock(IBinder.class)); |
| 818 | BackupRecord backupTarget = new BackupRecord(null, 0, 0); |
| 819 | backupTarget.app = client; |
| 820 | doReturn(backupTarget).when(sService.mBackupTargets).get(anyInt()); |
| 821 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 822 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 823 | doReturn(null).when(sService.mBackupTargets).get(anyInt()); |
| 824 | |
| 825 | assertEquals(BACKUP_APP_ADJ, app.setAdj); |
| 826 | |
| 827 | client.maxAdj = PERSISTENT_PROC_ADJ; |
| 828 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 829 | |
| 830 | assertEquals(PERSISTENT_SERVICE_ADJ, app.setAdj); |
| 831 | } |
| 832 | |
| 833 | @SuppressWarnings("GuardedBy") |
| 834 | @Test |
| 835 | public void testUpdateOomAdj_DoOne_Service_NotPerceptible() { |
| 836 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 837 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 838 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 839 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 840 | bindService(app, client, null, Context.BIND_NOT_PERCEPTIBLE, mock(IBinder.class)); |
| 841 | client.runningRemoteAnimation = true; |
| 842 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 843 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 844 | |
| 845 | assertEquals(PERCEPTIBLE_LOW_APP_ADJ, app.setAdj); |
| 846 | } |
| 847 | |
| 848 | @SuppressWarnings("GuardedBy") |
| 849 | @Test |
| 850 | public void testUpdateOomAdj_DoOne_Service_NotVisible() { |
| 851 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 852 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 853 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 854 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 855 | bindService(app, client, null, Context.BIND_NOT_VISIBLE, mock(IBinder.class)); |
| 856 | client.runningRemoteAnimation = true; |
| 857 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 858 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 859 | |
| 860 | assertEquals(PERCEPTIBLE_APP_ADJ, app.setAdj); |
| 861 | } |
| 862 | |
| 863 | @SuppressWarnings("GuardedBy") |
| 864 | @Test |
| 865 | public void testUpdateOomAdj_DoOne_Service_Perceptible() { |
| 866 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 867 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 868 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 869 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 870 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 871 | client.setHasOverlayUi(true); |
| 872 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 873 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 874 | |
| 875 | assertEquals(PERCEPTIBLE_APP_ADJ, app.setAdj); |
| 876 | } |
| 877 | |
| 878 | @SuppressWarnings("GuardedBy") |
| 879 | @Test |
| 880 | public void testUpdateOomAdj_DoOne_Service_Other() { |
| 881 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 882 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 883 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 884 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 885 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 886 | client.runningRemoteAnimation = true; |
| 887 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 888 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 889 | |
| 890 | assertEquals(VISIBLE_APP_ADJ, app.setAdj); |
| 891 | } |
| 892 | |
| 893 | @SuppressWarnings("GuardedBy") |
| 894 | @Test |
| 895 | public void testUpdateOomAdj_DoOne_Service_Bind_ImportantBg() { |
| 896 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 897 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 898 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 899 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 900 | bindService(app, client, null, Context.BIND_IMPORTANT_BACKGROUND, mock(IBinder.class)); |
| 901 | client.setHasOverlayUi(true); |
| 902 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 903 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 904 | |
| 905 | assertEquals(PROCESS_STATE_IMPORTANT_BACKGROUND, app.setProcState); |
| 906 | } |
| 907 | |
| 908 | @SuppressWarnings("GuardedBy") |
| 909 | @Test |
| 910 | public void testUpdateOomAdj_DoOne_Provider_Self() { |
| 911 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 912 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 913 | bindProvider(app, app, null, null, false); |
| 914 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 915 | |
| 916 | assertProcStates(app, PROCESS_STATE_CACHED_EMPTY, UNKNOWN_ADJ, SCHED_GROUP_BACKGROUND); |
| 917 | } |
| 918 | |
| 919 | @SuppressWarnings("GuardedBy") |
| 920 | @Test |
| 921 | public void testUpdateOomAdj_DoOne_Provider_Cached_Activity() { |
| 922 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 923 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 924 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 925 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 926 | bindProvider(app, client, null, null, false); |
| 927 | client.treatLikeActivity = true; |
| 928 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 929 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 930 | |
| 931 | assertProcStates(app, PROCESS_STATE_CACHED_EMPTY, UNKNOWN_ADJ, SCHED_GROUP_BACKGROUND); |
| 932 | } |
| 933 | |
| 934 | @SuppressWarnings("GuardedBy") |
| 935 | @Test |
| 936 | public void testUpdateOomAdj_DoOne_Provider_TopApp() { |
| 937 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 938 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 939 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 940 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 941 | bindProvider(app, client, null, null, false); |
| 942 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 943 | doReturn(client).when(sService).getTopAppLocked(); |
| 944 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 945 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 946 | doReturn(null).when(sService).getTopAppLocked(); |
| 947 | |
| 948 | assertProcStates(app, PROCESS_STATE_BOUND_TOP, FOREGROUND_APP_ADJ, SCHED_GROUP_DEFAULT); |
| 949 | } |
| 950 | |
| 951 | @SuppressWarnings("GuardedBy") |
| 952 | @Test |
| 953 | public void testUpdateOomAdj_DoOne_Provider_FgService() { |
| 954 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 955 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 956 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 957 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 958 | client.setHasForegroundServices(true, 0); |
| 959 | bindProvider(app, client, null, null, false); |
| 960 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 961 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 962 | |
| 963 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 964 | SCHED_GROUP_DEFAULT); |
| 965 | } |
| 966 | |
| 967 | @SuppressWarnings("GuardedBy") |
| 968 | @Test |
| 969 | public void testUpdateOomAdj_DoOne_Provider_ExternalProcessHandles() { |
| 970 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 971 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 972 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 973 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 974 | bindProvider(app, client, null, null, true); |
| 975 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 976 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 977 | |
| 978 | assertProcStates(app, PROCESS_STATE_IMPORTANT_FOREGROUND, FOREGROUND_APP_ADJ, |
| 979 | SCHED_GROUP_DEFAULT); |
| 980 | } |
| 981 | |
| 982 | @SuppressWarnings("GuardedBy") |
| 983 | @Test |
| 984 | public void testUpdateOomAdj_DoOne_Provider_Retention() { |
| 985 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 986 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 987 | app.lastProviderTime = SystemClock.uptimeMillis(); |
| 988 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 989 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 990 | |
| 991 | assertProcStates(app, PROCESS_STATE_LAST_ACTIVITY, PREVIOUS_APP_ADJ, |
| 992 | SCHED_GROUP_BACKGROUND); |
| 993 | } |
| 994 | |
| 995 | @SuppressWarnings("GuardedBy") |
| 996 | @Test |
| 997 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByTop() { |
| 998 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 999 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1000 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1001 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1002 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1003 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1004 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1005 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1006 | doReturn(PROCESS_STATE_TOP).when(sService.mAtmInternal).getTopProcessState(); |
| 1007 | doReturn(client2).when(sService).getTopAppLocked(); |
| 1008 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1009 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1010 | doReturn(null).when(sService).getTopAppLocked(); |
| 1011 | |
Hui Yu | 814cedf | 2020-03-11 15:31:46 -0700 | [diff] [blame] | 1012 | assertProcStates(app, PROCESS_STATE_BOUND_TOP, VISIBLE_APP_ADJ, |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1013 | SCHED_GROUP_DEFAULT); |
| 1014 | } |
| 1015 | |
| 1016 | @SuppressWarnings("GuardedBy") |
| 1017 | @Test |
| 1018 | public void testUpdateOomAdj_DoOne_Service_BoundByFgService_Branch() { |
| 1019 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1020 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1021 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1022 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1023 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1024 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1025 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1026 | bindService(app, client2, null, 0, mock(IBinder.class)); |
| 1027 | client2.setHasForegroundServices(true, 0); |
| 1028 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1029 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1030 | |
| 1031 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1032 | SCHED_GROUP_DEFAULT); |
| 1033 | } |
| 1034 | |
| 1035 | @SuppressWarnings("GuardedBy") |
| 1036 | @Test |
| 1037 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService() { |
| 1038 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1039 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1040 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1041 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1042 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1043 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1044 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1045 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1046 | client2.setHasForegroundServices(true, 0); |
| 1047 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1048 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1049 | |
| 1050 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1051 | SCHED_GROUP_DEFAULT); |
| 1052 | } |
| 1053 | |
| 1054 | @SuppressWarnings("GuardedBy") |
| 1055 | @Test |
| 1056 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService_Cycle() { |
| 1057 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1058 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1059 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1060 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1061 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1062 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1063 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1064 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1065 | client2.setHasForegroundServices(true, 0); |
| 1066 | bindService(client2, app, null, 0, mock(IBinder.class)); |
Jing Ji | 619d644 | 2019-11-21 18:26:40 -0800 | [diff] [blame] | 1067 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1068 | lru.clear(); |
| 1069 | lru.add(app); |
| 1070 | lru.add(client); |
| 1071 | lru.add(client2); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1072 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
Jing Ji | 619d644 | 2019-11-21 18:26:40 -0800 | [diff] [blame] | 1073 | sService.mOomAdjuster.updateOomAdjLocked(app, true, OomAdjuster.OOM_ADJ_REASON_NONE); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1074 | |
| 1075 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1076 | SCHED_GROUP_DEFAULT); |
Jing Ji | 619d644 | 2019-11-21 18:26:40 -0800 | [diff] [blame] | 1077 | assertProcStates(client, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1078 | SCHED_GROUP_DEFAULT); |
| 1079 | assertProcStates(client2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1080 | SCHED_GROUP_DEFAULT); |
| 1081 | |
| 1082 | client2.setHasForegroundServices(false, 0); |
| 1083 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1084 | sService.mOomAdjuster.updateOomAdjLocked(client2, true, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1085 | |
| 1086 | assertEquals(PROCESS_STATE_CACHED_EMPTY, client2.setProcState); |
| 1087 | assertEquals(PROCESS_STATE_CACHED_EMPTY, client.setProcState); |
| 1088 | assertEquals(PROCESS_STATE_CACHED_EMPTY, app.setProcState); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | @SuppressWarnings("GuardedBy") |
| 1092 | @Test |
Jing Ji | 6708c41 | 2019-12-04 22:33:30 -0800 | [diff] [blame] | 1093 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService_Cycle_2() { |
| 1094 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1095 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1096 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1097 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1098 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1099 | bindService(client, app, null, 0, mock(IBinder.class)); |
| 1100 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1101 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1102 | bindService(client2, client, null, 0, mock(IBinder.class)); |
| 1103 | client.setHasForegroundServices(true, 0); |
| 1104 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1105 | lru.clear(); |
| 1106 | lru.add(app); |
| 1107 | lru.add(client); |
| 1108 | lru.add(client2); |
| 1109 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1110 | sService.mOomAdjuster.updateOomAdjLocked(app, true, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1111 | |
| 1112 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1113 | SCHED_GROUP_DEFAULT); |
| 1114 | assertProcStates(client, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1115 | SCHED_GROUP_DEFAULT); |
| 1116 | assertProcStates(client2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1117 | SCHED_GROUP_DEFAULT); |
| 1118 | } |
| 1119 | |
| 1120 | @SuppressWarnings("GuardedBy") |
| 1121 | @Test |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1122 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService_Cycle_Branch() { |
| 1123 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1124 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1125 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1126 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1127 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1128 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1129 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1130 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1131 | client2.setHasForegroundServices(true, 0); |
| 1132 | bindService(client2, app, null, 0, mock(IBinder.class)); |
| 1133 | ProcessRecord client3 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1134 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1135 | client3.forcingToImportant = new Object(); |
| 1136 | bindService(app, client3, null, 0, mock(IBinder.class)); |
| 1137 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1138 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1139 | |
| 1140 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1141 | SCHED_GROUP_DEFAULT); |
| 1142 | } |
| 1143 | |
| 1144 | @SuppressWarnings("GuardedBy") |
| 1145 | @Test |
| 1146 | public void testUpdateOomAdj_DoOne_Service_Chain_Perceptible_Cycle_Branch() { |
| 1147 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1148 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1149 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1150 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1151 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1152 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1153 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1154 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1155 | bindService(client2, app, null, 0, mock(IBinder.class)); |
| 1156 | doReturn(mock(WindowProcessController.class)).when(client2).getWindowProcessController(); |
| 1157 | WindowProcessController wpc = client2.getWindowProcessController(); |
| 1158 | doReturn(true).when(wpc).isHomeProcess(); |
| 1159 | ProcessRecord client3 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1160 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1161 | client3.forcingToImportant = new Object(); |
| 1162 | bindService(app, client3, null, 0, mock(IBinder.class)); |
| 1163 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1164 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1165 | |
| 1166 | assertProcStates(app, PROCESS_STATE_TRANSIENT_BACKGROUND, PERCEPTIBLE_APP_ADJ, |
| 1167 | SCHED_GROUP_DEFAULT); |
| 1168 | } |
| 1169 | |
| 1170 | @SuppressWarnings("GuardedBy") |
| 1171 | @Test |
| 1172 | public void testUpdateOomAdj_DoOne_Service_Chain_Perceptible_Cycle_2() { |
| 1173 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1174 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1175 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1176 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1177 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1178 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1179 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1180 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1181 | bindService(client2, app, null, 0, mock(IBinder.class)); |
| 1182 | doReturn(mock(WindowProcessController.class)).when(client2).getWindowProcessController(); |
| 1183 | WindowProcessController wpc = client2.getWindowProcessController(); |
| 1184 | doReturn(true).when(wpc).isHomeProcess(); |
| 1185 | ProcessRecord client3 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1186 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1187 | ProcessRecord client4 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1188 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1189 | client4.forcingToImportant = new Object(); |
| 1190 | bindService(app, client4, null, 0, mock(IBinder.class)); |
| 1191 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1192 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1193 | |
| 1194 | assertProcStates(app, PROCESS_STATE_TRANSIENT_BACKGROUND, PERCEPTIBLE_APP_ADJ, |
| 1195 | SCHED_GROUP_DEFAULT); |
| 1196 | } |
| 1197 | |
| 1198 | @SuppressWarnings("GuardedBy") |
| 1199 | @Test |
| 1200 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService_Cycle_Branch_2() { |
| 1201 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1202 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1203 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1204 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1205 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1206 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1207 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1208 | bindService(client, client2, null, 0, mock(IBinder.class)); |
| 1209 | bindService(client2, app, null, 0, mock(IBinder.class)); |
| 1210 | doReturn(mock(WindowProcessController.class)).when(client2).getWindowProcessController(); |
| 1211 | WindowProcessController wpc = client2.getWindowProcessController(); |
| 1212 | doReturn(true).when(wpc).isHomeProcess(); |
| 1213 | ProcessRecord client3 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1214 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1215 | client3.forcingToImportant = new Object(); |
| 1216 | bindService(app, client3, null, 0, mock(IBinder.class)); |
| 1217 | ProcessRecord client4 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1218 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1219 | client4.setHasForegroundServices(true, 0); |
| 1220 | bindService(app, client4, null, 0, mock(IBinder.class)); |
| 1221 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1222 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1223 | |
| 1224 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1225 | SCHED_GROUP_DEFAULT); |
| 1226 | } |
| 1227 | |
| 1228 | @SuppressWarnings("GuardedBy") |
| 1229 | @Test |
| 1230 | public void testUpdateOomAdj_DoOne_Service_Chain_BoundByFgService_Branch_3() { |
| 1231 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1232 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1233 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1234 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1235 | doReturn(mock(WindowProcessController.class)).when(client).getWindowProcessController(); |
| 1236 | WindowProcessController wpc = client.getWindowProcessController(); |
| 1237 | doReturn(true).when(wpc).isHomeProcess(); |
| 1238 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1239 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1240 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1241 | bindService(app, client2, null, 0, mock(IBinder.class)); |
| 1242 | client2.setHasForegroundServices(true, 0); |
| 1243 | ProcessRecord client3 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1244 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1245 | client3.forcingToImportant = new Object(); |
| 1246 | bindService(app, client3, null, 0, mock(IBinder.class)); |
| 1247 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1248 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1249 | |
| 1250 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1251 | SCHED_GROUP_DEFAULT); |
| 1252 | } |
| 1253 | |
| 1254 | @SuppressWarnings("GuardedBy") |
| 1255 | @Test |
| 1256 | public void testUpdateOomAdj_DoOne_Service_Provider() { |
| 1257 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1258 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1259 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1260 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1261 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1262 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1263 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1264 | bindProvider(client, client2, null, null, false); |
| 1265 | client2.setHasForegroundServices(true, 0); |
| 1266 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1267 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1268 | |
| 1269 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1270 | SCHED_GROUP_DEFAULT); |
| 1271 | } |
| 1272 | |
| 1273 | @SuppressWarnings("GuardedBy") |
| 1274 | @Test |
| 1275 | public void testUpdateOomAdj_DoOne_Service_Provider_Cycle() { |
| 1276 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1277 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1278 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1279 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1280 | bindService(app, client, null, 0, mock(IBinder.class)); |
| 1281 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1282 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1283 | bindProvider(client, client2, null, null, false); |
| 1284 | client2.setHasForegroundServices(true, 0); |
| 1285 | bindService(client2, app, null, 0, mock(IBinder.class)); |
| 1286 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1287 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1288 | |
| 1289 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1290 | SCHED_GROUP_DEFAULT); |
| 1291 | } |
| 1292 | |
| 1293 | @SuppressWarnings("GuardedBy") |
| 1294 | @Test |
| 1295 | public void testUpdateOomAdj_DoOne_Provider_Chain_BoundByFgService() { |
| 1296 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1297 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1298 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1299 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1300 | bindProvider(app, client, null, null, false); |
| 1301 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1302 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1303 | bindProvider(client, client2, null, null, false); |
| 1304 | client2.setHasForegroundServices(true, 0); |
| 1305 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1306 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1307 | |
| 1308 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1309 | SCHED_GROUP_DEFAULT); |
| 1310 | } |
| 1311 | |
| 1312 | @SuppressWarnings("GuardedBy") |
| 1313 | @Test |
| 1314 | public void testUpdateOomAdj_DoOne_Provider_Chain_BoundByFgService_Cycle() { |
| 1315 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1316 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1317 | ProcessRecord client = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1318 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1319 | bindProvider(app, client, null, null, false); |
| 1320 | ProcessRecord client2 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1321 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1322 | bindProvider(client, client2, null, null, false); |
| 1323 | client2.setHasForegroundServices(true, 0); |
| 1324 | bindProvider(client2, app, null, null, false); |
| 1325 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1326 | sService.mOomAdjuster.updateOomAdjLocked(app, false, OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1327 | |
| 1328 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1329 | SCHED_GROUP_DEFAULT); |
| 1330 | } |
| 1331 | |
| 1332 | @SuppressWarnings("GuardedBy") |
| 1333 | @Test |
| 1334 | public void testUpdateOomAdj_DoAll_Unbound() { |
| 1335 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1336 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1337 | app.forcingToImportant = new Object(); |
| 1338 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1339 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1340 | app2.setHasForegroundServices(true, 0); |
| 1341 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1342 | lru.clear(); |
| 1343 | lru.add(app); |
| 1344 | lru.add(app2); |
| 1345 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1346 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1347 | lru.clear(); |
| 1348 | |
| 1349 | assertProcStates(app, PROCESS_STATE_TRANSIENT_BACKGROUND, PERCEPTIBLE_APP_ADJ, |
| 1350 | SCHED_GROUP_DEFAULT); |
| 1351 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1352 | SCHED_GROUP_DEFAULT); |
| 1353 | } |
| 1354 | |
| 1355 | @SuppressWarnings("GuardedBy") |
| 1356 | @Test |
| 1357 | public void testUpdateOomAdj_DoAll_BoundFgService() { |
| 1358 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1359 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1360 | app.forcingToImportant = new Object(); |
| 1361 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1362 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1363 | app2.setHasForegroundServices(true, 0); |
| 1364 | bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1365 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1366 | lru.clear(); |
| 1367 | lru.add(app); |
| 1368 | lru.add(app2); |
| 1369 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1370 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1371 | lru.clear(); |
| 1372 | |
| 1373 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1374 | SCHED_GROUP_DEFAULT); |
| 1375 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1376 | SCHED_GROUP_DEFAULT); |
| 1377 | } |
| 1378 | |
| 1379 | @SuppressWarnings("GuardedBy") |
| 1380 | @Test |
| 1381 | public void testUpdateOomAdj_DoAll_BoundFgService_Cycle() { |
| 1382 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1383 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1384 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1385 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1386 | bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1387 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1388 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1389 | bindService(app2, app3, null, 0, mock(IBinder.class)); |
| 1390 | app3.setHasForegroundServices(true, 0); |
| 1391 | bindService(app3, app, null, 0, mock(IBinder.class)); |
| 1392 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1393 | lru.clear(); |
| 1394 | lru.add(app); |
| 1395 | lru.add(app2); |
| 1396 | lru.add(app3); |
| 1397 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1398 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1399 | lru.clear(); |
| 1400 | |
| 1401 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1402 | SCHED_GROUP_DEFAULT); |
| 1403 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1404 | SCHED_GROUP_DEFAULT); |
| 1405 | assertProcStates(app3, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1406 | SCHED_GROUP_DEFAULT); |
Jing Ji | ea036c8 | 2020-05-28 21:45:21 -0700 | [diff] [blame] | 1407 | assertEquals("service", app.adjType); |
| 1408 | assertEquals("service", app2.adjType); |
| 1409 | assertEquals("fg-service", app3.adjType); |
| 1410 | assertEquals(false, app.isCached()); |
| 1411 | assertEquals(false, app2.isCached()); |
| 1412 | assertEquals(false, app3.isCached()); |
| 1413 | assertEquals(false, app.empty); |
| 1414 | assertEquals(false, app2.empty); |
| 1415 | assertEquals(false, app3.empty); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | @SuppressWarnings("GuardedBy") |
| 1419 | @Test |
| 1420 | public void testUpdateOomAdj_DoAll_BoundFgService_Cycle_Branch_2() { |
| 1421 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1422 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1423 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1424 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1425 | ServiceRecord s = bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1426 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1427 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1428 | bindService(app2, app3, null, 0, mock(IBinder.class)); |
| 1429 | bindService(app3, app, null, 0, mock(IBinder.class)); |
| 1430 | doReturn(mock(WindowProcessController.class)).when(app3).getWindowProcessController(); |
| 1431 | WindowProcessController wpc = app3.getWindowProcessController(); |
| 1432 | doReturn(true).when(wpc).isHomeProcess(); |
| 1433 | ProcessRecord app4 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1434 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1435 | app4.setHasOverlayUi(true); |
| 1436 | bindService(app, app4, s, 0, mock(IBinder.class)); |
| 1437 | ProcessRecord app5 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1438 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1439 | app5.setHasForegroundServices(true, 0); |
| 1440 | bindService(app, app5, s, 0, mock(IBinder.class)); |
| 1441 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1442 | lru.clear(); |
| 1443 | lru.add(app); |
| 1444 | lru.add(app2); |
| 1445 | lru.add(app3); |
| 1446 | lru.add(app4); |
| 1447 | lru.add(app5); |
| 1448 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1449 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1450 | lru.clear(); |
| 1451 | |
| 1452 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1453 | SCHED_GROUP_DEFAULT); |
| 1454 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1455 | SCHED_GROUP_DEFAULT); |
| 1456 | assertProcStates(app3, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1457 | SCHED_GROUP_DEFAULT); |
| 1458 | assertProcStates(app4, PROCESS_STATE_IMPORTANT_FOREGROUND, PERCEPTIBLE_APP_ADJ, |
| 1459 | SCHED_GROUP_DEFAULT); |
| 1460 | assertProcStates(app5, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1461 | SCHED_GROUP_DEFAULT); |
| 1462 | } |
| 1463 | |
| 1464 | @SuppressWarnings("GuardedBy") |
| 1465 | @Test |
| 1466 | public void testUpdateOomAdj_DoAll_BoundFgService_Cycle_Branch_3() { |
| 1467 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1468 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1469 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1470 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1471 | ServiceRecord s = bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1472 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1473 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1474 | bindService(app2, app3, null, 0, mock(IBinder.class)); |
| 1475 | bindService(app3, app, null, 0, mock(IBinder.class)); |
| 1476 | doReturn(mock(WindowProcessController.class)).when(app3).getWindowProcessController(); |
| 1477 | WindowProcessController wpc = app3.getWindowProcessController(); |
| 1478 | doReturn(true).when(wpc).isHomeProcess(); |
| 1479 | ProcessRecord app4 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1480 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1481 | app4.setHasOverlayUi(true); |
| 1482 | bindService(app, app4, s, 0, mock(IBinder.class)); |
| 1483 | ProcessRecord app5 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1484 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1485 | app5.setHasForegroundServices(true, 0); |
| 1486 | bindService(app, app5, s, 0, mock(IBinder.class)); |
| 1487 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1488 | lru.clear(); |
| 1489 | lru.add(app5); |
| 1490 | lru.add(app4); |
| 1491 | lru.add(app3); |
| 1492 | lru.add(app2); |
| 1493 | lru.add(app); |
| 1494 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1495 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1496 | lru.clear(); |
| 1497 | |
| 1498 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1499 | SCHED_GROUP_DEFAULT); |
| 1500 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1501 | SCHED_GROUP_DEFAULT); |
| 1502 | assertProcStates(app3, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1503 | SCHED_GROUP_DEFAULT); |
| 1504 | assertProcStates(app4, PROCESS_STATE_IMPORTANT_FOREGROUND, PERCEPTIBLE_APP_ADJ, |
| 1505 | SCHED_GROUP_DEFAULT); |
| 1506 | assertProcStates(app5, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1507 | SCHED_GROUP_DEFAULT); |
| 1508 | } |
| 1509 | |
| 1510 | @SuppressWarnings("GuardedBy") |
| 1511 | @Test |
| 1512 | public void testUpdateOomAdj_DoAll_BoundFgService_Cycle_Branch_4() { |
| 1513 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1514 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1515 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1516 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1517 | ServiceRecord s = bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1518 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1519 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1520 | bindService(app2, app3, null, 0, mock(IBinder.class)); |
| 1521 | bindService(app3, app, null, 0, mock(IBinder.class)); |
| 1522 | doReturn(mock(WindowProcessController.class)).when(app3).getWindowProcessController(); |
| 1523 | WindowProcessController wpc = app3.getWindowProcessController(); |
| 1524 | doReturn(true).when(wpc).isHomeProcess(); |
| 1525 | ProcessRecord app4 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1526 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1527 | app4.setHasOverlayUi(true); |
| 1528 | bindService(app, app4, s, 0, mock(IBinder.class)); |
| 1529 | ProcessRecord app5 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1530 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1531 | app5.setHasForegroundServices(true, 0); |
| 1532 | bindService(app, app5, s, 0, mock(IBinder.class)); |
| 1533 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1534 | lru.clear(); |
| 1535 | lru.add(app3); |
| 1536 | lru.add(app4); |
| 1537 | lru.add(app2); |
| 1538 | lru.add(app); |
| 1539 | lru.add(app5); |
| 1540 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1541 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1542 | lru.clear(); |
| 1543 | |
| 1544 | assertProcStates(app, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1545 | SCHED_GROUP_DEFAULT); |
| 1546 | assertProcStates(app2, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1547 | SCHED_GROUP_DEFAULT); |
| 1548 | assertProcStates(app3, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1549 | SCHED_GROUP_DEFAULT); |
| 1550 | assertProcStates(app4, PROCESS_STATE_IMPORTANT_FOREGROUND, PERCEPTIBLE_APP_ADJ, |
| 1551 | SCHED_GROUP_DEFAULT); |
| 1552 | assertProcStates(app5, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1553 | SCHED_GROUP_DEFAULT); |
| 1554 | } |
| 1555 | |
| 1556 | @SuppressWarnings("GuardedBy") |
| 1557 | @Test |
| 1558 | public void testUpdateOomAdj_DoAll_Provider_Cycle_Branch_2() { |
| 1559 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1560 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1561 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1562 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1563 | ContentProviderRecord cr = bindProvider(app, app2, null, null, false); |
| 1564 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1565 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1566 | bindProvider(app2, app3, null, null, false); |
| 1567 | bindProvider(app3, app, null, null, false); |
| 1568 | doReturn(mock(WindowProcessController.class)).when(app3).getWindowProcessController(); |
| 1569 | WindowProcessController wpc = app3.getWindowProcessController(); |
| 1570 | doReturn(true).when(wpc).isHomeProcess(); |
| 1571 | ProcessRecord app4 = spy(makeDefaultProcessRecord(MOCKAPP4_PID, MOCKAPP4_UID, |
| 1572 | MOCKAPP4_PROCESSNAME, MOCKAPP4_PACKAGENAME, false)); |
| 1573 | app4.setHasOverlayUi(true); |
| 1574 | bindProvider(app, app4, cr, null, false); |
| 1575 | ProcessRecord app5 = spy(makeDefaultProcessRecord(MOCKAPP5_PID, MOCKAPP5_UID, |
| 1576 | MOCKAPP5_PROCESSNAME, MOCKAPP5_PACKAGENAME, false)); |
| 1577 | app5.setHasForegroundServices(true, 0); |
| 1578 | bindProvider(app, app5, cr, null, false); |
| 1579 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1580 | lru.clear(); |
| 1581 | lru.add(app); |
| 1582 | lru.add(app2); |
| 1583 | lru.add(app3); |
| 1584 | lru.add(app4); |
| 1585 | lru.add(app5); |
| 1586 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1587 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1588 | lru.clear(); |
| 1589 | |
| 1590 | assertProcStates(app, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1591 | SCHED_GROUP_DEFAULT); |
| 1592 | assertProcStates(app2, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1593 | SCHED_GROUP_DEFAULT); |
| 1594 | assertProcStates(app3, PROCESS_STATE_BOUND_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1595 | SCHED_GROUP_DEFAULT); |
| 1596 | assertProcStates(app4, PROCESS_STATE_IMPORTANT_FOREGROUND, PERCEPTIBLE_APP_ADJ, |
| 1597 | SCHED_GROUP_DEFAULT); |
| 1598 | assertProcStates(app5, PROCESS_STATE_FOREGROUND_SERVICE, PERCEPTIBLE_APP_ADJ, |
| 1599 | SCHED_GROUP_DEFAULT); |
| 1600 | } |
| 1601 | |
Jing Ji | 327ba18 | 2019-08-08 14:10:39 -0700 | [diff] [blame] | 1602 | @SuppressWarnings("GuardedBy") |
| 1603 | @Test |
| 1604 | public void testUpdateOomAdj_DoAll_ServiceB() { |
| 1605 | ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, |
| 1606 | MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, false)); |
| 1607 | ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID, |
| 1608 | MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false)); |
| 1609 | long now = SystemClock.uptimeMillis(); |
| 1610 | ServiceRecord s = bindService(app, app2, null, 0, mock(IBinder.class)); |
| 1611 | s.startRequested = true; |
| 1612 | s.lastActivity = now; |
| 1613 | s = bindService(app2, app, null, 0, mock(IBinder.class)); |
| 1614 | s.startRequested = true; |
| 1615 | s.lastActivity = now; |
| 1616 | ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID, |
| 1617 | MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false)); |
| 1618 | s = mock(ServiceRecord.class); |
| 1619 | s.app = app3; |
| 1620 | setFieldValue(ServiceRecord.class, s, "connections", |
| 1621 | new ArrayMap<IBinder, ArrayList<ConnectionRecord>>()); |
Darryl L Johnson | d02044c | 2020-04-17 11:52:50 -0700 | [diff] [blame] | 1622 | app3.startService(s); |
Jing Ji | 327ba18 | 2019-08-08 14:10:39 -0700 | [diff] [blame] | 1623 | doCallRealMethod().when(s).getConnections(); |
| 1624 | s.startRequested = true; |
| 1625 | s.lastActivity = now; |
| 1626 | ArrayList<ProcessRecord> lru = sService.mProcessList.mLruProcesses; |
| 1627 | lru.clear(); |
| 1628 | lru.add(app3); |
| 1629 | lru.add(app2); |
| 1630 | lru.add(app); |
| 1631 | sService.mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 1632 | sService.mOomAdjuster.mNumServiceProcs = 3; |
| 1633 | sService.mOomAdjuster.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE); |
| 1634 | lru.clear(); |
| 1635 | |
| 1636 | assertEquals(SERVICE_B_ADJ, app3.setAdj); |
| 1637 | assertEquals(SERVICE_ADJ, app2.setAdj); |
| 1638 | assertEquals(SERVICE_ADJ, app.setAdj); |
| 1639 | } |
| 1640 | |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1641 | private ProcessRecord makeDefaultProcessRecord(int pid, int uid, String processName, |
| 1642 | String packageName, boolean hasShownUi) { |
| 1643 | long now = SystemClock.uptimeMillis(); |
| 1644 | return makeProcessRecord(sService, pid, uid, processName, |
| 1645 | packageName, 12345, Build.VERSION_CODES.CUR_DEVELOPMENT, |
| 1646 | now, now, now, 12345, UNKNOWN_ADJ, UNKNOWN_ADJ, |
| 1647 | UNKNOWN_ADJ, CACHED_APP_MAX_ADJ, |
| 1648 | SCHED_GROUP_DEFAULT, SCHED_GROUP_DEFAULT, |
| 1649 | PROCESS_STATE_NONEXISTENT, PROCESS_STATE_NONEXISTENT, |
| 1650 | PROCESS_STATE_NONEXISTENT, PROCESS_STATE_NONEXISTENT, |
| 1651 | 0, 0, false, false, false, ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE, |
| 1652 | false, false, false, hasShownUi, false, false, false, false, false, false, null, |
| 1653 | 0, 0, 0, true, 0, null, false); |
| 1654 | } |
| 1655 | |
| 1656 | private ProcessRecord makeProcessRecord(ActivityManagerService service, int pid, int uid, |
| 1657 | String processName, String packageName, long versionCode, int targetSdkVersion, |
| 1658 | long lastActivityTime, long lastPssTime, long nextPssTime, long lastPss, int maxAdj, |
| 1659 | int setRawAdj, int curAdj, int setAdj, int curSchedGroup, int setSchedGroup, |
| 1660 | int curProcState, int repProcState, int curRawProcState, int setProcState, |
| 1661 | int connectionGroup, int connectionImportance, boolean serviceb, |
| 1662 | boolean hasClientActivities, boolean hasForegroundServices, int fgServiceTypes, |
| 1663 | boolean hasForegroundActivities, boolean repForegroundActivities, boolean systemNoUi, |
| 1664 | boolean hasShownUi, boolean hasTopUi, boolean hasOverlayUi, |
| 1665 | boolean runningRemoteAnimation, boolean hasAboveClient, boolean treatLikeActivity, |
| 1666 | boolean killedByAm, Object forcingToImportant, int numOfCurReceivers, |
| 1667 | long lastProviderTime, long lastTopTime, boolean cached, int numOfExecutingServices, |
| 1668 | String isolatedEntryPoint, boolean execServicesFg) { |
| 1669 | ApplicationInfo ai = spy(new ApplicationInfo()); |
| 1670 | ai.uid = uid; |
| 1671 | ai.packageName = packageName; |
| 1672 | ai.longVersionCode = versionCode; |
| 1673 | ai.targetSdkVersion = targetSdkVersion; |
| 1674 | ProcessRecord app = new ProcessRecord(service, ai, processName, uid); |
| 1675 | app.thread = mock(IApplicationThread.class); |
| 1676 | app.lastActivityTime = lastActivityTime; |
| 1677 | app.lastPssTime = lastPssTime; |
| 1678 | app.nextPssTime = nextPssTime; |
| 1679 | app.lastPss = lastPss; |
| 1680 | app.maxAdj = maxAdj; |
| 1681 | app.setRawAdj = setRawAdj; |
| 1682 | app.curAdj = curAdj; |
| 1683 | app.setAdj = setAdj; |
| 1684 | app.setCurrentSchedulingGroup(curSchedGroup); |
| 1685 | app.setSchedGroup = setSchedGroup; |
| 1686 | app.setCurProcState(curProcState); |
| 1687 | app.setReportedProcState(repProcState); |
| 1688 | app.setCurRawProcState(curRawProcState); |
| 1689 | app.setProcState = setProcState; |
| 1690 | app.connectionGroup = connectionGroup; |
| 1691 | app.connectionImportance = connectionImportance; |
| 1692 | app.serviceb = serviceb; |
| 1693 | app.setHasClientActivities(hasClientActivities); |
| 1694 | app.setHasForegroundServices(hasForegroundServices, fgServiceTypes); |
| 1695 | app.setHasClientActivities(hasForegroundActivities); |
| 1696 | app.repForegroundActivities = repForegroundActivities; |
| 1697 | app.systemNoUi = systemNoUi; |
| 1698 | app.hasShownUi = hasShownUi; |
| 1699 | app.setHasTopUi(hasTopUi); |
| 1700 | app.setHasOverlayUi(hasOverlayUi); |
| 1701 | app.runningRemoteAnimation = runningRemoteAnimation; |
| 1702 | app.hasAboveClient = hasAboveClient; |
| 1703 | app.treatLikeActivity = treatLikeActivity; |
| 1704 | app.killedByAm = killedByAm; |
| 1705 | app.forcingToImportant = forcingToImportant; |
| 1706 | for (int i = 0; i < numOfCurReceivers; i++) { |
| 1707 | app.curReceivers.add(mock(BroadcastRecord.class)); |
| 1708 | } |
| 1709 | app.lastProviderTime = lastProviderTime; |
| 1710 | app.lastTopTime = lastTopTime; |
Darryl L Johnson | 72aac10 | 2020-01-15 15:16:36 -0800 | [diff] [blame] | 1711 | app.setCached(cached); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1712 | for (int i = 0; i < numOfExecutingServices; i++) { |
| 1713 | app.executingServices.add(mock(ServiceRecord.class)); |
| 1714 | } |
| 1715 | app.isolatedEntryPoint = isolatedEntryPoint; |
| 1716 | app.execServicesFg = execServicesFg; |
| 1717 | return app; |
| 1718 | } |
| 1719 | |
| 1720 | private ServiceRecord bindService(ProcessRecord service, ProcessRecord client, |
| 1721 | ServiceRecord record, int bindFlags, IBinder binder) { |
| 1722 | if (record == null) { |
| 1723 | record = mock(ServiceRecord.class); |
| 1724 | record.app = service; |
| 1725 | setFieldValue(ServiceRecord.class, record, "connections", |
| 1726 | new ArrayMap<IBinder, ArrayList<ConnectionRecord>>()); |
Darryl L Johnson | d02044c | 2020-04-17 11:52:50 -0700 | [diff] [blame] | 1727 | service.startService(record); |
Jing Ji | 05dfd91 | 2019-08-02 13:44:15 -0700 | [diff] [blame] | 1728 | doCallRealMethod().when(record).getConnections(); |
| 1729 | } |
| 1730 | AppBindRecord binding = new AppBindRecord(record, null, client); |
| 1731 | ConnectionRecord cr = spy(new ConnectionRecord(binding, |
| 1732 | mock(ActivityServiceConnectionsHolder.class), |
| 1733 | mock(IServiceConnection.class), bindFlags, |
| 1734 | 0, null, client.uid, client.processName, client.info.packageName)); |
| 1735 | doCallRealMethod().when(record).addConnection(any(IBinder.class), |
| 1736 | any(ConnectionRecord.class)); |
| 1737 | record.addConnection(binder, cr); |
| 1738 | client.connections.add(cr); |
| 1739 | binding.connections.add(cr); |
| 1740 | doNothing().when(cr).trackProcState(anyInt(), anyInt(), anyLong()); |
| 1741 | return record; |
| 1742 | } |
| 1743 | |
| 1744 | private ContentProviderRecord bindProvider(ProcessRecord publisher, ProcessRecord client, |
| 1745 | ContentProviderRecord record, String name, boolean hasExternalProviders) { |
| 1746 | if (record == null) { |
| 1747 | record = mock(ContentProviderRecord.class); |
| 1748 | publisher.pubProviders.put(name, record); |
| 1749 | record.proc = publisher; |
| 1750 | setFieldValue(ContentProviderRecord.class, record, "connections", |
| 1751 | new ArrayList<ContentProviderConnection>()); |
| 1752 | doReturn(hasExternalProviders).when(record).hasExternalProcessHandles(); |
| 1753 | } |
| 1754 | ContentProviderConnection conn = spy(new ContentProviderConnection(record, client, |
| 1755 | client.info.packageName)); |
| 1756 | record.connections.add(conn); |
| 1757 | client.conProviders.add(conn); |
| 1758 | return record; |
| 1759 | } |
| 1760 | |
| 1761 | private void assertProcStates(ProcessRecord app, int expectedProcState, int expectedAdj, |
| 1762 | int expectedSchedGroup) { |
| 1763 | assertEquals(expectedProcState, app.setProcState); |
| 1764 | assertEquals(expectedAdj, app.setAdj); |
| 1765 | assertEquals(expectedSchedGroup, app.setSchedGroup); |
| 1766 | } |
| 1767 | } |