blob: e02e98f2329f307024c6d1734914858b88197950 [file] [log] [blame]
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001/*
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.shortcutmanagertest;
17
18import static junit.framework.Assert.assertEquals;
19import static junit.framework.Assert.assertFalse;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22import static junit.framework.Assert.assertTrue;
23import static junit.framework.Assert.fail;
24
25import static org.mockito.Matchers.any;
26import static org.mockito.Matchers.anyList;
27import static org.mockito.Matchers.anyString;
28import static org.mockito.Matchers.eq;
29import static org.mockito.Mockito.reset;
30import static org.mockito.Mockito.times;
31import static org.mockito.Mockito.verify;
32
33import android.app.Instrumentation;
34import android.content.Context;
35import android.content.pm.LauncherApps;
36import android.content.pm.ShortcutInfo;
37import android.graphics.Bitmap;
38import android.graphics.BitmapFactory;
39import android.os.BaseBundle;
40import android.os.Bundle;
41import android.os.ParcelFileDescriptor;
42import android.os.UserHandle;
43import android.test.MoreAsserts;
44import android.util.Log;
45
46import junit.framework.Assert;
47
48import org.hamcrest.BaseMatcher;
49import org.hamcrest.Description;
50import org.hamcrest.Matcher;
51import org.mockito.Mockito;
52
53import java.io.BufferedReader;
54import java.io.FileReader;
55import java.io.IOException;
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.Collection;
59import java.util.HashSet;
60import java.util.List;
61import java.util.Set;
62import java.util.function.BooleanSupplier;
63import java.util.function.Function;
64import java.util.function.Predicate;
65
66public class ShortcutManagerTestUtils {
67 private static final String TAG = "ShortcutManagerUtils";
68
69 private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true
70
71 private static final int STANDARD_TIMEOUT_SEC = 5;
72
73 private ShortcutManagerTestUtils() {
74 }
75
76 private static List<String> readAll(ParcelFileDescriptor pfd) {
77 try {
78 try {
79 final ArrayList<String> ret = new ArrayList<>();
80 try (BufferedReader r = new BufferedReader(
81 new FileReader(pfd.getFileDescriptor()))) {
82 String line;
83 while ((line = r.readLine()) != null) {
84 ret.add(line);
85 }
86 r.readLine();
87 }
88 return ret;
89 } finally {
90 pfd.close();
91 }
92 } catch (IOException e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 private static String concatResult(List<String> result) {
98 final StringBuilder sb = new StringBuilder();
99 for (String s : result) {
100 sb.append(s);
101 sb.append("\n");
102 }
103 return sb.toString();
104 }
105
106 private static List<String> runCommand(Instrumentation instrumentation, String command) {
107 return runCommand(instrumentation, command, null);
108 }
109 private static List<String> runCommand(Instrumentation instrumentation, String command,
110 Predicate<List<String>> resultAsserter) {
111 Log.d(TAG, "Running command: " + command);
112 final List<String> result;
113 try {
114 result = readAll(
115 instrumentation.getUiAutomation().executeShellCommand(command));
116 } catch (Exception e) {
117 throw new RuntimeException(e);
118 }
119 if (resultAsserter != null && !resultAsserter.test(result)) {
120 fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
121 }
122 return result;
123 }
124
125 private static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
126 runCommand(instrumentation, command, result -> result.size() == 0);
127 }
128
129 private static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
130 Predicate<List<String>> resultAsserter) {
131 return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
132 }
133
134 public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
135 String command) {
136 return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
137 }
138
139 public static String getDefaultLauncher(Instrumentation instrumentation) {
140 final String PREFIX = "Launcher: ComponentInfo{";
141 final String POSTFIX = "}";
142 final List<String> result = runShortcutCommandForSuccess(
143 instrumentation, "get-default-launcher");
144 for (String s : result) {
145 if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
146 return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
147 }
148 }
149 fail("Default launcher not found");
150 return null;
151 }
152
153 public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
154 runCommandForNoOutput(instrumentation, "cmd package set-home-activity " + component);
155 }
156
157 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
158 setDefaultLauncher(instrumentation, packageContext.getPackageName()
159 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
160 }
161
162 public static void overrideConfig(Instrumentation instrumentation, String config) {
163 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
164 }
165
166 public static void resetConfig(Instrumentation instrumentation) {
167 runShortcutCommandForSuccess(instrumentation, "reset-config");
168 }
169
170 public static void resetThrottling(Instrumentation instrumentation) {
171 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
172 }
173
174 public static void resetAllThrottling(Instrumentation instrumentation) {
175 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
176 }
177
178 public static void clearShortcuts(Instrumentation instrumentation, int userId,
179 String packageName) {
180 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
181 + " --user " + userId + " " + packageName);
182 }
183
184 public static void dumpsysShortcut(Instrumentation instrumentation) {
185 if (!ENABLE_DUMPSYS) {
186 return;
187 }
188 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
189 Log.e(TAG, s);
190 }
191 }
192
193 public static Bundle makeBundle(Object... keysAndValues) {
194 assertTrue((keysAndValues.length % 2) == 0);
195
196 if (keysAndValues.length == 0) {
197 return null;
198 }
199 final Bundle ret = new Bundle();
200
201 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
202 final String key = keysAndValues[i].toString();
203 final Object value = keysAndValues[i + 1];
204
205 if (value == null) {
206 ret.putString(key, null);
207 } else if (value instanceof Integer) {
208 ret.putInt(key, (Integer) value);
209 } else if (value instanceof String) {
210 ret.putString(key, (String) value);
211 } else if (value instanceof Bundle) {
212 ret.putBundle(key, (Bundle) value);
213 } else {
214 fail("Type not supported yet: " + value.getClass().getName());
215 }
216 }
217 return ret;
218 }
219
220 public static <T> List<T> list(T... array) {
221 return Arrays.asList(array);
222 }
223
224 public static <T> Set<T> hashSet(Set<T> in) {
225 return new HashSet<T>(in);
226 }
227
228 public static <T> Set<T> set(T... values) {
229 return set(v -> v, values);
230 }
231
232 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
233 return set(converter, Arrays.asList(values));
234 }
235
236 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
237 final HashSet<T> ret = new HashSet<>();
238 for (V v : values) {
239 ret.add(converter.apply(v));
240 }
241 return ret;
242 }
243
244 public static void resetAll(Collection<?> mocks) {
245 for (Object o : mocks) {
246 reset(o);
247 }
248 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700249
250 public static <T> List<T> assertEmpty(List<T> list) {
251 assertEquals(0, list.size());
252 return list;
253 }
254
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700255 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
256 String expectedExceptionMessageRegex, Runnable r) {
257 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
258 }
259
Makoto Onuki22fcc682016-05-17 14:52:19 -0700260 public static void assertCannotUpdateImmutable(Runnable r) {
261 assertExpectException(
262 IllegalArgumentException.class, "may not be manipulated via APIs", r);
263 }
264
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700265 public static void assertDynamicShortcutCountExceeded(Runnable r) {
266 assertExpectException(IllegalArgumentException.class,
267 "Max number of dynamic shortcuts exceeded", r);
268 }
269
270 public static void assertExpectException(String message,
271 Class<? extends Throwable> expectedExceptionType,
272 String expectedExceptionMessageRegex, Runnable r) {
273 try {
274 r.run();
275 Assert.fail("Expected exception type " + expectedExceptionType.getName()
276 + " was not thrown (message=" + message + ")");
277 } catch (Throwable e) {
278 Assert.assertTrue(
279 "Expected exception type was " + expectedExceptionType.getName()
280 + " but caught " + e + " (message=" + message + ")",
281 expectedExceptionType.isAssignableFrom(e.getClass()));
282 if (expectedExceptionMessageRegex != null) {
283 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
284 }
285 }
286 }
287
288 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
289 String... expectedIds) {
290 final HashSet<String> expected = new HashSet<>(list(expectedIds));
291 final HashSet<String> actual = new HashSet<>();
292 for (ShortcutInfo s : actualShortcuts) {
293 actual.add(s.getId());
294 }
295
296 // Compare the sets.
297 assertEquals(expected, actual);
298 return actualShortcuts;
299 }
300
301 public static List<ShortcutInfo> assertAllHaveIntents(
302 List<ShortcutInfo> actualShortcuts) {
303 for (ShortcutInfo s : actualShortcuts) {
304 assertNotNull("ID " + s.getId(), s.getIntent());
305 }
306 return actualShortcuts;
307 }
308
309 public static List<ShortcutInfo> assertAllNotHaveIntents(
310 List<ShortcutInfo> actualShortcuts) {
311 for (ShortcutInfo s : actualShortcuts) {
312 assertNull("ID " + s.getId(), s.getIntent());
313 }
314 return actualShortcuts;
315 }
316
317 public static List<ShortcutInfo> assertAllHaveTitle(
318 List<ShortcutInfo> actualShortcuts) {
319 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700320 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700321 }
322 return actualShortcuts;
323 }
324
325 public static List<ShortcutInfo> assertAllNotHaveTitle(
326 List<ShortcutInfo> actualShortcuts) {
327 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700328 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700329 }
330 return actualShortcuts;
331 }
332
333 public static List<ShortcutInfo> assertAllHaveIconResId(
334 List<ShortcutInfo> actualShortcuts) {
335 for (ShortcutInfo s : actualShortcuts) {
336 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
337 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
338 }
339 return actualShortcuts;
340 }
341
342 public static List<ShortcutInfo> assertAllHaveIconFile(
343 List<ShortcutInfo> actualShortcuts) {
344 for (ShortcutInfo s : actualShortcuts) {
345 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
346 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
347 }
348 return actualShortcuts;
349 }
350
351 public static List<ShortcutInfo> assertAllHaveIcon(
352 List<ShortcutInfo> actualShortcuts) {
353 for (ShortcutInfo s : actualShortcuts) {
354 assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource());
355 }
356 return actualShortcuts;
357 }
358
359 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
360 List<ShortcutInfo> actualShortcuts) {
361 for (ShortcutInfo s : actualShortcuts) {
362 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
363 }
364 return actualShortcuts;
365 }
366
367 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
368 List<ShortcutInfo> actualShortcuts) {
369 for (ShortcutInfo s : actualShortcuts) {
370 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
371 }
372 return actualShortcuts;
373 }
374
375 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
376 for (ShortcutInfo s : actualShortcuts) {
377 assertTrue("ID " + s.getId(), s.isDynamic());
378 }
379 return actualShortcuts;
380 }
381
382 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
383 for (ShortcutInfo s : actualShortcuts) {
384 assertTrue("ID " + s.getId(), s.isPinned());
385 }
386 return actualShortcuts;
387 }
388
389 public static List<ShortcutInfo> assertAllDynamicOrPinned(
390 List<ShortcutInfo> actualShortcuts) {
391 for (ShortcutInfo s : actualShortcuts) {
392 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
393 }
394 return actualShortcuts;
395 }
396
Makoto Onuki22fcc682016-05-17 14:52:19 -0700397 public static List<ShortcutInfo> assertAllManifest(
398 List<ShortcutInfo> actualShortcuts) {
399 for (ShortcutInfo s : actualShortcuts) {
400 assertTrue("ID " + s.getId(), s.isManifestShortcut());
401 }
402 return actualShortcuts;
403 }
404
405 public static List<ShortcutInfo> assertAllNotManifest(
406 List<ShortcutInfo> actualShortcuts) {
407 for (ShortcutInfo s : actualShortcuts) {
408 assertFalse("ID " + s.getId(), s.isManifestShortcut());
409 }
410 return actualShortcuts;
411 }
412
413 public static List<ShortcutInfo> assertAllDisabled(
414 List<ShortcutInfo> actualShortcuts) {
415 for (ShortcutInfo s : actualShortcuts) {
416 assertTrue("ID " + s.getId(), !s.isEnabled());
417 }
418 return actualShortcuts;
419 }
420
421 public static List<ShortcutInfo> assertAllEnabled(
422 List<ShortcutInfo> actualShortcuts) {
423 for (ShortcutInfo s : actualShortcuts) {
424 assertTrue("ID " + s.getId(), s.isEnabled());
425 }
426 return actualShortcuts;
427 }
428
429 public static List<ShortcutInfo> assertAllImmutable(
430 List<ShortcutInfo> actualShortcuts) {
431 for (ShortcutInfo s : actualShortcuts) {
432 assertTrue("ID " + s.getId(), s.isImmutable());
433 }
434 return actualShortcuts;
435 }
436
Makoto Onuki20c95f82016-05-11 16:51:01 -0700437 public static List<ShortcutInfo> assertAllStringsResolved(
438 List<ShortcutInfo> actualShortcuts) {
439 for (ShortcutInfo s : actualShortcuts) {
440 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
441 }
442 return actualShortcuts;
443 }
444
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700445 public static void assertDynamicOnly(ShortcutInfo si) {
446 assertTrue(si.isDynamic());
447 assertFalse(si.isPinned());
448 }
449
450 public static void assertPinnedOnly(ShortcutInfo si) {
451 assertFalse(si.isDynamic());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700452 assertFalse(si.isManifestShortcut());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700453 assertTrue(si.isPinned());
454 }
455
456 public static void assertDynamicAndPinned(ShortcutInfo si) {
457 assertTrue(si.isDynamic());
458 assertTrue(si.isPinned());
459 }
460
461 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
462 assertEquals("width", expectedWidth, bitmap.getWidth());
463 assertEquals("height", expectedHeight, bitmap.getHeight());
464 }
465
466 public static <T> void assertAllUnique(Collection<T> list) {
467 final Set<Object> set = new HashSet<>();
468 for (T item : list) {
469 if (set.contains(item)) {
470 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
471 }
472 set.add(item);
473 }
474 }
475
Makoto Onuki39686e82016-04-13 18:03:00 -0700476 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
477 for (ShortcutInfo si : list) {
478 if (si.getId().equals(id)) {
479 return si;
480 }
481 }
482 fail("Shortcut " + id + " not found in the list");
483 return null;
484 }
485
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700486 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
487 assertNotNull(pfd);
488 try {
489 try {
490 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
491 } finally {
492 pfd.close();
493 }
494 } catch (IOException e) {
495 throw new RuntimeException(e);
496 }
497 }
498
499 public static void assertBundleEmpty(BaseBundle b) {
500 assertTrue(b == null || b.size() == 0);
501 }
502
503 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
504 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
505 any(UserHandle.class));
506 }
507
508 public static void assertCallbackReceived(LauncherApps.Callback mock,
509 UserHandle user, String packageName, String... ids) {
510 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
511 eq(user));
512 }
513
514 public static boolean checkAssertSuccess(Runnable r) {
515 try {
516 r.run();
517 return true;
518 } catch (AssertionError e) {
519 return false;
520 }
521 }
522
523 public static <T> T checkArgument(Predicate<T> checker, String description,
524 List<T> matchedCaptor) {
525 final Matcher<T> m = new BaseMatcher<T>() {
526 @Override
527 public boolean matches(Object item) {
528 if (item == null) {
529 return false;
530 }
531 final T value = (T) item;
532 if (!checker.test(value)) {
533 return false;
534 }
535
536 if (matchedCaptor != null) {
537 matchedCaptor.add(value);
538 }
539 return true;
540 }
541
542 @Override
543 public void describeTo(Description d) {
544 d.appendText(description);
545 }
546 };
547 return Mockito.argThat(m);
548 }
549
550 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
551 return checkArgument((List<ShortcutInfo> list) -> {
552 final Set<String> actualSet = set(si -> si.getId(), list);
553 return actualSet.equals(set(ids));
554
555 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
556 }
557
558 public static void waitUntil(String message, BooleanSupplier condition) {
559 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
560 }
561
562 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
563 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
564 while (System.currentTimeMillis() < timeout) {
565 if (condition.getAsBoolean()) {
566 return;
567 }
568 try {
569 Thread.sleep(100);
570 } catch (InterruptedException e) {
571 throw new RuntimeException(e);
572 }
573 }
574 fail("Timed out for: " + message);
575 }
576}