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