blob: 132ed98ee5bc83ca113819406c4892e9730b97da [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;
Makoto Onukib08790c2016-06-23 14:05:46 -070029import static org.mockito.Mockito.mock;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070030import static org.mockito.Mockito.reset;
31import static org.mockito.Mockito.times;
32import static org.mockito.Mockito.verify;
33
34import android.app.Instrumentation;
Makoto Onuki7001a612016-05-27 13:24:28 -070035import android.content.ComponentName;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070036import android.content.Context;
37import android.content.pm.LauncherApps;
Makoto Onukib08790c2016-06-23 14:05:46 -070038import android.content.pm.LauncherApps.Callback;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070039import android.content.pm.ShortcutInfo;
40import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
42import android.os.BaseBundle;
43import android.os.Bundle;
Makoto Onukib08790c2016-06-23 14:05:46 -070044import android.os.Handler;
45import android.os.Looper;
Makoto Onuki7001a612016-05-27 13:24:28 -070046import android.os.Parcel;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070047import android.os.ParcelFileDescriptor;
Makoto Onukib5a012f2016-06-21 11:13:53 -070048import android.os.PersistableBundle;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070049import android.os.UserHandle;
50import android.test.MoreAsserts;
51import android.util.Log;
52
53import junit.framework.Assert;
54
55import org.hamcrest.BaseMatcher;
56import org.hamcrest.Description;
57import org.hamcrest.Matcher;
Makoto Onuki9c850012016-07-26 15:50:50 -070058import org.json.JSONException;
59import org.json.JSONObject;
Makoto Onukib08790c2016-06-23 14:05:46 -070060import org.mockito.ArgumentCaptor;
Makoto Onukid0010c52017-03-30 14:17:35 -070061import org.mockito.ArgumentMatcher;
62import org.mockito.ArgumentMatchers;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070063import org.mockito.Mockito;
Paul Duffin192bb0b2017-03-09 18:49:41 +000064import org.mockito.hamcrest.MockitoHamcrest;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070065
66import java.io.BufferedReader;
Makoto Onuki0b9d1db2016-07-18 14:16:41 -070067import java.io.File;
68import java.io.FileNotFoundException;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070069import java.io.FileReader;
70import java.io.IOException;
71import java.util.ArrayList;
72import java.util.Arrays;
73import java.util.Collection;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070074import java.util.Collections;
75import java.util.Comparator;
76import java.util.LinkedHashSet;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070077import java.util.List;
78import java.util.Set;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070079import java.util.SortedSet;
80import java.util.TreeSet;
Makoto Onukib08790c2016-06-23 14:05:46 -070081import java.util.concurrent.CountDownLatch;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070082import java.util.function.BooleanSupplier;
Makoto Onukidf6da042016-06-16 09:51:40 -070083import java.util.function.Consumer;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070084import java.util.function.Function;
85import java.util.function.Predicate;
86
Makoto Onuki51ab2b32016-06-02 11:03:51 -070087/**
88 * Common utility methods for ShortcutManager tests. This is used by both CTS and the unit tests.
89 * Because it's used by CTS too, it can only access the public APIs.
90 */
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070091public class ShortcutManagerTestUtils {
92 private static final String TAG = "ShortcutManagerUtils";
93
Makoto Onuki9c850012016-07-26 15:50:50 -070094 private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070095
96 private static final int STANDARD_TIMEOUT_SEC = 5;
97
Makoto Onuki9e1f5592016-06-08 12:30:23 -070098 private static final String[] EMPTY_STRINGS = new String[0];
99
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700100 private ShortcutManagerTestUtils() {
101 }
102
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700103 public static List<String> readAll(File file) throws FileNotFoundException {
104 return readAll(ParcelFileDescriptor.open(
105 file.getAbsoluteFile(), ParcelFileDescriptor.MODE_READ_ONLY));
106 }
107
108 public static List<String> readAll(ParcelFileDescriptor pfd) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700109 try {
110 try {
111 final ArrayList<String> ret = new ArrayList<>();
112 try (BufferedReader r = new BufferedReader(
113 new FileReader(pfd.getFileDescriptor()))) {
114 String line;
115 while ((line = r.readLine()) != null) {
116 ret.add(line);
117 }
118 r.readLine();
119 }
120 return ret;
121 } finally {
122 pfd.close();
123 }
124 } catch (IOException e) {
125 throw new RuntimeException(e);
126 }
127 }
128
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700129 public static String concatResult(List<String> result) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700130 final StringBuilder sb = new StringBuilder();
131 for (String s : result) {
132 sb.append(s);
133 sb.append("\n");
134 }
135 return sb.toString();
136 }
137
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700138 public static boolean resultContains(List<String> result, String expected) {
139 for (String line : result) {
140 if (line.contains(expected)) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147 public static List<String> assertSuccess(List<String> result) {
148 if (!resultContains(result, "Success")) {
149 fail("Command failed. Result was:\n" + concatResult(result));
150 }
151 return result;
152 }
153
154 public static List<String> assertContains(List<String> result, String expected) {
155 if (!resultContains(result, expected)) {
156 fail("Didn't contain expected string=" + expected
157 + "\nActual:\n" + concatResult(result));
158 }
159 return result;
160 }
161
Makoto Onuki7051d162016-08-19 14:39:43 -0700162 public static List<String> runCommand(Instrumentation instrumentation, String command) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700163 return runCommand(instrumentation, command, null);
164 }
Makoto Onuki7051d162016-08-19 14:39:43 -0700165 public static List<String> runCommand(Instrumentation instrumentation, String command,
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700166 Predicate<List<String>> resultAsserter) {
167 Log.d(TAG, "Running command: " + command);
168 final List<String> result;
169 try {
170 result = readAll(
171 instrumentation.getUiAutomation().executeShellCommand(command));
172 } catch (Exception e) {
173 throw new RuntimeException(e);
174 }
175 if (resultAsserter != null && !resultAsserter.test(result)) {
176 fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
177 }
178 return result;
179 }
180
Makoto Onuki7051d162016-08-19 14:39:43 -0700181 public static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700182 runCommand(instrumentation, command, result -> result.size() == 0);
183 }
184
Makoto Onuki7051d162016-08-19 14:39:43 -0700185 public static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700186 Predicate<List<String>> resultAsserter) {
187 return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
188 }
189
190 public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
191 String command) {
192 return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
193 }
194
195 public static String getDefaultLauncher(Instrumentation instrumentation) {
196 final String PREFIX = "Launcher: ComponentInfo{";
197 final String POSTFIX = "}";
198 final List<String> result = runShortcutCommandForSuccess(
199 instrumentation, "get-default-launcher");
200 for (String s : result) {
201 if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
202 return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
203 }
204 }
205 fail("Default launcher not found");
206 return null;
207 }
208
209 public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
Makoto Onuki1ae63b02016-08-22 13:15:36 -0700210 runCommand(instrumentation, "cmd package set-home-activity --user "
211 + instrumentation.getContext().getUserId() + " " + component,
Makoto Onuki3bdbf982016-06-23 16:56:35 -0700212 result -> result.contains("Success"));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700213 }
214
215 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
216 setDefaultLauncher(instrumentation, packageContext.getPackageName()
217 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
218 }
219
220 public static void overrideConfig(Instrumentation instrumentation, String config) {
221 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
222 }
223
224 public static void resetConfig(Instrumentation instrumentation) {
225 runShortcutCommandForSuccess(instrumentation, "reset-config");
226 }
227
228 public static void resetThrottling(Instrumentation instrumentation) {
229 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
230 }
231
232 public static void resetAllThrottling(Instrumentation instrumentation) {
233 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
234 }
235
236 public static void clearShortcuts(Instrumentation instrumentation, int userId,
237 String packageName) {
238 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
239 + " --user " + userId + " " + packageName);
240 }
241
Makoto Onuki9c850012016-07-26 15:50:50 -0700242 public static void anyContains(List<String> result, String expected) {
243 for (String l : result) {
244 if (l.contains(expected)) {
245 return;
246 }
247 }
248 fail("Result didn't contain '" + expected + "': was\n" + result);
249 }
250
251 public static void enableComponent(Instrumentation instrumentation, ComponentName cn,
252 boolean enable) {
253
254 final String word = (enable ? "enable" : "disable");
255 runCommand(instrumentation,
256 "pm " + word + " " + cn.flattenToString()
257 , result ->concatResult(result).contains(word));
258 }
259
260 public static void appOps(Instrumentation instrumentation, String packageName,
261 String op, String mode) {
262 runCommand(instrumentation, "appops set " + packageName + " " + op + " " + mode);
263 }
264
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700265 public static void dumpsysShortcut(Instrumentation instrumentation) {
266 if (!ENABLE_DUMPSYS) {
267 return;
268 }
Makoto Onuki3bdbf982016-06-23 16:56:35 -0700269 Log.e(TAG, "Dumpsys shortcut");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700270 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
271 Log.e(TAG, s);
272 }
273 }
274
Makoto Onuki9c850012016-07-26 15:50:50 -0700275 public static JSONObject getCheckinDump(Instrumentation instrumentation) throws JSONException {
276 return new JSONObject(concatResult(runCommand(instrumentation, "dumpsys shortcut -c")));
277 }
278
279 public static boolean isLowRamDevice(Instrumentation instrumentation) throws JSONException {
280 return getCheckinDump(instrumentation).getBoolean("lowRam");
281 }
282
283 public static int getIconSize(Instrumentation instrumentation) throws JSONException {
284 return getCheckinDump(instrumentation).getInt("iconSize");
285 }
286
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700287 public static Bundle makeBundle(Object... keysAndValues) {
288 assertTrue((keysAndValues.length % 2) == 0);
289
290 if (keysAndValues.length == 0) {
291 return null;
292 }
293 final Bundle ret = new Bundle();
294
295 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
296 final String key = keysAndValues[i].toString();
297 final Object value = keysAndValues[i + 1];
298
299 if (value == null) {
300 ret.putString(key, null);
301 } else if (value instanceof Integer) {
302 ret.putInt(key, (Integer) value);
303 } else if (value instanceof String) {
304 ret.putString(key, (String) value);
305 } else if (value instanceof Bundle) {
306 ret.putBundle(key, (Bundle) value);
307 } else {
308 fail("Type not supported yet: " + value.getClass().getName());
309 }
310 }
311 return ret;
312 }
313
Makoto Onukib5a012f2016-06-21 11:13:53 -0700314 public static PersistableBundle makePersistableBundle(Object... keysAndValues) {
315 assertTrue((keysAndValues.length % 2) == 0);
316
317 if (keysAndValues.length == 0) {
318 return null;
319 }
320 final PersistableBundle ret = new PersistableBundle();
321
322 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
323 final String key = keysAndValues[i].toString();
324 final Object value = keysAndValues[i + 1];
325
326 if (value == null) {
327 ret.putString(key, null);
328 } else if (value instanceof Integer) {
329 ret.putInt(key, (Integer) value);
330 } else if (value instanceof String) {
331 ret.putString(key, (String) value);
332 } else if (value instanceof PersistableBundle) {
333 ret.putPersistableBundle(key, (PersistableBundle) value);
334 } else {
335 fail("Type not supported yet: " + value.getClass().getName());
336 }
337 }
338 return ret;
339 }
340
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700341 public static <T> List<T> list(T... array) {
342 return Arrays.asList(array);
343 }
344
345 public static <T> Set<T> hashSet(Set<T> in) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700346 return new LinkedHashSet<>(in);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700347 }
348
349 public static <T> Set<T> set(T... values) {
350 return set(v -> v, values);
351 }
352
353 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
354 return set(converter, Arrays.asList(values));
355 }
356
357 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700358 final LinkedHashSet<T> ret = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700359 for (V v : values) {
360 ret.add(converter.apply(v));
361 }
362 return ret;
363 }
364
365 public static void resetAll(Collection<?> mocks) {
366 for (Object o : mocks) {
367 reset(o);
368 }
369 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700370
Makoto Onukidf6da042016-06-16 09:51:40 -0700371 public static <T extends Collection<?>> T assertEmpty(T collection) {
372 if (collection == null) {
373 return collection; // okay.
374 }
375 assertEquals(0, collection.size());
376 return collection;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700377 }
378
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700379 public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
380 final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
381 ret.removeIf(si -> !p.test(si));
382 return ret;
383 }
384
Makoto Onuki7001a612016-05-27 13:24:28 -0700385 public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
386 ComponentName activity) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700387 return filter(list, si ->
388 (si.getActivity().equals(activity)
Makoto Onukib5a012f2016-06-21 11:13:53 -0700389 && (si.isDeclaredInManifest() || si.isDynamic())));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700390 }
391
392 public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
393 return filter(list, si -> si.getLastChangedTimestamp() >= time);
Makoto Onuki7001a612016-05-27 13:24:28 -0700394 }
395
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700396 @FunctionalInterface
397 public interface ExceptionRunnable {
398 void run() throws Exception;
399 }
400
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700401 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700402 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700403 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
404 }
405
Makoto Onuki22fcc682016-05-17 14:52:19 -0700406 public static void assertCannotUpdateImmutable(Runnable r) {
407 assertExpectException(
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700408 IllegalArgumentException.class, "may not be manipulated via APIs", r::run);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700409 }
410
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700411 public static void assertDynamicShortcutCountExceeded(Runnable r) {
412 assertExpectException(IllegalArgumentException.class,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700413 "Max number of dynamic shortcuts exceeded", r::run);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700414 }
415
416 public static void assertExpectException(String message,
417 Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700418 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700419 try {
420 r.run();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700421 } catch (Throwable e) {
422 Assert.assertTrue(
423 "Expected exception type was " + expectedExceptionType.getName()
424 + " but caught " + e + " (message=" + message + ")",
425 expectedExceptionType.isAssignableFrom(e.getClass()));
426 if (expectedExceptionMessageRegex != null) {
427 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
428 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700429 return; // Pass
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700430 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700431 Assert.fail("Expected exception type " + expectedExceptionType.getName()
432 + " was not thrown");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700433 }
434
435 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
436 String... expectedIds) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700437 final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
438 final SortedSet<String> actual = new TreeSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700439 for (ShortcutInfo s : actualShortcuts) {
440 actual.add(s.getId());
441 }
442
443 // Compare the sets.
444 assertEquals(expected, actual);
445 return actualShortcuts;
446 }
447
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700448 public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
449 String... expectedIds) {
450 final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
451 final ArrayList<String> actual = new ArrayList<>();
452 for (ShortcutInfo s : actualShortcuts) {
453 actual.add(s.getId());
454 }
455 assertEquals(expected, actual);
456 return actualShortcuts;
457 }
458
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700459 public static List<ShortcutInfo> assertAllHaveIntents(
460 List<ShortcutInfo> actualShortcuts) {
461 for (ShortcutInfo s : actualShortcuts) {
462 assertNotNull("ID " + s.getId(), s.getIntent());
463 }
464 return actualShortcuts;
465 }
466
467 public static List<ShortcutInfo> assertAllNotHaveIntents(
468 List<ShortcutInfo> actualShortcuts) {
469 for (ShortcutInfo s : actualShortcuts) {
470 assertNull("ID " + s.getId(), s.getIntent());
471 }
472 return actualShortcuts;
473 }
474
475 public static List<ShortcutInfo> assertAllHaveTitle(
476 List<ShortcutInfo> actualShortcuts) {
477 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700478 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700479 }
480 return actualShortcuts;
481 }
482
483 public static List<ShortcutInfo> assertAllNotHaveTitle(
484 List<ShortcutInfo> actualShortcuts) {
485 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700486 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700487 }
488 return actualShortcuts;
489 }
490
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700491 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
492 List<ShortcutInfo> actualShortcuts) {
493 for (ShortcutInfo s : actualShortcuts) {
494 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
495 }
496 return actualShortcuts;
497 }
498
499 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
500 List<ShortcutInfo> actualShortcuts) {
501 for (ShortcutInfo s : actualShortcuts) {
502 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
503 }
504 return actualShortcuts;
505 }
506
507 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
508 for (ShortcutInfo s : actualShortcuts) {
509 assertTrue("ID " + s.getId(), s.isDynamic());
510 }
511 return actualShortcuts;
512 }
513
Hakan Seyalioglu58fc95d2016-12-13 15:23:22 -0800514 public static List<ShortcutInfo> assertAllChooser(List<ShortcutInfo> actualShortcuts) {
515 for (ShortcutInfo s : actualShortcuts) {
516 assertTrue("ID " + s.getId(), s.isChooser());
517 }
518 return actualShortcuts;
519 }
520
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700521 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
522 for (ShortcutInfo s : actualShortcuts) {
523 assertTrue("ID " + s.getId(), s.isPinned());
524 }
525 return actualShortcuts;
526 }
527
528 public static List<ShortcutInfo> assertAllDynamicOrPinned(
529 List<ShortcutInfo> actualShortcuts) {
530 for (ShortcutInfo s : actualShortcuts) {
531 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
532 }
533 return actualShortcuts;
534 }
535
Makoto Onuki22fcc682016-05-17 14:52:19 -0700536 public static List<ShortcutInfo> assertAllManifest(
537 List<ShortcutInfo> actualShortcuts) {
538 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700539 assertTrue("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700540 }
541 return actualShortcuts;
542 }
543
544 public static List<ShortcutInfo> assertAllNotManifest(
545 List<ShortcutInfo> actualShortcuts) {
546 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700547 assertFalse("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700548 }
549 return actualShortcuts;
550 }
551
552 public static List<ShortcutInfo> assertAllDisabled(
553 List<ShortcutInfo> actualShortcuts) {
554 for (ShortcutInfo s : actualShortcuts) {
555 assertTrue("ID " + s.getId(), !s.isEnabled());
556 }
557 return actualShortcuts;
558 }
559
560 public static List<ShortcutInfo> assertAllEnabled(
561 List<ShortcutInfo> actualShortcuts) {
562 for (ShortcutInfo s : actualShortcuts) {
563 assertTrue("ID " + s.getId(), s.isEnabled());
564 }
565 return actualShortcuts;
566 }
567
568 public static List<ShortcutInfo> assertAllImmutable(
569 List<ShortcutInfo> actualShortcuts) {
570 for (ShortcutInfo s : actualShortcuts) {
571 assertTrue("ID " + s.getId(), s.isImmutable());
572 }
573 return actualShortcuts;
574 }
575
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700576 public static void assertDynamicOnly(ShortcutInfo si) {
577 assertTrue(si.isDynamic());
578 assertFalse(si.isPinned());
579 }
580
581 public static void assertPinnedOnly(ShortcutInfo si) {
582 assertFalse(si.isDynamic());
Makoto Onukib5a012f2016-06-21 11:13:53 -0700583 assertFalse(si.isDeclaredInManifest());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700584 assertTrue(si.isPinned());
585 }
586
587 public static void assertDynamicAndPinned(ShortcutInfo si) {
588 assertTrue(si.isDynamic());
589 assertTrue(si.isPinned());
590 }
591
592 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
593 assertEquals("width", expectedWidth, bitmap.getWidth());
594 assertEquals("height", expectedHeight, bitmap.getHeight());
595 }
596
597 public static <T> void assertAllUnique(Collection<T> list) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700598 final Set<Object> set = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700599 for (T item : list) {
600 if (set.contains(item)) {
601 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
602 }
603 set.add(item);
604 }
605 }
606
Makoto Onuki39686e82016-04-13 18:03:00 -0700607 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
608 for (ShortcutInfo si : list) {
609 if (si.getId().equals(id)) {
610 return si;
611 }
612 }
613 fail("Shortcut " + id + " not found in the list");
614 return null;
615 }
616
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700617 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
618 assertNotNull(pfd);
619 try {
620 try {
621 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
622 } finally {
623 pfd.close();
624 }
625 } catch (IOException e) {
626 throw new RuntimeException(e);
627 }
628 }
629
630 public static void assertBundleEmpty(BaseBundle b) {
631 assertTrue(b == null || b.size() == 0);
632 }
633
634 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
635 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
636 any(UserHandle.class));
637 }
638
639 public static void assertCallbackReceived(LauncherApps.Callback mock,
640 UserHandle user, String packageName, String... ids) {
641 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
642 eq(user));
643 }
644
645 public static boolean checkAssertSuccess(Runnable r) {
646 try {
647 r.run();
648 return true;
649 } catch (AssertionError e) {
650 return false;
651 }
652 }
653
654 public static <T> T checkArgument(Predicate<T> checker, String description,
655 List<T> matchedCaptor) {
656 final Matcher<T> m = new BaseMatcher<T>() {
657 @Override
658 public boolean matches(Object item) {
659 if (item == null) {
660 return false;
661 }
662 final T value = (T) item;
663 if (!checker.test(value)) {
664 return false;
665 }
666
667 if (matchedCaptor != null) {
668 matchedCaptor.add(value);
669 }
670 return true;
671 }
672
673 @Override
674 public void describeTo(Description d) {
675 d.appendText(description);
676 }
677 };
Paul Duffin192bb0b2017-03-09 18:49:41 +0000678 return MockitoHamcrest.argThat(m);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700679 }
680
681 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
682 return checkArgument((List<ShortcutInfo> list) -> {
683 final Set<String> actualSet = set(si -> si.getId(), list);
684 return actualSet.equals(set(ids));
685
686 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
687 }
688
Makoto Onuki7001a612016-05-27 13:24:28 -0700689 public static ShortcutInfo parceled(ShortcutInfo si) {
690 Parcel p = Parcel.obtain();
691 p.writeParcelable(si, 0);
692 p.setDataPosition(0);
693 ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader());
694 p.recycle();
695 return si2;
696 }
697
698 public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) {
699 if (list == null) {
700 return null;
701 }
702 final List<ShortcutInfo> ret = new ArrayList<>(list.size());
703 for (ShortcutInfo si : list) {
704 ret.add(parceled(si));
705 }
706
707 return ret;
708 }
709
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700710 private static final Comparator<ShortcutInfo> sRankComparator =
711 (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank());
712
713 public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) {
714 final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts);
715 Collections.sort(ret, sRankComparator);
716 return ret;
717 }
718
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700719 public static void waitUntil(String message, BooleanSupplier condition) {
720 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
721 }
722
723 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
724 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
725 while (System.currentTimeMillis() < timeout) {
726 if (condition.getAsBoolean()) {
727 return;
728 }
729 try {
730 Thread.sleep(100);
731 } catch (InterruptedException e) {
732 throw new RuntimeException(e);
733 }
734 }
735 fail("Timed out for: " + message);
736 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700737
Makoto Onukid0010c52017-03-30 14:17:35 -0700738 public static final <T> T anyOrNull(Class<T> clazz) {
739 return ArgumentMatchers.argThat(value -> true);
740 }
741
742 public static final String anyStringOrNull() {
743 return ArgumentMatchers.argThat(value -> true);
744 }
745
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700746 public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) {
747 return new ShortcutListAsserter(list);
748 }
749
Makoto Onuki2d895c32016-12-02 15:48:40 -0800750 public static ShortcutListAsserter assertWith(ShortcutInfo... list) {
751 return assertWith(list(list));
752 }
753
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700754 /**
755 * New style assertion that allows chained calls.
756 */
757 public static class ShortcutListAsserter {
Makoto Onukidf6da042016-06-16 09:51:40 -0700758 private final ShortcutListAsserter mOriginal;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700759 private final List<ShortcutInfo> mList;
760
761 ShortcutListAsserter(List<ShortcutInfo> list) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700762 this(null, list);
763 }
764
765 private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700766 mOriginal = (original == null) ? this : original;
767 mList = (list == null) ? new ArrayList<>(0) : new ArrayList<>(list);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700768 }
769
Makoto Onukidf6da042016-06-16 09:51:40 -0700770 public ShortcutListAsserter revertToOriginalList() {
771 return mOriginal;
772 }
773
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700774 public ShortcutListAsserter selectDynamic() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700775 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700776 filter(mList, ShortcutInfo::isDynamic));
777 }
778
779 public ShortcutListAsserter selectManifest() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700780 return new ShortcutListAsserter(this,
Makoto Onukib5a012f2016-06-21 11:13:53 -0700781 filter(mList, ShortcutInfo::isDeclaredInManifest));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700782 }
783
784 public ShortcutListAsserter selectPinned() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700785 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700786 filter(mList, ShortcutInfo::isPinned));
787 }
788
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800789 public ShortcutListAsserter selectFloating() {
790 return new ShortcutListAsserter(this,
791 filter(mList, (si -> si.isPinned()
792 && !(si.isDynamic() || si.isDeclaredInManifest()))));
793 }
794
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700795 public ShortcutListAsserter selectByActivity(ComponentName activity) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700796 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700797 ShortcutManagerTestUtils.filterByActivity(mList, activity));
798 }
799
800 public ShortcutListAsserter selectByChangedSince(long time) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700801 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700802 ShortcutManagerTestUtils.changedSince(mList, time));
803 }
804
Makoto Onukidf6da042016-06-16 09:51:40 -0700805 public ShortcutListAsserter selectByIds(String... ids) {
806 final Set<String> idSet = set(ids);
807 final ArrayList<ShortcutInfo> selected = new ArrayList<>();
808 for (ShortcutInfo si : mList) {
809 if (idSet.contains(si.getId())) {
810 selected.add(si);
811 idSet.remove(si.getId());
812 }
813 }
814 if (idSet.size() > 0) {
815 fail("Shortcuts not found for IDs=" + idSet);
816 }
817
818 return new ShortcutListAsserter(this, selected);
819 }
820
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700821 public ShortcutListAsserter toSortByRank() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700822 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700823 ShortcutManagerTestUtils.sortedByRank(mList));
824 }
825
Makoto Onukidf6da042016-06-16 09:51:40 -0700826 public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) {
827 c.accept(mList);
828 return this;
829 }
830
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700831 public ShortcutListAsserter haveIds(String... expectedIds) {
832 assertShortcutIds(mList, expectedIds);
833 return this;
834 }
835
836 public ShortcutListAsserter haveIdsOrdered(String... expectedIds) {
837 assertShortcutIdsOrdered(mList, expectedIds);
838 return this;
839 }
840
841 private ShortcutListAsserter haveSequentialRanks() {
842 for (int i = 0; i < mList.size(); i++) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700843 final ShortcutInfo si = mList.get(i);
844 assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank());
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700845 }
846 return this;
847 }
848
849 public ShortcutListAsserter haveRanksInOrder(String... expectedIds) {
850 toSortByRank()
851 .haveSequentialRanks()
852 .haveIdsOrdered(expectedIds);
853 return this;
854 }
855
856 public ShortcutListAsserter isEmpty() {
857 assertEquals(0, mList.size());
858 return this;
859 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700860
861 public ShortcutListAsserter areAllDynamic() {
862 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic()));
863 return this;
864 }
865
866 public ShortcutListAsserter areAllNotDynamic() {
867 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic()));
868 return this;
869 }
870
871 public ShortcutListAsserter areAllPinned() {
872 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned()));
873 return this;
874 }
875
876 public ShortcutListAsserter areAllNotPinned() {
877 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned()));
878 return this;
879 }
880
881 public ShortcutListAsserter areAllManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700882 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700883 return this;
884 }
885
886 public ShortcutListAsserter areAllNotManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700887 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700888 return this;
889 }
890
891 public ShortcutListAsserter areAllImmutable() {
892 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable()));
893 return this;
894 }
895
896 public ShortcutListAsserter areAllMutable() {
897 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable()));
898 return this;
899 }
900
901 public ShortcutListAsserter areAllEnabled() {
902 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled()));
903 return this;
904 }
905
906 public ShortcutListAsserter areAllDisabled() {
907 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
908 return this;
909 }
910
Makoto Onuki2d895c32016-12-02 15:48:40 -0800911 public ShortcutListAsserter areAllFloating() {
912 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
913 s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
914 return this;
915 }
916
917 public ShortcutListAsserter areAllNotFloating() {
918 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
919 !(s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic())));
920 return this;
921 }
922
923 public ShortcutListAsserter areAllOrphan() {
924 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
925 !s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
926 return this;
927 }
928
929 public ShortcutListAsserter areAllNotOrphan() {
930 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
931 s.isPinned() || s.isDeclaredInManifest() || s.isDynamic()));
932 return this;
933 }
934
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700935 public ShortcutListAsserter areAllWithKeyFieldsOnly() {
936 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly()));
937 return this;
938 }
939
940 public ShortcutListAsserter areAllNotWithKeyFieldsOnly() {
941 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly()));
942 return this;
943 }
944
Makoto Onukib08790c2016-06-23 14:05:46 -0700945 public ShortcutListAsserter areAllWithActivity(ComponentName activity) {
Makoto Onuki255461f2017-01-10 11:47:25 -0800946 forAllShortcuts(s -> assertEquals("id=" + s.getId(), activity, s.getActivity()));
Makoto Onukib08790c2016-06-23 14:05:46 -0700947 return this;
948 }
949
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800950 public ShortcutListAsserter areAllWithNoActivity() {
951 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getActivity()));
952 return this;
953 }
954
Makoto Onukia01f4f02016-12-15 15:58:41 -0800955 public ShortcutListAsserter areAllWithIntent() {
956 forAllShortcuts(s -> assertNotNull("id=" + s.getId(), s.getIntent()));
957 return this;
958 }
959
960 public ShortcutListAsserter areAllWithNoIntent() {
961 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getIntent()));
962 return this;
963 }
964
Makoto Onukidf6da042016-06-16 09:51:40 -0700965 public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700966 boolean found = false;
Makoto Onukidf6da042016-06-16 09:51:40 -0700967 for (int i = 0; i < mList.size(); i++) {
968 final ShortcutInfo si = mList.get(i);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700969 found = true;
Makoto Onukidf6da042016-06-16 09:51:40 -0700970 sa.accept(si);
971 }
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700972 assertTrue("No shortcuts found.", found);
Makoto Onukidf6da042016-06-16 09:51:40 -0700973 return this;
974 }
975
976 public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
977 Consumer<ShortcutInfo> sa) {
978 boolean found = false;
979 for (int i = 0; i < mList.size(); i++) {
980 final ShortcutInfo si = mList.get(i);
981 if (p.test(si)) {
982 found = true;
983 try {
984 sa.accept(si);
985 } catch (Throwable e) {
986 throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
987 }
988 }
989 }
990 assertTrue("Shortcut with the given condition not found.", found);
991 return this;
992 }
993
994 public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
995 forShortcut(si -> si.getId().equals(id), sa);
996
997 return this;
998 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700999 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07001000
1001 public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) {
1002 if (b1 == null && b2 == null) {
1003 return; // pass
1004 }
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001005 assertNotNull("b1 is null but b2 is not", b1);
1006 assertNotNull("b2 is null but b1 is not", b2);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001007
1008 // HashSet makes the error message readable.
1009 assertEquals(set(b1.keySet()), set(b2.keySet()));
1010
1011 for (String key : b1.keySet()) {
1012 final Object v1 = b1.get(key);
1013 final Object v2 = b2.get(key);
1014 if (v1 == null) {
1015 if (v2 == null) {
1016 return;
1017 }
1018 }
1019 if (v1.equals(v2)) {
1020 return;
1021 }
1022
1023 assertTrue("Only either value is null: key=" + key
1024 + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null);
1025 assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass());
1026
1027 if (v1 instanceof BaseBundle) {
1028 assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2);
1029
1030 } else if (v1 instanceof boolean[]) {
1031 assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2));
1032
1033 } else if (v1 instanceof int[]) {
1034 MoreAsserts.assertEquals((int[]) v1, (int[]) v2);
1035
1036 } else if (v1 instanceof double[]) {
1037 MoreAsserts.assertEquals((double[]) v1, (double[]) v2);
1038
1039 } else if (v1 instanceof String[]) {
1040 MoreAsserts.assertEquals((String[]) v1, (String[]) v2);
1041
1042 } else if (v1 instanceof Double) {
1043 if (((Double) v1).isNaN()) {
1044 assertTrue(((Double) v2).isNaN());
1045 } else {
1046 assertEquals(v1, v2);
1047 }
1048
1049 } else {
1050 assertEquals(v1, v2);
1051 }
1052 }
1053 }
Makoto Onukib08790c2016-06-23 14:05:46 -07001054
1055 public static void waitOnMainThread() throws InterruptedException {
1056 final CountDownLatch latch = new CountDownLatch(1);
1057
1058 new Handler(Looper.getMainLooper()).post(() -> latch.countDown());
1059
1060 latch.await();
1061 }
1062
1063 public static class LauncherCallbackAsserter {
1064 private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class);
1065
1066 private Callback getMockCallback() {
1067 return mCallback;
1068 }
1069
1070 public LauncherCallbackAsserter assertNoCallbackCalled() {
1071 verify(mCallback, times(0)).onShortcutsChanged(
1072 anyString(),
1073 any(List.class),
1074 any(UserHandle.class));
1075 return this;
1076 }
1077
1078 public LauncherCallbackAsserter assertNoCallbackCalledForPackage(
1079 String publisherPackageName) {
1080 verify(mCallback, times(0)).onShortcutsChanged(
1081 eq(publisherPackageName),
1082 any(List.class),
1083 any(UserHandle.class));
1084 return this;
1085 }
1086
1087 public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser(
1088 String publisherPackageName, UserHandle publisherUserHandle) {
1089 verify(mCallback, times(0)).onShortcutsChanged(
1090 eq(publisherPackageName),
1091 any(List.class),
1092 eq(publisherUserHandle));
1093 return this;
1094 }
1095
1096 public ShortcutListAsserter assertCallbackCalledForPackageAndUser(
1097 String publisherPackageName, UserHandle publisherUserHandle) {
1098 final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class);
1099 verify(mCallback, times(1)).onShortcutsChanged(
1100 eq(publisherPackageName),
1101 shortcuts.capture(),
1102 eq(publisherUserHandle));
1103 return new ShortcutListAsserter(shortcuts.getValue());
1104 }
1105 }
1106
1107 public static LauncherCallbackAsserter assertForLauncherCallback(
1108 LauncherApps launcherApps, Runnable body) throws InterruptedException {
1109 final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter();
1110 launcherApps.registerCallback(asserter.getMockCallback(),
1111 new Handler(Looper.getMainLooper()));
1112
1113 body.run();
1114
1115 waitOnMainThread();
1116
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001117 // TODO unregister doesn't work well during unit tests. Figure out and fix it.
1118 // launcherApps.unregisterCallback(asserter.getMockCallback());
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001119
Makoto Onukib08790c2016-06-23 14:05:46 -07001120 return asserter;
1121 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001122
Makoto Onukia01f4f02016-12-15 15:58:41 -08001123 public static LauncherCallbackAsserter assertForLauncherCallbackNoThrow(
1124 LauncherApps launcherApps, Runnable body) {
1125 try {
1126 return assertForLauncherCallback(launcherApps, body);
1127 } catch (InterruptedException e) {
1128 fail("Caught InterruptedException");
1129 return null; // Never happens.
1130 }
1131 }
1132
Makoto Onuki9c850012016-07-26 15:50:50 -07001133 public static void retryUntil(BooleanSupplier checker, String message) {
Makoto Onukid67bf6a2016-08-19 09:10:08 -07001134 retryUntil(checker, message, 30);
1135 }
1136
1137 public static void retryUntil(BooleanSupplier checker, String message, long timeoutSeconds) {
1138 final long timeOut = System.currentTimeMillis() + timeoutSeconds * 1000;
Makoto Onuki9c850012016-07-26 15:50:50 -07001139 while (!checker.getAsBoolean()) {
Makoto Onukia210ccf2016-07-27 14:08:48 -07001140 if (System.currentTimeMillis() > timeOut) {
1141 break;
1142 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001143 try {
1144 Thread.sleep(200);
1145 } catch (InterruptedException ignore) {
1146 }
1147 }
1148 assertTrue(message, checker.getAsBoolean());
1149 }
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001150}