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