blob: 712bc1e8c30118754e5071764f498c189d71de4a [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;
Makoto Onuki7001a612016-05-27 13:24:28 -070034import android.content.ComponentName;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070035import android.content.Context;
36import android.content.pm.LauncherApps;
37import android.content.pm.ShortcutInfo;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
40import android.os.BaseBundle;
41import android.os.Bundle;
Makoto Onuki7001a612016-05-27 13:24:28 -070042import android.os.Parcel;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070043import android.os.ParcelFileDescriptor;
44import android.os.UserHandle;
45import android.test.MoreAsserts;
Makoto Onukidf6da042016-06-16 09:51:40 -070046import android.util.ArraySet;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070047import android.util.Log;
48
49import junit.framework.Assert;
Makoto Onukidf6da042016-06-16 09:51:40 -070050import junit.framework.AssertionFailedError;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070051
52import org.hamcrest.BaseMatcher;
53import org.hamcrest.Description;
54import org.hamcrest.Matcher;
55import org.mockito.Mockito;
56
57import java.io.BufferedReader;
58import java.io.FileReader;
59import java.io.IOException;
60import java.util.ArrayList;
61import java.util.Arrays;
62import java.util.Collection;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070063import java.util.Collections;
64import java.util.Comparator;
65import java.util.LinkedHashSet;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070066import java.util.List;
67import java.util.Set;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070068import java.util.SortedSet;
69import java.util.TreeSet;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070070import java.util.function.BooleanSupplier;
Makoto Onukidf6da042016-06-16 09:51:40 -070071import java.util.function.Consumer;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070072import java.util.function.Function;
73import java.util.function.Predicate;
74
Makoto Onuki51ab2b32016-06-02 11:03:51 -070075/**
76 * Common utility methods for ShortcutManager tests. This is used by both CTS and the unit tests.
77 * Because it's used by CTS too, it can only access the public APIs.
78 */
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070079public class ShortcutManagerTestUtils {
80 private static final String TAG = "ShortcutManagerUtils";
81
Makoto Onuki9e1f5592016-06-08 12:30:23 -070082 private static final boolean ENABLE_DUMPSYS = false; // DO NOT SUBMIT WITH true
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070083
84 private static final int STANDARD_TIMEOUT_SEC = 5;
85
Makoto Onuki9e1f5592016-06-08 12:30:23 -070086 private static final String[] EMPTY_STRINGS = new String[0];
87
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070088 private ShortcutManagerTestUtils() {
89 }
90
91 private static List<String> readAll(ParcelFileDescriptor pfd) {
92 try {
93 try {
94 final ArrayList<String> ret = new ArrayList<>();
95 try (BufferedReader r = new BufferedReader(
96 new FileReader(pfd.getFileDescriptor()))) {
97 String line;
98 while ((line = r.readLine()) != null) {
99 ret.add(line);
100 }
101 r.readLine();
102 }
103 return ret;
104 } finally {
105 pfd.close();
106 }
107 } catch (IOException e) {
108 throw new RuntimeException(e);
109 }
110 }
111
112 private static String concatResult(List<String> result) {
113 final StringBuilder sb = new StringBuilder();
114 for (String s : result) {
115 sb.append(s);
116 sb.append("\n");
117 }
118 return sb.toString();
119 }
120
121 private static List<String> runCommand(Instrumentation instrumentation, String command) {
122 return runCommand(instrumentation, command, null);
123 }
124 private static List<String> runCommand(Instrumentation instrumentation, String command,
125 Predicate<List<String>> resultAsserter) {
126 Log.d(TAG, "Running command: " + command);
127 final List<String> result;
128 try {
129 result = readAll(
130 instrumentation.getUiAutomation().executeShellCommand(command));
131 } catch (Exception e) {
132 throw new RuntimeException(e);
133 }
134 if (resultAsserter != null && !resultAsserter.test(result)) {
135 fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
136 }
137 return result;
138 }
139
140 private static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
141 runCommand(instrumentation, command, result -> result.size() == 0);
142 }
143
144 private static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
145 Predicate<List<String>> resultAsserter) {
146 return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
147 }
148
149 public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
150 String command) {
151 return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
152 }
153
154 public static String getDefaultLauncher(Instrumentation instrumentation) {
155 final String PREFIX = "Launcher: ComponentInfo{";
156 final String POSTFIX = "}";
157 final List<String> result = runShortcutCommandForSuccess(
158 instrumentation, "get-default-launcher");
159 for (String s : result) {
160 if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
161 return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
162 }
163 }
164 fail("Default launcher not found");
165 return null;
166 }
167
168 public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
169 runCommandForNoOutput(instrumentation, "cmd package set-home-activity " + component);
170 }
171
172 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
173 setDefaultLauncher(instrumentation, packageContext.getPackageName()
174 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
175 }
176
177 public static void overrideConfig(Instrumentation instrumentation, String config) {
178 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
179 }
180
181 public static void resetConfig(Instrumentation instrumentation) {
182 runShortcutCommandForSuccess(instrumentation, "reset-config");
183 }
184
185 public static void resetThrottling(Instrumentation instrumentation) {
186 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
187 }
188
189 public static void resetAllThrottling(Instrumentation instrumentation) {
190 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
191 }
192
193 public static void clearShortcuts(Instrumentation instrumentation, int userId,
194 String packageName) {
195 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
196 + " --user " + userId + " " + packageName);
197 }
198
199 public static void dumpsysShortcut(Instrumentation instrumentation) {
200 if (!ENABLE_DUMPSYS) {
201 return;
202 }
203 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
204 Log.e(TAG, s);
205 }
206 }
207
208 public static Bundle makeBundle(Object... keysAndValues) {
209 assertTrue((keysAndValues.length % 2) == 0);
210
211 if (keysAndValues.length == 0) {
212 return null;
213 }
214 final Bundle ret = new Bundle();
215
216 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
217 final String key = keysAndValues[i].toString();
218 final Object value = keysAndValues[i + 1];
219
220 if (value == null) {
221 ret.putString(key, null);
222 } else if (value instanceof Integer) {
223 ret.putInt(key, (Integer) value);
224 } else if (value instanceof String) {
225 ret.putString(key, (String) value);
226 } else if (value instanceof Bundle) {
227 ret.putBundle(key, (Bundle) value);
228 } else {
229 fail("Type not supported yet: " + value.getClass().getName());
230 }
231 }
232 return ret;
233 }
234
235 public static <T> List<T> list(T... array) {
236 return Arrays.asList(array);
237 }
238
239 public static <T> Set<T> hashSet(Set<T> in) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700240 return new LinkedHashSet<>(in);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700241 }
242
243 public static <T> Set<T> set(T... values) {
244 return set(v -> v, values);
245 }
246
247 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
248 return set(converter, Arrays.asList(values));
249 }
250
251 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700252 final LinkedHashSet<T> ret = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700253 for (V v : values) {
254 ret.add(converter.apply(v));
255 }
256 return ret;
257 }
258
259 public static void resetAll(Collection<?> mocks) {
260 for (Object o : mocks) {
261 reset(o);
262 }
263 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700264
Makoto Onukidf6da042016-06-16 09:51:40 -0700265 public static <T extends Collection<?>> T assertEmpty(T collection) {
266 if (collection == null) {
267 return collection; // okay.
268 }
269 assertEquals(0, collection.size());
270 return collection;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700271 }
272
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700273 public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
274 final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
275 ret.removeIf(si -> !p.test(si));
276 return ret;
277 }
278
Makoto Onuki7001a612016-05-27 13:24:28 -0700279 public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
280 ComponentName activity) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700281 return filter(list, si ->
282 (si.getActivity().equals(activity)
283 && (si.isManifestShortcut() || si.isDynamic())));
284 }
285
286 public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
287 return filter(list, si -> si.getLastChangedTimestamp() >= time);
Makoto Onuki7001a612016-05-27 13:24:28 -0700288 }
289
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700290 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
291 String expectedExceptionMessageRegex, Runnable r) {
292 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
293 }
294
Makoto Onuki22fcc682016-05-17 14:52:19 -0700295 public static void assertCannotUpdateImmutable(Runnable r) {
296 assertExpectException(
297 IllegalArgumentException.class, "may not be manipulated via APIs", r);
298 }
299
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700300 public static void assertDynamicShortcutCountExceeded(Runnable r) {
301 assertExpectException(IllegalArgumentException.class,
302 "Max number of dynamic shortcuts exceeded", r);
303 }
304
305 public static void assertExpectException(String message,
306 Class<? extends Throwable> expectedExceptionType,
307 String expectedExceptionMessageRegex, Runnable r) {
308 try {
309 r.run();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700310 } catch (Throwable e) {
311 Assert.assertTrue(
312 "Expected exception type was " + expectedExceptionType.getName()
313 + " but caught " + e + " (message=" + message + ")",
314 expectedExceptionType.isAssignableFrom(e.getClass()));
315 if (expectedExceptionMessageRegex != null) {
316 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
317 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700318 return; // Pass
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700319 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700320 Assert.fail("Expected exception type " + expectedExceptionType.getName()
321 + " was not thrown");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700322 }
323
324 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
325 String... expectedIds) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700326 final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
327 final SortedSet<String> actual = new TreeSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700328 for (ShortcutInfo s : actualShortcuts) {
329 actual.add(s.getId());
330 }
331
332 // Compare the sets.
333 assertEquals(expected, actual);
334 return actualShortcuts;
335 }
336
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700337 public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
338 String... expectedIds) {
339 final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
340 final ArrayList<String> actual = new ArrayList<>();
341 for (ShortcutInfo s : actualShortcuts) {
342 actual.add(s.getId());
343 }
344 assertEquals(expected, actual);
345 return actualShortcuts;
346 }
347
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700348 public static List<ShortcutInfo> assertAllHaveIntents(
349 List<ShortcutInfo> actualShortcuts) {
350 for (ShortcutInfo s : actualShortcuts) {
351 assertNotNull("ID " + s.getId(), s.getIntent());
352 }
353 return actualShortcuts;
354 }
355
356 public static List<ShortcutInfo> assertAllNotHaveIntents(
357 List<ShortcutInfo> actualShortcuts) {
358 for (ShortcutInfo s : actualShortcuts) {
359 assertNull("ID " + s.getId(), s.getIntent());
360 }
361 return actualShortcuts;
362 }
363
364 public static List<ShortcutInfo> assertAllHaveTitle(
365 List<ShortcutInfo> actualShortcuts) {
366 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700367 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700368 }
369 return actualShortcuts;
370 }
371
372 public static List<ShortcutInfo> assertAllNotHaveTitle(
373 List<ShortcutInfo> actualShortcuts) {
374 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700375 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700376 }
377 return actualShortcuts;
378 }
379
380 public static List<ShortcutInfo> assertAllHaveIconResId(
381 List<ShortcutInfo> actualShortcuts) {
382 for (ShortcutInfo s : actualShortcuts) {
383 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
384 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
385 }
386 return actualShortcuts;
387 }
388
389 public static List<ShortcutInfo> assertAllHaveIconFile(
390 List<ShortcutInfo> actualShortcuts) {
391 for (ShortcutInfo s : actualShortcuts) {
392 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
393 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
394 }
395 return actualShortcuts;
396 }
397
398 public static List<ShortcutInfo> assertAllHaveIcon(
399 List<ShortcutInfo> actualShortcuts) {
400 for (ShortcutInfo s : actualShortcuts) {
401 assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource());
402 }
403 return actualShortcuts;
404 }
405
406 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
407 List<ShortcutInfo> actualShortcuts) {
408 for (ShortcutInfo s : actualShortcuts) {
409 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
410 }
411 return actualShortcuts;
412 }
413
414 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
415 List<ShortcutInfo> actualShortcuts) {
416 for (ShortcutInfo s : actualShortcuts) {
417 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
418 }
419 return actualShortcuts;
420 }
421
422 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
423 for (ShortcutInfo s : actualShortcuts) {
424 assertTrue("ID " + s.getId(), s.isDynamic());
425 }
426 return actualShortcuts;
427 }
428
429 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
430 for (ShortcutInfo s : actualShortcuts) {
431 assertTrue("ID " + s.getId(), s.isPinned());
432 }
433 return actualShortcuts;
434 }
435
436 public static List<ShortcutInfo> assertAllDynamicOrPinned(
437 List<ShortcutInfo> actualShortcuts) {
438 for (ShortcutInfo s : actualShortcuts) {
439 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
440 }
441 return actualShortcuts;
442 }
443
Makoto Onuki22fcc682016-05-17 14:52:19 -0700444 public static List<ShortcutInfo> assertAllManifest(
445 List<ShortcutInfo> actualShortcuts) {
446 for (ShortcutInfo s : actualShortcuts) {
447 assertTrue("ID " + s.getId(), s.isManifestShortcut());
448 }
449 return actualShortcuts;
450 }
451
452 public static List<ShortcutInfo> assertAllNotManifest(
453 List<ShortcutInfo> actualShortcuts) {
454 for (ShortcutInfo s : actualShortcuts) {
455 assertFalse("ID " + s.getId(), s.isManifestShortcut());
456 }
457 return actualShortcuts;
458 }
459
460 public static List<ShortcutInfo> assertAllDisabled(
461 List<ShortcutInfo> actualShortcuts) {
462 for (ShortcutInfo s : actualShortcuts) {
463 assertTrue("ID " + s.getId(), !s.isEnabled());
464 }
465 return actualShortcuts;
466 }
467
468 public static List<ShortcutInfo> assertAllEnabled(
469 List<ShortcutInfo> actualShortcuts) {
470 for (ShortcutInfo s : actualShortcuts) {
471 assertTrue("ID " + s.getId(), s.isEnabled());
472 }
473 return actualShortcuts;
474 }
475
476 public static List<ShortcutInfo> assertAllImmutable(
477 List<ShortcutInfo> actualShortcuts) {
478 for (ShortcutInfo s : actualShortcuts) {
479 assertTrue("ID " + s.getId(), s.isImmutable());
480 }
481 return actualShortcuts;
482 }
483
Makoto Onuki20c95f82016-05-11 16:51:01 -0700484 public static List<ShortcutInfo> assertAllStringsResolved(
485 List<ShortcutInfo> actualShortcuts) {
486 for (ShortcutInfo s : actualShortcuts) {
487 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
488 }
489 return actualShortcuts;
490 }
491
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700492 public static void assertDynamicOnly(ShortcutInfo si) {
493 assertTrue(si.isDynamic());
494 assertFalse(si.isPinned());
495 }
496
497 public static void assertPinnedOnly(ShortcutInfo si) {
498 assertFalse(si.isDynamic());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700499 assertFalse(si.isManifestShortcut());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700500 assertTrue(si.isPinned());
501 }
502
503 public static void assertDynamicAndPinned(ShortcutInfo si) {
504 assertTrue(si.isDynamic());
505 assertTrue(si.isPinned());
506 }
507
508 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
509 assertEquals("width", expectedWidth, bitmap.getWidth());
510 assertEquals("height", expectedHeight, bitmap.getHeight());
511 }
512
513 public static <T> void assertAllUnique(Collection<T> list) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700514 final Set<Object> set = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700515 for (T item : list) {
516 if (set.contains(item)) {
517 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
518 }
519 set.add(item);
520 }
521 }
522
Makoto Onuki39686e82016-04-13 18:03:00 -0700523 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
524 for (ShortcutInfo si : list) {
525 if (si.getId().equals(id)) {
526 return si;
527 }
528 }
529 fail("Shortcut " + id + " not found in the list");
530 return null;
531 }
532
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700533 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
534 assertNotNull(pfd);
535 try {
536 try {
537 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
538 } finally {
539 pfd.close();
540 }
541 } catch (IOException e) {
542 throw new RuntimeException(e);
543 }
544 }
545
546 public static void assertBundleEmpty(BaseBundle b) {
547 assertTrue(b == null || b.size() == 0);
548 }
549
550 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
551 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
552 any(UserHandle.class));
553 }
554
555 public static void assertCallbackReceived(LauncherApps.Callback mock,
556 UserHandle user, String packageName, String... ids) {
557 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
558 eq(user));
559 }
560
561 public static boolean checkAssertSuccess(Runnable r) {
562 try {
563 r.run();
564 return true;
565 } catch (AssertionError e) {
566 return false;
567 }
568 }
569
570 public static <T> T checkArgument(Predicate<T> checker, String description,
571 List<T> matchedCaptor) {
572 final Matcher<T> m = new BaseMatcher<T>() {
573 @Override
574 public boolean matches(Object item) {
575 if (item == null) {
576 return false;
577 }
578 final T value = (T) item;
579 if (!checker.test(value)) {
580 return false;
581 }
582
583 if (matchedCaptor != null) {
584 matchedCaptor.add(value);
585 }
586 return true;
587 }
588
589 @Override
590 public void describeTo(Description d) {
591 d.appendText(description);
592 }
593 };
594 return Mockito.argThat(m);
595 }
596
597 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
598 return checkArgument((List<ShortcutInfo> list) -> {
599 final Set<String> actualSet = set(si -> si.getId(), list);
600 return actualSet.equals(set(ids));
601
602 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
603 }
604
Makoto Onuki7001a612016-05-27 13:24:28 -0700605 public static ShortcutInfo parceled(ShortcutInfo si) {
606 Parcel p = Parcel.obtain();
607 p.writeParcelable(si, 0);
608 p.setDataPosition(0);
609 ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader());
610 p.recycle();
611 return si2;
612 }
613
614 public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) {
615 if (list == null) {
616 return null;
617 }
618 final List<ShortcutInfo> ret = new ArrayList<>(list.size());
619 for (ShortcutInfo si : list) {
620 ret.add(parceled(si));
621 }
622
623 return ret;
624 }
625
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700626 private static final Comparator<ShortcutInfo> sRankComparator =
627 (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank());
628
629 public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) {
630 final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts);
631 Collections.sort(ret, sRankComparator);
632 return ret;
633 }
634
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700635 public static void waitUntil(String message, BooleanSupplier condition) {
636 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
637 }
638
639 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
640 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
641 while (System.currentTimeMillis() < timeout) {
642 if (condition.getAsBoolean()) {
643 return;
644 }
645 try {
646 Thread.sleep(100);
647 } catch (InterruptedException e) {
648 throw new RuntimeException(e);
649 }
650 }
651 fail("Timed out for: " + message);
652 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700653
654 public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) {
655 return new ShortcutListAsserter(list);
656 }
657
658 /**
659 * New style assertion that allows chained calls.
660 */
661 public static class ShortcutListAsserter {
Makoto Onukidf6da042016-06-16 09:51:40 -0700662 private final ShortcutListAsserter mOriginal;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700663 private final List<ShortcutInfo> mList;
664
665 ShortcutListAsserter(List<ShortcutInfo> list) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700666 this(null, list);
667 }
668
669 private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) {
670 mOriginal = original == null ? this : original;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700671 mList = new ArrayList<>(list);
672 }
673
Makoto Onukidf6da042016-06-16 09:51:40 -0700674 public ShortcutListAsserter revertToOriginalList() {
675 return mOriginal;
676 }
677
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700678 public ShortcutListAsserter selectDynamic() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700679 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700680 filter(mList, ShortcutInfo::isDynamic));
681 }
682
683 public ShortcutListAsserter selectManifest() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700684 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700685 filter(mList, ShortcutInfo::isManifestShortcut));
686 }
687
688 public ShortcutListAsserter selectPinned() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700689 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700690 filter(mList, ShortcutInfo::isPinned));
691 }
692
693 public ShortcutListAsserter selectByActivity(ComponentName activity) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700694 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700695 ShortcutManagerTestUtils.filterByActivity(mList, activity));
696 }
697
698 public ShortcutListAsserter selectByChangedSince(long time) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700699 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700700 ShortcutManagerTestUtils.changedSince(mList, time));
701 }
702
Makoto Onukidf6da042016-06-16 09:51:40 -0700703 public ShortcutListAsserter selectByIds(String... ids) {
704 final Set<String> idSet = set(ids);
705 final ArrayList<ShortcutInfo> selected = new ArrayList<>();
706 for (ShortcutInfo si : mList) {
707 if (idSet.contains(si.getId())) {
708 selected.add(si);
709 idSet.remove(si.getId());
710 }
711 }
712 if (idSet.size() > 0) {
713 fail("Shortcuts not found for IDs=" + idSet);
714 }
715
716 return new ShortcutListAsserter(this, selected);
717 }
718
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700719 public ShortcutListAsserter toSortByRank() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700720 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700721 ShortcutManagerTestUtils.sortedByRank(mList));
722 }
723
Makoto Onukidf6da042016-06-16 09:51:40 -0700724 public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) {
725 c.accept(mList);
726 return this;
727 }
728
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700729 public ShortcutListAsserter haveIds(String... expectedIds) {
730 assertShortcutIds(mList, expectedIds);
731 return this;
732 }
733
734 public ShortcutListAsserter haveIdsOrdered(String... expectedIds) {
735 assertShortcutIdsOrdered(mList, expectedIds);
736 return this;
737 }
738
739 private ShortcutListAsserter haveSequentialRanks() {
740 for (int i = 0; i < mList.size(); i++) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700741 final ShortcutInfo si = mList.get(i);
742 assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank());
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700743 }
744 return this;
745 }
746
747 public ShortcutListAsserter haveRanksInOrder(String... expectedIds) {
748 toSortByRank()
749 .haveSequentialRanks()
750 .haveIdsOrdered(expectedIds);
751 return this;
752 }
753
754 public ShortcutListAsserter isEmpty() {
755 assertEquals(0, mList.size());
756 return this;
757 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700758
759 public ShortcutListAsserter areAllDynamic() {
760 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic()));
761 return this;
762 }
763
764 public ShortcutListAsserter areAllNotDynamic() {
765 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic()));
766 return this;
767 }
768
769 public ShortcutListAsserter areAllPinned() {
770 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned()));
771 return this;
772 }
773
774 public ShortcutListAsserter areAllNotPinned() {
775 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned()));
776 return this;
777 }
778
779 public ShortcutListAsserter areAllManifest() {
780 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isManifestShortcut()));
781 return this;
782 }
783
784 public ShortcutListAsserter areAllNotManifest() {
785 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isManifestShortcut()));
786 return this;
787 }
788
789 public ShortcutListAsserter areAllImmutable() {
790 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable()));
791 return this;
792 }
793
794 public ShortcutListAsserter areAllMutable() {
795 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable()));
796 return this;
797 }
798
799 public ShortcutListAsserter areAllEnabled() {
800 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled()));
801 return this;
802 }
803
804 public ShortcutListAsserter areAllDisabled() {
805 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
806 return this;
807 }
808
809 public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
810 for (int i = 0; i < mList.size(); i++) {
811 final ShortcutInfo si = mList.get(i);
812 sa.accept(si);
813 }
814 return this;
815 }
816
817 public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
818 Consumer<ShortcutInfo> sa) {
819 boolean found = false;
820 for (int i = 0; i < mList.size(); i++) {
821 final ShortcutInfo si = mList.get(i);
822 if (p.test(si)) {
823 found = true;
824 try {
825 sa.accept(si);
826 } catch (Throwable e) {
827 throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
828 }
829 }
830 }
831 assertTrue("Shortcut with the given condition not found.", found);
832 return this;
833 }
834
835 public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
836 forShortcut(si -> si.getId().equals(id), sa);
837
838 return this;
839 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700840 }
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700841}