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