blob: a664f21bc0dc18591de6bdebaa8431f6602dcd32 [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 Onuki5ba0d3e2016-04-11 14:03:46 -070061import org.mockito.Mockito;
62
63import java.io.BufferedReader;
Makoto Onuki0b9d1db2016-07-18 14:16:41 -070064import java.io.File;
65import java.io.FileNotFoundException;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070066import java.io.FileReader;
67import java.io.IOException;
68import java.util.ArrayList;
69import java.util.Arrays;
70import java.util.Collection;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070071import java.util.Collections;
72import java.util.Comparator;
73import java.util.LinkedHashSet;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070074import java.util.List;
75import java.util.Set;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070076import java.util.SortedSet;
77import java.util.TreeSet;
Makoto Onukib08790c2016-06-23 14:05:46 -070078import java.util.concurrent.CountDownLatch;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070079import java.util.function.BooleanSupplier;
Makoto Onukidf6da042016-06-16 09:51:40 -070080import java.util.function.Consumer;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070081import java.util.function.Function;
82import java.util.function.Predicate;
83
Makoto Onuki51ab2b32016-06-02 11:03:51 -070084/**
85 * Common utility methods for ShortcutManager tests. This is used by both CTS and the unit tests.
86 * Because it's used by CTS too, it can only access the public APIs.
87 */
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070088public class ShortcutManagerTestUtils {
89 private static final String TAG = "ShortcutManagerUtils";
90
Makoto Onuki9c850012016-07-26 15:50:50 -070091 private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070092
93 private static final int STANDARD_TIMEOUT_SEC = 5;
94
Makoto Onuki9e1f5592016-06-08 12:30:23 -070095 private static final String[] EMPTY_STRINGS = new String[0];
96
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070097 private ShortcutManagerTestUtils() {
98 }
99
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700100 public static List<String> readAll(File file) throws FileNotFoundException {
101 return readAll(ParcelFileDescriptor.open(
102 file.getAbsoluteFile(), ParcelFileDescriptor.MODE_READ_ONLY));
103 }
104
105 public static List<String> readAll(ParcelFileDescriptor pfd) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700106 try {
107 try {
108 final ArrayList<String> ret = new ArrayList<>();
109 try (BufferedReader r = new BufferedReader(
110 new FileReader(pfd.getFileDescriptor()))) {
111 String line;
112 while ((line = r.readLine()) != null) {
113 ret.add(line);
114 }
115 r.readLine();
116 }
117 return ret;
118 } finally {
119 pfd.close();
120 }
121 } catch (IOException e) {
122 throw new RuntimeException(e);
123 }
124 }
125
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700126 public static String concatResult(List<String> result) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700127 final StringBuilder sb = new StringBuilder();
128 for (String s : result) {
129 sb.append(s);
130 sb.append("\n");
131 }
132 return sb.toString();
133 }
134
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700135 public static boolean resultContains(List<String> result, String expected) {
136 for (String line : result) {
137 if (line.contains(expected)) {
138 return true;
139 }
140 }
141 return false;
142 }
143
144 public static List<String> assertSuccess(List<String> result) {
145 if (!resultContains(result, "Success")) {
146 fail("Command failed. Result was:\n" + concatResult(result));
147 }
148 return result;
149 }
150
151 public static List<String> assertContains(List<String> result, String expected) {
152 if (!resultContains(result, expected)) {
153 fail("Didn't contain expected string=" + expected
154 + "\nActual:\n" + concatResult(result));
155 }
156 return result;
157 }
158
Makoto Onuki7051d162016-08-19 14:39:43 -0700159 public static List<String> runCommand(Instrumentation instrumentation, String command) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700160 return runCommand(instrumentation, command, null);
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 Predicate<List<String>> resultAsserter) {
164 Log.d(TAG, "Running command: " + command);
165 final List<String> result;
166 try {
167 result = readAll(
168 instrumentation.getUiAutomation().executeShellCommand(command));
169 } catch (Exception e) {
170 throw new RuntimeException(e);
171 }
172 if (resultAsserter != null && !resultAsserter.test(result)) {
173 fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
174 }
175 return result;
176 }
177
Makoto Onuki7051d162016-08-19 14:39:43 -0700178 public static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700179 runCommand(instrumentation, command, result -> result.size() == 0);
180 }
181
Makoto Onuki7051d162016-08-19 14:39:43 -0700182 public static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700183 Predicate<List<String>> resultAsserter) {
184 return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
185 }
186
187 public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
188 String command) {
189 return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
190 }
191
192 public static String getDefaultLauncher(Instrumentation instrumentation) {
193 final String PREFIX = "Launcher: ComponentInfo{";
194 final String POSTFIX = "}";
195 final List<String> result = runShortcutCommandForSuccess(
196 instrumentation, "get-default-launcher");
197 for (String s : result) {
198 if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
199 return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
200 }
201 }
202 fail("Default launcher not found");
203 return null;
204 }
205
206 public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
Makoto Onuki1ae63b02016-08-22 13:15:36 -0700207 runCommand(instrumentation, "cmd package set-home-activity --user "
208 + instrumentation.getContext().getUserId() + " " + component,
Makoto Onuki3bdbf982016-06-23 16:56:35 -0700209 result -> result.contains("Success"));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700210 }
211
212 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
213 setDefaultLauncher(instrumentation, packageContext.getPackageName()
214 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
215 }
216
217 public static void overrideConfig(Instrumentation instrumentation, String config) {
218 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
219 }
220
221 public static void resetConfig(Instrumentation instrumentation) {
222 runShortcutCommandForSuccess(instrumentation, "reset-config");
223 }
224
225 public static void resetThrottling(Instrumentation instrumentation) {
226 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
227 }
228
229 public static void resetAllThrottling(Instrumentation instrumentation) {
230 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
231 }
232
233 public static void clearShortcuts(Instrumentation instrumentation, int userId,
234 String packageName) {
235 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
236 + " --user " + userId + " " + packageName);
237 }
238
Makoto Onuki9c850012016-07-26 15:50:50 -0700239 public static void anyContains(List<String> result, String expected) {
240 for (String l : result) {
241 if (l.contains(expected)) {
242 return;
243 }
244 }
245 fail("Result didn't contain '" + expected + "': was\n" + result);
246 }
247
248 public static void enableComponent(Instrumentation instrumentation, ComponentName cn,
249 boolean enable) {
250
251 final String word = (enable ? "enable" : "disable");
252 runCommand(instrumentation,
253 "pm " + word + " " + cn.flattenToString()
254 , result ->concatResult(result).contains(word));
255 }
256
257 public static void appOps(Instrumentation instrumentation, String packageName,
258 String op, String mode) {
259 runCommand(instrumentation, "appops set " + packageName + " " + op + " " + mode);
260 }
261
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700262 public static void dumpsysShortcut(Instrumentation instrumentation) {
263 if (!ENABLE_DUMPSYS) {
264 return;
265 }
Makoto Onuki3bdbf982016-06-23 16:56:35 -0700266 Log.e(TAG, "Dumpsys shortcut");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700267 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
268 Log.e(TAG, s);
269 }
270 }
271
Makoto Onuki9c850012016-07-26 15:50:50 -0700272 public static JSONObject getCheckinDump(Instrumentation instrumentation) throws JSONException {
273 return new JSONObject(concatResult(runCommand(instrumentation, "dumpsys shortcut -c")));
274 }
275
276 public static boolean isLowRamDevice(Instrumentation instrumentation) throws JSONException {
277 return getCheckinDump(instrumentation).getBoolean("lowRam");
278 }
279
280 public static int getIconSize(Instrumentation instrumentation) throws JSONException {
281 return getCheckinDump(instrumentation).getInt("iconSize");
282 }
283
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700284 public static Bundle makeBundle(Object... keysAndValues) {
285 assertTrue((keysAndValues.length % 2) == 0);
286
287 if (keysAndValues.length == 0) {
288 return null;
289 }
290 final Bundle ret = new Bundle();
291
292 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
293 final String key = keysAndValues[i].toString();
294 final Object value = keysAndValues[i + 1];
295
296 if (value == null) {
297 ret.putString(key, null);
298 } else if (value instanceof Integer) {
299 ret.putInt(key, (Integer) value);
300 } else if (value instanceof String) {
301 ret.putString(key, (String) value);
302 } else if (value instanceof Bundle) {
303 ret.putBundle(key, (Bundle) value);
304 } else {
305 fail("Type not supported yet: " + value.getClass().getName());
306 }
307 }
308 return ret;
309 }
310
Makoto Onukib5a012f2016-06-21 11:13:53 -0700311 public static PersistableBundle makePersistableBundle(Object... keysAndValues) {
312 assertTrue((keysAndValues.length % 2) == 0);
313
314 if (keysAndValues.length == 0) {
315 return null;
316 }
317 final PersistableBundle ret = new PersistableBundle();
318
319 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
320 final String key = keysAndValues[i].toString();
321 final Object value = keysAndValues[i + 1];
322
323 if (value == null) {
324 ret.putString(key, null);
325 } else if (value instanceof Integer) {
326 ret.putInt(key, (Integer) value);
327 } else if (value instanceof String) {
328 ret.putString(key, (String) value);
329 } else if (value instanceof PersistableBundle) {
330 ret.putPersistableBundle(key, (PersistableBundle) value);
331 } else {
332 fail("Type not supported yet: " + value.getClass().getName());
333 }
334 }
335 return ret;
336 }
337
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700338 public static <T> List<T> list(T... array) {
339 return Arrays.asList(array);
340 }
341
342 public static <T> Set<T> hashSet(Set<T> in) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700343 return new LinkedHashSet<>(in);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700344 }
345
346 public static <T> Set<T> set(T... values) {
347 return set(v -> v, values);
348 }
349
350 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
351 return set(converter, Arrays.asList(values));
352 }
353
354 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700355 final LinkedHashSet<T> ret = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700356 for (V v : values) {
357 ret.add(converter.apply(v));
358 }
359 return ret;
360 }
361
362 public static void resetAll(Collection<?> mocks) {
363 for (Object o : mocks) {
364 reset(o);
365 }
366 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700367
Makoto Onukidf6da042016-06-16 09:51:40 -0700368 public static <T extends Collection<?>> T assertEmpty(T collection) {
369 if (collection == null) {
370 return collection; // okay.
371 }
372 assertEquals(0, collection.size());
373 return collection;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700374 }
375
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700376 public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
377 final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
378 ret.removeIf(si -> !p.test(si));
379 return ret;
380 }
381
Makoto Onuki7001a612016-05-27 13:24:28 -0700382 public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
383 ComponentName activity) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700384 return filter(list, si ->
385 (si.getActivity().equals(activity)
Makoto Onukib5a012f2016-06-21 11:13:53 -0700386 && (si.isDeclaredInManifest() || si.isDynamic())));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700387 }
388
389 public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
390 return filter(list, si -> si.getLastChangedTimestamp() >= time);
Makoto Onuki7001a612016-05-27 13:24:28 -0700391 }
392
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700393 @FunctionalInterface
394 public interface ExceptionRunnable {
395 void run() throws Exception;
396 }
397
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700398 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700399 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700400 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
401 }
402
Makoto Onuki22fcc682016-05-17 14:52:19 -0700403 public static void assertCannotUpdateImmutable(Runnable r) {
404 assertExpectException(
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700405 IllegalArgumentException.class, "may not be manipulated via APIs", r::run);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700406 }
407
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700408 public static void assertDynamicShortcutCountExceeded(Runnable r) {
409 assertExpectException(IllegalArgumentException.class,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700410 "Max number of dynamic shortcuts exceeded", r::run);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700411 }
412
413 public static void assertExpectException(String message,
414 Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700415 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700416 try {
417 r.run();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700418 } catch (Throwable e) {
419 Assert.assertTrue(
420 "Expected exception type was " + expectedExceptionType.getName()
421 + " but caught " + e + " (message=" + message + ")",
422 expectedExceptionType.isAssignableFrom(e.getClass()));
423 if (expectedExceptionMessageRegex != null) {
424 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
425 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700426 return; // Pass
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700427 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700428 Assert.fail("Expected exception type " + expectedExceptionType.getName()
429 + " was not thrown");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700430 }
431
432 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
433 String... expectedIds) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700434 final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
435 final SortedSet<String> actual = new TreeSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700436 for (ShortcutInfo s : actualShortcuts) {
437 actual.add(s.getId());
438 }
439
440 // Compare the sets.
441 assertEquals(expected, actual);
442 return actualShortcuts;
443 }
444
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700445 public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
446 String... expectedIds) {
447 final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
448 final ArrayList<String> actual = new ArrayList<>();
449 for (ShortcutInfo s : actualShortcuts) {
450 actual.add(s.getId());
451 }
452 assertEquals(expected, actual);
453 return actualShortcuts;
454 }
455
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700456 public static List<ShortcutInfo> assertAllHaveIntents(
457 List<ShortcutInfo> actualShortcuts) {
458 for (ShortcutInfo s : actualShortcuts) {
459 assertNotNull("ID " + s.getId(), s.getIntent());
460 }
461 return actualShortcuts;
462 }
463
464 public static List<ShortcutInfo> assertAllNotHaveIntents(
465 List<ShortcutInfo> actualShortcuts) {
466 for (ShortcutInfo s : actualShortcuts) {
467 assertNull("ID " + s.getId(), s.getIntent());
468 }
469 return actualShortcuts;
470 }
471
472 public static List<ShortcutInfo> assertAllHaveTitle(
473 List<ShortcutInfo> actualShortcuts) {
474 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700475 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700476 }
477 return actualShortcuts;
478 }
479
480 public static List<ShortcutInfo> assertAllNotHaveTitle(
481 List<ShortcutInfo> actualShortcuts) {
482 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700483 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700484 }
485 return actualShortcuts;
486 }
487
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700488 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
489 List<ShortcutInfo> actualShortcuts) {
490 for (ShortcutInfo s : actualShortcuts) {
491 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
492 }
493 return actualShortcuts;
494 }
495
496 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
497 List<ShortcutInfo> actualShortcuts) {
498 for (ShortcutInfo s : actualShortcuts) {
499 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
500 }
501 return actualShortcuts;
502 }
503
504 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
505 for (ShortcutInfo s : actualShortcuts) {
506 assertTrue("ID " + s.getId(), s.isDynamic());
507 }
508 return actualShortcuts;
509 }
510
511 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
512 for (ShortcutInfo s : actualShortcuts) {
513 assertTrue("ID " + s.getId(), s.isPinned());
514 }
515 return actualShortcuts;
516 }
517
518 public static List<ShortcutInfo> assertAllDynamicOrPinned(
519 List<ShortcutInfo> actualShortcuts) {
520 for (ShortcutInfo s : actualShortcuts) {
521 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
522 }
523 return actualShortcuts;
524 }
525
Makoto Onuki22fcc682016-05-17 14:52:19 -0700526 public static List<ShortcutInfo> assertAllManifest(
527 List<ShortcutInfo> actualShortcuts) {
528 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700529 assertTrue("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700530 }
531 return actualShortcuts;
532 }
533
534 public static List<ShortcutInfo> assertAllNotManifest(
535 List<ShortcutInfo> actualShortcuts) {
536 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700537 assertFalse("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700538 }
539 return actualShortcuts;
540 }
541
542 public static List<ShortcutInfo> assertAllDisabled(
543 List<ShortcutInfo> actualShortcuts) {
544 for (ShortcutInfo s : actualShortcuts) {
545 assertTrue("ID " + s.getId(), !s.isEnabled());
546 }
547 return actualShortcuts;
548 }
549
550 public static List<ShortcutInfo> assertAllEnabled(
551 List<ShortcutInfo> actualShortcuts) {
552 for (ShortcutInfo s : actualShortcuts) {
553 assertTrue("ID " + s.getId(), s.isEnabled());
554 }
555 return actualShortcuts;
556 }
557
558 public static List<ShortcutInfo> assertAllImmutable(
559 List<ShortcutInfo> actualShortcuts) {
560 for (ShortcutInfo s : actualShortcuts) {
561 assertTrue("ID " + s.getId(), s.isImmutable());
562 }
563 return actualShortcuts;
564 }
565
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700566 public static void assertDynamicOnly(ShortcutInfo si) {
567 assertTrue(si.isDynamic());
568 assertFalse(si.isPinned());
569 }
570
571 public static void assertPinnedOnly(ShortcutInfo si) {
572 assertFalse(si.isDynamic());
Makoto Onukib5a012f2016-06-21 11:13:53 -0700573 assertFalse(si.isDeclaredInManifest());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700574 assertTrue(si.isPinned());
575 }
576
577 public static void assertDynamicAndPinned(ShortcutInfo si) {
578 assertTrue(si.isDynamic());
579 assertTrue(si.isPinned());
580 }
581
582 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
583 assertEquals("width", expectedWidth, bitmap.getWidth());
584 assertEquals("height", expectedHeight, bitmap.getHeight());
585 }
586
587 public static <T> void assertAllUnique(Collection<T> list) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700588 final Set<Object> set = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700589 for (T item : list) {
590 if (set.contains(item)) {
591 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
592 }
593 set.add(item);
594 }
595 }
596
Makoto Onuki39686e82016-04-13 18:03:00 -0700597 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
598 for (ShortcutInfo si : list) {
599 if (si.getId().equals(id)) {
600 return si;
601 }
602 }
603 fail("Shortcut " + id + " not found in the list");
604 return null;
605 }
606
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700607 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
608 assertNotNull(pfd);
609 try {
610 try {
611 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
612 } finally {
613 pfd.close();
614 }
615 } catch (IOException e) {
616 throw new RuntimeException(e);
617 }
618 }
619
620 public static void assertBundleEmpty(BaseBundle b) {
621 assertTrue(b == null || b.size() == 0);
622 }
623
624 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
625 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
626 any(UserHandle.class));
627 }
628
629 public static void assertCallbackReceived(LauncherApps.Callback mock,
630 UserHandle user, String packageName, String... ids) {
631 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
632 eq(user));
633 }
634
635 public static boolean checkAssertSuccess(Runnable r) {
636 try {
637 r.run();
638 return true;
639 } catch (AssertionError e) {
640 return false;
641 }
642 }
643
644 public static <T> T checkArgument(Predicate<T> checker, String description,
645 List<T> matchedCaptor) {
646 final Matcher<T> m = new BaseMatcher<T>() {
647 @Override
648 public boolean matches(Object item) {
649 if (item == null) {
650 return false;
651 }
652 final T value = (T) item;
653 if (!checker.test(value)) {
654 return false;
655 }
656
657 if (matchedCaptor != null) {
658 matchedCaptor.add(value);
659 }
660 return true;
661 }
662
663 @Override
664 public void describeTo(Description d) {
665 d.appendText(description);
666 }
667 };
668 return Mockito.argThat(m);
669 }
670
671 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
672 return checkArgument((List<ShortcutInfo> list) -> {
673 final Set<String> actualSet = set(si -> si.getId(), list);
674 return actualSet.equals(set(ids));
675
676 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
677 }
678
Makoto Onuki7001a612016-05-27 13:24:28 -0700679 public static ShortcutInfo parceled(ShortcutInfo si) {
680 Parcel p = Parcel.obtain();
681 p.writeParcelable(si, 0);
682 p.setDataPosition(0);
683 ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader());
684 p.recycle();
685 return si2;
686 }
687
688 public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) {
689 if (list == null) {
690 return null;
691 }
692 final List<ShortcutInfo> ret = new ArrayList<>(list.size());
693 for (ShortcutInfo si : list) {
694 ret.add(parceled(si));
695 }
696
697 return ret;
698 }
699
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700700 private static final Comparator<ShortcutInfo> sRankComparator =
701 (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank());
702
703 public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) {
704 final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts);
705 Collections.sort(ret, sRankComparator);
706 return ret;
707 }
708
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700709 public static void waitUntil(String message, BooleanSupplier condition) {
710 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
711 }
712
713 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
714 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
715 while (System.currentTimeMillis() < timeout) {
716 if (condition.getAsBoolean()) {
717 return;
718 }
719 try {
720 Thread.sleep(100);
721 } catch (InterruptedException e) {
722 throw new RuntimeException(e);
723 }
724 }
725 fail("Timed out for: " + message);
726 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700727
728 public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) {
729 return new ShortcutListAsserter(list);
730 }
731
Makoto Onuki2d895c32016-12-02 15:48:40 -0800732 public static ShortcutListAsserter assertWith(ShortcutInfo... list) {
733 return assertWith(list(list));
734 }
735
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700736 /**
737 * New style assertion that allows chained calls.
738 */
739 public static class ShortcutListAsserter {
Makoto Onukidf6da042016-06-16 09:51:40 -0700740 private final ShortcutListAsserter mOriginal;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700741 private final List<ShortcutInfo> mList;
742
743 ShortcutListAsserter(List<ShortcutInfo> list) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700744 this(null, list);
745 }
746
747 private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700748 mOriginal = (original == null) ? this : original;
749 mList = (list == null) ? new ArrayList<>(0) : new ArrayList<>(list);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700750 }
751
Makoto Onukidf6da042016-06-16 09:51:40 -0700752 public ShortcutListAsserter revertToOriginalList() {
753 return mOriginal;
754 }
755
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700756 public ShortcutListAsserter selectDynamic() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700757 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700758 filter(mList, ShortcutInfo::isDynamic));
759 }
760
761 public ShortcutListAsserter selectManifest() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700762 return new ShortcutListAsserter(this,
Makoto Onukib5a012f2016-06-21 11:13:53 -0700763 filter(mList, ShortcutInfo::isDeclaredInManifest));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700764 }
765
766 public ShortcutListAsserter selectPinned() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700767 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700768 filter(mList, ShortcutInfo::isPinned));
769 }
770
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800771 public ShortcutListAsserter selectFloating() {
772 return new ShortcutListAsserter(this,
773 filter(mList, (si -> si.isPinned()
774 && !(si.isDynamic() || si.isDeclaredInManifest()))));
775 }
776
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700777 public ShortcutListAsserter selectByActivity(ComponentName activity) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700778 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700779 ShortcutManagerTestUtils.filterByActivity(mList, activity));
780 }
781
782 public ShortcutListAsserter selectByChangedSince(long time) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700783 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700784 ShortcutManagerTestUtils.changedSince(mList, time));
785 }
786
Makoto Onukidf6da042016-06-16 09:51:40 -0700787 public ShortcutListAsserter selectByIds(String... ids) {
788 final Set<String> idSet = set(ids);
789 final ArrayList<ShortcutInfo> selected = new ArrayList<>();
790 for (ShortcutInfo si : mList) {
791 if (idSet.contains(si.getId())) {
792 selected.add(si);
793 idSet.remove(si.getId());
794 }
795 }
796 if (idSet.size() > 0) {
797 fail("Shortcuts not found for IDs=" + idSet);
798 }
799
800 return new ShortcutListAsserter(this, selected);
801 }
802
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700803 public ShortcutListAsserter toSortByRank() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700804 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700805 ShortcutManagerTestUtils.sortedByRank(mList));
806 }
807
Makoto Onukidf6da042016-06-16 09:51:40 -0700808 public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) {
809 c.accept(mList);
810 return this;
811 }
812
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700813 public ShortcutListAsserter haveIds(String... expectedIds) {
814 assertShortcutIds(mList, expectedIds);
815 return this;
816 }
817
818 public ShortcutListAsserter haveIdsOrdered(String... expectedIds) {
819 assertShortcutIdsOrdered(mList, expectedIds);
820 return this;
821 }
822
823 private ShortcutListAsserter haveSequentialRanks() {
824 for (int i = 0; i < mList.size(); i++) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700825 final ShortcutInfo si = mList.get(i);
826 assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank());
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700827 }
828 return this;
829 }
830
831 public ShortcutListAsserter haveRanksInOrder(String... expectedIds) {
832 toSortByRank()
833 .haveSequentialRanks()
834 .haveIdsOrdered(expectedIds);
835 return this;
836 }
837
838 public ShortcutListAsserter isEmpty() {
839 assertEquals(0, mList.size());
840 return this;
841 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700842
843 public ShortcutListAsserter areAllDynamic() {
844 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic()));
845 return this;
846 }
847
848 public ShortcutListAsserter areAllNotDynamic() {
849 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic()));
850 return this;
851 }
852
853 public ShortcutListAsserter areAllPinned() {
854 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned()));
855 return this;
856 }
857
858 public ShortcutListAsserter areAllNotPinned() {
859 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned()));
860 return this;
861 }
862
863 public ShortcutListAsserter areAllManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700864 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700865 return this;
866 }
867
868 public ShortcutListAsserter areAllNotManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700869 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700870 return this;
871 }
872
873 public ShortcutListAsserter areAllImmutable() {
874 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable()));
875 return this;
876 }
877
878 public ShortcutListAsserter areAllMutable() {
879 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable()));
880 return this;
881 }
882
883 public ShortcutListAsserter areAllEnabled() {
884 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled()));
885 return this;
886 }
887
888 public ShortcutListAsserter areAllDisabled() {
889 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
890 return this;
891 }
892
Makoto Onuki2d895c32016-12-02 15:48:40 -0800893 public ShortcutListAsserter areAllFloating() {
894 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
895 s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
896 return this;
897 }
898
899 public ShortcutListAsserter areAllNotFloating() {
900 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
901 !(s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic())));
902 return this;
903 }
904
905 public ShortcutListAsserter areAllOrphan() {
906 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
907 !s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
908 return this;
909 }
910
911 public ShortcutListAsserter areAllNotOrphan() {
912 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
913 s.isPinned() || s.isDeclaredInManifest() || s.isDynamic()));
914 return this;
915 }
916
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700917 public ShortcutListAsserter areAllWithKeyFieldsOnly() {
918 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly()));
919 return this;
920 }
921
922 public ShortcutListAsserter areAllNotWithKeyFieldsOnly() {
923 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly()));
924 return this;
925 }
926
Makoto Onukib08790c2016-06-23 14:05:46 -0700927 public ShortcutListAsserter areAllWithActivity(ComponentName activity) {
928 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.getActivity().equals(activity)));
929 return this;
930 }
931
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800932 public ShortcutListAsserter areAllWithNoActivity() {
933 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getActivity()));
934 return this;
935 }
936
Makoto Onukia01f4f02016-12-15 15:58:41 -0800937 public ShortcutListAsserter areAllWithIntent() {
938 forAllShortcuts(s -> assertNotNull("id=" + s.getId(), s.getIntent()));
939 return this;
940 }
941
942 public ShortcutListAsserter areAllWithNoIntent() {
943 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getIntent()));
944 return this;
945 }
946
Makoto Onukidf6da042016-06-16 09:51:40 -0700947 public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700948 boolean found = false;
Makoto Onukidf6da042016-06-16 09:51:40 -0700949 for (int i = 0; i < mList.size(); i++) {
950 final ShortcutInfo si = mList.get(i);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700951 found = true;
Makoto Onukidf6da042016-06-16 09:51:40 -0700952 sa.accept(si);
953 }
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700954 assertTrue("No shortcuts found.", found);
Makoto Onukidf6da042016-06-16 09:51:40 -0700955 return this;
956 }
957
958 public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
959 Consumer<ShortcutInfo> sa) {
960 boolean found = false;
961 for (int i = 0; i < mList.size(); i++) {
962 final ShortcutInfo si = mList.get(i);
963 if (p.test(si)) {
964 found = true;
965 try {
966 sa.accept(si);
967 } catch (Throwable e) {
968 throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
969 }
970 }
971 }
972 assertTrue("Shortcut with the given condition not found.", found);
973 return this;
974 }
975
976 public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
977 forShortcut(si -> si.getId().equals(id), sa);
978
979 return this;
980 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700981 }
Makoto Onukib5a012f2016-06-21 11:13:53 -0700982
983 public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) {
984 if (b1 == null && b2 == null) {
985 return; // pass
986 }
Makoto Onuki440a1ea2016-07-20 14:21:18 -0700987 assertNotNull("b1 is null but b2 is not", b1);
988 assertNotNull("b2 is null but b1 is not", b2);
Makoto Onukib5a012f2016-06-21 11:13:53 -0700989
990 // HashSet makes the error message readable.
991 assertEquals(set(b1.keySet()), set(b2.keySet()));
992
993 for (String key : b1.keySet()) {
994 final Object v1 = b1.get(key);
995 final Object v2 = b2.get(key);
996 if (v1 == null) {
997 if (v2 == null) {
998 return;
999 }
1000 }
1001 if (v1.equals(v2)) {
1002 return;
1003 }
1004
1005 assertTrue("Only either value is null: key=" + key
1006 + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null);
1007 assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass());
1008
1009 if (v1 instanceof BaseBundle) {
1010 assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2);
1011
1012 } else if (v1 instanceof boolean[]) {
1013 assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2));
1014
1015 } else if (v1 instanceof int[]) {
1016 MoreAsserts.assertEquals((int[]) v1, (int[]) v2);
1017
1018 } else if (v1 instanceof double[]) {
1019 MoreAsserts.assertEquals((double[]) v1, (double[]) v2);
1020
1021 } else if (v1 instanceof String[]) {
1022 MoreAsserts.assertEquals((String[]) v1, (String[]) v2);
1023
1024 } else if (v1 instanceof Double) {
1025 if (((Double) v1).isNaN()) {
1026 assertTrue(((Double) v2).isNaN());
1027 } else {
1028 assertEquals(v1, v2);
1029 }
1030
1031 } else {
1032 assertEquals(v1, v2);
1033 }
1034 }
1035 }
Makoto Onukib08790c2016-06-23 14:05:46 -07001036
1037 public static void waitOnMainThread() throws InterruptedException {
1038 final CountDownLatch latch = new CountDownLatch(1);
1039
1040 new Handler(Looper.getMainLooper()).post(() -> latch.countDown());
1041
1042 latch.await();
1043 }
1044
1045 public static class LauncherCallbackAsserter {
1046 private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class);
1047
1048 private Callback getMockCallback() {
1049 return mCallback;
1050 }
1051
1052 public LauncherCallbackAsserter assertNoCallbackCalled() {
1053 verify(mCallback, times(0)).onShortcutsChanged(
1054 anyString(),
1055 any(List.class),
1056 any(UserHandle.class));
1057 return this;
1058 }
1059
1060 public LauncherCallbackAsserter assertNoCallbackCalledForPackage(
1061 String publisherPackageName) {
1062 verify(mCallback, times(0)).onShortcutsChanged(
1063 eq(publisherPackageName),
1064 any(List.class),
1065 any(UserHandle.class));
1066 return this;
1067 }
1068
1069 public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser(
1070 String publisherPackageName, UserHandle publisherUserHandle) {
1071 verify(mCallback, times(0)).onShortcutsChanged(
1072 eq(publisherPackageName),
1073 any(List.class),
1074 eq(publisherUserHandle));
1075 return this;
1076 }
1077
1078 public ShortcutListAsserter assertCallbackCalledForPackageAndUser(
1079 String publisherPackageName, UserHandle publisherUserHandle) {
1080 final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class);
1081 verify(mCallback, times(1)).onShortcutsChanged(
1082 eq(publisherPackageName),
1083 shortcuts.capture(),
1084 eq(publisherUserHandle));
1085 return new ShortcutListAsserter(shortcuts.getValue());
1086 }
1087 }
1088
1089 public static LauncherCallbackAsserter assertForLauncherCallback(
1090 LauncherApps launcherApps, Runnable body) throws InterruptedException {
1091 final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter();
1092 launcherApps.registerCallback(asserter.getMockCallback(),
1093 new Handler(Looper.getMainLooper()));
1094
1095 body.run();
1096
1097 waitOnMainThread();
1098
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001099 // TODO unregister doesn't work well during unit tests. Figure out and fix it.
1100 // launcherApps.unregisterCallback(asserter.getMockCallback());
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001101
Makoto Onukib08790c2016-06-23 14:05:46 -07001102 return asserter;
1103 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001104
Makoto Onukia01f4f02016-12-15 15:58:41 -08001105 public static LauncherCallbackAsserter assertForLauncherCallbackNoThrow(
1106 LauncherApps launcherApps, Runnable body) {
1107 try {
1108 return assertForLauncherCallback(launcherApps, body);
1109 } catch (InterruptedException e) {
1110 fail("Caught InterruptedException");
1111 return null; // Never happens.
1112 }
1113 }
1114
Makoto Onuki9c850012016-07-26 15:50:50 -07001115 public static void retryUntil(BooleanSupplier checker, String message) {
Makoto Onukid67bf6a2016-08-19 09:10:08 -07001116 retryUntil(checker, message, 30);
1117 }
1118
1119 public static void retryUntil(BooleanSupplier checker, String message, long timeoutSeconds) {
1120 final long timeOut = System.currentTimeMillis() + timeoutSeconds * 1000;
Makoto Onuki9c850012016-07-26 15:50:50 -07001121 while (!checker.getAsBoolean()) {
Makoto Onukia210ccf2016-07-27 14:08:48 -07001122 if (System.currentTimeMillis() > timeOut) {
1123 break;
1124 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001125 try {
1126 Thread.sleep(200);
1127 } catch (InterruptedException ignore) {
1128 }
1129 }
1130 assertTrue(message, checker.getAsBoolean());
1131 }
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001132}