Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.pm; |
| 17 | |
Makoto Onuki | d0010c5 | 2017-03-30 14:17:35 -0700 | [diff] [blame] | 18 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull; |
| 19 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 20 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList; |
| 21 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet; |
| 22 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list; |
| 23 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle; |
| 24 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set; |
| 25 | |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 27 | import static org.mockito.Matchers.any; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 28 | import static org.mockito.Matchers.anyInt; |
| 29 | import static org.mockito.Matchers.anyString; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 30 | import static org.mockito.Matchers.eq; |
| 31 | import static org.mockito.Mockito.doAnswer; |
| 32 | import static org.mockito.Mockito.mock; |
| 33 | import static org.mockito.Mockito.reset; |
| 34 | import static org.mockito.Mockito.spy; |
Makoto Onuki | ff14f73 | 2016-06-30 17:07:25 -0700 | [diff] [blame] | 35 | import static org.mockito.Mockito.times; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 36 | import static org.mockito.Mockito.verify; |
| 37 | import static org.mockito.Mockito.when; |
| 38 | |
| 39 | import android.annotation.NonNull; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 40 | import android.annotation.UserIdInt; |
| 41 | import android.app.Activity; |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 42 | import android.app.ActivityManager; |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 43 | import android.app.ActivityManagerInternal; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 44 | import android.app.IUidObserver; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 45 | import android.app.usage.UsageStatsManagerInternal; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 46 | import android.content.ActivityNotFoundException; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
| 48 | import android.content.ComponentName; |
| 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.content.IntentFilter; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 52 | import android.content.IntentSender; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 53 | import android.content.pm.ActivityInfo; |
| 54 | import android.content.pm.ApplicationInfo; |
| 55 | import android.content.pm.ILauncherApps; |
| 56 | import android.content.pm.LauncherApps; |
| 57 | import android.content.pm.LauncherApps.ShortcutQuery; |
| 58 | import android.content.pm.PackageInfo; |
| 59 | import android.content.pm.PackageManager; |
| 60 | import android.content.pm.PackageManagerInternal; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 61 | import android.content.pm.ResolveInfo; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 62 | import android.content.pm.ShortcutInfo; |
| 63 | import android.content.pm.ShortcutManager; |
| 64 | import android.content.pm.ShortcutServiceInternal; |
| 65 | import android.content.pm.Signature; |
| 66 | import android.content.pm.UserInfo; |
| 67 | import android.content.res.Resources; |
| 68 | import android.content.res.XmlResourceParser; |
| 69 | import android.graphics.drawable.Icon; |
| 70 | import android.net.Uri; |
| 71 | import android.os.Bundle; |
| 72 | import android.os.FileUtils; |
| 73 | import android.os.Handler; |
| 74 | import android.os.Looper; |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 75 | import android.os.PersistableBundle; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 76 | import android.os.Process; |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 77 | import android.os.RemoteException; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 78 | import android.os.UserHandle; |
| 79 | import android.os.UserManager; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 80 | import android.os.UserManagerInternal; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 81 | import android.test.InstrumentationTestCase; |
| 82 | import android.test.mock.MockContext; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 83 | import android.util.ArrayMap; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 84 | import android.util.Log; |
| 85 | import android.util.Pair; |
| 86 | |
| 87 | import com.android.internal.util.Preconditions; |
| 88 | import com.android.server.LocalServices; |
| 89 | import com.android.server.SystemService; |
| 90 | import com.android.server.pm.LauncherAppsService.LauncherAppsImpl; |
| 91 | import com.android.server.pm.ShortcutUser.PackageWithUser; |
| 92 | |
| 93 | import org.junit.Assert; |
| 94 | import org.mockito.ArgumentCaptor; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame^] | 95 | import org.mockito.invocation.InvocationOnMock; |
| 96 | import org.mockito.stubbing.Answer; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 97 | |
| 98 | import java.io.BufferedReader; |
| 99 | import java.io.ByteArrayOutputStream; |
| 100 | import java.io.File; |
| 101 | import java.io.FileReader; |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 102 | import java.io.IOException; |
| 103 | import java.io.InputStreamReader; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 104 | import java.io.PrintWriter; |
| 105 | import java.util.ArrayList; |
| 106 | import java.util.HashMap; |
| 107 | import java.util.HashSet; |
| 108 | import java.util.LinkedHashMap; |
| 109 | import java.util.List; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 110 | import java.util.Locale; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 111 | import java.util.Map; |
| 112 | import java.util.Set; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 113 | import java.util.function.BiFunction; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 114 | import java.util.function.BiPredicate; |
| 115 | import java.util.function.Consumer; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame^] | 116 | import java.util.function.Function; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 117 | |
| 118 | public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { |
| 119 | protected static final String TAG = "ShortcutManagerTest"; |
| 120 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 121 | protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true |
| 122 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Whether to enable dump or not. Should be only true when debugging to avoid bugs where |
| 125 | * dump affecting the behavior. |
| 126 | */ |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 127 | protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true |
| 128 | || DUMP_IN_TEARDOWN || ShortcutService.DEBUG; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 129 | |
| 130 | protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability. |
| 131 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 132 | protected static final String MAIN_ACTIVITY_CLASS = "MainActivity"; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 133 | protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity"; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 134 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 135 | // public for mockito |
| 136 | public class BaseContext extends MockContext { |
| 137 | @Override |
| 138 | public Object getSystemService(String name) { |
| 139 | switch (name) { |
| 140 | case Context.USER_SERVICE: |
| 141 | return mMockUserManager; |
| 142 | } |
| 143 | throw new UnsupportedOperationException(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String getSystemServiceName(Class<?> serviceClass) { |
| 148 | return getTestContext().getSystemServiceName(serviceClass); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public PackageManager getPackageManager() { |
| 153 | return mMockPackageManager; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Resources getResources() { |
| 158 | return getTestContext().getResources(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 163 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 164 | // ignore. |
| 165 | return null; |
| 166 | } |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 167 | |
| 168 | @Override |
| 169 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 170 | // ignore. |
| 171 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public void startActivityAsUser(Intent intent, UserHandle user) { |
| 175 | // ignore, use spy to intercept it. |
| 176 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** Context used in the client side */ |
| 180 | public class ClientContext extends BaseContext { |
| 181 | @Override |
| 182 | public String getPackageName() { |
| 183 | return mInjectedClientPackage; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int getUserId() { |
| 188 | return getCallingUserId(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** Context used in the service side */ |
| 193 | public class ServiceContext extends BaseContext { |
| 194 | long injectClearCallingIdentity() { |
| 195 | final int prevCallingUid = mInjectedCallingUid; |
| 196 | mInjectedCallingUid = Process.SYSTEM_UID; |
| 197 | return prevCallingUid; |
| 198 | } |
| 199 | |
| 200 | void injectRestoreCallingIdentity(long token) { |
| 201 | mInjectedCallingUid = (int) token; |
| 202 | } |
| 203 | |
| 204 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 205 | public int getUserId() { |
| 206 | return UserHandle.USER_SYSTEM; |
| 207 | } |
| 208 | |
| 209 | public PackageInfo injectGetActivitiesWithMetadata( |
| 210 | String packageName, @UserIdInt int userId) { |
| 211 | return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId); |
| 212 | } |
| 213 | |
| 214 | public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) { |
| 215 | return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key); |
| 216 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 217 | |
| 218 | public void sendIntentSender(IntentSender intent) { |
| 219 | // Placeholder for spying. |
| 220 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** ShortcutService with injection override methods. */ |
| 224 | protected final class ShortcutServiceTestable extends ShortcutService { |
| 225 | final ServiceContext mContext; |
| 226 | IUidObserver mUidObserver; |
| 227 | |
| 228 | public ShortcutServiceTestable(ServiceContext context, Looper looper) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 229 | super(context, looper, /* onyForPackageManagerApis */ false); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 230 | mContext = context; |
| 231 | } |
| 232 | |
| 233 | @Override |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 234 | public String injectGetLocaleTagsForUser(@UserIdInt int userId) { |
| 235 | return mInjectedLocale.toLanguageTag(); |
| 236 | } |
| 237 | |
| 238 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 239 | boolean injectShouldPerformVerification() { |
| 240 | return true; // Always verify during unit tests. |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | String injectShortcutManagerConstants() { |
| 245 | return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + "," |
| 246 | + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + "," |
| 247 | + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=" |
| 248 | + MAX_UPDATES_PER_INTERVAL + "," |
| 249 | + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + "," |
| 250 | + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=" |
| 251 | + MAX_ICON_DIMENSION_LOWRAM + "," |
| 252 | + ConfigConstants.KEY_ICON_FORMAT + "=PNG," |
| 253 | + ConfigConstants.KEY_ICON_QUALITY + "=100"; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | long injectClearCallingIdentity() { |
| 258 | return mContext.injectClearCallingIdentity(); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | void injectRestoreCallingIdentity(long token) { |
| 263 | mContext.injectRestoreCallingIdentity(token); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | int injectDipToPixel(int dip) { |
| 268 | return dip; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | long injectCurrentTimeMillis() { |
| 273 | return mInjectedCurrentTimeMillis; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | long injectElapsedRealtime() { |
| 278 | // TODO This should be kept separately from mInjectedCurrentTimeMillis, since |
| 279 | // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests. |
| 280 | return mInjectedCurrentTimeMillis - START_TIME; |
| 281 | } |
| 282 | |
| 283 | @Override |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 284 | long injectUptimeMillis() { |
| 285 | return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime; |
| 286 | } |
| 287 | |
| 288 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 289 | int injectBinderCallingUid() { |
| 290 | return mInjectedCallingUid; |
| 291 | } |
| 292 | |
| 293 | @Override |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 294 | int injectBinderCallingPid() { |
| 295 | // Note it's not used in tests, so just return a "random" value. |
| 296 | return mInjectedCallingUid * 123; |
| 297 | } |
| 298 | |
| 299 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 300 | int injectGetPackageUid(String packageName, int userId) { |
| 301 | return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | File injectSystemDataPath() { |
| 306 | return new File(mInjectedFilePathRoot, "system"); |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | File injectUserDataPath(@UserIdInt int userId) { |
| 311 | return new File(mInjectedFilePathRoot, "user-" + userId); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) { |
| 316 | // Can't check |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | boolean injectIsLowRamDevice() { |
| 321 | return mInjectedIsLowRamDevice; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | void injectRegisterUidObserver(IUidObserver observer, int which) { |
| 326 | mUidObserver = observer; |
| 327 | } |
| 328 | |
| 329 | @Override |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 330 | boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId, |
| 331 | int callingPid, int callingUid) { |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 332 | return mDefaultLauncherChecker.test(callingPackage, userId); |
| 333 | } |
| 334 | |
| 335 | @Override |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 336 | boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) { |
| 337 | return mInjectHasUnlimitedShortcutsApiCallsPermission; |
| 338 | } |
| 339 | |
| 340 | @Override |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 341 | ComponentName getDefaultLauncher(@UserIdInt int userId) { |
| 342 | final ComponentName activity = mDefaultLauncher.get(userId); |
| 343 | if (activity != null) { |
| 344 | return activity; |
| 345 | } |
| 346 | return super.getDefaultLauncher(userId); |
| 347 | } |
| 348 | |
| 349 | @Override |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 350 | PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId, |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 351 | boolean getSignatures) { |
| 352 | return getInjectedPackageInfo(packageName, userId, getSignatures); |
| 353 | } |
| 354 | |
| 355 | @Override |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 356 | ApplicationInfo injectApplicationInfoWithUninstalled( |
| 357 | String packageName, @UserIdInt int userId) { |
| 358 | PackageInfo pi = injectPackageInfoWithUninstalled( |
| 359 | packageName, userId, /* getSignatures= */ false); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 360 | return pi != null ? pi.applicationInfo : null; |
| 361 | } |
| 362 | |
| 363 | @Override |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 364 | List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) { |
| 365 | return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 369 | ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity, |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 370 | @UserIdInt int userId) { |
| 371 | final PackageInfo pi = mContext.injectGetActivitiesWithMetadata( |
| 372 | activity.getPackageName(), userId); |
| 373 | if (pi == null || pi.activities == null) { |
| 374 | return null; |
| 375 | } |
| 376 | for (ActivityInfo ai : pi.activities) { |
| 377 | if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) { |
| 378 | continue; |
| 379 | } |
| 380 | if (activity.equals(ai.getComponentName())) { |
| 381 | return ai; |
| 382 | } |
| 383 | } |
| 384 | return null; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) { |
| 389 | if (!mEnabledActivityChecker.test(activity, userId)) { |
| 390 | return false; |
| 391 | } |
| 392 | return mMainActivityChecker.test(activity, userId); |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) { |
| 397 | final PackageInfo pi = mContext.injectGetActivitiesWithMetadata( |
| 398 | packageName, userId); |
| 399 | if (pi == null || pi.activities == null) { |
| 400 | return null; |
| 401 | } |
| 402 | final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length); |
| 403 | for (int i = 0; i < pi.activities.length; i++) { |
| 404 | if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) { |
| 405 | continue; |
| 406 | } |
| 407 | final ResolveInfo ri = new ResolveInfo(); |
| 408 | ri.activityInfo = pi.activities[i]; |
| 409 | ret.add(ri); |
| 410 | } |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) { |
| 417 | return mMainActivityFetcher.apply(packageName, userId); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | @Override |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 421 | ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName, |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 422 | int launcherUserId, int requestType) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 423 | return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId); |
| 424 | } |
| 425 | |
| 426 | @Override |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 427 | boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) { |
| 428 | return mEnabledActivityChecker.test(activity, userId); |
| 429 | } |
| 430 | |
| 431 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 432 | XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) { |
| 433 | return mContext.injectXmlMetaData(activityInfo, key); |
| 434 | } |
| 435 | |
| 436 | @Override |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 437 | void injectPostToHandler(Runnable r) { |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 438 | runOnHandler(r); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 442 | void injectRunOnNewThread(Runnable r) { |
| 443 | runOnHandler(r); |
| 444 | } |
| 445 | |
| 446 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 447 | void injectEnforceCallingPermission(String permission, String message) { |
| 448 | if (!mCallerPermissions.contains(permission)) { |
| 449 | throw new SecurityException("Missing permission: " + permission); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | @Override |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 454 | boolean injectIsSafeModeEnabled() { |
| 455 | return mSafeMode; |
| 456 | } |
| 457 | |
| 458 | @Override |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 459 | String injectBuildFingerprint() { |
| 460 | return mInjectedBuildFingerprint; |
| 461 | } |
| 462 | |
| 463 | @Override |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 464 | void injectSendIntentSender(IntentSender intent, Intent extras) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 465 | mContext.sendIntentSender(intent); |
| 466 | } |
| 467 | |
| 468 | @Override |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 469 | boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) { |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 470 | return mInjectCheckAccessShortcutsPermission; |
| 471 | } |
| 472 | |
| 473 | @Override |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 474 | void wtf(String message, Throwable th) { |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 475 | // During tests, WTF is fatal. |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 476 | fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th)); |
| 477 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** ShortcutManager with injection override methods. */ |
| 481 | protected class ShortcutManagerTestable extends ShortcutManager { |
| 482 | public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) { |
| 483 | super(context, service); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | protected int injectMyUserId() { |
| 488 | return UserHandle.getUserId(mInjectedCallingUid); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) { |
| 493 | // Note to simulate the binder RPC, we need to clone the incoming arguments. |
| 494 | // Otherwise bad things will happen because they're mutable. |
| 495 | return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList)); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) { |
| 500 | // Note to simulate the binder RPC, we need to clone the incoming arguments. |
| 501 | return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList)); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) { |
| 506 | // Note to simulate the binder RPC, we need to clone the incoming arguments. |
| 507 | return super.updateShortcuts(cloneShortcutList(shortcutInfoList)); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | protected class LauncherAppImplTestable extends LauncherAppsImpl { |
| 512 | final ServiceContext mContext; |
| 513 | |
| 514 | public LauncherAppImplTestable(ServiceContext context) { |
| 515 | super(context); |
| 516 | mContext = context; |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void verifyCallingPackage(String callingPackage) { |
| 521 | // SKIP |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | void postToPackageMonitorHandler(Runnable r) { |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 526 | runOnHandler(r); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | @Override |
| 530 | int injectBinderCallingUid() { |
| 531 | return mInjectedCallingUid; |
| 532 | } |
| 533 | |
| 534 | @Override |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 535 | int injectBinderCallingPid() { |
| 536 | // Note it's not used in tests, so just return a "random" value. |
| 537 | return mInjectedCallingUid * 123; |
| 538 | } |
| 539 | |
| 540 | @Override |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 541 | long injectClearCallingIdentity() { |
| 542 | final int prevCallingUid = mInjectedCallingUid; |
| 543 | mInjectedCallingUid = Process.SYSTEM_UID; |
| 544 | return prevCallingUid; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | void injectRestoreCallingIdentity(long token) { |
| 549 | mInjectedCallingUid = (int) token; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | protected class LauncherAppsTestable extends LauncherApps { |
| 554 | public LauncherAppsTestable(Context context, ILauncherApps service) { |
| 555 | super(context, service); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | public static class ShortcutActivity extends Activity { |
| 560 | } |
| 561 | |
| 562 | public static class ShortcutActivity2 extends Activity { |
| 563 | } |
| 564 | |
| 565 | public static class ShortcutActivity3 extends Activity { |
| 566 | } |
| 567 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 568 | protected Looper mLooper; |
| 569 | protected Handler mHandler; |
| 570 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 571 | protected ServiceContext mServiceContext; |
| 572 | protected ClientContext mClientContext; |
| 573 | |
| 574 | protected ShortcutServiceTestable mService; |
| 575 | protected ShortcutManagerTestable mManager; |
| 576 | protected ShortcutServiceInternal mInternal; |
| 577 | |
| 578 | protected LauncherAppImplTestable mLauncherAppImpl; |
| 579 | |
| 580 | // LauncherApps has per-instace state, so we need a differnt instance for each launcher. |
| 581 | protected final Map<Pair<Integer, String>, LauncherAppsTestable> |
| 582 | mLauncherAppsMap = new HashMap<>(); |
| 583 | protected LauncherAppsTestable mLauncherApps; // Current one |
| 584 | |
| 585 | protected File mInjectedFilePathRoot; |
| 586 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 587 | protected boolean mSafeMode; |
| 588 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 589 | protected long mInjectedCurrentTimeMillis; |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 590 | protected long mDeepSleepTime; // Used to calculate "uptimeMillis". |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 591 | |
| 592 | protected boolean mInjectedIsLowRamDevice; |
| 593 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 594 | protected Locale mInjectedLocale = Locale.ENGLISH; |
| 595 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 596 | protected int mInjectedCallingUid; |
| 597 | protected String mInjectedClientPackage; |
| 598 | |
| 599 | protected Map<String, PackageInfo> mInjectedPackages; |
| 600 | |
| 601 | protected Set<PackageWithUser> mUninstalledPackages; |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 602 | protected Set<PackageWithUser> mDisabledPackages; |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 603 | protected Set<PackageWithUser> mEphemeralPackages; |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 604 | protected Set<String> mSystemPackages; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 605 | |
| 606 | protected PackageManager mMockPackageManager; |
| 607 | protected PackageManagerInternal mMockPackageManagerInternal; |
| 608 | protected UserManager mMockUserManager; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 609 | protected UserManagerInternal mMockUserManagerInternal; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 610 | protected UsageStatsManagerInternal mMockUsageStatsManagerInternal; |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 611 | protected ActivityManagerInternal mMockActivityManagerInternal; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 612 | |
| 613 | protected static final String CALLING_PACKAGE_1 = "com.android.test.1"; |
| 614 | protected static final int CALLING_UID_1 = 10001; |
| 615 | |
| 616 | protected static final String CALLING_PACKAGE_2 = "com.android.test.2"; |
| 617 | protected static final int CALLING_UID_2 = 10002; |
| 618 | |
| 619 | protected static final String CALLING_PACKAGE_3 = "com.android.test.3"; |
| 620 | protected static final int CALLING_UID_3 = 10003; |
| 621 | |
| 622 | protected static final String CALLING_PACKAGE_4 = "com.android.test.4"; |
| 623 | protected static final int CALLING_UID_4 = 10004; |
| 624 | |
| 625 | protected static final String LAUNCHER_1 = "com.android.launcher.1"; |
| 626 | protected static final int LAUNCHER_UID_1 = 10011; |
| 627 | |
| 628 | protected static final String LAUNCHER_2 = "com.android.launcher.2"; |
| 629 | protected static final int LAUNCHER_UID_2 = 10012; |
| 630 | |
| 631 | protected static final String LAUNCHER_3 = "com.android.launcher.3"; |
| 632 | protected static final int LAUNCHER_UID_3 = 10013; |
| 633 | |
| 634 | protected static final String LAUNCHER_4 = "com.android.launcher.4"; |
| 635 | protected static final int LAUNCHER_UID_4 = 10014; |
| 636 | |
| 637 | protected static final int USER_0 = UserHandle.USER_SYSTEM; |
| 638 | protected static final int USER_10 = 10; |
| 639 | protected static final int USER_11 = 11; |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 640 | protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set) |
| 641 | protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set) |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 642 | |
| 643 | protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0); |
| 644 | protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10); |
| 645 | protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11); |
| 646 | protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 647 | protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 648 | |
| 649 | protected static final UserInfo USER_INFO_0 = withProfileGroupId( |
| 650 | new UserInfo(USER_0, "user0", |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 651 | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 652 | |
| 653 | protected static final UserInfo USER_INFO_10 = |
| 654 | new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED); |
| 655 | |
| 656 | protected static final UserInfo USER_INFO_11 = |
| 657 | new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED); |
| 658 | |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 659 | /* |
| 660 | * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set. |
| 661 | * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able |
| 662 | * to see the main profile, which would break tons of unit tests. We avoid it by not setting |
| 663 | * MANAGED_PROFILE for P0. |
| 664 | * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile |
| 665 | * can't access main profile's shortcuts.) |
| 666 | */ |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 667 | protected static final UserInfo USER_INFO_P0 = withProfileGroupId( |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 668 | new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 669 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 670 | protected static final UserInfo USER_INFO_P1 = withProfileGroupId( |
| 671 | new UserInfo(USER_P1, "userP1", |
| 672 | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0); |
| 673 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 674 | protected BiPredicate<String, Integer> mDefaultLauncherChecker = |
| 675 | (callingPackage, userId) -> |
| 676 | LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage) |
| 677 | || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage); |
| 678 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 679 | private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>(); |
| 680 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 681 | protected BiPredicate<ComponentName, Integer> mMainActivityChecker = |
| 682 | (activity, userId) -> true; |
| 683 | |
| 684 | protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher = |
| 685 | (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS); |
| 686 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 687 | protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher = |
| 688 | (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS); |
| 689 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 690 | protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker |
| 691 | = (activity, userId) -> true; // all activities are enabled. |
| 692 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 693 | protected static final long START_TIME = 1440000000101L; |
| 694 | |
| 695 | protected static final long INTERVAL = 10000; |
| 696 | |
| 697 | protected static final int MAX_SHORTCUTS = 10; |
| 698 | |
| 699 | protected static final int MAX_UPDATES_PER_INTERVAL = 3; |
| 700 | |
| 701 | protected static final int MAX_ICON_DIMENSION = 128; |
| 702 | |
| 703 | protected static final int MAX_ICON_DIMENSION_LOWRAM = 32; |
| 704 | |
| 705 | protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery(); |
| 706 | |
| 707 | protected final ArrayList<String> mCallerPermissions = new ArrayList<>(); |
| 708 | |
| 709 | protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId |
| 710 | = new HashMap<>(); |
| 711 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 712 | protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>(); |
| 713 | protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>(); |
| 714 | protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>(); |
| 715 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 716 | protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher"; |
| 717 | protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name"; |
| 718 | protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0; |
| 719 | |
| 720 | protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings"; |
| 721 | protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback"; |
| 722 | protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999; |
| 723 | |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 724 | protected String mInjectedBuildFingerprint = "build1"; |
| 725 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 726 | protected boolean mInjectCheckAccessShortcutsPermission = false; |
| 727 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 728 | protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false; |
| 729 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 730 | static { |
| 731 | QUERY_ALL.setQueryFlags( |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 732 | ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | @Override |
| 736 | protected void setUp() throws Exception { |
| 737 | super.setUp(); |
| 738 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 739 | mLooper = Looper.getMainLooper(); |
| 740 | mHandler = new Handler(mLooper); |
| 741 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 742 | mServiceContext = spy(new ServiceContext()); |
| 743 | mClientContext = new ClientContext(); |
| 744 | |
| 745 | mMockPackageManager = mock(PackageManager.class); |
| 746 | mMockPackageManagerInternal = mock(PackageManagerInternal.class); |
| 747 | mMockUserManager = mock(UserManager.class); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 748 | mMockUserManagerInternal = mock(UserManagerInternal.class); |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 749 | mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 750 | mMockActivityManagerInternal = mock(ActivityManagerInternal.class); |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 751 | |
| 752 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 753 | LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal); |
| 754 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
| 755 | LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 756 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 757 | LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 758 | LocalServices.removeServiceForTest(UserManagerInternal.class); |
| 759 | LocalServices.addService(UserManagerInternal.class, mMockUserManagerInternal); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 760 | |
| 761 | // Prepare injection values. |
| 762 | |
| 763 | mInjectedCurrentTimeMillis = START_TIME; |
| 764 | |
| 765 | mInjectedPackages = new HashMap<>(); |
| 766 | addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1); |
| 767 | addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2); |
| 768 | addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3); |
| 769 | addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10); |
| 770 | addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4); |
| 771 | addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5); |
| 772 | addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6); |
| 773 | addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10); |
| 774 | |
| 775 | // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target. |
| 776 | updatePackageInfo(CALLING_PACKAGE_3, |
| 777 | pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP); |
| 778 | updatePackageInfo(LAUNCHER_3, |
| 779 | pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP); |
| 780 | |
| 781 | mUninstalledPackages = new HashSet<>(); |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 782 | mDisabledPackages = new HashSet<>(); |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 783 | mSystemPackages = new HashSet<>(); |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 784 | mEphemeralPackages = new HashSet<>(); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 785 | |
| 786 | mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files"); |
| 787 | |
| 788 | deleteAllSavedFiles(); |
| 789 | |
| 790 | // Set up users. |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 791 | mUserInfos.put(USER_0, USER_INFO_0); |
| 792 | mUserInfos.put(USER_10, USER_INFO_10); |
| 793 | mUserInfos.put(USER_11, USER_INFO_11); |
| 794 | mUserInfos.put(USER_P0, USER_INFO_P0); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 795 | mUserInfos.put(USER_P1, USER_INFO_P1); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 796 | |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 797 | when(mMockUserManagerInternal.isUserUnlockingOrUnlocked(anyInt())) |
| 798 | .thenAnswer(inv -> { |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 799 | final int userId = (Integer) inv.getArguments()[0]; |
| 800 | return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId)); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 801 | }); |
| 802 | when(mMockUserManagerInternal.getProfileParentId(anyInt())) |
| 803 | .thenAnswer(inv -> { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 804 | final int userId = (Integer) inv.getArguments()[0]; |
| 805 | final UserInfo ui = mUserInfos.get(userId); |
| 806 | assertNotNull(ui); |
| 807 | if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 808 | return userId; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 809 | } |
| 810 | final UserInfo parent = mUserInfos.get(ui.profileGroupId); |
| 811 | assertNotNull(parent); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 812 | return parent.id; |
| 813 | }); |
| 814 | |
| 815 | when(mMockUserManagerInternal.isProfileAccessible(anyInt(), anyInt(), anyString(), |
| 816 | anyBoolean())).thenAnswer(inv -> { |
| 817 | final int callingUserId = (Integer) inv.getArguments()[0]; |
| 818 | final int targetUserId = (Integer) inv.getArguments()[1]; |
| 819 | if (targetUserId == callingUserId) { |
| 820 | return true; |
| 821 | } |
| 822 | final UserInfo callingUserInfo = mUserInfos.get(callingUserId); |
| 823 | final UserInfo targetUserInfo = mUserInfos.get(targetUserId); |
| 824 | if (callingUserInfo == null || callingUserInfo.isManagedProfile() |
| 825 | || targetUserInfo == null || !targetUserInfo.isEnabled()) { |
| 826 | return false; |
| 827 | } |
| 828 | if (targetUserInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 829 | && targetUserInfo.profileGroupId == callingUserInfo.profileGroupId) { |
| 830 | return true; |
| 831 | } |
| 832 | final boolean isExternal = (Boolean) inv.getArguments()[3]; |
| 833 | if (!isExternal) { |
| 834 | return false; |
| 835 | } |
| 836 | throw new SecurityException(inv.getArguments()[2] + " for unrelated profile " |
| 837 | + targetUserId); |
| 838 | }); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 839 | |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame^] | 840 | when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>( |
| 841 | inv -> mUserInfos.get((Integer) inv.getArguments()[0]))); |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 842 | when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn( |
| 843 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 844 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 845 | // User 0 and P0 are always running |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 846 | mRunningUsers.put(USER_0, true); |
| 847 | mRunningUsers.put(USER_10, false); |
| 848 | mRunningUsers.put(USER_11, false); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 849 | mRunningUsers.put(USER_P0, true); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 850 | mRunningUsers.put(USER_P1, true); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 851 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 852 | // Unlock all users by default. |
| 853 | mUnlockedUsers.put(USER_0, true); |
| 854 | mUnlockedUsers.put(USER_10, true); |
| 855 | mUnlockedUsers.put(USER_11, true); |
| 856 | mUnlockedUsers.put(USER_P0, true); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 857 | mUnlockedUsers.put(USER_P1, true); |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 858 | |
| 859 | // Set up resources |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 860 | setUpAppResources(); |
| 861 | |
| 862 | // Start the service. |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 863 | initService(); |
| 864 | setCaller(CALLING_PACKAGE_1); |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 865 | |
| 866 | if (ENABLE_DUMP) { |
| 867 | Log.d(TAG, "setUp done"); |
| 868 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame^] | 871 | /** |
| 872 | * Returns a boolean but also checks if the current UID is SYSTEM_UID. |
| 873 | */ |
| 874 | protected class AnswerWithSystemCheck<T> implements Answer<T> { |
| 875 | private final Function<InvocationOnMock, T> mChecker; |
| 876 | |
| 877 | public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) { |
| 878 | mChecker = checker; |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public T answer(InvocationOnMock invocation) throws Throwable { |
| 883 | assertEquals("Must be called on SYSTEM UID.", |
| 884 | Process.SYSTEM_UID, mInjectedCallingUid); |
| 885 | return mChecker.apply(invocation); |
| 886 | } |
| 887 | } |
| 888 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 889 | private static boolean b(Boolean value) { |
| 890 | return (value != null && value); |
| 891 | } |
| 892 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 893 | protected void setUpAppResources() throws Exception { |
| 894 | setUpAppResources(/* offset = */ 0); |
| 895 | } |
| 896 | |
| 897 | protected void setUpAppResources(int ressIdOffset) throws Exception { |
| 898 | // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app |
| 899 | // has resource IDs changed. |
| 900 | |
| 901 | doAnswer(pmInvocation -> { |
| 902 | assertEquals(Process.SYSTEM_UID, mInjectedCallingUid); |
| 903 | |
| 904 | final String packageName = (String) pmInvocation.getArguments()[0]; |
| 905 | final int userId = (Integer) pmInvocation.getArguments()[1]; |
| 906 | |
| 907 | final Resources res = mock(Resources.class); |
| 908 | |
| 909 | doAnswer(resInvocation -> { |
| 910 | final int argResId = (Integer) resInvocation.getArguments()[0]; |
| 911 | |
| 912 | return "string-" + packageName + "-user:" + userId + "-res:" + argResId |
| 913 | + "/" + mInjectedLocale; |
| 914 | }).when(res).getString(anyInt()); |
| 915 | |
| 916 | doAnswer(resInvocation -> { |
| 917 | final int resId = (Integer) resInvocation.getArguments()[0]; |
| 918 | |
| 919 | // Always use the "string" resource type. The type doesn't matter during the test. |
| 920 | return packageName + ":string/r" + resId; |
| 921 | }).when(res).getResourceName(anyInt()); |
| 922 | |
| 923 | doAnswer(resInvocation -> { |
| 924 | final String argResName = (String) resInvocation.getArguments()[0]; |
| 925 | final String argType = (String) resInvocation.getArguments()[1]; |
| 926 | final String argPackageName = (String) resInvocation.getArguments()[2]; |
| 927 | |
| 928 | // See the above code. getResourceName() will just use "r" + res ID as the entry |
| 929 | // name. |
| 930 | String entryName = argResName; |
| 931 | if (entryName.contains("/")) { |
| 932 | entryName = ShortcutInfo.getResourceEntryName(entryName); |
| 933 | } |
| 934 | return Integer.parseInt(entryName.substring(1)) + ressIdOffset; |
Makoto Onuki | d0010c5 | 2017-03-30 14:17:35 -0700 | [diff] [blame] | 935 | }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 936 | return res; |
| 937 | }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt()); |
| 938 | } |
| 939 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 940 | protected static UserInfo withProfileGroupId(UserInfo in, int groupId) { |
| 941 | in.profileGroupId = groupId; |
| 942 | return in; |
| 943 | } |
| 944 | |
| 945 | @Override |
| 946 | protected void tearDown() throws Exception { |
| 947 | if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown"); |
| 948 | |
| 949 | shutdownServices(); |
| 950 | |
| 951 | super.tearDown(); |
| 952 | } |
| 953 | |
| 954 | protected Context getTestContext() { |
| 955 | return getInstrumentation().getContext(); |
| 956 | } |
| 957 | |
Makoto Onuki | a1d38b3 | 2016-06-10 15:32:26 -0700 | [diff] [blame] | 958 | protected ShortcutManager getManager() { |
| 959 | return mManager; |
| 960 | } |
| 961 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 962 | protected void deleteAllSavedFiles() { |
| 963 | // Empty the data directory. |
| 964 | if (mInjectedFilePathRoot.exists()) { |
| 965 | Assert.assertTrue("failed to delete dir", |
| 966 | FileUtils.deleteContents(mInjectedFilePathRoot)); |
| 967 | } |
| 968 | mInjectedFilePathRoot.mkdirs(); |
| 969 | } |
| 970 | |
| 971 | /** (Re-) init the manager and the service. */ |
| 972 | protected void initService() { |
| 973 | shutdownServices(); |
| 974 | |
| 975 | LocalServices.removeServiceForTest(ShortcutServiceInternal.class); |
| 976 | |
| 977 | // Instantiate targets. |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 978 | mService = new ShortcutServiceTestable(mServiceContext, mLooper); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 979 | mManager = new ShortcutManagerTestable(mClientContext, mService); |
| 980 | |
| 981 | mInternal = LocalServices.getService(ShortcutServiceInternal.class); |
| 982 | |
| 983 | mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext); |
| 984 | mLauncherApps = null; |
| 985 | mLauncherAppsMap.clear(); |
| 986 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 987 | // Send boot sequence events. |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 988 | mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 989 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 990 | mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | protected void shutdownServices() { |
| 994 | if (mService != null) { |
| 995 | // Flush all the unsaved data from the previous instance. |
| 996 | mService.saveDirtyInfo(); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 997 | |
| 998 | // Make sure everything is consistent. |
| 999 | mService.verifyStates(); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1000 | } |
| 1001 | LocalServices.removeServiceForTest(ShortcutServiceInternal.class); |
| 1002 | |
| 1003 | mService = null; |
| 1004 | mManager = null; |
| 1005 | mInternal = null; |
| 1006 | mLauncherAppImpl = null; |
| 1007 | mLauncherApps = null; |
| 1008 | mLauncherAppsMap.clear(); |
| 1009 | } |
| 1010 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 1011 | protected void runOnHandler(Runnable r) { |
| 1012 | final long token = mServiceContext.injectClearCallingIdentity(); |
| 1013 | try { |
| 1014 | r.run(); |
| 1015 | } finally { |
| 1016 | mServiceContext.injectRestoreCallingIdentity(token); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1020 | protected void addPackage(String packageName, int uid, int version) { |
| 1021 | addPackage(packageName, uid, version, packageName); |
| 1022 | } |
| 1023 | |
| 1024 | protected Signature[] genSignatures(String... signatures) { |
| 1025 | final Signature[] sigs = new Signature[signatures.length]; |
| 1026 | for (int i = 0; i < signatures.length; i++){ |
| 1027 | sigs[i] = new Signature(signatures[i].getBytes()); |
| 1028 | } |
| 1029 | return sigs; |
| 1030 | } |
| 1031 | |
| 1032 | protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) { |
| 1033 | final PackageInfo pi = new PackageInfo(); |
| 1034 | pi.packageName = packageName; |
| 1035 | pi.applicationInfo = new ApplicationInfo(); |
| 1036 | pi.applicationInfo.uid = uid; |
| 1037 | pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED |
| 1038 | | ApplicationInfo.FLAG_ALLOW_BACKUP; |
| 1039 | pi.versionCode = version; |
| 1040 | pi.applicationInfo.versionCode = version; |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 1041 | pi.signatures = null; |
| 1042 | pi.signingCertificateHistory = new Signature[][] {genSignatures(signatures)}; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1043 | |
| 1044 | return pi; |
| 1045 | } |
| 1046 | |
| 1047 | protected void addPackage(String packageName, int uid, int version, String... signatures) { |
| 1048 | mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures)); |
| 1049 | } |
| 1050 | |
| 1051 | protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) { |
| 1052 | c.accept(mInjectedPackages.get(packageName)); |
| 1053 | } |
| 1054 | |
| 1055 | protected void updatePackageVersion(String packageName, int increment) { |
| 1056 | updatePackageInfo(packageName, pi -> { |
| 1057 | pi.versionCode += increment; |
| 1058 | pi.applicationInfo.versionCode += increment; |
| 1059 | }); |
| 1060 | } |
| 1061 | |
| 1062 | protected void updatePackageLastUpdateTime(String packageName, long increment) { |
| 1063 | updatePackageInfo(packageName, pi -> { |
| 1064 | pi.lastUpdateTime += increment; |
| 1065 | }); |
| 1066 | } |
| 1067 | |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 1068 | protected void setPackageLastUpdateTime(String packageName, long value) { |
| 1069 | updatePackageInfo(packageName, pi -> { |
| 1070 | pi.lastUpdateTime = value; |
| 1071 | }); |
| 1072 | } |
| 1073 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1074 | protected void uninstallPackage(int userId, String packageName) { |
| 1075 | if (ENABLE_DUMP) { |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 1076 | Log.v(TAG, "Uninstall package " + packageName + " / " + userId); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1077 | } |
| 1078 | mUninstalledPackages.add(PackageWithUser.of(userId, packageName)); |
| 1079 | } |
| 1080 | |
| 1081 | protected void installPackage(int userId, String packageName) { |
| 1082 | if (ENABLE_DUMP) { |
| 1083 | Log.v(TAG, "Install package " + packageName + " / " + userId); |
| 1084 | } |
| 1085 | mUninstalledPackages.remove(PackageWithUser.of(userId, packageName)); |
| 1086 | } |
| 1087 | |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 1088 | protected void disablePackage(int userId, String packageName) { |
| 1089 | if (ENABLE_DUMP) { |
| 1090 | Log.v(TAG, "Disable package " + packageName + " / " + userId); |
| 1091 | } |
| 1092 | mDisabledPackages.add(PackageWithUser.of(userId, packageName)); |
| 1093 | } |
| 1094 | |
| 1095 | protected void enablePackage(int userId, String packageName) { |
| 1096 | if (ENABLE_DUMP) { |
| 1097 | Log.v(TAG, "Enable package " + packageName + " / " + userId); |
| 1098 | } |
| 1099 | mDisabledPackages.remove(PackageWithUser.of(userId, packageName)); |
| 1100 | } |
| 1101 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1102 | PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId, |
| 1103 | boolean getSignatures) { |
| 1104 | final PackageInfo pi = mInjectedPackages.get(packageName); |
| 1105 | if (pi == null) return null; |
| 1106 | |
| 1107 | final PackageInfo ret = new PackageInfo(); |
| 1108 | ret.packageName = pi.packageName; |
| 1109 | ret.versionCode = pi.versionCode; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1110 | ret.versionCodeMajor = pi.versionCodeMajor; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1111 | ret.lastUpdateTime = pi.lastUpdateTime; |
| 1112 | |
| 1113 | ret.applicationInfo = new ApplicationInfo(pi.applicationInfo); |
| 1114 | ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid); |
| 1115 | ret.applicationInfo.packageName = pi.packageName; |
| 1116 | |
| 1117 | if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) { |
| 1118 | ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED; |
| 1119 | } |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 1120 | if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) { |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1121 | ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT; |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 1122 | } |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 1123 | if (mSystemPackages.contains(packageName)) { |
| 1124 | ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 1125 | } |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 1126 | ret.applicationInfo.enabled = |
| 1127 | !mDisabledPackages.contains(PackageWithUser.of(userId, packageName)); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1128 | |
| 1129 | if (getSignatures) { |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 1130 | ret.signatures = null; |
| 1131 | ret.signingCertificateHistory = pi.signingCertificateHistory; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) { |
| 1138 | if (pi != null && pi.applicationInfo != null) { |
| 1139 | list.add(pi.applicationInfo); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | protected List<ApplicationInfo> getInstalledApplications(int userId) { |
| 1144 | final ArrayList<ApplicationInfo> ret = new ArrayList<>(); |
| 1145 | |
| 1146 | addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret); |
| 1147 | addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret); |
| 1148 | addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret); |
| 1149 | addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret); |
| 1150 | addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret); |
| 1151 | addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret); |
| 1152 | addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret); |
| 1153 | addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret); |
| 1154 | |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 1158 | private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) { |
| 1159 | if (pi != null) { |
| 1160 | list.add(pi); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1164 | private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) { |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 1165 | final ArrayList<PackageInfo> ret = new ArrayList<>(); |
| 1166 | |
| 1167 | addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret); |
| 1168 | addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret); |
| 1169 | addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret); |
| 1170 | addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret); |
| 1171 | addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret); |
| 1172 | addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret); |
| 1173 | addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret); |
| 1174 | addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret); |
| 1175 | |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1179 | protected void addManifestShortcutResource(ComponentName activity, int resId) { |
| 1180 | final String packageName = activity.getPackageName(); |
| 1181 | LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName); |
| 1182 | if (map == null) { |
| 1183 | map = new LinkedHashMap<>(); |
| 1184 | mActivityMetadataResId.put(packageName, map); |
| 1185 | } |
| 1186 | map.put(activity, resId); |
| 1187 | } |
| 1188 | |
| 1189 | protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) { |
| 1190 | final PackageInfo ret = getInjectedPackageInfo(packageName, userId, |
| 1191 | /* getSignatures=*/ false); |
| 1192 | |
| 1193 | final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName); |
| 1194 | if (activities != null) { |
| 1195 | final ArrayList<ActivityInfo> list = new ArrayList<>(); |
| 1196 | |
| 1197 | for (ComponentName cn : activities.keySet()) { |
| 1198 | ActivityInfo ai = new ActivityInfo(); |
| 1199 | ai.packageName = cn.getPackageName(); |
| 1200 | ai.name = cn.getClassName(); |
| 1201 | ai.metaData = new Bundle(); |
| 1202 | ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn)); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1203 | ai.applicationInfo = ret.applicationInfo; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1204 | list.add(ai); |
| 1205 | } |
| 1206 | ret.activities = list.toArray(new ActivityInfo[list.size()]); |
| 1207 | } |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) { |
| 1212 | if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) { |
| 1213 | return null; |
| 1214 | } |
| 1215 | final int resId = activityInfo.metaData.getInt(key); |
| 1216 | return getTestContext().getResources().getXml(resId); |
| 1217 | } |
| 1218 | |
| 1219 | /** Replace the current calling package */ |
| 1220 | protected void setCaller(String packageName, int userId) { |
| 1221 | mInjectedClientPackage = packageName; |
| 1222 | mInjectedCallingUid = |
| 1223 | Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false), |
| 1224 | "Unknown package").applicationInfo.uid; |
| 1225 | |
| 1226 | // Set up LauncherApps for this caller. |
| 1227 | final Pair<Integer, String> key = Pair.create(userId, packageName); |
| 1228 | if (!mLauncherAppsMap.containsKey(key)) { |
| 1229 | mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl)); |
| 1230 | } |
| 1231 | mLauncherApps = mLauncherAppsMap.get(key); |
| 1232 | } |
| 1233 | |
| 1234 | protected void setCaller(String packageName) { |
| 1235 | setCaller(packageName, UserHandle.USER_SYSTEM); |
| 1236 | } |
| 1237 | |
| 1238 | protected String getCallingPackage() { |
| 1239 | return mInjectedClientPackage; |
| 1240 | } |
| 1241 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1242 | /** |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1243 | * This controls {@link ShortcutService#hasShortcutHostPermission}, but |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1244 | * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use |
| 1245 | * {@link #setDefaultLauncher(int, ComponentName)}. |
| 1246 | */ |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1247 | protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) { |
| 1248 | mDefaultLauncherChecker = p; |
| 1249 | } |
| 1250 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1251 | /** |
| 1252 | * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by |
| 1253 | * {@link #setDefaultLauncherChecker} too. |
| 1254 | */ |
| 1255 | protected void setDefaultLauncher(int userId, ComponentName launcherActivity) { |
| 1256 | mDefaultLauncher.put(userId, launcherActivity); |
| 1257 | |
| 1258 | final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker; |
| 1259 | mDefaultLauncherChecker = (checkPackageName, checkUserId) -> { |
| 1260 | if ((checkUserId == userId) && (launcherActivity != null)) { |
| 1261 | return launcherActivity.getPackageName().equals(checkPackageName); |
| 1262 | } |
| 1263 | return oldChecker.test(checkPackageName, checkUserId); |
| 1264 | }; |
| 1265 | } |
| 1266 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1267 | protected void runWithCaller(String packageName, int userId, Runnable r) { |
| 1268 | final String previousPackage = mInjectedClientPackage; |
| 1269 | final int previousUserId = UserHandle.getUserId(mInjectedCallingUid); |
| 1270 | |
| 1271 | setCaller(packageName, userId); |
| 1272 | |
| 1273 | r.run(); |
| 1274 | |
| 1275 | setCaller(previousPackage, previousUserId); |
| 1276 | } |
| 1277 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1278 | protected void runWithSystemUid(Runnable r) { |
| 1279 | final int origUid = mInjectedCallingUid; |
| 1280 | mInjectedCallingUid = Process.SYSTEM_UID; |
| 1281 | r.run(); |
| 1282 | mInjectedCallingUid = origUid; |
| 1283 | } |
| 1284 | |
| 1285 | protected void lookupAndFillInResourceNames(ShortcutInfo si) { |
| 1286 | runWithSystemUid(() -> si.lookupAndFillInResourceNames( |
| 1287 | mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId()))); |
| 1288 | } |
| 1289 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1290 | protected int getCallingUserId() { |
| 1291 | return UserHandle.getUserId(mInjectedCallingUid); |
| 1292 | } |
| 1293 | |
| 1294 | protected UserHandle getCallingUser() { |
| 1295 | return UserHandle.of(getCallingUserId()); |
| 1296 | } |
| 1297 | |
| 1298 | /** For debugging */ |
| 1299 | protected void dumpsysOnLogcat() { |
| 1300 | dumpsysOnLogcat(""); |
| 1301 | } |
| 1302 | |
| 1303 | protected void dumpsysOnLogcat(String message) { |
| 1304 | dumpsysOnLogcat(message, false); |
| 1305 | } |
| 1306 | |
| 1307 | protected void dumpsysOnLogcat(String message, boolean force) { |
| 1308 | if (force || !ENABLE_DUMP) return; |
| 1309 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1310 | Log.v(TAG, "Dumping ShortcutService: " + message); |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 1311 | for (String line : dumpsys("-u").split("\n")) { |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1312 | Log.v(TAG, line); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1316 | protected String dumpCheckin() { |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 1317 | return dumpsys("--checkin"); |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 1320 | protected String dumpsys(String... args) { |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1321 | final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions); |
| 1322 | mCallerPermissions.add(android.Manifest.permission.DUMP); |
| 1323 | try { |
| 1324 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 1325 | final PrintWriter pw = new PrintWriter(out); |
Makoto Onuki | c4361e3 | 2017-04-03 11:24:25 -0700 | [diff] [blame] | 1326 | mService.dumpNoCheck(/* fd */ null, pw, args); |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1327 | pw.close(); |
| 1328 | |
| 1329 | return out.toString(); |
| 1330 | } finally { |
| 1331 | mCallerPermissions.clear(); |
| 1332 | mCallerPermissions.addAll(origPermissions); |
| 1333 | } |
| 1334 | } |
| 1335 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1336 | /** |
| 1337 | * For debugging, dump arbitrary file on logcat. |
| 1338 | */ |
| 1339 | protected void dumpFileOnLogcat(String path) { |
| 1340 | dumpFileOnLogcat(path, ""); |
| 1341 | } |
| 1342 | |
| 1343 | protected void dumpFileOnLogcat(String path, String message) { |
| 1344 | if (!ENABLE_DUMP) return; |
| 1345 | |
| 1346 | Log.v(TAG, "Dumping file: " + path + " " + message); |
| 1347 | final StringBuilder sb = new StringBuilder(); |
| 1348 | try (BufferedReader br = new BufferedReader(new FileReader(path))) { |
| 1349 | String line; |
| 1350 | while ((line = br.readLine()) != null) { |
| 1351 | Log.v(TAG, line); |
| 1352 | } |
| 1353 | } catch (Exception e) { |
| 1354 | Log.e(TAG, "Couldn't read file", e); |
| 1355 | fail("Exception " + e); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * For debugging, dump the main state file on logcat. |
| 1361 | */ |
| 1362 | protected void dumpBaseStateFile() { |
| 1363 | mService.saveDirtyInfo(); |
| 1364 | dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath() |
| 1365 | + "/system/" + ShortcutService.FILENAME_BASE_STATE); |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * For debugging, dump per-user state file on logcat. |
| 1370 | */ |
| 1371 | protected void dumpUserFile(int userId) { |
| 1372 | dumpUserFile(userId, ""); |
| 1373 | } |
| 1374 | |
| 1375 | protected void dumpUserFile(int userId, String message) { |
| 1376 | mService.saveDirtyInfo(); |
| 1377 | dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath() |
| 1378 | + "/user-" + userId |
| 1379 | + "/" + ShortcutService.FILENAME_USER_PACKAGES, message); |
| 1380 | } |
| 1381 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1382 | /** |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1383 | * Make a shortcut with an ID only. |
| 1384 | */ |
| 1385 | protected ShortcutInfo makeShortcutIdOnly(String id) { |
| 1386 | return new ShortcutInfo.Builder(mClientContext, id).build(); |
| 1387 | } |
| 1388 | |
| 1389 | /** |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1390 | * Make a shortcut with an ID. |
| 1391 | */ |
| 1392 | protected ShortcutInfo makeShortcut(String id) { |
| 1393 | return makeShortcut( |
| 1394 | id, "Title-" + id, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1395 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1398 | @Deprecated // Title was renamed to short label. |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1399 | protected ShortcutInfo makeShortcutWithTitle(String id, String title) { |
| 1400 | return makeShortcut( |
| 1401 | id, title, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1402 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1405 | protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) { |
| 1406 | return makeShortcut( |
| 1407 | id, shortLabel, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1408 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1411 | /** |
| 1412 | * Make a shortcut with an ID and timestamp. |
| 1413 | */ |
| 1414 | protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) { |
| 1415 | final ShortcutInfo s = makeShortcut( |
| 1416 | id, "Title-" + id, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1417 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1418 | s.setTimestamp(timestamp); |
| 1419 | return s; |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Make a shortcut with an ID, a timestamp and an activity component |
| 1424 | */ |
| 1425 | protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp, |
| 1426 | ComponentName activity) { |
| 1427 | final ShortcutInfo s = makeShortcut( |
| 1428 | id, "Title-" + id, activity, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1429 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1430 | s.setTimestamp(timestamp); |
| 1431 | return s; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * Make a shortcut with an ID and icon. |
| 1436 | */ |
| 1437 | protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) { |
| 1438 | return makeShortcut( |
| 1439 | id, "Title-" + id, /* activity =*/ null, icon, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1440 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | protected ShortcutInfo makePackageShortcut(String packageName, String id) { |
| 1444 | String origCaller = getCallingPackage(); |
| 1445 | |
| 1446 | setCaller(packageName); |
| 1447 | ShortcutInfo s = makeShortcut( |
| 1448 | id, "Title-" + id, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1449 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1450 | setCaller(origCaller); // restore the caller |
| 1451 | |
| 1452 | return s; |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * Make multiple shortcuts with IDs. |
| 1457 | */ |
| 1458 | protected List<ShortcutInfo> makeShortcuts(String... ids) { |
| 1459 | final ArrayList<ShortcutInfo> ret = new ArrayList(); |
| 1460 | for (String id : ids) { |
| 1461 | ret.add(makeShortcut(id)); |
| 1462 | } |
| 1463 | return ret; |
| 1464 | } |
| 1465 | |
| 1466 | protected ShortcutInfo.Builder makeShortcutBuilder() { |
| 1467 | return new ShortcutInfo.Builder(mClientContext); |
| 1468 | } |
| 1469 | |
| 1470 | protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) { |
| 1471 | return makeShortcut( |
| 1472 | id, "Title-" + id, activity, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1473 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1476 | protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) { |
| 1477 | return makeShortcut( |
| 1478 | id, "Title-" + id, /* activity =*/ null, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1479 | intent, /* rank =*/ 0); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1482 | protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity, |
| 1483 | String title) { |
| 1484 | return makeShortcut( |
| 1485 | id, title, activity, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1486 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity, |
| 1490 | int rank) { |
| 1491 | return makeShortcut( |
| 1492 | id, "Title-" + id, activity, /* icon =*/ null, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1493 | makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * Make a shortcut with details. |
| 1498 | */ |
| 1499 | protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1500 | Icon icon, Intent intent, int rank) { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1501 | final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id) |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1502 | .setActivity(new ComponentName(mClientContext.getPackageName(), "main")) |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1503 | .setShortLabel(title) |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1504 | .setRank(rank) |
| 1505 | .setIntent(intent); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1506 | if (icon != null) { |
| 1507 | b.setIcon(icon); |
| 1508 | } |
| 1509 | if (activity != null) { |
| 1510 | b.setActivity(activity); |
| 1511 | } |
| 1512 | final ShortcutInfo s = b.build(); |
| 1513 | |
| 1514 | s.setTimestamp(mInjectedCurrentTimeMillis); // HACK |
| 1515 | |
| 1516 | return s; |
| 1517 | } |
| 1518 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1519 | protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) { |
| 1520 | return makeShortcut( |
| 1521 | id, "Title-" + id, /* activity =*/ null, /* icon =*/ null, |
| 1522 | intents, /* rank =*/ 0); |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * Make a shortcut with details. |
| 1527 | */ |
| 1528 | protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity, |
| 1529 | Icon icon, Intent[] intents, int rank) { |
| 1530 | final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id) |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1531 | .setActivity(new ComponentName(mClientContext.getPackageName(), "main")) |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1532 | .setShortLabel(title) |
| 1533 | .setRank(rank) |
| 1534 | .setIntents(intents); |
| 1535 | if (icon != null) { |
| 1536 | b.setIcon(icon); |
| 1537 | } |
| 1538 | if (activity != null) { |
| 1539 | b.setActivity(activity); |
| 1540 | } |
| 1541 | final ShortcutInfo s = b.build(); |
| 1542 | |
| 1543 | s.setTimestamp(mInjectedCurrentTimeMillis); // HACK |
| 1544 | |
| 1545 | return s; |
| 1546 | } |
| 1547 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1548 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1549 | * Make a shortcut with details. |
| 1550 | */ |
| 1551 | protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent, |
| 1552 | PersistableBundle extras) { |
| 1553 | final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id) |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1554 | .setActivity(new ComponentName(mClientContext.getPackageName(), "main")) |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1555 | .setShortLabel("title-" + id) |
| 1556 | .setExtras(extras) |
| 1557 | .setIntent(intent); |
| 1558 | final ShortcutInfo s = b.build(); |
| 1559 | |
| 1560 | s.setTimestamp(mInjectedCurrentTimeMillis); // HACK |
| 1561 | |
| 1562 | return s; |
| 1563 | } |
| 1564 | |
| 1565 | /** |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1566 | * Make an intent. |
| 1567 | */ |
| 1568 | protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) { |
| 1569 | final Intent intent = new Intent(action); |
| 1570 | intent.setComponent(makeComponent(clazz)); |
| 1571 | intent.replaceExtras(makeBundle(bundleKeysAndValues)); |
| 1572 | return intent; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Make an component name, with the client context. |
| 1577 | */ |
| 1578 | @NonNull |
| 1579 | protected ComponentName makeComponent(Class<?> clazz) { |
| 1580 | return new ComponentName(mClientContext, clazz); |
| 1581 | } |
| 1582 | |
| 1583 | @NonNull |
| 1584 | protected ShortcutInfo findById(List<ShortcutInfo> list, String id) { |
| 1585 | for (ShortcutInfo s : list) { |
| 1586 | if (s.getId().equals(id)) { |
| 1587 | return s; |
| 1588 | } |
| 1589 | } |
| 1590 | fail("Shortcut with id " + id + " not found"); |
| 1591 | return null; |
| 1592 | } |
| 1593 | |
| 1594 | protected void assertSystem() { |
| 1595 | assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid); |
| 1596 | } |
| 1597 | |
| 1598 | protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) { |
| 1599 | assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked()); |
| 1600 | assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked()); |
| 1601 | } |
| 1602 | |
| 1603 | public static List<ShortcutInfo> assertAllNotHaveIcon( |
| 1604 | List<ShortcutInfo> actualShortcuts) { |
| 1605 | for (ShortcutInfo s : actualShortcuts) { |
| 1606 | assertNull("ID " + s.getId(), s.getIcon()); |
| 1607 | } |
| 1608 | return actualShortcuts; |
| 1609 | } |
| 1610 | |
| 1611 | @NonNull |
| 1612 | protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts, |
| 1613 | int shortcutFlags) { |
| 1614 | for (ShortcutInfo s : actualShortcuts) { |
| 1615 | assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags, |
| 1616 | s.hasFlags(shortcutFlags)); |
| 1617 | } |
| 1618 | return actualShortcuts; |
| 1619 | } |
| 1620 | |
| 1621 | protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) { |
| 1622 | return mService.getPackageShortcutForTest(packageName, shortcutId, userId); |
| 1623 | } |
| 1624 | |
| 1625 | protected void assertShortcutExists(String packageName, String shortcutId, int userId) { |
| 1626 | assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null); |
| 1627 | } |
| 1628 | |
| 1629 | protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) { |
| 1630 | assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null); |
| 1631 | } |
| 1632 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1633 | protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter, |
| 1634 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
| 1635 | reset(mMockActivityManagerInternal); |
| 1636 | shortcutStarter.run(); |
| 1637 | |
| 1638 | final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class); |
| 1639 | verify(mMockActivityManagerInternal).startActivitiesAsPackage( |
| 1640 | eq(packageName), |
| 1641 | eq(userId), |
| 1642 | intentsCaptor.capture(), |
Makoto Onuki | d0010c5 | 2017-03-30 14:17:35 -0700 | [diff] [blame] | 1643 | anyOrNull(Bundle.class)); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1644 | return intentsCaptor.getValue(); |
| 1645 | } |
| 1646 | |
| 1647 | protected Intent[] launchShortcutAndGetIntents( |
| 1648 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
| 1649 | return launchShortcutAndGetIntentsInner( |
| 1650 | () -> { |
| 1651 | mLauncherApps.startShortcut(packageName, shortcutId, null, null, |
| 1652 | UserHandle.of(userId)); |
| 1653 | }, packageName, shortcutId, userId |
| 1654 | ); |
| 1655 | } |
| 1656 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1657 | protected Intent launchShortcutAndGetIntent( |
| 1658 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1659 | final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId); |
| 1660 | assertEquals(1, intents.length); |
| 1661 | return intents[0]; |
| 1662 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1663 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1664 | protected Intent[] launchShortcutAndGetIntents_withShortcutInfo( |
| 1665 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
| 1666 | return launchShortcutAndGetIntentsInner( |
| 1667 | () -> { |
| 1668 | mLauncherApps.startShortcut( |
| 1669 | getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null); |
| 1670 | }, packageName, shortcutId, userId |
| 1671 | ); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | protected Intent launchShortcutAndGetIntent_withShortcutInfo( |
| 1675 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1676 | final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo( |
| 1677 | packageName, shortcutId, userId); |
| 1678 | assertEquals(1, intents.length); |
| 1679 | return intents[0]; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId, |
| 1683 | int userId) { |
| 1684 | assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId)); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1687 | protected void assertShortcutNotLaunched(@NonNull String packageName, |
| 1688 | @NonNull String shortcutId, int userId) { |
| 1689 | reset(mMockActivityManagerInternal); |
| 1690 | try { |
| 1691 | mLauncherApps.startShortcut(packageName, shortcutId, null, null, |
| 1692 | UserHandle.of(userId)); |
| 1693 | fail("ActivityNotFoundException was not thrown"); |
| 1694 | } catch (ActivityNotFoundException expected) { |
| 1695 | } |
| 1696 | // This shouldn't have been called. |
| 1697 | verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage( |
| 1698 | anyString(), |
| 1699 | anyInt(), |
| 1700 | any(Intent[].class), |
Makoto Onuki | d0010c5 | 2017-03-30 14:17:35 -0700 | [diff] [blame] | 1701 | anyOrNull(Bundle.class)); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | protected void assertStartShortcutThrowsException(@NonNull String packageName, |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1705 | @NonNull String shortcutId, int userId, Class<?> expectedException) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1706 | Exception thrown = null; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1707 | try { |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1708 | mLauncherApps.startShortcut(packageName, shortcutId, null, null, |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1709 | UserHandle.of(userId)); |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1710 | } catch (Exception e) { |
| 1711 | thrown = e; |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1712 | } |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1713 | assertNotNull("Exception was not thrown", thrown); |
| 1714 | assertEquals("Exception type different", expectedException, thrown.getClass()); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | protected void assertBitmapDirectories(int userId, String... expectedDirectories) { |
| 1718 | final Set<String> expected = hashSet(set(expectedDirectories)); |
| 1719 | |
| 1720 | final Set<String> actual = new HashSet<>(); |
| 1721 | |
| 1722 | final File[] files = mService.getUserBitmapFilePath(userId).listFiles(); |
| 1723 | if (files != null) { |
| 1724 | for (File child : files) { |
| 1725 | if (child.isDirectory()) { |
| 1726 | actual.add(child.getName()); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | assertEquals(expected, actual); |
| 1732 | } |
| 1733 | |
| 1734 | protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) { |
| 1735 | final Set<String> expected = hashSet(set(expectedFiles)); |
| 1736 | |
| 1737 | final Set<String> actual = new HashSet<>(); |
| 1738 | |
| 1739 | final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName) |
| 1740 | .listFiles(); |
| 1741 | if (files != null) { |
| 1742 | for (File child : files) { |
| 1743 | if (child.isFile()) { |
| 1744 | actual.add(child.getName()); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | assertEquals(expected, actual); |
| 1750 | } |
| 1751 | |
| 1752 | protected String getBitmapFilename(int userId, String packageName, String shortcutId) { |
| 1753 | final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId); |
| 1754 | if (si == null) { |
| 1755 | return null; |
| 1756 | } |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1757 | mService.waitForBitmapSavesForTest(); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1758 | return new File(si.getBitmapPath()).getName(); |
| 1759 | } |
| 1760 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1761 | protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) { |
| 1762 | final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId); |
| 1763 | if (si == null) { |
| 1764 | return null; |
| 1765 | } |
| 1766 | mService.waitForBitmapSavesForTest(); |
| 1767 | return new File(si.getBitmapPath()).getAbsolutePath(); |
| 1768 | } |
| 1769 | |
Makoto Onuki | f3ba2e0 | 2016-07-12 09:18:50 -0700 | [diff] [blame] | 1770 | /** |
| 1771 | * @return all shortcuts stored internally for the caller. This reflects the *internal* view |
| 1772 | * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would |
| 1773 | * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door" |
| 1774 | * which performs some extra checks, like {@link ShortcutPackage#onRestored}. |
| 1775 | */ |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1776 | protected List<ShortcutInfo> getCallerShortcuts() { |
| 1777 | final ShortcutPackage p = mService.getPackageShortcutForTest( |
| 1778 | getCallingPackage(), getCallingUserId()); |
| 1779 | return p == null ? null : p.getAllShortcutsForTest(); |
| 1780 | } |
| 1781 | |
Makoto Onuki | f3ba2e0 | 2016-07-12 09:18:50 -0700 | [diff] [blame] | 1782 | /** |
| 1783 | * @return all shortcuts owned by caller that are actually visible via ShortcutManager. |
| 1784 | * See also {@link #getCallerShortcuts}. |
| 1785 | */ |
| 1786 | protected List<ShortcutInfo> getCallerVisibleShortcuts() { |
| 1787 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(); |
| 1788 | ret.addAll(mManager.getDynamicShortcuts()); |
| 1789 | ret.addAll(mManager.getPinnedShortcuts()); |
| 1790 | ret.addAll(mManager.getManifestShortcuts()); |
| 1791 | return ret; |
| 1792 | } |
| 1793 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1794 | protected ShortcutInfo getCallerShortcut(String shortcutId) { |
| 1795 | return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId()); |
| 1796 | } |
| 1797 | |
| 1798 | protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) { |
| 1799 | final List<ShortcutInfo>[] ret = new List[1]; |
| 1800 | runWithCaller(launcher, userId, () -> { |
| 1801 | final ShortcutQuery q = new ShortcutQuery(); |
| 1802 | q.setQueryFlags(queryFlags); |
| 1803 | ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId)); |
| 1804 | }); |
| 1805 | return ret[0]; |
| 1806 | } |
| 1807 | |
| 1808 | protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) { |
| 1809 | return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED); |
| 1810 | } |
| 1811 | |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1812 | protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) { |
| 1813 | final ShortcutQuery q = new ShortcutQuery(); |
| 1814 | q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC |
| 1815 | | ShortcutQuery.FLAG_MATCH_PINNED); |
| 1816 | return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId)); |
| 1817 | } |
| 1818 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1819 | protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId, |
| 1820 | int userId) { |
| 1821 | final List<ShortcutInfo> infoList = |
| 1822 | mLauncherApps.getShortcutInfo(packageName, list(shortcutId), |
| 1823 | UserHandle.of(userId)); |
| 1824 | assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size()); |
| 1825 | return infoList.get(0); |
| 1826 | } |
| 1827 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1828 | protected Intent genPackageAddIntent(String packageName, int userId) { |
| 1829 | installPackage(userId, packageName); |
| 1830 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1831 | Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1832 | i.setData(Uri.parse("package:" + packageName)); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1833 | i.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1834 | return i; |
| 1835 | } |
| 1836 | |
| 1837 | protected Intent genPackageDeleteIntent(String pakcageName, int userId) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1838 | uninstallPackage(userId, pakcageName); |
| 1839 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1840 | Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED); |
| 1841 | i.setData(Uri.parse("package:" + pakcageName)); |
| 1842 | i.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1843 | return i; |
| 1844 | } |
| 1845 | |
| 1846 | protected Intent genPackageUpdateIntent(String pakcageName, int userId) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 1847 | installPackage(userId, pakcageName); |
| 1848 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1849 | Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED); |
| 1850 | i.setData(Uri.parse("package:" + pakcageName)); |
| 1851 | i.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1852 | i.putExtra(Intent.EXTRA_REPLACING, true); |
| 1853 | return i; |
| 1854 | } |
| 1855 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1856 | protected Intent genPackageChangedIntent(String pakcageName, int userId) { |
| 1857 | Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED); |
| 1858 | i.setData(Uri.parse("package:" + pakcageName)); |
| 1859 | i.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1860 | return i; |
| 1861 | } |
| 1862 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1863 | protected Intent genPackageDataClear(String packageName, int userId) { |
| 1864 | Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED); |
| 1865 | i.setData(Uri.parse("package:" + packageName)); |
| 1866 | i.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1867 | return i; |
| 1868 | } |
| 1869 | |
| 1870 | protected void assertExistsAndShadow(ShortcutPackageItem spi) { |
| 1871 | assertNotNull(spi); |
| 1872 | assertTrue(spi.getPackageInfo().isShadow()); |
| 1873 | } |
| 1874 | |
| 1875 | protected File makeFile(File baseDirectory, String... paths) { |
| 1876 | File ret = baseDirectory; |
| 1877 | |
| 1878 | for (String path : paths) { |
| 1879 | ret = new File(ret, path); |
| 1880 | } |
| 1881 | |
| 1882 | return ret; |
| 1883 | } |
| 1884 | |
| 1885 | protected boolean bitmapDirectoryExists(String packageName, int userId) { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1886 | mService.waitForBitmapSavesForTest(); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1887 | final File path = new File(mService.getUserBitmapFilePath(userId), packageName); |
| 1888 | return path.isDirectory(); |
| 1889 | } |
| 1890 | protected static ShortcutQuery buildQuery(long changedSince, |
| 1891 | String packageName, ComponentName componentName, |
| 1892 | /* @ShortcutQuery.QueryFlags */ int flags) { |
| 1893 | return buildQuery(changedSince, packageName, null, componentName, flags); |
| 1894 | } |
| 1895 | |
| 1896 | protected static ShortcutQuery buildQuery(long changedSince, |
| 1897 | String packageName, List<String> shortcutIds, ComponentName componentName, |
| 1898 | /* @ShortcutQuery.QueryFlags */ int flags) { |
| 1899 | final ShortcutQuery q = new ShortcutQuery(); |
| 1900 | q.setChangedSince(changedSince); |
| 1901 | q.setPackage(packageName); |
| 1902 | q.setShortcutIds(shortcutIds); |
| 1903 | q.setActivity(componentName); |
| 1904 | q.setQueryFlags(flags); |
| 1905 | return q; |
| 1906 | } |
| 1907 | |
| 1908 | protected static ShortcutQuery buildAllQuery(String packageName) { |
| 1909 | final ShortcutQuery q = new ShortcutQuery(); |
| 1910 | q.setPackage(packageName); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 1911 | q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1912 | return q; |
| 1913 | } |
| 1914 | |
| 1915 | protected static ShortcutQuery buildPinnedQuery(String packageName) { |
| 1916 | final ShortcutQuery q = new ShortcutQuery(); |
| 1917 | q.setPackage(packageName); |
| 1918 | q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED); |
| 1919 | return q; |
| 1920 | } |
| 1921 | |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 1922 | protected static ShortcutQuery buildQueryWithFlags(int queryFlags) { |
| 1923 | final ShortcutQuery q = new ShortcutQuery(); |
| 1924 | q.setQueryFlags(queryFlags); |
| 1925 | return q; |
| 1926 | } |
| 1927 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1928 | protected void backupAndRestore() { |
| 1929 | int prevUid = mInjectedCallingUid; |
| 1930 | |
| 1931 | mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it. |
| 1932 | |
| 1933 | dumpsysOnLogcat("Before backup"); |
| 1934 | |
| 1935 | final byte[] payload = mService.getBackupPayload(USER_0); |
| 1936 | if (ENABLE_DUMP) { |
| 1937 | final String xml = new String(payload); |
| 1938 | Log.v(TAG, "Backup payload:"); |
| 1939 | for (String line : xml.split("\n")) { |
| 1940 | Log.v(TAG, line); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | // Before doing anything else, uninstall all packages. |
| 1945 | for (int userId : list(USER_0, USER_P0)) { |
| 1946 | for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3, |
| 1947 | LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) { |
| 1948 | uninstallPackage(userId, pkg); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | shutdownServices(); |
| 1953 | |
| 1954 | deleteAllSavedFiles(); |
| 1955 | |
| 1956 | initService(); |
| 1957 | mService.applyRestore(payload, USER_0); |
| 1958 | |
| 1959 | // handleUnlockUser will perform the gone package check, but it shouldn't remove |
| 1960 | // shadow information. |
| 1961 | mService.handleUnlockUser(USER_0); |
| 1962 | |
| 1963 | dumpsysOnLogcat("After restore"); |
| 1964 | |
| 1965 | mInjectedCallingUid = prevUid; |
| 1966 | } |
| 1967 | |
| 1968 | protected void prepareCrossProfileDataSet() { |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1969 | mRunningUsers.put(USER_10, true); // this test needs user 10. |
| 1970 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 1971 | runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { |
| 1972 | assertTrue(mManager.setDynamicShortcuts(list( |
| 1973 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"), |
| 1974 | makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6")))); |
| 1975 | }); |
| 1976 | runWithCaller(CALLING_PACKAGE_2, USER_0, () -> { |
| 1977 | assertTrue(mManager.setDynamicShortcuts(list( |
| 1978 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"), |
| 1979 | makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6")))); |
| 1980 | }); |
| 1981 | runWithCaller(CALLING_PACKAGE_3, USER_0, () -> { |
| 1982 | assertTrue(mManager.setDynamicShortcuts(list( |
| 1983 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"), |
| 1984 | makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6")))); |
| 1985 | }); |
| 1986 | runWithCaller(CALLING_PACKAGE_4, USER_0, () -> { |
| 1987 | assertTrue(mManager.setDynamicShortcuts(list())); |
| 1988 | }); |
| 1989 | runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> { |
| 1990 | assertTrue(mManager.setDynamicShortcuts(list( |
| 1991 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"), |
| 1992 | makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6")))); |
| 1993 | }); |
| 1994 | runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { |
| 1995 | assertTrue(mManager.setDynamicShortcuts(list( |
| 1996 | makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"), |
| 1997 | makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6")))); |
| 1998 | }); |
| 1999 | |
| 2000 | runWithCaller(LAUNCHER_1, USER_0, () -> { |
| 2001 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0); |
| 2002 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0); |
| 2003 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0); |
| 2004 | |
| 2005 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0); |
| 2006 | }); |
| 2007 | runWithCaller(LAUNCHER_2, USER_0, () -> { |
| 2008 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0); |
| 2009 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0); |
| 2010 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0); |
| 2011 | |
| 2012 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0); |
| 2013 | }); |
Makoto Onuki | f3ba2e0 | 2016-07-12 09:18:50 -0700 | [diff] [blame] | 2014 | |
| 2015 | // Note LAUNCHER_3 has allowBackup=false. |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 2016 | runWithCaller(LAUNCHER_3, USER_0, () -> { |
| 2017 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0); |
| 2018 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0); |
| 2019 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0); |
| 2020 | |
| 2021 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0); |
| 2022 | }); |
| 2023 | runWithCaller(LAUNCHER_4, USER_0, () -> { |
| 2024 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0); |
| 2025 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0); |
| 2026 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0); |
| 2027 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0); |
| 2028 | }); |
| 2029 | |
| 2030 | // Launcher on a managed profile is referring ot user 0! |
| 2031 | runWithCaller(LAUNCHER_1, USER_P0, () -> { |
| 2032 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0); |
| 2033 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0); |
| 2034 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"), |
| 2035 | HANDLE_USER_0); |
| 2036 | |
| 2037 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0); |
| 2038 | }); |
| 2039 | runWithCaller(LAUNCHER_1, USER_10, () -> { |
| 2040 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10); |
| 2041 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10); |
| 2042 | mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"), |
| 2043 | HANDLE_USER_10); |
| 2044 | }); |
| 2045 | |
| 2046 | // Then remove some dynamic shortcuts. |
| 2047 | runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { |
| 2048 | assertTrue(mManager.setDynamicShortcuts(list( |
| 2049 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3")))); |
| 2050 | }); |
| 2051 | runWithCaller(CALLING_PACKAGE_2, USER_0, () -> { |
| 2052 | assertTrue(mManager.setDynamicShortcuts(list( |
| 2053 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3")))); |
| 2054 | }); |
| 2055 | runWithCaller(CALLING_PACKAGE_3, USER_0, () -> { |
| 2056 | assertTrue(mManager.setDynamicShortcuts(list( |
| 2057 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3")))); |
| 2058 | }); |
| 2059 | runWithCaller(CALLING_PACKAGE_4, USER_0, () -> { |
| 2060 | assertTrue(mManager.setDynamicShortcuts(list())); |
| 2061 | }); |
| 2062 | runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> { |
| 2063 | assertTrue(mManager.setDynamicShortcuts(list( |
| 2064 | makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3")))); |
| 2065 | }); |
| 2066 | runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { |
| 2067 | assertTrue(mManager.setDynamicShortcuts(list( |
| 2068 | makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3")))); |
| 2069 | }); |
| 2070 | } |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 2071 | |
| 2072 | public static List<ShortcutInfo> assertAllHaveIconResId( |
| 2073 | List<ShortcutInfo> actualShortcuts) { |
| 2074 | for (ShortcutInfo s : actualShortcuts) { |
| 2075 | assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource()); |
| 2076 | assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile()); |
| 2077 | } |
| 2078 | return actualShortcuts; |
| 2079 | } |
| 2080 | |
| 2081 | public static List<ShortcutInfo> assertAllHaveIconFile( |
| 2082 | List<ShortcutInfo> actualShortcuts) { |
| 2083 | for (ShortcutInfo s : actualShortcuts) { |
| 2084 | assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource()); |
| 2085 | assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile()); |
| 2086 | } |
| 2087 | return actualShortcuts; |
| 2088 | } |
| 2089 | |
| 2090 | public static List<ShortcutInfo> assertAllHaveIcon( |
| 2091 | List<ShortcutInfo> actualShortcuts) { |
| 2092 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 2093 | assertTrue("ID " + s.getId() + " has no icon ", |
| 2094 | s.hasIconFile() || s.hasIconResource() || s.getIcon() != null); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 2095 | } |
| 2096 | return actualShortcuts; |
| 2097 | } |
| 2098 | |
| 2099 | public static List<ShortcutInfo> assertAllStringsResolved( |
| 2100 | List<ShortcutInfo> actualShortcuts) { |
| 2101 | for (ShortcutInfo s : actualShortcuts) { |
| 2102 | assertTrue("ID " + s.getId(), s.hasStringResourcesResolved()); |
| 2103 | } |
| 2104 | return actualShortcuts; |
| 2105 | } |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 2106 | |
| 2107 | public String readTestAsset(String assetPath) throws IOException { |
| 2108 | final StringBuilder sb = new StringBuilder(); |
| 2109 | try (BufferedReader br = new BufferedReader( |
| 2110 | new InputStreamReader( |
| 2111 | getTestContext().getResources().getAssets().open(assetPath)))) { |
| 2112 | String line; |
| 2113 | while ((line = br.readLine()) != null) { |
| 2114 | sb.append(line); |
| 2115 | sb.append(System.lineSeparator()); |
| 2116 | } |
| 2117 | } |
| 2118 | return sb.toString(); |
| 2119 | } |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 2120 | |
| 2121 | protected void prepareGetHomeActivitiesAsUser(ComponentName preferred, |
| 2122 | List<ResolveInfo> candidates, int userId) { |
| 2123 | doAnswer(inv -> { |
| 2124 | ((List) inv.getArguments()[0]).addAll(candidates); |
| 2125 | return preferred; |
| 2126 | }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId)); |
| 2127 | } |
| 2128 | |
| 2129 | protected static ComponentName cn(String packageName, String name) { |
| 2130 | return new ComponentName(packageName, name); |
| 2131 | } |
| 2132 | |
| 2133 | protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) { |
| 2134 | final ResolveInfo ri = new ResolveInfo(); |
| 2135 | ri.activityInfo = new ActivityInfo(); |
| 2136 | ri.activityInfo.applicationInfo = new ApplicationInfo(); |
| 2137 | |
| 2138 | ri.activityInfo.packageName = packageName; |
| 2139 | ri.activityInfo.name = name; |
| 2140 | if (isSystem) { |
| 2141 | ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 2142 | } |
| 2143 | ri.priority = priority; |
| 2144 | return ri; |
| 2145 | } |
| 2146 | |
| 2147 | protected static ResolveInfo getSystemLauncher() { |
| 2148 | return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true, |
| 2149 | PACKAGE_SYSTEM_LAUNCHER_PRIORITY); |
| 2150 | } |
| 2151 | |
| 2152 | protected static ResolveInfo getFallbackLauncher() { |
| 2153 | return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true, |
| 2154 | PACKAGE_FALLBACK_LAUNCHER_PRIORITY); |
| 2155 | } |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 2156 | |
Tony Mak | ed6ef62 | 2017-12-07 16:36:16 +0000 | [diff] [blame] | 2157 | protected void makeUidForeground(int uid) { |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 2158 | try { |
| 2159 | mService.mUidObserver.onUidStateChanged( |
Tony Mak | ed6ef62 | 2017-12-07 16:36:16 +0000 | [diff] [blame] | 2160 | uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0); |
| 2161 | } catch (RemoteException e) { |
| 2162 | e.rethrowAsRuntimeException(); |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | protected void makeCallerForeground() { |
| 2167 | makeUidForeground(mInjectedCallingUid); |
| 2168 | } |
| 2169 | |
| 2170 | protected void makeUidBackground(int uid) { |
| 2171 | try { |
| 2172 | mService.mUidObserver.onUidStateChanged( |
| 2173 | uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0); |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 2174 | } catch (RemoteException e) { |
| 2175 | e.rethrowAsRuntimeException(); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | protected void makeCallerBackground() { |
Tony Mak | ed6ef62 | 2017-12-07 16:36:16 +0000 | [diff] [blame] | 2180 | makeUidBackground(mInjectedCallingUid); |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | protected void publishManifestShortcutsAsCaller(int resId) { |
| 2184 | addManifestShortcutResource( |
| 2185 | new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()), |
| 2186 | resId); |
| 2187 | updatePackageVersion(getCallingPackage(), 1); |
| 2188 | mService.mPackageMonitor.onReceive(getTestContext(), |
| 2189 | genPackageAddIntent(getCallingPackage(), getCallingUserId())); |
| 2190 | } |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 2191 | |
| 2192 | protected void assertFileNotExists(String path) { |
| 2193 | final File f = new File(mInjectedFilePathRoot, path); |
| 2194 | assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists()); |
| 2195 | } |
| 2196 | |
| 2197 | protected void assertFileExistsWithContent(String path) { |
| 2198 | final File f = new File(mInjectedFilePathRoot, path); |
| 2199 | assertTrue("File should exist: " + f.getAbsolutePath(), f.exists()); |
| 2200 | assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0); |
| 2201 | } |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 2202 | } |