Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.usage; |
| 18 | |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 19 | import static android.app.usage.UsageEvents.Event.FOREGROUND_SERVICE_START; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 20 | import static android.app.usage.UsageEvents.Event.NOTIFICATION_SEEN; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 21 | import static android.app.usage.UsageEvents.Event.SLICE_PINNED; |
| 22 | import static android.app.usage.UsageEvents.Event.SLICE_PINNED_PRIV; |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 23 | import static android.app.usage.UsageEvents.Event.SYSTEM_INTERACTION; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 24 | import static android.app.usage.UsageEvents.Event.USER_INTERACTION; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 25 | import static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 26 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_SYSTEM; |
| 27 | import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER; |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 28 | import static android.app.usage.UsageStatsManager.REASON_MAIN_PREDICTED; |
| 29 | import static android.app.usage.UsageStatsManager.REASON_MAIN_TIMEOUT; |
| 30 | import static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 31 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE; |
| 32 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE; |
| 33 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY; |
| 34 | import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_UNDEFINED; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 35 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_NON_DOZE; |
| 36 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_MOVE_TO_FOREGROUND; |
| 37 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SYNC_ADAPTER; |
| 38 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_SYSTEM_INTERACTION; |
| 39 | import static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_USER_INTERACTION; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 40 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 41 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_EXEMPTED; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 42 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_FREQUENT; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 43 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 44 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 45 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RESTRICTED; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 46 | import static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 47 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 48 | import static org.junit.Assert.assertEquals; |
| 49 | import static org.junit.Assert.assertFalse; |
| 50 | import static org.junit.Assert.assertNotEquals; |
| 51 | import static org.junit.Assert.assertTrue; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 52 | import static org.junit.Assert.fail; |
Yan Zhu | df47a14 | 2019-07-30 10:31:04 -0700 | [diff] [blame] | 53 | import static org.junit.Assume.assumeTrue; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 54 | import static org.mockito.ArgumentMatchers.eq; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 55 | import static org.mockito.Matchers.anyInt; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 56 | import static org.mockito.Mockito.doReturn; |
| 57 | import static org.mockito.Mockito.mock; |
| 58 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 59 | import android.annotation.NonNull; |
| 60 | import android.app.ActivityManager; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 61 | import android.app.usage.AppStandbyInfo; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 62 | import android.app.usage.UsageEvents; |
| 63 | import android.appwidget.AppWidgetManager; |
| 64 | import android.content.Context; |
| 65 | import android.content.ContextWrapper; |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 66 | import android.content.pm.ActivityInfo; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 67 | import android.content.pm.ApplicationInfo; |
| 68 | import android.content.pm.PackageInfo; |
| 69 | import android.content.pm.PackageManager; |
| 70 | import android.hardware.display.DisplayManager; |
| 71 | import android.os.Handler; |
| 72 | import android.os.Looper; |
| 73 | import android.os.RemoteException; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 74 | import android.os.UserHandle; |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 75 | import android.platform.test.annotations.Presubmit; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 76 | import android.util.ArraySet; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 77 | import android.view.Display; |
| 78 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 79 | import androidx.test.InstrumentationRegistry; |
| 80 | import androidx.test.filters.SmallTest; |
| 81 | import androidx.test.runner.AndroidJUnit4; |
| 82 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 83 | import com.android.server.SystemService; |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 84 | import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 85 | |
| 86 | import org.junit.Before; |
| 87 | import org.junit.Test; |
| 88 | import org.junit.runner.RunWith; |
| 89 | |
| 90 | import java.io.File; |
| 91 | import java.util.ArrayList; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 92 | import java.util.Arrays; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 93 | import java.util.Collections; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 94 | import java.util.List; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 95 | import java.util.Set; |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 96 | import java.util.concurrent.CountDownLatch; |
Michael Wachenschwanz | 5ca5cb6 | 2018-04-25 16:12:59 -0700 | [diff] [blame] | 97 | import java.util.concurrent.TimeUnit; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Unit test for AppStandbyController. |
| 101 | */ |
| 102 | @RunWith(AndroidJUnit4.class) |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 103 | @Presubmit |
| 104 | @SmallTest |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 105 | public class AppStandbyControllerTests { |
| 106 | |
| 107 | private static final String PACKAGE_1 = "com.example.foo"; |
| 108 | private static final int UID_1 = 10000; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 109 | private static final String PACKAGE_EXEMPTED_1 = "com.android.exempted"; |
| 110 | private static final int UID_EXEMPTED_1 = 10001; |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 111 | private static final String PACKAGE_SYSTEM_HEADFULL = "com.example.system.headfull"; |
| 112 | private static final int UID_SYSTEM_HEADFULL = 10002; |
| 113 | private static final String PACKAGE_SYSTEM_HEADLESS = "com.example.system.headless"; |
| 114 | private static final int UID_SYSTEM_HEADLESS = 10003; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 115 | private static final int USER_ID = 0; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 116 | private static final int USER_ID2 = 10; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 117 | private static final UserHandle USER_HANDLE_USER2 = new UserHandle(USER_ID2); |
| 118 | private static final int USER_ID3 = 11; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 119 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 120 | private static final String PACKAGE_UNKNOWN = "com.example.unknown"; |
| 121 | |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 122 | private static final String ADMIN_PKG = "com.android.admin"; |
| 123 | private static final String ADMIN_PKG2 = "com.android.admin2"; |
| 124 | private static final String ADMIN_PKG3 = "com.android.admin3"; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 125 | |
| 126 | private static final long MINUTE_MS = 60 * 1000; |
| 127 | private static final long HOUR_MS = 60 * MINUTE_MS; |
| 128 | private static final long DAY_MS = 24 * HOUR_MS; |
| 129 | |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 130 | private static final long WORKING_SET_THRESHOLD = 12 * HOUR_MS; |
| 131 | private static final long FREQUENT_THRESHOLD = 24 * HOUR_MS; |
| 132 | private static final long RARE_THRESHOLD = 48 * HOUR_MS; |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 133 | private static final long RESTRICTED_THRESHOLD = 96 * HOUR_MS; |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 134 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 135 | /** Mock variable used in {@link MyInjector#isPackageInstalled(String, int, int)} */ |
| 136 | private static boolean isPackageInstalled = true; |
| 137 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 138 | private MyInjector mInjector; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 139 | private AppStandbyController mController; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 140 | |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 141 | private CountDownLatch mStateChangedLatch = new CountDownLatch(1); |
| 142 | private AppIdleStateChangeListener mListener = new AppIdleStateChangeListener() { |
| 143 | @Override |
| 144 | public void onAppIdleStateChanged(String packageName, int userId, |
| 145 | boolean idle, int bucket, int reason) { |
| 146 | mStateChangedLatch.countDown(); |
| 147 | } |
| 148 | }; |
| 149 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 150 | static class MyContextWrapper extends ContextWrapper { |
| 151 | PackageManager mockPm = mock(PackageManager.class); |
| 152 | |
| 153 | public MyContextWrapper(Context base) { |
| 154 | super(base); |
| 155 | } |
| 156 | |
| 157 | public PackageManager getPackageManager() { |
| 158 | return mockPm; |
| 159 | } |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 160 | |
| 161 | public Object getSystemService(@NonNull String name) { |
| 162 | if (Context.ACTIVITY_SERVICE.equals(name)) { |
| 163 | return mock(ActivityManager.class); |
| 164 | } |
| 165 | return super.getSystemService(name); |
| 166 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static class MyInjector extends AppStandbyController.Injector { |
| 170 | long mElapsedRealtime; |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 171 | boolean mIsAppIdleEnabled = true; |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 172 | boolean mIsCharging; |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 173 | boolean mIsRestrictedBucketEnabled = true; |
Kweku Adams | ede42f9 | 2020-02-27 15:38:44 -0800 | [diff] [blame] | 174 | List<String> mNonIdleWhitelistApps = new ArrayList<>(); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 175 | boolean mDisplayOn; |
| 176 | DisplayManager.DisplayListener mDisplayListener; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 177 | String mBoundWidgetPackage = PACKAGE_EXEMPTED_1; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 178 | int[] mRunningUsers = new int[] {USER_ID}; |
| 179 | List<UserHandle> mCrossProfileTargets = Collections.emptyList(); |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 180 | boolean mDeviceIdleMode = false; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 181 | |
| 182 | MyInjector(Context context, Looper looper) { |
| 183 | super(context, looper); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | void onBootPhase(int phase) { |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | int getBootPhase() { |
| 192 | return SystemService.PHASE_BOOT_COMPLETED; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | long elapsedRealtime() { |
| 197 | return mElapsedRealtime; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | long currentTimeMillis() { |
| 202 | return mElapsedRealtime; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | boolean isAppIdleEnabled() { |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 207 | return mIsAppIdleEnabled; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 211 | boolean isCharging() { |
| 212 | return mIsCharging; |
| 213 | } |
| 214 | |
| 215 | @Override |
Kweku Adams | effeb2a | 2020-06-16 10:11:56 -0700 | [diff] [blame] | 216 | boolean isNonIdleWhitelisted(String packageName) { |
Kweku Adams | ede42f9 | 2020-02-27 15:38:44 -0800 | [diff] [blame] | 217 | return mNonIdleWhitelistApps.contains(packageName); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 221 | boolean isRestrictedBucketEnabled() { |
| 222 | return mIsRestrictedBucketEnabled; |
| 223 | } |
| 224 | |
| 225 | @Override |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 226 | File getDataSystemDirectory() { |
| 227 | return new File(getContext().getFilesDir(), Long.toString(Math.randomLongInternal())); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | void noteEvent(int event, String packageName, int uid) throws RemoteException { |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | boolean isPackageEphemeral(int userId, String packageName) { |
| 236 | // TODO: update when testing ephemeral apps scenario |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | @Override |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 241 | boolean isPackageInstalled(String packageName, int flags, int userId) { |
| 242 | // Should always return true (default value) unless testing for an uninstalled app |
| 243 | return isPackageInstalled; |
| 244 | } |
| 245 | |
| 246 | @Override |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 247 | int[] getRunningUserIds() { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 248 | return mRunningUsers; |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
| 252 | boolean isDefaultDisplayOn() { |
| 253 | return mDisplayOn; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | void registerDisplayListener(DisplayManager.DisplayListener listener, Handler handler) { |
| 258 | mDisplayListener = listener; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | String getActiveNetworkScorer() { |
| 263 | return null; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public boolean isBoundWidgetPackage(AppWidgetManager appWidgetManager, String packageName, |
| 268 | int userId) { |
| 269 | return packageName != null && packageName.equals(mBoundWidgetPackage); |
| 270 | } |
| 271 | |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 272 | @Override |
| 273 | String getAppIdleSettings() { |
| 274 | return "screen_thresholds=0/0/0/" + HOUR_MS + ",elapsed_thresholds=0/" |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 275 | + WORKING_SET_THRESHOLD + "/" + FREQUENT_THRESHOLD + "/" + RARE_THRESHOLD |
| 276 | + "/" + RESTRICTED_THRESHOLD; |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 279 | @Override |
| 280 | public boolean isDeviceIdleMode() { |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 281 | return mDeviceIdleMode; |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 284 | @Override |
| 285 | public List<UserHandle> getValidCrossProfileTargets(String pkg, int userId) { |
| 286 | return mCrossProfileTargets; |
| 287 | } |
| 288 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 289 | // Internal methods |
| 290 | |
| 291 | void setDisplayOn(boolean on) { |
| 292 | mDisplayOn = on; |
| 293 | if (mDisplayListener != null) { |
| 294 | mDisplayListener.onDisplayChanged(Display.DEFAULT_DISPLAY); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private void setupPm(PackageManager mockPm) throws PackageManager.NameNotFoundException { |
| 300 | List<PackageInfo> packages = new ArrayList<>(); |
| 301 | PackageInfo pi = new PackageInfo(); |
| 302 | pi.applicationInfo = new ApplicationInfo(); |
| 303 | pi.applicationInfo.uid = UID_1; |
| 304 | pi.packageName = PACKAGE_1; |
| 305 | packages.add(pi); |
| 306 | |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 307 | PackageInfo pie = new PackageInfo(); |
| 308 | pie.applicationInfo = new ApplicationInfo(); |
| 309 | pie.applicationInfo.uid = UID_EXEMPTED_1; |
| 310 | pie.packageName = PACKAGE_EXEMPTED_1; |
| 311 | packages.add(pie); |
| 312 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 313 | PackageInfo pis = new PackageInfo(); |
| 314 | pis.activities = new ActivityInfo[]{mock(ActivityInfo.class)}; |
| 315 | pis.applicationInfo = new ApplicationInfo(); |
| 316 | pis.applicationInfo.uid = UID_SYSTEM_HEADFULL; |
| 317 | pis.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; |
| 318 | pis.packageName = PACKAGE_SYSTEM_HEADFULL; |
| 319 | packages.add(pis); |
| 320 | |
| 321 | PackageInfo pish = new PackageInfo(); |
| 322 | pish.applicationInfo = new ApplicationInfo(); |
| 323 | pish.applicationInfo.uid = UID_SYSTEM_HEADLESS; |
| 324 | pish.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; |
| 325 | pish.packageName = PACKAGE_SYSTEM_HEADLESS; |
| 326 | packages.add(pish); |
| 327 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 328 | doReturn(packages).when(mockPm).getInstalledPackagesAsUser(anyInt(), anyInt()); |
| 329 | try { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 330 | for (int i = 0; i < packages.size(); ++i) { |
| 331 | PackageInfo pkg = packages.get(i); |
| 332 | |
| 333 | doReturn(pkg.applicationInfo.uid).when(mockPm) |
| 334 | .getPackageUidAsUser(eq(pkg.packageName), anyInt()); |
| 335 | doReturn(pkg.applicationInfo.uid).when(mockPm) |
| 336 | .getPackageUidAsUser(eq(pkg.packageName), anyInt(), anyInt()); |
| 337 | doReturn(pkg.applicationInfo).when(mockPm) |
| 338 | .getApplicationInfo(eq(pkg.packageName), anyInt()); |
| 339 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 340 | } catch (PackageManager.NameNotFoundException nnfe) {} |
| 341 | } |
| 342 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 343 | private void setChargingState(AppStandbyController controller, boolean charging) { |
| 344 | mInjector.mIsCharging = charging; |
| 345 | if (controller != null) { |
| 346 | controller.setChargingState(charging); |
| 347 | } |
| 348 | } |
| 349 | |
Kweku Adams | 1e8947c | 2018-11-05 18:06:13 -0800 | [diff] [blame] | 350 | private void setAppIdleEnabled(AppStandbyController controller, boolean enabled) { |
| 351 | mInjector.mIsAppIdleEnabled = enabled; |
| 352 | if (controller != null) { |
| 353 | controller.setAppIdleEnabled(enabled); |
| 354 | } |
| 355 | } |
| 356 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 357 | private AppStandbyController setupController() throws Exception { |
| 358 | mInjector.mElapsedRealtime = 0; |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 359 | setupPm(mInjector.getContext().getPackageManager()); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 360 | AppStandbyController controller = new AppStandbyController(mInjector); |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 361 | controller.initializeDefaultsForSystemApps(USER_ID); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 362 | controller.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 363 | controller.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
| 364 | mInjector.setDisplayOn(false); |
| 365 | mInjector.setDisplayOn(true); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 366 | setChargingState(controller, false); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 367 | controller.checkIdleStates(USER_ID); |
Amith Yamasani | 777b153 | 2018-01-28 23:20:07 +0000 | [diff] [blame] | 368 | assertNotEquals(STANDBY_BUCKET_EXEMPTED, |
| 369 | controller.getAppStandbyBucket(PACKAGE_1, USER_ID, |
| 370 | mInjector.mElapsedRealtime, false)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 371 | |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 372 | controller.addListener(mListener); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 373 | return controller; |
| 374 | } |
| 375 | |
Michael Wachenschwanz | 5ca5cb6 | 2018-04-25 16:12:59 -0700 | [diff] [blame] | 376 | private long getCurrentTime() { |
| 377 | return TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); |
| 378 | } |
| 379 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 380 | @Before |
| 381 | public void setUp() throws Exception { |
| 382 | MyContextWrapper myContext = new MyContextWrapper(InstrumentationRegistry.getContext()); |
| 383 | mInjector = new MyInjector(myContext, Looper.getMainLooper()); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 384 | mController = setupController(); |
Michael Wachenschwanz | 5ca5cb6 | 2018-04-25 16:12:59 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 387 | @Test |
Yan Zhu | df47a14 | 2019-07-30 10:31:04 -0700 | [diff] [blame] | 388 | public void testBoundWidgetPackageExempt() throws Exception { |
| 389 | assumeTrue(mInjector.getContext().getSystemService(AppWidgetManager.class) != null); |
Kweku Adams | 107be82 | 2020-05-14 08:02:45 -0700 | [diff] [blame] | 390 | assertEquals(STANDBY_BUCKET_ACTIVE, |
Yan Zhu | df47a14 | 2019-07-30 10:31:04 -0700 | [diff] [blame] | 391 | mController.getAppStandbyBucket(PACKAGE_EXEMPTED_1, USER_ID, |
| 392 | mInjector.mElapsedRealtime, false)); |
| 393 | } |
| 394 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 395 | private static class TestParoleListener extends AppIdleStateChangeListener { |
| 396 | private boolean mIsParoleOn = false; |
| 397 | private CountDownLatch mLatch; |
| 398 | private boolean mIsExpecting = false; |
| 399 | private boolean mExpectedParoleState; |
| 400 | |
| 401 | boolean getParoleState() { |
| 402 | synchronized (this) { |
| 403 | return mIsParoleOn; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void rearmLatch(boolean expectedParoleState) { |
| 408 | synchronized (this) { |
| 409 | mLatch = new CountDownLatch(1); |
| 410 | mIsExpecting = true; |
| 411 | mExpectedParoleState = expectedParoleState; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void awaitOnLatch(long time) throws Exception { |
| 416 | mLatch.await(time, TimeUnit.MILLISECONDS); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, |
| 421 | int bucket, int reason) { |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onParoleStateChanged(boolean isParoleOn) { |
| 426 | synchronized (this) { |
| 427 | // Only record information if it is being looked for |
| 428 | if (mLatch != null && mLatch.getCount() > 0) { |
| 429 | mIsParoleOn = isParoleOn; |
| 430 | if (mIsExpecting && isParoleOn == mExpectedParoleState) { |
| 431 | mLatch.countDown(); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 438 | @Test |
| 439 | public void testIsAppIdle_Charging() throws Exception { |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 440 | TestParoleListener paroleListener = new TestParoleListener(); |
| 441 | mController.addListener(paroleListener); |
| 442 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 443 | setChargingState(mController, false); |
| 444 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 445 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 446 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 447 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 448 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 449 | assertFalse(mController.isInParole()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 450 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 451 | paroleListener.rearmLatch(true); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 452 | setChargingState(mController, true); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 453 | paroleListener.awaitOnLatch(2000); |
| 454 | assertTrue(paroleListener.getParoleState()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 455 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 456 | assertFalse(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 457 | assertFalse(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 458 | assertTrue(mController.isInParole()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 459 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 460 | paroleListener.rearmLatch(false); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 461 | setChargingState(mController, false); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 462 | paroleListener.awaitOnLatch(2000); |
| 463 | assertFalse(paroleListener.getParoleState()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 464 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 465 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 466 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 467 | assertFalse(mController.isInParole()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Test |
| 471 | public void testIsAppIdle_Enabled() throws Exception { |
| 472 | setChargingState(mController, false); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 473 | TestParoleListener paroleListener = new TestParoleListener(); |
| 474 | mController.addListener(paroleListener); |
| 475 | |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 476 | setAppIdleEnabled(mController, true); |
| 477 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 478 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 479 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 480 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 481 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
| 482 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 483 | paroleListener.rearmLatch(false); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 484 | setAppIdleEnabled(mController, false); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 485 | paroleListener.awaitOnLatch(2000); |
| 486 | assertTrue(paroleListener.mIsParoleOn); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 487 | assertFalse(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 488 | assertFalse(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
| 489 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 490 | paroleListener.rearmLatch(true); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 491 | setAppIdleEnabled(mController, true); |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 492 | paroleListener.awaitOnLatch(2000); |
| 493 | assertFalse(paroleListener.getParoleState()); |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 494 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 495 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
| 496 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, false)); |
| 497 | } |
| 498 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 499 | private void assertTimeout(AppStandbyController controller, long elapsedTime, int bucket) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 500 | assertTimeout(controller, elapsedTime, bucket, USER_ID); |
| 501 | } |
| 502 | |
| 503 | private void assertTimeout(AppStandbyController controller, long elapsedTime, int bucket, |
| 504 | int userId) { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 505 | mInjector.mElapsedRealtime = elapsedTime; |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 506 | controller.checkIdleStates(userId); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 507 | assertEquals(bucket, |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 508 | controller.getAppStandbyBucket(PACKAGE_1, userId, mInjector.mElapsedRealtime, |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 509 | false)); |
| 510 | } |
| 511 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 512 | private void reportEvent(AppStandbyController controller, int eventType, long elapsedTime, |
| 513 | String packageName) { |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 514 | // Back to ACTIVE on event |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 515 | mInjector.mElapsedRealtime = elapsedTime; |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 516 | UsageEvents.Event ev = new UsageEvents.Event(); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 517 | ev.mPackage = packageName; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 518 | ev.mEventType = eventType; |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 519 | controller.reportEvent(ev, USER_ID); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 522 | private int getStandbyBucket(AppStandbyController controller, String packageName) { |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 523 | return getStandbyBucket(USER_ID, controller, packageName); |
| 524 | } |
| 525 | |
| 526 | private int getStandbyBucket(int userId, AppStandbyController controller, String packageName) { |
| 527 | return controller.getAppStandbyBucket(packageName, userId, mInjector.mElapsedRealtime, |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 528 | true); |
| 529 | } |
| 530 | |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 531 | private int getStandbyBucketReason(String packageName) { |
| 532 | return mController.getAppStandbyBucketReason(packageName, USER_ID, |
| 533 | mInjector.mElapsedRealtime); |
| 534 | } |
| 535 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 536 | private void assertBucket(int bucket) { |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 537 | assertBucket(bucket, PACKAGE_1); |
| 538 | } |
| 539 | |
| 540 | private void assertBucket(int bucket, String pkg) { |
| 541 | assertEquals(bucket, getStandbyBucket(mController, pkg)); |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 544 | private void assertNotBucket(int bucket) { |
| 545 | assertNotEquals(bucket, getStandbyBucket(mController, PACKAGE_1)); |
| 546 | } |
| 547 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 548 | @Test |
| 549 | public void testBuckets() throws Exception { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 550 | assertTimeout(mController, 0, STANDBY_BUCKET_NEVER); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 551 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 552 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 553 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 554 | // ACTIVE bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 555 | assertTimeout(mController, WORKING_SET_THRESHOLD - 1, STANDBY_BUCKET_ACTIVE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 556 | |
| 557 | // WORKING_SET bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 558 | assertTimeout(mController, WORKING_SET_THRESHOLD + 1, STANDBY_BUCKET_WORKING_SET); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 559 | |
| 560 | // WORKING_SET bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 561 | assertTimeout(mController, FREQUENT_THRESHOLD - 1, STANDBY_BUCKET_WORKING_SET); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 562 | |
| 563 | // FREQUENT bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 564 | assertTimeout(mController, FREQUENT_THRESHOLD + 1, STANDBY_BUCKET_FREQUENT); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 565 | |
| 566 | // RARE bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 567 | assertTimeout(mController, RARE_THRESHOLD + 1, STANDBY_BUCKET_RARE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 568 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 569 | // RESTRICTED bucket |
| 570 | assertTimeout(mController, RESTRICTED_THRESHOLD + 1, STANDBY_BUCKET_RESTRICTED); |
| 571 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 572 | reportEvent(mController, USER_INTERACTION, RARE_THRESHOLD + 1, PACKAGE_1); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 573 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 574 | assertTimeout(mController, RARE_THRESHOLD + 1, STANDBY_BUCKET_ACTIVE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 575 | |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 576 | // RESTRICTED bucket |
| 577 | assertTimeout(mController, RESTRICTED_THRESHOLD * 2 + 2, STANDBY_BUCKET_RESTRICTED); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | @Test |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 581 | public void testSetAppStandbyBucket() throws Exception { |
| 582 | // For a known package, standby bucket should be set properly |
| 583 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 584 | mInjector.mElapsedRealtime = HOUR_MS; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 585 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 586 | REASON_MAIN_TIMEOUT); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 587 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
| 588 | |
| 589 | // For an unknown package, standby bucket should not be set, hence NEVER is returned |
| 590 | // Ensure the unknown package is not already in history by removing it |
| 591 | mController.clearAppIdleForPackage(PACKAGE_UNKNOWN, USER_ID); |
| 592 | isPackageInstalled = false; // Mock package is not installed |
| 593 | mController.setAppStandbyBucket(PACKAGE_UNKNOWN, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 594 | REASON_MAIN_TIMEOUT); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 595 | isPackageInstalled = true; // Reset mocked variable for other tests |
| 596 | assertEquals(STANDBY_BUCKET_NEVER, getStandbyBucket(mController, PACKAGE_UNKNOWN)); |
| 597 | } |
| 598 | |
| 599 | @Test |
| 600 | public void testAppStandbyBucketOnInstallAndUninstall() throws Exception { |
| 601 | // On package install, standby bucket should be ACTIVE |
| 602 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_UNKNOWN); |
| 603 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_UNKNOWN)); |
| 604 | |
| 605 | // On uninstall, package should not exist in history and should return a NEVER bucket |
| 606 | mController.clearAppIdleForPackage(PACKAGE_UNKNOWN, USER_ID); |
| 607 | assertEquals(STANDBY_BUCKET_NEVER, getStandbyBucket(mController, PACKAGE_UNKNOWN)); |
| 608 | // Ensure uninstalled app is not in history |
| 609 | List<AppStandbyInfo> buckets = mController.getAppStandbyBuckets(USER_ID); |
| 610 | for(AppStandbyInfo bucket : buckets) { |
| 611 | if (bucket.mPackageName.equals(PACKAGE_UNKNOWN)) { |
| 612 | fail("packageName found in app idle history after uninstall."); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | @Test |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 618 | public void testScreenTimeAndBuckets() throws Exception { |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 619 | mInjector.setDisplayOn(false); |
| 620 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 621 | assertTimeout(mController, 0, STANDBY_BUCKET_NEVER); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 622 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 623 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 624 | |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 625 | // ACTIVE bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 626 | assertTimeout(mController, WORKING_SET_THRESHOLD - 1, STANDBY_BUCKET_ACTIVE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 627 | |
| 628 | // WORKING_SET bucket |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 629 | assertTimeout(mController, WORKING_SET_THRESHOLD + 1, STANDBY_BUCKET_WORKING_SET); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 630 | |
| 631 | // RARE bucket, should fail because the screen wasn't ON. |
Amith Yamasani | 301e94a | 2017-11-17 16:35:44 -0800 | [diff] [blame] | 632 | mInjector.mElapsedRealtime = RARE_THRESHOLD + 1; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 633 | mController.checkIdleStates(USER_ID); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 634 | assertNotEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 635 | |
| 636 | mInjector.setDisplayOn(true); |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 637 | assertTimeout(mController, RARE_THRESHOLD + 2 * HOUR_MS + 1, STANDBY_BUCKET_RARE); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | @Test |
| 641 | public void testForcedIdle() throws Exception { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 642 | mController.forceIdleState(PACKAGE_1, USER_ID, true); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 643 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 644 | assertTrue(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 645 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 646 | mController.forceIdleState(PACKAGE_1, USER_ID, false); |
| 647 | assertEquals(STANDBY_BUCKET_ACTIVE, mController.getAppStandbyBucket(PACKAGE_1, USER_ID, 0, |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 648 | true)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 649 | assertFalse(mController.isAppIdleFiltered(PACKAGE_1, UID_1, USER_ID, 0)); |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 650 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 651 | |
| 652 | @Test |
| 653 | public void testNotificationEvent() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 654 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 655 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 656 | mInjector.mElapsedRealtime = 1; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 657 | reportEvent(mController, NOTIFICATION_SEEN, mInjector.mElapsedRealtime, PACKAGE_1); |
| 658 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 659 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 660 | mController.forceIdleState(PACKAGE_1, USER_ID, true); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 661 | reportEvent(mController, NOTIFICATION_SEEN, mInjector.mElapsedRealtime, PACKAGE_1); |
| 662 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 663 | } |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 664 | |
| 665 | @Test |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 666 | public void testSlicePinnedEvent() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 667 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 668 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 669 | mInjector.mElapsedRealtime = 1; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 670 | reportEvent(mController, SLICE_PINNED, mInjector.mElapsedRealtime, PACKAGE_1); |
| 671 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 672 | |
| 673 | mController.forceIdleState(PACKAGE_1, USER_ID, true); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 674 | reportEvent(mController, SLICE_PINNED, mInjector.mElapsedRealtime, PACKAGE_1); |
| 675 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Test |
| 679 | public void testSlicePinnedPrivEvent() throws Exception { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 680 | mController.forceIdleState(PACKAGE_1, USER_ID, true); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 681 | reportEvent(mController, SLICE_PINNED_PRIV, mInjector.mElapsedRealtime, PACKAGE_1); |
| 682 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | @Test |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 686 | public void testPredictionTimedOut() throws Exception { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 687 | // Set it to timeout or usage, so that prediction can override it |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 688 | mInjector.mElapsedRealtime = HOUR_MS; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 689 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 690 | REASON_MAIN_TIMEOUT); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 691 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 692 | |
| 693 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 694 | REASON_MAIN_PREDICTED); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 695 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 696 | |
| 697 | // Fast forward 12 hours |
| 698 | mInjector.mElapsedRealtime += WORKING_SET_THRESHOLD; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 699 | mController.checkIdleStates(USER_ID); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 700 | // Should still be in predicted bucket, since prediction timeout is 1 day since prediction |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 701 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 702 | // Fast forward two more hours |
| 703 | mInjector.mElapsedRealtime += 2 * HOUR_MS; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 704 | mController.checkIdleStates(USER_ID); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 705 | // Should have now applied prediction timeout |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 706 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 707 | |
| 708 | // Fast forward RARE bucket |
| 709 | mInjector.mElapsedRealtime += RARE_THRESHOLD; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 710 | mController.checkIdleStates(USER_ID); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 711 | // Should continue to apply prediction timeout |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 712 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Test |
| 716 | public void testOverrides() throws Exception { |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 717 | // Can force to NEVER |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 718 | mInjector.mElapsedRealtime = HOUR_MS; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 719 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_NEVER, |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 720 | REASON_MAIN_FORCED_BY_USER); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 721 | assertEquals(STANDBY_BUCKET_NEVER, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 722 | |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 723 | // Prediction can't override FORCED reasons |
| 724 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 725 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 726 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 727 | REASON_MAIN_PREDICTED); |
| 728 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 729 | |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 730 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 731 | REASON_MAIN_FORCED_BY_USER); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 732 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 733 | REASON_MAIN_PREDICTED); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 734 | assertEquals(STANDBY_BUCKET_FREQUENT, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 735 | |
| 736 | // Prediction can't override NEVER |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 737 | mInjector.mElapsedRealtime = 2 * HOUR_MS; |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 738 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_NEVER, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 739 | REASON_MAIN_DEFAULT); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 740 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 741 | REASON_MAIN_PREDICTED); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 742 | assertEquals(STANDBY_BUCKET_NEVER, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 743 | |
| 744 | // Prediction can't set to NEVER |
| 745 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 746 | REASON_MAIN_USAGE); |
Amith Yamasani | 9388519 | 2017-12-13 11:52:10 -0800 | [diff] [blame] | 747 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_NEVER, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 748 | REASON_MAIN_PREDICTED); |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 749 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 750 | |
| 751 | // Prediction can't remove from RESTRICTED |
| 752 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 753 | REASON_MAIN_FORCED_BY_USER); |
| 754 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 755 | REASON_MAIN_PREDICTED); |
| 756 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 757 | |
| 758 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 759 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 760 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 761 | REASON_MAIN_PREDICTED); |
| 762 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 763 | |
| 764 | // Force from user can remove from RESTRICTED |
| 765 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 766 | REASON_MAIN_FORCED_BY_USER); |
| 767 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 768 | REASON_MAIN_FORCED_BY_USER); |
| 769 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 770 | |
| 771 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 772 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 773 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 774 | REASON_MAIN_FORCED_BY_USER); |
| 775 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 776 | |
| 777 | // Force from system can remove from RESTRICTED if it was put it in due to system |
| 778 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 779 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 780 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 781 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 782 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 783 | |
| 784 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 785 | REASON_MAIN_FORCED_BY_USER); |
| 786 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 787 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 788 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 789 | |
| 790 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 791 | REASON_MAIN_PREDICTED); |
| 792 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 793 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 794 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 795 | |
| 796 | // Non-user usage can't remove from RESTRICTED |
| 797 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 798 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 799 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 800 | REASON_MAIN_USAGE); |
| 801 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 802 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 803 | REASON_MAIN_USAGE | REASON_SUB_USAGE_SYSTEM_INTERACTION); |
| 804 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 805 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 806 | REASON_MAIN_USAGE | REASON_SUB_USAGE_SYNC_ADAPTER); |
| 807 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 808 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 809 | REASON_MAIN_USAGE | REASON_SUB_USAGE_EXEMPTED_SYNC_SCHEDULED_NON_DOZE); |
| 810 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 811 | |
| 812 | // Explicit user usage can remove from RESTRICTED |
| 813 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 814 | REASON_MAIN_FORCED_BY_USER); |
| 815 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
| 816 | REASON_MAIN_USAGE | REASON_SUB_USAGE_USER_INTERACTION); |
| 817 | assertEquals(STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 818 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 819 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 820 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 821 | REASON_MAIN_USAGE | REASON_SUB_USAGE_MOVE_TO_FOREGROUND); |
| 822 | assertEquals(STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
Amith Yamasani | bd7b302 | 2017-12-06 17:40:25 -0800 | [diff] [blame] | 823 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 824 | |
| 825 | @Test |
| 826 | public void testTimeout() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 827 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 828 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 829 | |
| 830 | mInjector.mElapsedRealtime = 2000; |
| 831 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 832 | REASON_MAIN_PREDICTED); |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 833 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 834 | |
| 835 | // bucketing works after timeout |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 836 | mInjector.mElapsedRealtime = mController.mPredictionTimeoutMillis - 100; |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 837 | mController.checkIdleStates(USER_ID); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 838 | // Use recent prediction |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 839 | assertBucket(STANDBY_BUCKET_FREQUENT); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 840 | |
| 841 | // Way past prediction timeout, use system thresholds |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 842 | mInjector.mElapsedRealtime = RARE_THRESHOLD; |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 843 | mController.checkIdleStates(USER_ID); |
| 844 | assertBucket(STANDBY_BUCKET_RARE); |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 845 | } |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 846 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 847 | /** |
| 848 | * Test that setAppStandbyBucket to RESTRICTED doesn't change the bucket until the usage |
| 849 | * timeout has passed. |
| 850 | */ |
| 851 | @Test |
| 852 | public void testTimeoutBeforeRestricted() throws Exception { |
| 853 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 854 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 855 | |
| 856 | mInjector.mElapsedRealtime += WORKING_SET_THRESHOLD; |
| 857 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 858 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 859 | // Bucket shouldn't change |
| 860 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 861 | |
| 862 | // bucketing works after timeout |
| 863 | mInjector.mElapsedRealtime += DAY_MS; |
| 864 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 865 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 866 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 867 | |
| 868 | // Way past all timeouts. Make sure timeout processing doesn't raise bucket. |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 869 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 870 | mController.checkIdleStates(USER_ID); |
| 871 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Test that an app is put into the RESTRICTED bucket after enough time has passed. |
| 876 | */ |
| 877 | @Test |
| 878 | public void testRestrictedDelay() throws Exception { |
| 879 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 880 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 881 | |
Kweku Adams | 109cd9c | 2020-02-11 11:10:52 -0800 | [diff] [blame] | 882 | mInjector.mElapsedRealtime += mInjector.getAutoRestrictedBucketDelayMs() - 5000; |
| 883 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 884 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 885 | // Bucket shouldn't change |
| 886 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 887 | |
| 888 | // bucketing works after timeout |
| 889 | mInjector.mElapsedRealtime += 6000; |
| 890 | |
| 891 | Thread.sleep(6000); |
| 892 | // Enough time has passed. The app should automatically be put into the RESTRICTED bucket. |
| 893 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Test that an app is put into the RESTRICTED bucket after enough time has passed. |
| 898 | */ |
| 899 | @Test |
| 900 | public void testRestrictedDelay_DelayChange() throws Exception { |
| 901 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 902 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 903 | |
| 904 | mInjector.mAutoRestrictedBucketDelayMs = 2 * HOUR_MS; |
| 905 | mInjector.mElapsedRealtime += 2 * HOUR_MS - 5000; |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 906 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 907 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 908 | // Bucket shouldn't change |
| 909 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 910 | |
| 911 | // bucketing works after timeout |
| 912 | mInjector.mElapsedRealtime += 6000; |
| 913 | |
| 914 | Thread.sleep(6000); |
| 915 | // Enough time has passed. The app should automatically be put into the RESTRICTED bucket. |
| 916 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 917 | } |
| 918 | |
Kweku Adams | 5439133 | 2020-04-08 14:56:46 -0700 | [diff] [blame] | 919 | /** |
| 920 | * Test that an app is "timed out" into the RESTRICTED bucket if prediction tries to put it into |
| 921 | * a low bucket after the RESTRICTED timeout. |
| 922 | */ |
| 923 | @Test |
| 924 | public void testRestrictedTimeoutOverridesRestoredLowBucketPrediction() { |
| 925 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 926 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 927 | |
| 928 | // Predict to RARE Not long enough to time out into RESTRICTED. |
| 929 | mInjector.mElapsedRealtime += RARE_THRESHOLD; |
| 930 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 931 | REASON_MAIN_PREDICTED); |
| 932 | assertBucket(STANDBY_BUCKET_RARE); |
| 933 | |
| 934 | // Add a short timeout event |
| 935 | mInjector.mElapsedRealtime += 1000; |
| 936 | reportEvent(mController, SYSTEM_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 937 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 938 | mInjector.mElapsedRealtime += 1000; |
| 939 | mController.checkIdleStates(USER_ID); |
| 940 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 941 | |
| 942 | // Long enough that it could have timed out into RESTRICTED. Instead of reverting to |
| 943 | // predicted RARE, should go into RESTRICTED |
| 944 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 945 | mController.checkIdleStates(USER_ID); |
| 946 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 947 | |
| 948 | // Ensure that prediction can still raise it out despite this override. |
| 949 | mInjector.mElapsedRealtime += 1; |
| 950 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 951 | REASON_MAIN_PREDICTED); |
| 952 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Test that an app is "timed out" into the RESTRICTED bucket if prediction tries to put it into |
| 957 | * a low bucket after the RESTRICTED timeout. |
| 958 | */ |
| 959 | @Test |
| 960 | public void testRestrictedTimeoutOverridesPredictionLowBucket() { |
| 961 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 962 | |
| 963 | // Not long enough to time out into RESTRICTED. |
| 964 | mInjector.mElapsedRealtime += RARE_THRESHOLD; |
| 965 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 966 | REASON_MAIN_PREDICTED); |
| 967 | assertBucket(STANDBY_BUCKET_RARE); |
| 968 | |
| 969 | mInjector.mElapsedRealtime += 1; |
| 970 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 971 | |
| 972 | // Long enough that it could have timed out into RESTRICTED. |
| 973 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 974 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 975 | REASON_MAIN_PREDICTED); |
| 976 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 977 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 978 | REASON_MAIN_PREDICTED); |
| 979 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 980 | } |
| 981 | |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 982 | @Test |
Kweku Adams | 2d79ce5 | 2020-05-05 12:31:35 -0700 | [diff] [blame] | 983 | public void testRestrictedBucketDisabled() { |
| 984 | mInjector.mIsRestrictedBucketEnabled = false; |
| 985 | // Get the controller to read the new value. Capturing the ContentObserver isn't possible |
| 986 | // at the moment. |
| 987 | mController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 988 | |
| 989 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 990 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD; |
| 991 | |
| 992 | // Nothing should be able to put it into the RESTRICTED bucket. |
| 993 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 994 | REASON_MAIN_TIMEOUT); |
| 995 | assertNotBucket(STANDBY_BUCKET_RESTRICTED); |
| 996 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 997 | REASON_MAIN_PREDICTED); |
| 998 | assertNotBucket(STANDBY_BUCKET_RESTRICTED); |
| 999 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1000 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 1001 | assertNotBucket(STANDBY_BUCKET_RESTRICTED); |
| 1002 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1003 | REASON_MAIN_FORCED_BY_USER); |
| 1004 | assertNotBucket(STANDBY_BUCKET_RESTRICTED); |
| 1005 | } |
| 1006 | |
| 1007 | @Test |
| 1008 | public void testRestrictedBucket_EnabledToDisabled() { |
| 1009 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1010 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD; |
| 1011 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1012 | REASON_MAIN_FORCED_BY_SYSTEM); |
| 1013 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1014 | |
| 1015 | mInjector.mIsRestrictedBucketEnabled = false; |
| 1016 | // Get the controller to read the new value. Capturing the ContentObserver isn't possible |
| 1017 | // at the moment. |
| 1018 | mController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 1019 | |
| 1020 | mController.checkIdleStates(USER_ID); |
| 1021 | assertNotBucket(STANDBY_BUCKET_RESTRICTED); |
| 1022 | } |
| 1023 | |
| 1024 | @Test |
Kweku Adams | 496594c | 2020-03-16 12:26:01 -0700 | [diff] [blame] | 1025 | public void testPredictionRaiseFromRestrictedTimeout_highBucket() { |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 1026 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1027 | |
| 1028 | // Way past all timeouts. App times out into RESTRICTED bucket. |
| 1029 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1030 | mController.checkIdleStates(USER_ID); |
| 1031 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1032 | |
| 1033 | // Since the app timed out into RESTRICTED, prediction should be able to remove from the |
| 1034 | // bucket. |
| 1035 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD; |
| 1036 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 1037 | REASON_MAIN_PREDICTED); |
Kweku Adams | 496594c | 2020-03-16 12:26:01 -0700 | [diff] [blame] | 1038 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1039 | } |
| 1040 | |
| 1041 | @Test |
| 1042 | public void testPredictionRaiseFromRestrictedTimeout_lowBucket() { |
| 1043 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1044 | |
| 1045 | // Way past all timeouts. App times out into RESTRICTED bucket. |
| 1046 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1047 | mController.checkIdleStates(USER_ID); |
| 1048 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1049 | |
| 1050 | // Prediction into a low bucket means no expectation of the app being used, so we shouldn't |
| 1051 | // elevate the app from RESTRICTED. |
| 1052 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD; |
| 1053 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 1054 | REASON_MAIN_PREDICTED); |
| 1055 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
Kweku Adams | 4297b5d | 2020-02-06 15:56:12 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | @Test |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1059 | public void testCascadingTimeouts() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1060 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1061 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1062 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1063 | reportEvent(mController, NOTIFICATION_SEEN, 1000, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1064 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1065 | |
| 1066 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_WORKING_SET, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1067 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1068 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1069 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1070 | mInjector.mElapsedRealtime = 2000 + mController.mStrongUsageTimeoutMillis; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1071 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1072 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1073 | assertBucket(STANDBY_BUCKET_WORKING_SET); |
| 1074 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1075 | mInjector.mElapsedRealtime = 2000 + mController.mNotificationSeenTimeoutMillis; |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1076 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1077 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1078 | assertBucket(STANDBY_BUCKET_FREQUENT); |
| 1079 | } |
| 1080 | |
| 1081 | @Test |
| 1082 | public void testOverlappingTimeouts() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1083 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1084 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1085 | |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1086 | reportEvent(mController, NOTIFICATION_SEEN, 1000, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1087 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1088 | |
| 1089 | // Overlapping USER_INTERACTION before previous one times out |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1090 | reportEvent(mController, USER_INTERACTION, mController.mStrongUsageTimeoutMillis - 1000, |
| 1091 | PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1092 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1093 | |
| 1094 | // Still in ACTIVE after first USER_INTERACTION times out |
| 1095 | mInjector.mElapsedRealtime = mController.mStrongUsageTimeoutMillis + 1000; |
| 1096 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1097 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1098 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1099 | |
| 1100 | // Both timed out, so NOTIFICATION_SEEN timeout should be effective |
| 1101 | mInjector.mElapsedRealtime = mController.mStrongUsageTimeoutMillis * 2 + 2000; |
| 1102 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1103 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1104 | assertBucket(STANDBY_BUCKET_WORKING_SET); |
| 1105 | |
| 1106 | mInjector.mElapsedRealtime = mController.mNotificationSeenTimeoutMillis + 2000; |
| 1107 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1108 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1109 | assertBucket(STANDBY_BUCKET_RARE); |
| 1110 | } |
| 1111 | |
| 1112 | @Test |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 1113 | public void testSystemInteractionTimeout() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1114 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 1115 | // Fast forward to RARE |
| 1116 | mInjector.mElapsedRealtime = RARE_THRESHOLD + 100; |
| 1117 | mController.checkIdleStates(USER_ID); |
| 1118 | assertBucket(STANDBY_BUCKET_RARE); |
| 1119 | |
| 1120 | // Trigger a SYSTEM_INTERACTION and verify bucket |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1121 | reportEvent(mController, SYSTEM_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
Amith Yamasani | 7f53c7b | 2018-03-25 21:55:50 -0700 | [diff] [blame] | 1122 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1123 | |
| 1124 | // Verify it's still in ACTIVE close to end of timeout |
| 1125 | mInjector.mElapsedRealtime += mController.mSystemInteractionTimeoutMillis - 100; |
| 1126 | mController.checkIdleStates(USER_ID); |
| 1127 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1128 | |
| 1129 | // Verify bucket moves to RARE after timeout |
| 1130 | mInjector.mElapsedRealtime += 200; |
| 1131 | mController.checkIdleStates(USER_ID); |
| 1132 | assertBucket(STANDBY_BUCKET_RARE); |
| 1133 | } |
| 1134 | |
| 1135 | @Test |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 1136 | public void testInitialForegroundServiceTimeout() throws Exception { |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 1137 | mInjector.mElapsedRealtime = 1 * RARE_THRESHOLD + 100; |
| 1138 | // Make sure app is in NEVER bucket |
| 1139 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_NEVER, |
Kweku Adams | c182d5e | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 1140 | REASON_MAIN_FORCED_BY_USER); |
Michael Wachenschwanz | 6ced0ee | 2019-04-15 16:43:28 -0700 | [diff] [blame] | 1141 | mController.checkIdleStates(USER_ID); |
| 1142 | assertBucket(STANDBY_BUCKET_NEVER); |
| 1143 | |
| 1144 | mInjector.mElapsedRealtime += 100; |
| 1145 | |
| 1146 | // Trigger a FOREGROUND_SERVICE_START and verify bucket |
| 1147 | reportEvent(mController, FOREGROUND_SERVICE_START, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1148 | mController.checkIdleStates(USER_ID); |
| 1149 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1150 | |
| 1151 | // Verify it's still in ACTIVE close to end of timeout |
| 1152 | mInjector.mElapsedRealtime += mController.mInitialForegroundServiceStartTimeoutMillis - 100; |
| 1153 | mController.checkIdleStates(USER_ID); |
| 1154 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1155 | |
| 1156 | // Verify bucket moves to RARE after timeout |
| 1157 | mInjector.mElapsedRealtime += 200; |
| 1158 | mController.checkIdleStates(USER_ID); |
| 1159 | assertBucket(STANDBY_BUCKET_RARE); |
| 1160 | |
| 1161 | // Trigger a FOREGROUND_SERVICE_START again |
| 1162 | reportEvent(mController, FOREGROUND_SERVICE_START, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1163 | mController.checkIdleStates(USER_ID); |
| 1164 | // Bucket should not be immediately elevated on subsequent service starts |
| 1165 | assertBucket(STANDBY_BUCKET_RARE); |
| 1166 | } |
| 1167 | |
| 1168 | @Test |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1169 | public void testPredictionNotOverridden() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1170 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1171 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1172 | |
| 1173 | mInjector.mElapsedRealtime = WORKING_SET_THRESHOLD - 1000; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1174 | reportEvent(mController, NOTIFICATION_SEEN, mInjector.mElapsedRealtime, PACKAGE_1); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1175 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1176 | |
| 1177 | // Falls back to WORKING_SET |
| 1178 | mInjector.mElapsedRealtime += 5000; |
| 1179 | mController.checkIdleStates(USER_ID); |
| 1180 | assertBucket(STANDBY_BUCKET_WORKING_SET); |
| 1181 | |
| 1182 | // Predict to ACTIVE |
| 1183 | mInjector.mElapsedRealtime += 1000; |
| 1184 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1185 | REASON_MAIN_PREDICTED); |
Amith Yamasani | bbbad9c | 2018-02-10 16:46:38 -0800 | [diff] [blame] | 1186 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1187 | |
| 1188 | // CheckIdleStates should not change the prediction |
| 1189 | mInjector.mElapsedRealtime += 1000; |
| 1190 | mController.checkIdleStates(USER_ID); |
| 1191 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1192 | } |
| 1193 | |
| 1194 | @Test |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 1195 | public void testPredictionStrikesBack() throws Exception { |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1196 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 1197 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1198 | |
| 1199 | // Predict to FREQUENT |
| 1200 | mInjector.mElapsedRealtime = RARE_THRESHOLD; |
| 1201 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 1202 | REASON_MAIN_PREDICTED); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 1203 | assertBucket(STANDBY_BUCKET_FREQUENT); |
| 1204 | |
| 1205 | // Add a short timeout event |
| 1206 | mInjector.mElapsedRealtime += 1000; |
Varun Shah | 7609b75 | 2018-10-15 15:07:47 -0700 | [diff] [blame] | 1207 | reportEvent(mController, SYSTEM_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
Amith Yamasani | 3154dcf | 2018-03-27 18:24:04 -0700 | [diff] [blame] | 1208 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1209 | mInjector.mElapsedRealtime += 1000; |
| 1210 | mController.checkIdleStates(USER_ID); |
| 1211 | assertBucket(STANDBY_BUCKET_ACTIVE); |
| 1212 | |
| 1213 | // Verify it reverted to predicted |
| 1214 | mInjector.mElapsedRealtime += WORKING_SET_THRESHOLD / 2; |
| 1215 | mController.checkIdleStates(USER_ID); |
| 1216 | assertBucket(STANDBY_BUCKET_FREQUENT); |
| 1217 | } |
| 1218 | |
| 1219 | @Test |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 1220 | public void testSystemForcedFlags_NotAddedForUserForce() throws Exception { |
| 1221 | final int expectedReason = REASON_MAIN_FORCED_BY_USER; |
| 1222 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1223 | REASON_MAIN_FORCED_BY_USER); |
| 1224 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1225 | assertEquals(expectedReason, getStandbyBucketReason(PACKAGE_1)); |
| 1226 | |
| 1227 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1228 | REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE); |
| 1229 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1230 | assertEquals(expectedReason, getStandbyBucketReason(PACKAGE_1)); |
| 1231 | } |
| 1232 | |
| 1233 | @Test |
| 1234 | public void testSystemForcedFlags_AddedForSystemForce() throws Exception { |
| 1235 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 1236 | REASON_MAIN_DEFAULT); |
| 1237 | mInjector.mElapsedRealtime += 4 * RESTRICTED_THRESHOLD; |
| 1238 | |
| 1239 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1240 | REASON_MAIN_FORCED_BY_SYSTEM |
| 1241 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
| 1242 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1243 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM |
| 1244 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE, |
| 1245 | getStandbyBucketReason(PACKAGE_1)); |
| 1246 | |
| 1247 | mController.restrictApp(PACKAGE_1, USER_ID, REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE); |
| 1248 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1249 | // Flags should be combined |
| 1250 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM |
| 1251 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE |
| 1252 | | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE, getStandbyBucketReason(PACKAGE_1)); |
| 1253 | } |
| 1254 | |
| 1255 | @Test |
| 1256 | public void testSystemForcedFlags_SystemForceChangesBuckets() throws Exception { |
| 1257 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, |
| 1258 | REASON_MAIN_DEFAULT); |
| 1259 | mInjector.mElapsedRealtime += 4 * RESTRICTED_THRESHOLD; |
| 1260 | |
| 1261 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
| 1262 | REASON_MAIN_FORCED_BY_SYSTEM |
| 1263 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
| 1264 | assertEquals(STANDBY_BUCKET_FREQUENT, getStandbyBucket(mController, PACKAGE_1)); |
| 1265 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM |
| 1266 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE, |
| 1267 | getStandbyBucketReason(PACKAGE_1)); |
| 1268 | |
| 1269 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_FREQUENT, |
| 1270 | REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE); |
| 1271 | assertEquals(STANDBY_BUCKET_FREQUENT, getStandbyBucket(mController, PACKAGE_1)); |
| 1272 | // Flags should be combined |
| 1273 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM |
| 1274 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE |
| 1275 | | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE, |
| 1276 | getStandbyBucketReason(PACKAGE_1)); |
| 1277 | |
| 1278 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 1279 | REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY); |
| 1280 | assertEquals(STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 1281 | // Flags should be combined |
| 1282 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY, |
| 1283 | getStandbyBucketReason(PACKAGE_1)); |
| 1284 | |
| 1285 | mController.restrictApp(PACKAGE_1, USER_ID, REASON_SUB_FORCED_SYSTEM_FLAG_UNDEFINED); |
| 1286 | assertEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1287 | // Flags should not be combined since the bucket changed. |
| 1288 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_UNDEFINED, |
| 1289 | getStandbyBucketReason(PACKAGE_1)); |
| 1290 | } |
| 1291 | |
| 1292 | @Test |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1293 | public void testAddActiveDeviceAdmin() { |
| 1294 | assertActiveAdmins(USER_ID, (String[]) null); |
| 1295 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1296 | |
| 1297 | mController.addActiveDeviceAdmin(ADMIN_PKG, USER_ID); |
| 1298 | assertActiveAdmins(USER_ID, ADMIN_PKG); |
| 1299 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1300 | |
| 1301 | mController.addActiveDeviceAdmin(ADMIN_PKG, USER_ID); |
| 1302 | assertActiveAdmins(USER_ID, ADMIN_PKG); |
| 1303 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1304 | |
| 1305 | mController.addActiveDeviceAdmin(ADMIN_PKG2, USER_ID2); |
| 1306 | assertActiveAdmins(USER_ID, ADMIN_PKG); |
| 1307 | assertActiveAdmins(USER_ID2, ADMIN_PKG2); |
| 1308 | } |
| 1309 | |
| 1310 | @Test |
| 1311 | public void testSetActiveAdminApps() { |
| 1312 | assertActiveAdmins(USER_ID, (String[]) null); |
| 1313 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1314 | |
| 1315 | setActiveAdmins(USER_ID, ADMIN_PKG, ADMIN_PKG2); |
| 1316 | assertActiveAdmins(USER_ID, ADMIN_PKG, ADMIN_PKG2); |
| 1317 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1318 | |
| 1319 | mController.addActiveDeviceAdmin(ADMIN_PKG2, USER_ID2); |
| 1320 | setActiveAdmins(USER_ID2, ADMIN_PKG); |
| 1321 | assertActiveAdmins(USER_ID, ADMIN_PKG, ADMIN_PKG2); |
| 1322 | assertActiveAdmins(USER_ID2, ADMIN_PKG); |
| 1323 | |
| 1324 | mController.setActiveAdminApps(null, USER_ID); |
| 1325 | assertActiveAdmins(USER_ID, (String[]) null); |
| 1326 | } |
| 1327 | |
| 1328 | @Test |
| 1329 | public void isActiveDeviceAdmin() { |
| 1330 | assertActiveAdmins(USER_ID, (String[]) null); |
| 1331 | assertActiveAdmins(USER_ID2, (String[]) null); |
| 1332 | |
| 1333 | mController.addActiveDeviceAdmin(ADMIN_PKG, USER_ID); |
| 1334 | assertIsActiveAdmin(ADMIN_PKG, USER_ID); |
| 1335 | assertIsNotActiveAdmin(ADMIN_PKG, USER_ID2); |
| 1336 | |
| 1337 | mController.addActiveDeviceAdmin(ADMIN_PKG2, USER_ID2); |
| 1338 | mController.addActiveDeviceAdmin(ADMIN_PKG, USER_ID2); |
| 1339 | assertIsActiveAdmin(ADMIN_PKG, USER_ID); |
| 1340 | assertIsNotActiveAdmin(ADMIN_PKG2, USER_ID); |
| 1341 | assertIsActiveAdmin(ADMIN_PKG, USER_ID2); |
| 1342 | assertIsActiveAdmin(ADMIN_PKG2, USER_ID2); |
| 1343 | |
| 1344 | setActiveAdmins(USER_ID2, ADMIN_PKG2); |
| 1345 | assertIsActiveAdmin(ADMIN_PKG2, USER_ID2); |
| 1346 | assertIsNotActiveAdmin(ADMIN_PKG, USER_ID2); |
| 1347 | assertIsActiveAdmin(ADMIN_PKG, USER_ID); |
| 1348 | assertIsNotActiveAdmin(ADMIN_PKG2, USER_ID); |
| 1349 | } |
| 1350 | |
Michael Wachenschwanz | 9a3501d | 2020-02-10 23:17:28 -0800 | [diff] [blame] | 1351 | @Test |
| 1352 | public void testUserInteraction_CrossProfile() throws Exception { |
| 1353 | mInjector.mRunningUsers = new int[] {USER_ID, USER_ID2, USER_ID3}; |
| 1354 | mInjector.mCrossProfileTargets = Arrays.asList(USER_HANDLE_USER2); |
| 1355 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 1356 | assertEquals("Cross profile connected package bucket should be elevated on usage", |
| 1357 | STANDBY_BUCKET_ACTIVE, getStandbyBucket(USER_ID2, mController, PACKAGE_1)); |
| 1358 | assertEquals("Not Cross profile connected package bucket should not be elevated on usage", |
| 1359 | STANDBY_BUCKET_NEVER, getStandbyBucket(USER_ID3, mController, PACKAGE_1)); |
| 1360 | |
| 1361 | assertTimeout(mController, WORKING_SET_THRESHOLD - 1, STANDBY_BUCKET_ACTIVE, USER_ID); |
| 1362 | assertTimeout(mController, WORKING_SET_THRESHOLD - 1, STANDBY_BUCKET_ACTIVE, USER_ID2); |
| 1363 | |
| 1364 | assertTimeout(mController, WORKING_SET_THRESHOLD + 1, STANDBY_BUCKET_WORKING_SET, USER_ID); |
| 1365 | assertTimeout(mController, WORKING_SET_THRESHOLD + 1, STANDBY_BUCKET_WORKING_SET, USER_ID2); |
| 1366 | |
| 1367 | mInjector.mCrossProfileTargets = Collections.emptyList(); |
| 1368 | reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1); |
| 1369 | assertEquals("No longer cross profile connected package bucket should not be " |
| 1370 | + "elevated on usage", |
| 1371 | STANDBY_BUCKET_WORKING_SET, getStandbyBucket(USER_ID2, mController, PACKAGE_1)); |
| 1372 | } |
| 1373 | |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 1374 | @Test |
| 1375 | public void testUnexemptedSyncScheduled() throws Exception { |
| 1376 | mStateChangedLatch = new CountDownLatch(1); |
| 1377 | mController.addListener(mListener); |
| 1378 | assertEquals("Test package did not start in the Never bucket", STANDBY_BUCKET_NEVER, |
| 1379 | getStandbyBucket(mController, PACKAGE_1)); |
| 1380 | |
| 1381 | mController.postReportSyncScheduled(PACKAGE_1, USER_ID, false); |
| 1382 | mStateChangedLatch.await(100, TimeUnit.MILLISECONDS); |
| 1383 | assertEquals("Unexempted sync scheduled should bring the package out of the Never bucket", |
| 1384 | STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 1385 | |
| 1386 | setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM); |
| 1387 | |
| 1388 | mStateChangedLatch = new CountDownLatch(1); |
| 1389 | mController.postReportSyncScheduled(PACKAGE_1, USER_ID, false); |
| 1390 | mStateChangedLatch.await(100, TimeUnit.MILLISECONDS); |
| 1391 | assertEquals("Unexempted sync scheduled should not elevate a non Never bucket", |
| 1392 | STANDBY_BUCKET_RARE, getStandbyBucket(mController, PACKAGE_1)); |
| 1393 | } |
| 1394 | |
| 1395 | @Test |
| 1396 | public void testExemptedSyncScheduled() throws Exception { |
| 1397 | setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM); |
| 1398 | mInjector.mDeviceIdleMode = true; |
| 1399 | mStateChangedLatch = new CountDownLatch(1); |
| 1400 | mController.postReportSyncScheduled(PACKAGE_1, USER_ID, true); |
| 1401 | mStateChangedLatch.await(100, TimeUnit.MILLISECONDS); |
| 1402 | assertEquals("Exempted sync scheduled in doze should set bucket to working set", |
| 1403 | STANDBY_BUCKET_WORKING_SET, getStandbyBucket(mController, PACKAGE_1)); |
| 1404 | |
| 1405 | setAndAssertBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, REASON_MAIN_FORCED_BY_SYSTEM); |
| 1406 | mInjector.mDeviceIdleMode = false; |
| 1407 | mStateChangedLatch = new CountDownLatch(1); |
| 1408 | mController.postReportSyncScheduled(PACKAGE_1, USER_ID, true); |
| 1409 | mStateChangedLatch.await(100, TimeUnit.MILLISECONDS); |
| 1410 | assertEquals("Exempted sync scheduled while not in doze should set bucket to active", |
| 1411 | STANDBY_BUCKET_ACTIVE, getStandbyBucket(mController, PACKAGE_1)); |
| 1412 | } |
| 1413 | |
Kweku Adams | 917f8a4 | 2020-02-26 17:18:03 -0800 | [diff] [blame] | 1414 | @Test |
| 1415 | public void testAppUpdateOnRestrictedBucketStatus() { |
| 1416 | // Updates shouldn't change bucket if the app timed out. |
| 1417 | // Way past all timeouts. App times out into RESTRICTED bucket. |
| 1418 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1419 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1420 | mController.checkIdleStates(USER_ID); |
| 1421 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1422 | |
| 1423 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID); |
| 1424 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1425 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID2); |
| 1426 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1427 | mController.maybeUnrestrictBuggyApp("com.random.package", USER_ID); |
| 1428 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1429 | |
| 1430 | // Updates shouldn't change bucket if the app was forced by the system for a non-buggy |
| 1431 | // reason. |
| 1432 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1433 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1434 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1435 | REASON_MAIN_FORCED_BY_SYSTEM |
| 1436 | | REASON_SUB_FORCED_SYSTEM_FLAG_BACKGROUND_RESOURCE_USAGE); |
| 1437 | |
| 1438 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID); |
| 1439 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1440 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID2); |
| 1441 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1442 | mController.maybeUnrestrictBuggyApp("com.random.package", USER_ID); |
| 1443 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1444 | |
| 1445 | // Updates should change bucket if the app was forced by the system for a buggy reason. |
| 1446 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1447 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1448 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1449 | REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY); |
| 1450 | |
| 1451 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID2); |
| 1452 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1453 | mController.maybeUnrestrictBuggyApp("com.random.package", USER_ID); |
| 1454 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1455 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID); |
| 1456 | assertNotEquals(STANDBY_BUCKET_RESTRICTED, getStandbyBucket(mController, PACKAGE_1)); |
| 1457 | |
| 1458 | // Updates shouldn't change bucket if the app was forced by the system for more than just |
| 1459 | // a buggy reason. |
| 1460 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1461 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1462 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1463 | REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE |
| 1464 | | REASON_SUB_FORCED_SYSTEM_FLAG_BUGGY); |
| 1465 | |
| 1466 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID); |
| 1467 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1468 | assertEquals(REASON_MAIN_FORCED_BY_SYSTEM | REASON_SUB_FORCED_SYSTEM_FLAG_ABUSE, |
| 1469 | getStandbyBucketReason(PACKAGE_1)); |
| 1470 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID2); |
| 1471 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1472 | mController.maybeUnrestrictBuggyApp("com.random.package", USER_ID); |
| 1473 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1474 | |
| 1475 | // Updates shouldn't change bucket if the app was forced by the user. |
| 1476 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1477 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD * 4; |
| 1478 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RESTRICTED, |
| 1479 | REASON_MAIN_FORCED_BY_USER); |
| 1480 | |
| 1481 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID); |
| 1482 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1483 | mController.maybeUnrestrictBuggyApp(PACKAGE_1, USER_ID2); |
| 1484 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1485 | mController.maybeUnrestrictBuggyApp("com.random.package", USER_ID); |
| 1486 | assertBucket(STANDBY_BUCKET_RESTRICTED); |
| 1487 | } |
| 1488 | |
Kweku Adams | 95cd952 | 2020-05-08 09:56:53 -0700 | [diff] [blame] | 1489 | @Test |
| 1490 | public void testSystemHeadlessAppElevated() { |
| 1491 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, PACKAGE_1); |
| 1492 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, |
| 1493 | PACKAGE_SYSTEM_HEADFULL); |
| 1494 | reportEvent(mController, USER_INTERACTION, mInjector.mElapsedRealtime, |
| 1495 | PACKAGE_SYSTEM_HEADLESS); |
| 1496 | mInjector.mElapsedRealtime += RESTRICTED_THRESHOLD; |
| 1497 | |
| 1498 | |
| 1499 | mController.setAppStandbyBucket(PACKAGE_SYSTEM_HEADFULL, USER_ID, STANDBY_BUCKET_RARE, |
| 1500 | REASON_MAIN_TIMEOUT); |
| 1501 | assertBucket(STANDBY_BUCKET_RARE, PACKAGE_SYSTEM_HEADFULL); |
| 1502 | |
| 1503 | // Make sure headless system apps don't get lowered. |
| 1504 | mController.setAppStandbyBucket(PACKAGE_SYSTEM_HEADLESS, USER_ID, STANDBY_BUCKET_RARE, |
| 1505 | REASON_MAIN_TIMEOUT); |
| 1506 | assertBucket(STANDBY_BUCKET_ACTIVE, PACKAGE_SYSTEM_HEADLESS); |
| 1507 | |
| 1508 | // Package 1 doesn't have activities and is headless, but is not a system app, so it can |
| 1509 | // be lowered. |
| 1510 | mController.setAppStandbyBucket(PACKAGE_1, USER_ID, STANDBY_BUCKET_RARE, |
| 1511 | REASON_MAIN_TIMEOUT); |
| 1512 | assertBucket(STANDBY_BUCKET_RARE, PACKAGE_1); |
| 1513 | } |
| 1514 | |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 1515 | private String getAdminAppsStr(int userId) { |
| 1516 | return getAdminAppsStr(userId, mController.getActiveAdminAppsForTest(userId)); |
| 1517 | } |
| 1518 | |
| 1519 | private String getAdminAppsStr(int userId, Set<String> adminApps) { |
| 1520 | return "admin apps for u" + userId + ": " |
| 1521 | + (adminApps == null ? "null" : Arrays.toString(adminApps.toArray())); |
| 1522 | } |
| 1523 | |
| 1524 | private void assertIsActiveAdmin(String adminApp, int userId) { |
| 1525 | assertTrue(adminApp + " should be an active admin; " + getAdminAppsStr(userId), |
| 1526 | mController.isActiveDeviceAdmin(adminApp, userId)); |
| 1527 | } |
| 1528 | |
| 1529 | private void assertIsNotActiveAdmin(String adminApp, int userId) { |
| 1530 | assertFalse(adminApp + " shouldn't be an active admin; " + getAdminAppsStr(userId), |
| 1531 | mController.isActiveDeviceAdmin(adminApp, userId)); |
| 1532 | } |
| 1533 | |
| 1534 | private void assertActiveAdmins(int userId, String... admins) { |
| 1535 | final Set<String> actualAdminApps = mController.getActiveAdminAppsForTest(userId); |
| 1536 | if (admins == null) { |
| 1537 | if (actualAdminApps != null && !actualAdminApps.isEmpty()) { |
| 1538 | fail("Admin apps should be null; " + getAdminAppsStr(userId, actualAdminApps)); |
| 1539 | } |
| 1540 | return; |
| 1541 | } |
| 1542 | assertEquals("No. of admin apps not equal; " + getAdminAppsStr(userId, actualAdminApps) |
| 1543 | + "; expected=" + Arrays.toString(admins), admins.length, actualAdminApps.size()); |
| 1544 | final Set<String> adminAppsCopy = new ArraySet<>(actualAdminApps); |
| 1545 | for (String admin : admins) { |
| 1546 | adminAppsCopy.remove(admin); |
| 1547 | } |
| 1548 | assertTrue("Unexpected admin apps; " + getAdminAppsStr(userId, actualAdminApps) |
| 1549 | + "; expected=" + Arrays.toString(admins), adminAppsCopy.isEmpty()); |
| 1550 | } |
| 1551 | |
| 1552 | private void setActiveAdmins(int userId, String... admins) { |
| 1553 | mController.setActiveAdminApps(new ArraySet<>(Arrays.asList(admins)), userId); |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 1554 | } |
Michael Wachenschwanz | 1103ff2 | 2020-02-11 20:40:26 -0800 | [diff] [blame] | 1555 | |
| 1556 | private void setAndAssertBucket(String pkg, int user, int bucket, int reason) throws Exception { |
| 1557 | mStateChangedLatch = new CountDownLatch(1); |
| 1558 | mController.setAppStandbyBucket(pkg, user, bucket, reason); |
| 1559 | mStateChangedLatch.await(100, TimeUnit.MILLISECONDS); |
| 1560 | assertEquals("Failed to set package bucket", bucket, |
| 1561 | getStandbyBucket(mController, PACKAGE_1)); |
| 1562 | } |
Amith Yamasani | 17fffee | 2017-09-29 13:17:43 -0700 | [diff] [blame] | 1563 | } |