blob: e36586eba26c7f4f53d6e02b5a6a86be16d8bd07 [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
Makoto Onukia4f89b12017-10-05 10:37:55 -070025import static org.junit.Assert.assertNotEquals;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070026import static org.mockito.Matchers.any;
27import static org.mockito.Matchers.anyList;
28import static org.mockito.Matchers.anyString;
29import static org.mockito.Matchers.eq;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -070030import static org.mockito.Mockito.atLeastOnce;
Makoto Onukib08790c2016-06-23 14:05:46 -070031import static org.mockito.Mockito.mock;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070032import static org.mockito.Mockito.reset;
33import static org.mockito.Mockito.times;
34import static org.mockito.Mockito.verify;
35
36import android.app.Instrumentation;
Makoto Onuki7001a612016-05-27 13:24:28 -070037import android.content.ComponentName;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070038import android.content.Context;
39import android.content.pm.LauncherApps;
Makoto Onukib08790c2016-06-23 14:05:46 -070040import android.content.pm.LauncherApps.Callback;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070041import android.content.pm.ShortcutInfo;
42import android.graphics.Bitmap;
43import android.graphics.BitmapFactory;
44import android.os.BaseBundle;
45import android.os.Bundle;
Makoto Onukib08790c2016-06-23 14:05:46 -070046import android.os.Handler;
47import android.os.Looper;
Makoto Onuki7001a612016-05-27 13:24:28 -070048import android.os.Parcel;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070049import android.os.ParcelFileDescriptor;
Makoto Onukib5a012f2016-06-21 11:13:53 -070050import android.os.PersistableBundle;
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -070051import android.os.UserHandle;
52import android.test.MoreAsserts;
53import android.util.Log;
54
55import junit.framework.Assert;
56
57import org.hamcrest.BaseMatcher;
58import org.hamcrest.Description;
59import org.hamcrest.Matcher;
Makoto Onuki9c850012016-07-26 15:50:50 -070060import org.json.JSONException;
61import org.json.JSONObject;
Makoto Onukib08790c2016-06-23 14:05:46 -070062import org.mockito.ArgumentCaptor;
Makoto Onukid0010c52017-03-30 14:17:35 -070063import org.mockito.ArgumentMatchers;
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 Onuki1e091c82018-02-01 11:58:03 -0800213 runCommand(instrumentation, "cmd shortcut clear-default-launcher --user "
214 + instrumentation.getContext().getUserId(),
215 result -> result.contains("Success"));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700216 }
217
218 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
219 setDefaultLauncher(instrumentation, packageContext.getPackageName()
220 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
221 }
222
223 public static void overrideConfig(Instrumentation instrumentation, String config) {
224 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
225 }
226
227 public static void resetConfig(Instrumentation instrumentation) {
228 runShortcutCommandForSuccess(instrumentation, "reset-config");
229 }
230
231 public static void resetThrottling(Instrumentation instrumentation) {
232 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
233 }
234
235 public static void resetAllThrottling(Instrumentation instrumentation) {
236 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
237 }
238
239 public static void clearShortcuts(Instrumentation instrumentation, int userId,
240 String packageName) {
241 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
242 + " --user " + userId + " " + packageName);
243 }
244
Makoto Onuki9c850012016-07-26 15:50:50 -0700245 public static void anyContains(List<String> result, String expected) {
246 for (String l : result) {
247 if (l.contains(expected)) {
248 return;
249 }
250 }
251 fail("Result didn't contain '" + expected + "': was\n" + result);
252 }
253
254 public static void enableComponent(Instrumentation instrumentation, ComponentName cn,
255 boolean enable) {
256
257 final String word = (enable ? "enable" : "disable");
258 runCommand(instrumentation,
259 "pm " + word + " " + cn.flattenToString()
260 , result ->concatResult(result).contains(word));
261 }
262
263 public static void appOps(Instrumentation instrumentation, String packageName,
264 String op, String mode) {
265 runCommand(instrumentation, "appops set " + packageName + " " + op + " " + mode);
266 }
267
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700268 public static void dumpsysShortcut(Instrumentation instrumentation) {
269 if (!ENABLE_DUMPSYS) {
270 return;
271 }
Makoto Onuki3bdbf982016-06-23 16:56:35 -0700272 Log.e(TAG, "Dumpsys shortcut");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700273 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
274 Log.e(TAG, s);
275 }
276 }
277
Makoto Onuki9c850012016-07-26 15:50:50 -0700278 public static JSONObject getCheckinDump(Instrumentation instrumentation) throws JSONException {
279 return new JSONObject(concatResult(runCommand(instrumentation, "dumpsys shortcut -c")));
280 }
281
282 public static boolean isLowRamDevice(Instrumentation instrumentation) throws JSONException {
283 return getCheckinDump(instrumentation).getBoolean("lowRam");
284 }
285
286 public static int getIconSize(Instrumentation instrumentation) throws JSONException {
287 return getCheckinDump(instrumentation).getInt("iconSize");
288 }
289
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700290 public static Bundle makeBundle(Object... keysAndValues) {
291 assertTrue((keysAndValues.length % 2) == 0);
292
293 if (keysAndValues.length == 0) {
294 return null;
295 }
296 final Bundle ret = new Bundle();
297
298 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
299 final String key = keysAndValues[i].toString();
300 final Object value = keysAndValues[i + 1];
301
302 if (value == null) {
303 ret.putString(key, null);
304 } else if (value instanceof Integer) {
305 ret.putInt(key, (Integer) value);
306 } else if (value instanceof String) {
307 ret.putString(key, (String) value);
308 } else if (value instanceof Bundle) {
309 ret.putBundle(key, (Bundle) value);
310 } else {
311 fail("Type not supported yet: " + value.getClass().getName());
312 }
313 }
314 return ret;
315 }
316
Makoto Onukib5a012f2016-06-21 11:13:53 -0700317 public static PersistableBundle makePersistableBundle(Object... keysAndValues) {
318 assertTrue((keysAndValues.length % 2) == 0);
319
320 if (keysAndValues.length == 0) {
321 return null;
322 }
323 final PersistableBundle ret = new PersistableBundle();
324
325 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
326 final String key = keysAndValues[i].toString();
327 final Object value = keysAndValues[i + 1];
328
329 if (value == null) {
330 ret.putString(key, null);
331 } else if (value instanceof Integer) {
332 ret.putInt(key, (Integer) value);
333 } else if (value instanceof String) {
334 ret.putString(key, (String) value);
335 } else if (value instanceof PersistableBundle) {
336 ret.putPersistableBundle(key, (PersistableBundle) value);
337 } else {
338 fail("Type not supported yet: " + value.getClass().getName());
339 }
340 }
341 return ret;
342 }
343
Makoto Onuki50a320e2017-05-31 14:38:42 -0700344 public static <T> T[] array(T... array) {
345 return array;
346 }
347
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700348 public static <T> List<T> list(T... array) {
349 return Arrays.asList(array);
350 }
351
352 public static <T> Set<T> hashSet(Set<T> in) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700353 return new LinkedHashSet<>(in);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700354 }
355
356 public static <T> Set<T> set(T... values) {
357 return set(v -> v, values);
358 }
359
360 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
361 return set(converter, Arrays.asList(values));
362 }
363
364 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700365 final LinkedHashSet<T> ret = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700366 for (V v : values) {
367 ret.add(converter.apply(v));
368 }
369 return ret;
370 }
371
372 public static void resetAll(Collection<?> mocks) {
373 for (Object o : mocks) {
374 reset(o);
375 }
376 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700377
Makoto Onukidf6da042016-06-16 09:51:40 -0700378 public static <T extends Collection<?>> T assertEmpty(T collection) {
379 if (collection == null) {
380 return collection; // okay.
381 }
382 assertEquals(0, collection.size());
383 return collection;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700384 }
385
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700386 public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
387 final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
388 ret.removeIf(si -> !p.test(si));
389 return ret;
390 }
391
Makoto Onuki7001a612016-05-27 13:24:28 -0700392 public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
393 ComponentName activity) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700394 return filter(list, si ->
395 (si.getActivity().equals(activity)
Makoto Onukib5a012f2016-06-21 11:13:53 -0700396 && (si.isDeclaredInManifest() || si.isDynamic())));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700397 }
398
399 public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
400 return filter(list, si -> si.getLastChangedTimestamp() >= time);
Makoto Onuki7001a612016-05-27 13:24:28 -0700401 }
402
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700403 @FunctionalInterface
404 public interface ExceptionRunnable {
405 void run() throws Exception;
406 }
407
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700408 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700409 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700410 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
411 }
412
Makoto Onuki22fcc682016-05-17 14:52:19 -0700413 public static void assertCannotUpdateImmutable(Runnable r) {
414 assertExpectException(
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700415 IllegalArgumentException.class, "may not be manipulated via APIs", r::run);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700416 }
417
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700418 public static void assertDynamicShortcutCountExceeded(Runnable r) {
419 assertExpectException(IllegalArgumentException.class,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700420 "Max number of dynamic shortcuts exceeded", r::run);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700421 }
422
423 public static void assertExpectException(String message,
424 Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700425 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700426 try {
427 r.run();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700428 } catch (Throwable e) {
429 Assert.assertTrue(
430 "Expected exception type was " + expectedExceptionType.getName()
431 + " but caught " + e + " (message=" + message + ")",
432 expectedExceptionType.isAssignableFrom(e.getClass()));
433 if (expectedExceptionMessageRegex != null) {
434 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
435 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700436 return; // Pass
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700437 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700438 Assert.fail("Expected exception type " + expectedExceptionType.getName()
439 + " was not thrown");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700440 }
441
442 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
443 String... expectedIds) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700444 final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
445 final SortedSet<String> actual = new TreeSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700446 for (ShortcutInfo s : actualShortcuts) {
447 actual.add(s.getId());
448 }
449
450 // Compare the sets.
451 assertEquals(expected, actual);
452 return actualShortcuts;
453 }
454
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700455 public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
456 String... expectedIds) {
457 final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
458 final ArrayList<String> actual = new ArrayList<>();
459 for (ShortcutInfo s : actualShortcuts) {
460 actual.add(s.getId());
461 }
462 assertEquals(expected, actual);
463 return actualShortcuts;
464 }
465
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700466 public static List<ShortcutInfo> assertAllHaveIntents(
467 List<ShortcutInfo> actualShortcuts) {
468 for (ShortcutInfo s : actualShortcuts) {
469 assertNotNull("ID " + s.getId(), s.getIntent());
470 }
471 return actualShortcuts;
472 }
473
474 public static List<ShortcutInfo> assertAllNotHaveIntents(
475 List<ShortcutInfo> actualShortcuts) {
476 for (ShortcutInfo s : actualShortcuts) {
477 assertNull("ID " + s.getId(), s.getIntent());
478 }
479 return actualShortcuts;
480 }
481
482 public static List<ShortcutInfo> assertAllHaveTitle(
483 List<ShortcutInfo> actualShortcuts) {
484 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700485 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700486 }
487 return actualShortcuts;
488 }
489
490 public static List<ShortcutInfo> assertAllNotHaveTitle(
491 List<ShortcutInfo> actualShortcuts) {
492 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700493 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700494 }
495 return actualShortcuts;
496 }
497
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700498 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
499 List<ShortcutInfo> actualShortcuts) {
500 for (ShortcutInfo s : actualShortcuts) {
501 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
502 }
503 return actualShortcuts;
504 }
505
506 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
507 List<ShortcutInfo> actualShortcuts) {
508 for (ShortcutInfo s : actualShortcuts) {
509 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
510 }
511 return actualShortcuts;
512 }
513
514 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
515 for (ShortcutInfo s : actualShortcuts) {
516 assertTrue("ID " + s.getId(), s.isDynamic());
517 }
518 return actualShortcuts;
519 }
520
521 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()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700903 areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukidf6da042016-06-16 09:51:40 -0700904 return this;
905 }
906
907 public ShortcutListAsserter areAllDisabled() {
908 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700909 forAllShortcuts(s -> assertNotEquals("id=" + s.getId(),
910 ShortcutInfo.DISABLED_REASON_NOT_DISABLED, s.getDisabledReason()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700911 return this;
912 }
913
Makoto Onuki2d895c32016-12-02 15:48:40 -0800914 public ShortcutListAsserter areAllFloating() {
915 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
916 s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
917 return this;
918 }
919
920 public ShortcutListAsserter areAllNotFloating() {
921 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
922 !(s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic())));
923 return this;
924 }
925
926 public ShortcutListAsserter areAllOrphan() {
927 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
928 !s.isPinned() && !s.isDeclaredInManifest() && !s.isDynamic()));
929 return this;
930 }
931
932 public ShortcutListAsserter areAllNotOrphan() {
933 forAllShortcuts(s -> assertTrue("id=" + s.getId(),
934 s.isPinned() || s.isDeclaredInManifest() || s.isDynamic()));
935 return this;
936 }
937
Makoto Onukia4f89b12017-10-05 10:37:55 -0700938 public ShortcutListAsserter areAllVisibleToPublisher() {
939 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isVisibleToPublisher()));
940 return this;
941 }
942
943 public ShortcutListAsserter areAllNotVisibleToPublisher() {
944 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isVisibleToPublisher()));
945 return this;
946 }
947
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700948 public ShortcutListAsserter areAllWithKeyFieldsOnly() {
949 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly()));
950 return this;
951 }
952
953 public ShortcutListAsserter areAllNotWithKeyFieldsOnly() {
954 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly()));
955 return this;
956 }
957
Makoto Onukib08790c2016-06-23 14:05:46 -0700958 public ShortcutListAsserter areAllWithActivity(ComponentName activity) {
Makoto Onuki255461f2017-01-10 11:47:25 -0800959 forAllShortcuts(s -> assertEquals("id=" + s.getId(), activity, s.getActivity()));
Makoto Onukib08790c2016-06-23 14:05:46 -0700960 return this;
961 }
962
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800963 public ShortcutListAsserter areAllWithNoActivity() {
964 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getActivity()));
965 return this;
966 }
967
Makoto Onukia01f4f02016-12-15 15:58:41 -0800968 public ShortcutListAsserter areAllWithIntent() {
969 forAllShortcuts(s -> assertNotNull("id=" + s.getId(), s.getIntent()));
970 return this;
971 }
972
973 public ShortcutListAsserter areAllWithNoIntent() {
974 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getIntent()));
975 return this;
976 }
977
Makoto Onukia4f89b12017-10-05 10:37:55 -0700978 public ShortcutListAsserter areAllWithDisabledReason(int disabledReason) {
979 forAllShortcuts(s -> assertEquals("id=" + s.getId(),
980 disabledReason, s.getDisabledReason()));
Makoto Onukib1588c02017-10-12 15:11:45 -0700981 if (disabledReason >= ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700982 areAllNotVisibleToPublisher();
Makoto Onukib1588c02017-10-12 15:11:45 -0700983 } else {
984 areAllVisibleToPublisher();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700985 }
986 return this;
987 }
988
Makoto Onukidf6da042016-06-16 09:51:40 -0700989 public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700990 boolean found = false;
Makoto Onukidf6da042016-06-16 09:51:40 -0700991 for (int i = 0; i < mList.size(); i++) {
992 final ShortcutInfo si = mList.get(i);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700993 found = true;
Makoto Onukidf6da042016-06-16 09:51:40 -0700994 sa.accept(si);
995 }
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700996 assertTrue("No shortcuts found.", found);
Makoto Onukidf6da042016-06-16 09:51:40 -0700997 return this;
998 }
999
1000 public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
1001 Consumer<ShortcutInfo> sa) {
1002 boolean found = false;
1003 for (int i = 0; i < mList.size(); i++) {
1004 final ShortcutInfo si = mList.get(i);
1005 if (p.test(si)) {
1006 found = true;
1007 try {
1008 sa.accept(si);
1009 } catch (Throwable e) {
1010 throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
1011 }
1012 }
1013 }
1014 assertTrue("Shortcut with the given condition not found.", found);
1015 return this;
1016 }
1017
1018 public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
1019 forShortcut(si -> si.getId().equals(id), sa);
1020
1021 return this;
1022 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001023 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07001024
1025 public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) {
1026 if (b1 == null && b2 == null) {
1027 return; // pass
1028 }
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001029 assertNotNull("b1 is null but b2 is not", b1);
1030 assertNotNull("b2 is null but b1 is not", b2);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001031
1032 // HashSet makes the error message readable.
1033 assertEquals(set(b1.keySet()), set(b2.keySet()));
1034
1035 for (String key : b1.keySet()) {
1036 final Object v1 = b1.get(key);
1037 final Object v2 = b2.get(key);
1038 if (v1 == null) {
1039 if (v2 == null) {
1040 return;
1041 }
1042 }
1043 if (v1.equals(v2)) {
1044 return;
1045 }
1046
1047 assertTrue("Only either value is null: key=" + key
1048 + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null);
1049 assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass());
1050
1051 if (v1 instanceof BaseBundle) {
1052 assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2);
1053
1054 } else if (v1 instanceof boolean[]) {
1055 assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2));
1056
1057 } else if (v1 instanceof int[]) {
1058 MoreAsserts.assertEquals((int[]) v1, (int[]) v2);
1059
1060 } else if (v1 instanceof double[]) {
1061 MoreAsserts.assertEquals((double[]) v1, (double[]) v2);
1062
1063 } else if (v1 instanceof String[]) {
1064 MoreAsserts.assertEquals((String[]) v1, (String[]) v2);
1065
1066 } else if (v1 instanceof Double) {
1067 if (((Double) v1).isNaN()) {
1068 assertTrue(((Double) v2).isNaN());
1069 } else {
1070 assertEquals(v1, v2);
1071 }
1072
1073 } else {
1074 assertEquals(v1, v2);
1075 }
1076 }
1077 }
Makoto Onukib08790c2016-06-23 14:05:46 -07001078
1079 public static void waitOnMainThread() throws InterruptedException {
1080 final CountDownLatch latch = new CountDownLatch(1);
1081
1082 new Handler(Looper.getMainLooper()).post(() -> latch.countDown());
1083
1084 latch.await();
1085 }
1086
1087 public static class LauncherCallbackAsserter {
1088 private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class);
1089
1090 private Callback getMockCallback() {
1091 return mCallback;
1092 }
1093
1094 public LauncherCallbackAsserter assertNoCallbackCalled() {
1095 verify(mCallback, times(0)).onShortcutsChanged(
1096 anyString(),
1097 any(List.class),
1098 any(UserHandle.class));
1099 return this;
1100 }
1101
1102 public LauncherCallbackAsserter assertNoCallbackCalledForPackage(
1103 String publisherPackageName) {
1104 verify(mCallback, times(0)).onShortcutsChanged(
1105 eq(publisherPackageName),
1106 any(List.class),
1107 any(UserHandle.class));
1108 return this;
1109 }
1110
1111 public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser(
1112 String publisherPackageName, UserHandle publisherUserHandle) {
1113 verify(mCallback, times(0)).onShortcutsChanged(
1114 eq(publisherPackageName),
1115 any(List.class),
1116 eq(publisherUserHandle));
1117 return this;
1118 }
1119
1120 public ShortcutListAsserter assertCallbackCalledForPackageAndUser(
1121 String publisherPackageName, UserHandle publisherUserHandle) {
1122 final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class);
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001123 verify(mCallback, atLeastOnce()).onShortcutsChanged(
Makoto Onukib08790c2016-06-23 14:05:46 -07001124 eq(publisherPackageName),
1125 shortcuts.capture(),
1126 eq(publisherUserHandle));
1127 return new ShortcutListAsserter(shortcuts.getValue());
1128 }
1129 }
1130
1131 public static LauncherCallbackAsserter assertForLauncherCallback(
1132 LauncherApps launcherApps, Runnable body) throws InterruptedException {
1133 final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter();
1134 launcherApps.registerCallback(asserter.getMockCallback(),
1135 new Handler(Looper.getMainLooper()));
1136
1137 body.run();
1138
1139 waitOnMainThread();
1140
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001141 // TODO unregister doesn't work well during unit tests. Figure out and fix it.
1142 // launcherApps.unregisterCallback(asserter.getMockCallback());
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001143
Makoto Onukib08790c2016-06-23 14:05:46 -07001144 return asserter;
1145 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001146
Makoto Onukia01f4f02016-12-15 15:58:41 -08001147 public static LauncherCallbackAsserter assertForLauncherCallbackNoThrow(
1148 LauncherApps launcherApps, Runnable body) {
1149 try {
1150 return assertForLauncherCallback(launcherApps, body);
1151 } catch (InterruptedException e) {
1152 fail("Caught InterruptedException");
1153 return null; // Never happens.
1154 }
1155 }
1156
Makoto Onuki9c850012016-07-26 15:50:50 -07001157 public static void retryUntil(BooleanSupplier checker, String message) {
Makoto Onukid67bf6a2016-08-19 09:10:08 -07001158 retryUntil(checker, message, 30);
1159 }
1160
1161 public static void retryUntil(BooleanSupplier checker, String message, long timeoutSeconds) {
1162 final long timeOut = System.currentTimeMillis() + timeoutSeconds * 1000;
Makoto Onuki9c850012016-07-26 15:50:50 -07001163 while (!checker.getAsBoolean()) {
Makoto Onukia210ccf2016-07-27 14:08:48 -07001164 if (System.currentTimeMillis() > timeOut) {
1165 break;
1166 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001167 try {
1168 Thread.sleep(200);
1169 } catch (InterruptedException ignore) {
1170 }
1171 }
1172 assertTrue(message, checker.getAsBoolean());
1173 }
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001174}