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