Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -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 | |
| 18 | import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list; |
| 19 | |
| 20 | import static org.mockito.Matchers.any; |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 21 | import static org.mockito.Matchers.eq; |
| 22 | import static org.mockito.Mockito.doAnswer; |
| 23 | |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.ActivityInfo; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.test.suitebuilder.annotation.SmallTest; |
| 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 34 | * Tests for {@link ShortcutService#hasShortcutHostPermissionInner}, which includes |
| 35 | * {@link ShortcutService#getDefaultLauncher}. |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 36 | */ |
| 37 | @SmallTest |
| 38 | public class ShortcutManagerTest6 extends BaseShortcutManagerTest { |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 39 | public void testHasShortcutHostPermissionInner_systemLauncherOnly() { |
| 40 | // Preferred isn't set, use the system launcher. |
| 41 | prepareGetHomeActivitiesAsUser( |
| 42 | /* preferred */ null, |
| 43 | list(getSystemLauncher(), getFallbackLauncher()), |
| 44 | USER_0); |
| 45 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 46 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 47 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 48 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 49 | |
| 50 | // Should be cached. |
| 51 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 52 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 53 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 54 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 55 | |
| 56 | // Also make sure the last known is saved, but the cached is not. |
| 57 | |
| 58 | initService(); |
| 59 | |
| 60 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 61 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 62 | assertEquals(null, |
| 63 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 64 | } |
| 65 | |
| 66 | public void testHasShortcutHostPermissionInner_with3pLauncher() { |
| 67 | // Preferred isn't set, still use the system launcher. |
| 68 | prepareGetHomeActivitiesAsUser( |
| 69 | /* preferred */ null, |
| 70 | list(getSystemLauncher(), getFallbackLauncher(), |
| 71 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 72 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 73 | ), |
| 74 | USER_0); |
| 75 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 76 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 77 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 78 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 79 | |
| 80 | // Should be cached. |
| 81 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 82 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 83 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 84 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 85 | } |
| 86 | |
| 87 | public void testHasShortcutHostPermissionInner_with3pLauncher_complicated() { |
| 88 | // Preferred is set. That's the default launcher. |
| 89 | prepareGetHomeActivitiesAsUser( |
| 90 | /* preferred */ cn(CALLING_PACKAGE_2, "name"), |
| 91 | list(getSystemLauncher(), getFallbackLauncher(), |
| 92 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 93 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 94 | ), |
| 95 | USER_0); |
| 96 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 97 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 98 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 99 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 100 | |
| 101 | // Should be cached. |
| 102 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 103 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 104 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 105 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 106 | |
| 107 | |
| 108 | // Once set, even after the preferred launcher is cleared, SM still allows it to access |
| 109 | // shortcuts. |
| 110 | prepareGetHomeActivitiesAsUser( |
| 111 | /* preferred */ null, |
| 112 | list(getSystemLauncher(), getFallbackLauncher(), |
| 113 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 114 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 115 | ), |
| 116 | USER_0); |
| 117 | |
| 118 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 119 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 120 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 121 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 122 | |
| 123 | // Should be cached. |
| 124 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 125 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 126 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 127 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 128 | |
| 129 | // However, if the component has been disabled, then we'll recalculate it. |
| 130 | mEnabledActivityChecker = (comp, user) -> false; |
| 131 | |
| 132 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 133 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 134 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 135 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 136 | |
| 137 | mEnabledActivityChecker = (comp, user) -> true; |
| 138 | |
| 139 | // Now the preferred changed. |
| 140 | prepareGetHomeActivitiesAsUser( |
| 141 | /* preferred */ cn(CALLING_PACKAGE_1, "xyz"), |
| 142 | list(getSystemLauncher(), getFallbackLauncher(), |
| 143 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 144 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 145 | ), |
| 146 | USER_0); |
| 147 | |
| 148 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 149 | |
| 150 | // Should be cached. |
| 151 | assertEquals(cn(CALLING_PACKAGE_1, "xyz"), |
| 152 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 153 | assertEquals(cn(CALLING_PACKAGE_1, "xyz"), |
| 154 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 155 | |
| 156 | |
| 157 | // As long as there's the cached launcher set, even if getHomeActivitiesAsUser() |
| 158 | // returns different values, the cached one is still the default. |
| 159 | prepareGetHomeActivitiesAsUser( |
| 160 | /* preferred */ getSystemLauncher().activityInfo.getComponentName(), |
| 161 | list(getSystemLauncher(), getFallbackLauncher()), |
| 162 | USER_0); |
| 163 | |
| 164 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 165 | |
| 166 | // Cached ones haven't changed. |
| 167 | assertEquals(cn(CALLING_PACKAGE_1, "xyz"), |
| 168 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 169 | assertEquals(cn(CALLING_PACKAGE_1, "xyz"), |
| 170 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 171 | |
| 172 | // However, now the "real" default launcher is the system one. So if the system |
| 173 | // launcher asks for shortcuts, we'll allow it. |
| 174 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 175 | |
| 176 | // Since the cache is updated, CALLING_PACKAGE_1 no longer has the permission. |
| 177 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 178 | |
| 179 | // Cached ones haven't changed. |
| 180 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 181 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 182 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 183 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 184 | } |
| 185 | |
| 186 | public void testHasShortcutHostPermissionInner_multiUser() { |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 187 | mRunningUsers.put(USER_10, true); |
| 188 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 189 | prepareGetHomeActivitiesAsUser( |
| 190 | /* preferred */ null, |
| 191 | list(getSystemLauncher(), getFallbackLauncher()), |
| 192 | USER_0); |
| 193 | |
| 194 | prepareGetHomeActivitiesAsUser( |
| 195 | /* preferred */ cn(CALLING_PACKAGE_2, "name"), |
| 196 | list(getSystemLauncher(), getFallbackLauncher(), |
| 197 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 198 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 199 | ), |
| 200 | USER_10); |
| 201 | |
| 202 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 203 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0)); |
| 204 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0)); |
| 205 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0)); |
| 206 | |
| 207 | // Check the cache. |
| 208 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 209 | mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher()); |
| 210 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 211 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 212 | |
| 213 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_10)); |
| 214 | assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_10)); |
| 215 | assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_10)); |
| 216 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_10)); |
| 217 | |
| 218 | // Check the cache. |
| 219 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 220 | mService.getUserShortcutsLocked(USER_10).getLastKnownLauncher()); |
| 221 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 222 | mService.getUserShortcutsLocked(USER_10).getCachedLauncher()); |
| 223 | } |
| 224 | |
| 225 | public void testHasShortcutHostPermissionInner_clearCache() { |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 226 | mRunningUsers.put(USER_10, true); |
| 227 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 228 | prepareGetHomeActivitiesAsUser( |
| 229 | /* preferred */ null, |
| 230 | list(getSystemLauncher(), getFallbackLauncher()), |
| 231 | USER_0); |
| 232 | |
| 233 | prepareGetHomeActivitiesAsUser( |
| 234 | /* preferred */ cn(CALLING_PACKAGE_2, "name"), |
| 235 | list(getSystemLauncher(), getFallbackLauncher(), |
| 236 | ri(CALLING_PACKAGE_1, "name", false, 0), |
| 237 | ri(CALLING_PACKAGE_2, "name", false, 0) |
| 238 | ), |
| 239 | USER_10); |
| 240 | |
| 241 | assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); |
| 242 | assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_10)); |
| 243 | |
| 244 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 245 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 246 | |
| 247 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 248 | mService.getUserShortcutsLocked(USER_10).getCachedLauncher()); |
| 249 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 250 | // Test it on a non-running user. |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 251 | // Send ACTION_PREFERRED_ACTIVITY_CHANGED on user 10. |
| 252 | // But the user is not running, so will be ignored. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 253 | mRunningUsers.put(USER_10, false); |
| 254 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 255 | mService.mPackageMonitor.onReceive(mServiceContext, |
| 256 | new Intent(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED).putExtra( |
| 257 | Intent.EXTRA_USER_HANDLE, USER_10)); |
| 258 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 259 | // Need to run the user again to access the internal status. |
| 260 | mRunningUsers.put(USER_10, true); |
| 261 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 262 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 263 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 264 | |
| 265 | assertEquals(cn(CALLING_PACKAGE_2, "name"), |
| 266 | mService.getUserShortcutsLocked(USER_10).getCachedLauncher()); |
| 267 | |
| 268 | // Send it again after starting the user. |
| 269 | mRunningUsers.put(USER_10, true); |
| 270 | mService.mPackageMonitor.onReceive(mServiceContext, |
| 271 | new Intent(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED).putExtra( |
| 272 | Intent.EXTRA_USER_HANDLE, USER_10)); |
| 273 | |
| 274 | assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), |
| 275 | mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); |
| 276 | |
| 277 | // Only user-10's cache is cleared. |
| 278 | assertEquals(null, |
| 279 | mService.getUserShortcutsLocked(USER_10).getCachedLauncher()); |
| 280 | |
| 281 | } |
| 282 | } |