ShortcutManagerTest7.java revision 0b9d1db5c1aab9017daf05abd0ba0f4de2c7192d
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 */ 16package com.android.server.pm; 17 18import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertContains; 19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException; 20import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertSuccess; 21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith; 22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list; 23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.readAll; 24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resultContains; 25 26import android.content.ComponentName; 27import android.os.Bundle; 28import android.os.ParcelFileDescriptor; 29import android.os.Process; 30import android.os.RemoteException; 31import android.os.ResultReceiver; 32import android.test.suitebuilder.annotation.SmallTest; 33 34import com.android.frameworks.servicestests.R; 35import com.android.server.pm.ShortcutService.ConfigConstants; 36 37import java.io.File; 38import java.io.IOException; 39import java.util.List; 40import java.util.concurrent.atomic.AtomicInteger; 41 42/** 43 * Unit test for "cmd shortcut" 44 * 45 * Launcher related commands are tested in 46 */ 47@SmallTest 48public class ShortcutManagerTest7 extends BaseShortcutManagerTest { 49 private List<String> callShellCommand(String... args) throws IOException, RemoteException { 50 51 // For reset to work, the current time needs to be incrementing. 52 mInjectedCurrentTimeMillis++; 53 54 final AtomicInteger resultCode = new AtomicInteger(Integer.MIN_VALUE); 55 56 final ResultReceiver rr = new ResultReceiver(mHandler) { 57 @Override 58 public void send(int resultCode_, Bundle resultData) { 59 resultCode.set(resultCode_); 60 } 61 }; 62 final File out = File.createTempFile("shellout-", ".tmp", 63 getTestContext().getCacheDir()); 64 try { 65 try (final ParcelFileDescriptor fd = ParcelFileDescriptor.open(out, 66 ParcelFileDescriptor.MODE_READ_WRITE)) { 67 mService.onShellCommand( 68 /* fdin*/ null, 69 /* fdout*/ fd.getFileDescriptor(), 70 /* fderr*/ fd.getFileDescriptor(), 71 args, rr); 72 } 73 return readAll(out); 74 } finally { 75 out.delete(); 76 } 77 } 78 79 public void testNonShell() throws Exception { 80 mService.mMaxUpdatesPerInterval = 99; 81 82 mInjectedCallingUid = 12345; 83 assertExpectException(SecurityException.class, "must be shell", 84 () -> callShellCommand("reset-config")); 85 86 mInjectedCallingUid = Process.SYSTEM_UID; 87 assertExpectException(SecurityException.class, "must be shell", 88 () -> callShellCommand("reset-config")); 89 90 assertEquals(99, mService.mMaxUpdatesPerInterval); 91 } 92 93 public void testRoot() throws Exception { 94 mService.mMaxUpdatesPerInterval = 99; 95 96 mInjectedCallingUid = Process.ROOT_UID; 97 assertSuccess(callShellCommand("reset-config")); 98 99 assertEquals(3, mService.mMaxUpdatesPerInterval); 100 } 101 102 public void testRestConfig() throws Exception { 103 mService.mMaxUpdatesPerInterval = 99; 104 105 mInjectedCallingUid = Process.SHELL_UID; 106 assertSuccess(callShellCommand("reset-config")); 107 108 assertEquals(3, mService.mMaxUpdatesPerInterval); 109 } 110 111 public void testOverrideConfig() throws Exception { 112 mService.mMaxUpdatesPerInterval = 99; 113 114 mInjectedCallingUid = Process.SHELL_UID; 115 assertSuccess(callShellCommand("override-config", 116 ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=1")); 117 118 assertEquals(1, mService.mMaxUpdatesPerInterval); 119 } 120 121 public void testResetThrottling() throws Exception { 122 prepareCrossProfileDataSet(); 123 124 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 125 assertTrue(mManager.getRemainingCallCount() < 3); 126 }); 127 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 128 assertTrue(mManager.getRemainingCallCount() < 3); 129 }); 130 131 mInjectedCallingUid = Process.SHELL_UID; 132 assertSuccess(callShellCommand("reset-throttling")); 133 134 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 135 assertEquals(3, mManager.getRemainingCallCount()); 136 }); 137 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 138 assertTrue(mManager.getRemainingCallCount() < 3); 139 }); 140 } 141 142 public void testResetThrottling_user_not_running() throws Exception { 143 prepareCrossProfileDataSet(); 144 145 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 146 assertTrue(mManager.getRemainingCallCount() < 3); 147 }); 148 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 149 assertTrue(mManager.getRemainingCallCount() < 3); 150 }); 151 152 mInjectedCallingUid = Process.SHELL_UID; 153 154 assertTrue(resultContains( 155 callShellCommand("reset-throttling", "--user", "10"), 156 "User 10 is not running or locked")); 157 158 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 159 assertTrue(mManager.getRemainingCallCount() < 3); 160 }); 161 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 162 assertTrue(mManager.getRemainingCallCount() < 3); 163 }); 164 } 165 166 public void testResetThrottling_user_running() throws Exception { 167 prepareCrossProfileDataSet(); 168 169 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 170 assertTrue(mManager.getRemainingCallCount() < 3); 171 }); 172 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 173 assertTrue(mManager.getRemainingCallCount() < 3); 174 }); 175 176 mRunningUsers.put(USER_10, true); 177 mUnlockedUsers.put(USER_10, true); 178 179 mInjectedCallingUid = Process.SHELL_UID; 180 assertSuccess(callShellCommand("reset-throttling", "--user", "10")); 181 182 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 183 assertTrue(mManager.getRemainingCallCount() < 3); 184 }); 185 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 186 assertEquals(3, mManager.getRemainingCallCount()); 187 }); 188 } 189 190 public void testResetAllThrottling() throws Exception { 191 prepareCrossProfileDataSet(); 192 193 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 194 assertTrue(mManager.getRemainingCallCount() < 3); 195 }); 196 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 197 assertTrue(mManager.getRemainingCallCount() < 3); 198 }); 199 200 mInjectedCallingUid = Process.SHELL_UID; 201 assertSuccess(callShellCommand("reset-all-throttling")); 202 203 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> { 204 assertEquals(3, mManager.getRemainingCallCount()); 205 }); 206 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 207 assertEquals(3, mManager.getRemainingCallCount()); 208 }); 209 } 210 211 public void testLauncherCommands() throws Exception { 212 prepareGetHomeActivitiesAsUser( 213 /* preferred */ null, 214 list(getSystemLauncher(), getFallbackLauncher()), 215 USER_0); 216 217 prepareGetHomeActivitiesAsUser( 218 /* preferred */ cn(CALLING_PACKAGE_2, "name"), 219 list(getSystemLauncher(), getFallbackLauncher(), 220 ri(CALLING_PACKAGE_1, "name", false, 0), 221 ri(CALLING_PACKAGE_2, "name", false, 0) 222 ), 223 USER_10); 224 225 assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0)); 226 227 // First, test "get". 228 229 mRunningUsers.put(USER_10, true); 230 mUnlockedUsers.put(USER_10, true); 231 mInjectedCallingUid = Process.SHELL_UID; 232 assertContains( 233 assertSuccess(callShellCommand("get-default-launcher")), 234 "Launcher: ComponentInfo{com.android.systemlauncher/systemlauncher_name}"); 235 236 assertContains( 237 assertSuccess(callShellCommand("get-default-launcher", "--user", "10")), 238 "Launcher: ComponentInfo{com.android.test.2/name}"); 239 240 // Next, test "clear". 241 assertSuccess(callShellCommand("clear-default-launcher", "--user", "10")); 242 243 // User-10's launcher should be cleared. 244 assertEquals(null, mService.getUserShortcutsLocked(USER_10).getLastKnownLauncher()); 245 assertEquals(null, mService.getUserShortcutsLocked(USER_10).getCachedLauncher()); 246 247 // but user'0's shouldn't. 248 assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME), 249 mService.getUserShortcutsLocked(USER_0).getCachedLauncher()); 250 251 // Change user-0's launcher. 252 prepareGetHomeActivitiesAsUser( 253 /* preferred */ cn(CALLING_PACKAGE_1, "name"), 254 list( 255 ri(CALLING_PACKAGE_1, "name", false, 0) 256 ), 257 USER_0); 258 assertContains( 259 assertSuccess(callShellCommand("get-default-launcher")), 260 "Launcher: ComponentInfo{com.android.test.1/name}"); 261 } 262 263 public void testUnloadUser() throws Exception { 264 prepareCrossProfileDataSet(); 265 266 assertNotNull(mService.getShortcutsForTest().get(USER_10)); 267 268 mRunningUsers.put(USER_10, true); 269 mUnlockedUsers.put(USER_10, true); 270 271 mInjectedCallingUid = Process.SHELL_UID; 272 assertSuccess(callShellCommand("unload-user", "--user", "10")); 273 274 assertNull(mService.getShortcutsForTest().get(USER_10)); 275 } 276 277 public void testClearShortcuts() throws Exception { 278 279 mRunningUsers.put(USER_10, true); 280 281 // Add two manifests and two dynamics. 282 addManifestShortcutResource( 283 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()), 284 R.xml.shortcut_2); 285 updatePackageVersion(CALLING_PACKAGE_1, 1); 286 mService.mPackageMonitor.onReceive(getTestContext(), 287 genPackageAddIntent(CALLING_PACKAGE_1, USER_10)); 288 289 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 290 assertTrue(mManager.addDynamicShortcuts(list( 291 makeShortcut("s1"), makeShortcut("s2")))); 292 }); 293 runWithCaller(LAUNCHER_1, USER_10, () -> { 294 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10); 295 }); 296 297 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 298 assertWith(getCallerShortcuts()) 299 .haveIds("ms1", "ms2", "s1", "s2") 300 .areAllEnabled() 301 302 .selectPinned() 303 .haveIds("ms2", "s2"); 304 }); 305 306 // First, call for a different package. 307 308 mRunningUsers.put(USER_10, true); 309 mUnlockedUsers.put(USER_10, true); 310 311 mInjectedCallingUid = Process.SHELL_UID; 312 assertSuccess(callShellCommand("clear-shortcuts", "--user", "10", CALLING_PACKAGE_2)); 313 314 // Shouldn't be cleared yet. 315 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 316 assertWith(getCallerShortcuts()) 317 .haveIds("ms1", "ms2", "s1", "s2") 318 .areAllEnabled() 319 320 .selectPinned() 321 .haveIds("ms2", "s2"); 322 }); 323 324 mInjectedCallingUid = Process.SHELL_UID; 325 assertSuccess(callShellCommand("clear-shortcuts", "--user", "10", CALLING_PACKAGE_1)); 326 327 // Only manifest shortcuts will remain, and are no longer pinned. 328 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> { 329 assertWith(getCallerShortcuts()) 330 .haveIds("ms1", "ms2") 331 .areAllEnabled() 332 .areAllNotPinned(); 333 }); 334 } 335} 336