blob: 99eb8468c20d5348378588206fa04f3b2a4672a3 [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 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 Onuki50a320e2017-05-31 14:38:42 -0700341 public static <T> T[] array(T... array) {
342 return array;
343 }
344
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700345 public static <T> List<T> list(T... array) {
346 return Arrays.asList(array);
347 }
348
349 public static <T> Set<T> hashSet(Set<T> in) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700350 return new LinkedHashSet<>(in);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700351 }
352
353 public static <T> Set<T> set(T... values) {
354 return set(v -> v, values);
355 }
356
357 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
358 return set(converter, Arrays.asList(values));
359 }
360
361 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700362 final LinkedHashSet<T> ret = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700363 for (V v : values) {
364 ret.add(converter.apply(v));
365 }
366 return ret;
367 }
368
369 public static void resetAll(Collection<?> mocks) {
370 for (Object o : mocks) {
371 reset(o);
372 }
373 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700374
Makoto Onukidf6da042016-06-16 09:51:40 -0700375 public static <T extends Collection<?>> T assertEmpty(T collection) {
376 if (collection == null) {
377 return collection; // okay.
378 }
379 assertEquals(0, collection.size());
380 return collection;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700381 }
382
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700383 public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
384 final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
385 ret.removeIf(si -> !p.test(si));
386 return ret;
387 }
388
Makoto Onuki7001a612016-05-27 13:24:28 -0700389 public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
390 ComponentName activity) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700391 return filter(list, si ->
392 (si.getActivity().equals(activity)
Makoto Onukib5a012f2016-06-21 11:13:53 -0700393 && (si.isDeclaredInManifest() || si.isDynamic())));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700394 }
395
396 public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
397 return filter(list, si -> si.getLastChangedTimestamp() >= time);
Makoto Onuki7001a612016-05-27 13:24:28 -0700398 }
399
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700400 @FunctionalInterface
401 public interface ExceptionRunnable {
402 void run() throws Exception;
403 }
404
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700405 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700406 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700407 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
408 }
409
Makoto Onuki22fcc682016-05-17 14:52:19 -0700410 public static void assertCannotUpdateImmutable(Runnable r) {
411 assertExpectException(
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700412 IllegalArgumentException.class, "may not be manipulated via APIs", r::run);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700413 }
414
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700415 public static void assertDynamicShortcutCountExceeded(Runnable r) {
416 assertExpectException(IllegalArgumentException.class,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700417 "Max number of dynamic shortcuts exceeded", r::run);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700418 }
419
420 public static void assertExpectException(String message,
421 Class<? extends Throwable> expectedExceptionType,
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700422 String expectedExceptionMessageRegex, ExceptionRunnable r) {
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700423 try {
424 r.run();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700425 } catch (Throwable e) {
426 Assert.assertTrue(
427 "Expected exception type was " + expectedExceptionType.getName()
428 + " but caught " + e + " (message=" + message + ")",
429 expectedExceptionType.isAssignableFrom(e.getClass()));
430 if (expectedExceptionMessageRegex != null) {
431 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
432 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700433 return; // Pass
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700434 }
Makoto Onukia1d38b32016-06-10 15:32:26 -0700435 Assert.fail("Expected exception type " + expectedExceptionType.getName()
436 + " was not thrown");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700437 }
438
439 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
440 String... expectedIds) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700441 final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
442 final SortedSet<String> actual = new TreeSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700443 for (ShortcutInfo s : actualShortcuts) {
444 actual.add(s.getId());
445 }
446
447 // Compare the sets.
448 assertEquals(expected, actual);
449 return actualShortcuts;
450 }
451
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700452 public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
453 String... expectedIds) {
454 final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
455 final ArrayList<String> actual = new ArrayList<>();
456 for (ShortcutInfo s : actualShortcuts) {
457 actual.add(s.getId());
458 }
459 assertEquals(expected, actual);
460 return actualShortcuts;
461 }
462
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700463 public static List<ShortcutInfo> assertAllHaveIntents(
464 List<ShortcutInfo> actualShortcuts) {
465 for (ShortcutInfo s : actualShortcuts) {
466 assertNotNull("ID " + s.getId(), s.getIntent());
467 }
468 return actualShortcuts;
469 }
470
471 public static List<ShortcutInfo> assertAllNotHaveIntents(
472 List<ShortcutInfo> actualShortcuts) {
473 for (ShortcutInfo s : actualShortcuts) {
474 assertNull("ID " + s.getId(), s.getIntent());
475 }
476 return actualShortcuts;
477 }
478
479 public static List<ShortcutInfo> assertAllHaveTitle(
480 List<ShortcutInfo> actualShortcuts) {
481 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700482 assertNotNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700483 }
484 return actualShortcuts;
485 }
486
487 public static List<ShortcutInfo> assertAllNotHaveTitle(
488 List<ShortcutInfo> actualShortcuts) {
489 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukieddbfec2016-05-31 17:04:34 -0700490 assertNull("ID " + s.getId(), s.getShortLabel());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700491 }
492 return actualShortcuts;
493 }
494
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700495 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
496 List<ShortcutInfo> actualShortcuts) {
497 for (ShortcutInfo s : actualShortcuts) {
498 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
499 }
500 return actualShortcuts;
501 }
502
503 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
504 List<ShortcutInfo> actualShortcuts) {
505 for (ShortcutInfo s : actualShortcuts) {
506 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
507 }
508 return actualShortcuts;
509 }
510
511 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
512 for (ShortcutInfo s : actualShortcuts) {
513 assertTrue("ID " + s.getId(), s.isDynamic());
514 }
515 return actualShortcuts;
516 }
517
518 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
519 for (ShortcutInfo s : actualShortcuts) {
520 assertTrue("ID " + s.getId(), s.isPinned());
521 }
522 return actualShortcuts;
523 }
524
525 public static List<ShortcutInfo> assertAllDynamicOrPinned(
526 List<ShortcutInfo> actualShortcuts) {
527 for (ShortcutInfo s : actualShortcuts) {
528 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
529 }
530 return actualShortcuts;
531 }
532
Makoto Onuki22fcc682016-05-17 14:52:19 -0700533 public static List<ShortcutInfo> assertAllManifest(
534 List<ShortcutInfo> actualShortcuts) {
535 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700536 assertTrue("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700537 }
538 return actualShortcuts;
539 }
540
541 public static List<ShortcutInfo> assertAllNotManifest(
542 List<ShortcutInfo> actualShortcuts) {
543 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700544 assertFalse("ID " + s.getId(), s.isDeclaredInManifest());
Makoto Onuki22fcc682016-05-17 14:52:19 -0700545 }
546 return actualShortcuts;
547 }
548
549 public static List<ShortcutInfo> assertAllDisabled(
550 List<ShortcutInfo> actualShortcuts) {
551 for (ShortcutInfo s : actualShortcuts) {
552 assertTrue("ID " + s.getId(), !s.isEnabled());
553 }
554 return actualShortcuts;
555 }
556
557 public static List<ShortcutInfo> assertAllEnabled(
558 List<ShortcutInfo> actualShortcuts) {
559 for (ShortcutInfo s : actualShortcuts) {
560 assertTrue("ID " + s.getId(), s.isEnabled());
561 }
562 return actualShortcuts;
563 }
564
565 public static List<ShortcutInfo> assertAllImmutable(
566 List<ShortcutInfo> actualShortcuts) {
567 for (ShortcutInfo s : actualShortcuts) {
568 assertTrue("ID " + s.getId(), s.isImmutable());
569 }
570 return actualShortcuts;
571 }
572
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700573 public static void assertDynamicOnly(ShortcutInfo si) {
574 assertTrue(si.isDynamic());
575 assertFalse(si.isPinned());
576 }
577
578 public static void assertPinnedOnly(ShortcutInfo si) {
579 assertFalse(si.isDynamic());
Makoto Onukib5a012f2016-06-21 11:13:53 -0700580 assertFalse(si.isDeclaredInManifest());
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700581 assertTrue(si.isPinned());
582 }
583
584 public static void assertDynamicAndPinned(ShortcutInfo si) {
585 assertTrue(si.isDynamic());
586 assertTrue(si.isPinned());
587 }
588
589 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
590 assertEquals("width", expectedWidth, bitmap.getWidth());
591 assertEquals("height", expectedHeight, bitmap.getHeight());
592 }
593
594 public static <T> void assertAllUnique(Collection<T> list) {
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700595 final Set<Object> set = new LinkedHashSet<>();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700596 for (T item : list) {
597 if (set.contains(item)) {
598 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
599 }
600 set.add(item);
601 }
602 }
603
Makoto Onuki39686e82016-04-13 18:03:00 -0700604 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
605 for (ShortcutInfo si : list) {
606 if (si.getId().equals(id)) {
607 return si;
608 }
609 }
610 fail("Shortcut " + id + " not found in the list");
611 return null;
612 }
613
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700614 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
615 assertNotNull(pfd);
616 try {
617 try {
618 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
619 } finally {
620 pfd.close();
621 }
622 } catch (IOException e) {
623 throw new RuntimeException(e);
624 }
625 }
626
627 public static void assertBundleEmpty(BaseBundle b) {
628 assertTrue(b == null || b.size() == 0);
629 }
630
631 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
632 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
633 any(UserHandle.class));
634 }
635
636 public static void assertCallbackReceived(LauncherApps.Callback mock,
637 UserHandle user, String packageName, String... ids) {
638 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
639 eq(user));
640 }
641
642 public static boolean checkAssertSuccess(Runnable r) {
643 try {
644 r.run();
645 return true;
646 } catch (AssertionError e) {
647 return false;
648 }
649 }
650
651 public static <T> T checkArgument(Predicate<T> checker, String description,
652 List<T> matchedCaptor) {
653 final Matcher<T> m = new BaseMatcher<T>() {
654 @Override
655 public boolean matches(Object item) {
656 if (item == null) {
657 return false;
658 }
659 final T value = (T) item;
660 if (!checker.test(value)) {
661 return false;
662 }
663
664 if (matchedCaptor != null) {
665 matchedCaptor.add(value);
666 }
667 return true;
668 }
669
670 @Override
671 public void describeTo(Description d) {
672 d.appendText(description);
673 }
674 };
Paul Duffin192bb0b2017-03-09 18:49:41 +0000675 return MockitoHamcrest.argThat(m);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700676 }
677
678 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
679 return checkArgument((List<ShortcutInfo> list) -> {
680 final Set<String> actualSet = set(si -> si.getId(), list);
681 return actualSet.equals(set(ids));
682
683 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
684 }
685
Makoto Onuki7001a612016-05-27 13:24:28 -0700686 public static ShortcutInfo parceled(ShortcutInfo si) {
687 Parcel p = Parcel.obtain();
688 p.writeParcelable(si, 0);
689 p.setDataPosition(0);
690 ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader());
691 p.recycle();
692 return si2;
693 }
694
695 public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) {
696 if (list == null) {
697 return null;
698 }
699 final List<ShortcutInfo> ret = new ArrayList<>(list.size());
700 for (ShortcutInfo si : list) {
701 ret.add(parceled(si));
702 }
703
704 return ret;
705 }
706
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700707 private static final Comparator<ShortcutInfo> sRankComparator =
708 (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank());
709
710 public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) {
711 final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts);
712 Collections.sort(ret, sRankComparator);
713 return ret;
714 }
715
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700716 public static void waitUntil(String message, BooleanSupplier condition) {
717 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
718 }
719
720 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
721 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
722 while (System.currentTimeMillis() < timeout) {
723 if (condition.getAsBoolean()) {
724 return;
725 }
726 try {
727 Thread.sleep(100);
728 } catch (InterruptedException e) {
729 throw new RuntimeException(e);
730 }
731 }
732 fail("Timed out for: " + message);
733 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700734
Makoto Onukid0010c52017-03-30 14:17:35 -0700735 public static final <T> T anyOrNull(Class<T> clazz) {
736 return ArgumentMatchers.argThat(value -> true);
737 }
738
739 public static final String anyStringOrNull() {
740 return ArgumentMatchers.argThat(value -> true);
741 }
742
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700743 public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) {
744 return new ShortcutListAsserter(list);
745 }
746
Makoto Onuki2d895c32016-12-02 15:48:40 -0800747 public static ShortcutListAsserter assertWith(ShortcutInfo... list) {
748 return assertWith(list(list));
749 }
750
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700751 /**
752 * New style assertion that allows chained calls.
753 */
754 public static class ShortcutListAsserter {
Makoto Onukidf6da042016-06-16 09:51:40 -0700755 private final ShortcutListAsserter mOriginal;
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700756 private final List<ShortcutInfo> mList;
757
758 ShortcutListAsserter(List<ShortcutInfo> list) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700759 this(null, list);
760 }
761
762 private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) {
Makoto Onukib08790c2016-06-23 14:05:46 -0700763 mOriginal = (original == null) ? this : original;
764 mList = (list == null) ? new ArrayList<>(0) : new ArrayList<>(list);
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700765 }
766
Makoto Onukidf6da042016-06-16 09:51:40 -0700767 public ShortcutListAsserter revertToOriginalList() {
768 return mOriginal;
769 }
770
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700771 public ShortcutListAsserter selectDynamic() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700772 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700773 filter(mList, ShortcutInfo::isDynamic));
774 }
775
776 public ShortcutListAsserter selectManifest() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700777 return new ShortcutListAsserter(this,
Makoto Onukib5a012f2016-06-21 11:13:53 -0700778 filter(mList, ShortcutInfo::isDeclaredInManifest));
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700779 }
780
781 public ShortcutListAsserter selectPinned() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700782 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700783 filter(mList, ShortcutInfo::isPinned));
784 }
785
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800786 public ShortcutListAsserter selectFloating() {
787 return new ShortcutListAsserter(this,
788 filter(mList, (si -> si.isPinned()
789 && !(si.isDynamic() || si.isDeclaredInManifest()))));
790 }
791
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700792 public ShortcutListAsserter selectByActivity(ComponentName activity) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700793 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700794 ShortcutManagerTestUtils.filterByActivity(mList, activity));
795 }
796
797 public ShortcutListAsserter selectByChangedSince(long time) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700798 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700799 ShortcutManagerTestUtils.changedSince(mList, time));
800 }
801
Makoto Onukidf6da042016-06-16 09:51:40 -0700802 public ShortcutListAsserter selectByIds(String... ids) {
803 final Set<String> idSet = set(ids);
804 final ArrayList<ShortcutInfo> selected = new ArrayList<>();
805 for (ShortcutInfo si : mList) {
806 if (idSet.contains(si.getId())) {
807 selected.add(si);
808 idSet.remove(si.getId());
809 }
810 }
811 if (idSet.size() > 0) {
812 fail("Shortcuts not found for IDs=" + idSet);
813 }
814
815 return new ShortcutListAsserter(this, selected);
816 }
817
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700818 public ShortcutListAsserter toSortByRank() {
Makoto Onukidf6da042016-06-16 09:51:40 -0700819 return new ShortcutListAsserter(this,
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700820 ShortcutManagerTestUtils.sortedByRank(mList));
821 }
822
Makoto Onukidf6da042016-06-16 09:51:40 -0700823 public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) {
824 c.accept(mList);
825 return this;
826 }
827
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700828 public ShortcutListAsserter haveIds(String... expectedIds) {
829 assertShortcutIds(mList, expectedIds);
830 return this;
831 }
832
833 public ShortcutListAsserter haveIdsOrdered(String... expectedIds) {
834 assertShortcutIdsOrdered(mList, expectedIds);
835 return this;
836 }
837
838 private ShortcutListAsserter haveSequentialRanks() {
839 for (int i = 0; i < mList.size(); i++) {
Makoto Onukidf6da042016-06-16 09:51:40 -0700840 final ShortcutInfo si = mList.get(i);
841 assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank());
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700842 }
843 return this;
844 }
845
846 public ShortcutListAsserter haveRanksInOrder(String... expectedIds) {
847 toSortByRank()
848 .haveSequentialRanks()
849 .haveIdsOrdered(expectedIds);
850 return this;
851 }
852
853 public ShortcutListAsserter isEmpty() {
854 assertEquals(0, mList.size());
855 return this;
856 }
Makoto Onukidf6da042016-06-16 09:51:40 -0700857
858 public ShortcutListAsserter areAllDynamic() {
859 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic()));
860 return this;
861 }
862
863 public ShortcutListAsserter areAllNotDynamic() {
864 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic()));
865 return this;
866 }
867
868 public ShortcutListAsserter areAllPinned() {
869 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned()));
870 return this;
871 }
872
873 public ShortcutListAsserter areAllNotPinned() {
874 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned()));
875 return this;
876 }
877
878 public ShortcutListAsserter areAllManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700879 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700880 return this;
881 }
882
883 public ShortcutListAsserter areAllNotManifest() {
Makoto Onukib5a012f2016-06-21 11:13:53 -0700884 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDeclaredInManifest()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700885 return this;
886 }
887
888 public ShortcutListAsserter areAllImmutable() {
889 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable()));
890 return this;
891 }
892
893 public ShortcutListAsserter areAllMutable() {
894 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable()));
895 return this;
896 }
897
898 public ShortcutListAsserter areAllEnabled() {
899 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700900 areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED);
Makoto Onukidf6da042016-06-16 09:51:40 -0700901 return this;
902 }
903
904 public ShortcutListAsserter areAllDisabled() {
905 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700906 forAllShortcuts(s -> assertNotEquals("id=" + s.getId(),
907 ShortcutInfo.DISABLED_REASON_NOT_DISABLED, s.getDisabledReason()));
Makoto Onukidf6da042016-06-16 09:51:40 -0700908 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 Onukia4f89b12017-10-05 10:37:55 -0700935 public ShortcutListAsserter areAllVisibleToPublisher() {
936 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isVisibleToPublisher()));
937 return this;
938 }
939
940 public ShortcutListAsserter areAllNotVisibleToPublisher() {
941 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isVisibleToPublisher()));
942 return this;
943 }
944
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700945 public ShortcutListAsserter areAllWithKeyFieldsOnly() {
946 forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly()));
947 return this;
948 }
949
950 public ShortcutListAsserter areAllNotWithKeyFieldsOnly() {
951 forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly()));
952 return this;
953 }
954
Makoto Onukib08790c2016-06-23 14:05:46 -0700955 public ShortcutListAsserter areAllWithActivity(ComponentName activity) {
Makoto Onuki255461f2017-01-10 11:47:25 -0800956 forAllShortcuts(s -> assertEquals("id=" + s.getId(), activity, s.getActivity()));
Makoto Onukib08790c2016-06-23 14:05:46 -0700957 return this;
958 }
959
Makoto Onuki106ff7a2016-12-01 10:17:57 -0800960 public ShortcutListAsserter areAllWithNoActivity() {
961 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getActivity()));
962 return this;
963 }
964
Makoto Onukia01f4f02016-12-15 15:58:41 -0800965 public ShortcutListAsserter areAllWithIntent() {
966 forAllShortcuts(s -> assertNotNull("id=" + s.getId(), s.getIntent()));
967 return this;
968 }
969
970 public ShortcutListAsserter areAllWithNoIntent() {
971 forAllShortcuts(s -> assertNull("id=" + s.getId(), s.getIntent()));
972 return this;
973 }
974
Makoto Onukia4f89b12017-10-05 10:37:55 -0700975 public ShortcutListAsserter areAllWithDisabledReason(int disabledReason) {
976 forAllShortcuts(s -> assertEquals("id=" + s.getId(),
977 disabledReason, s.getDisabledReason()));
Makoto Onukib1588c02017-10-12 15:11:45 -0700978 if (disabledReason >= ShortcutInfo.DISABLED_REASON_VERSION_LOWER) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700979 areAllNotVisibleToPublisher();
Makoto Onukib1588c02017-10-12 15:11:45 -0700980 } else {
981 areAllVisibleToPublisher();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700982 }
983 return this;
984 }
985
Makoto Onukidf6da042016-06-16 09:51:40 -0700986 public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700987 boolean found = false;
Makoto Onukidf6da042016-06-16 09:51:40 -0700988 for (int i = 0; i < mList.size(); i++) {
989 final ShortcutInfo si = mList.get(i);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700990 found = true;
Makoto Onukidf6da042016-06-16 09:51:40 -0700991 sa.accept(si);
992 }
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700993 assertTrue("No shortcuts found.", found);
Makoto Onukidf6da042016-06-16 09:51:40 -0700994 return this;
995 }
996
997 public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
998 Consumer<ShortcutInfo> sa) {
999 boolean found = false;
1000 for (int i = 0; i < mList.size(); i++) {
1001 final ShortcutInfo si = mList.get(i);
1002 if (p.test(si)) {
1003 found = true;
1004 try {
1005 sa.accept(si);
1006 } catch (Throwable e) {
1007 throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
1008 }
1009 }
1010 }
1011 assertTrue("Shortcut with the given condition not found.", found);
1012 return this;
1013 }
1014
1015 public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
1016 forShortcut(si -> si.getId().equals(id), sa);
1017
1018 return this;
1019 }
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001020 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07001021
1022 public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) {
1023 if (b1 == null && b2 == null) {
1024 return; // pass
1025 }
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001026 assertNotNull("b1 is null but b2 is not", b1);
1027 assertNotNull("b2 is null but b1 is not", b2);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001028
1029 // HashSet makes the error message readable.
1030 assertEquals(set(b1.keySet()), set(b2.keySet()));
1031
1032 for (String key : b1.keySet()) {
1033 final Object v1 = b1.get(key);
1034 final Object v2 = b2.get(key);
1035 if (v1 == null) {
1036 if (v2 == null) {
1037 return;
1038 }
1039 }
1040 if (v1.equals(v2)) {
1041 return;
1042 }
1043
1044 assertTrue("Only either value is null: key=" + key
1045 + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null);
1046 assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass());
1047
1048 if (v1 instanceof BaseBundle) {
1049 assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2);
1050
1051 } else if (v1 instanceof boolean[]) {
1052 assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2));
1053
1054 } else if (v1 instanceof int[]) {
1055 MoreAsserts.assertEquals((int[]) v1, (int[]) v2);
1056
1057 } else if (v1 instanceof double[]) {
1058 MoreAsserts.assertEquals((double[]) v1, (double[]) v2);
1059
1060 } else if (v1 instanceof String[]) {
1061 MoreAsserts.assertEquals((String[]) v1, (String[]) v2);
1062
1063 } else if (v1 instanceof Double) {
1064 if (((Double) v1).isNaN()) {
1065 assertTrue(((Double) v2).isNaN());
1066 } else {
1067 assertEquals(v1, v2);
1068 }
1069
1070 } else {
1071 assertEquals(v1, v2);
1072 }
1073 }
1074 }
Makoto Onukib08790c2016-06-23 14:05:46 -07001075
1076 public static void waitOnMainThread() throws InterruptedException {
1077 final CountDownLatch latch = new CountDownLatch(1);
1078
1079 new Handler(Looper.getMainLooper()).post(() -> latch.countDown());
1080
1081 latch.await();
1082 }
1083
1084 public static class LauncherCallbackAsserter {
1085 private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class);
1086
1087 private Callback getMockCallback() {
1088 return mCallback;
1089 }
1090
1091 public LauncherCallbackAsserter assertNoCallbackCalled() {
1092 verify(mCallback, times(0)).onShortcutsChanged(
1093 anyString(),
1094 any(List.class),
1095 any(UserHandle.class));
1096 return this;
1097 }
1098
1099 public LauncherCallbackAsserter assertNoCallbackCalledForPackage(
1100 String publisherPackageName) {
1101 verify(mCallback, times(0)).onShortcutsChanged(
1102 eq(publisherPackageName),
1103 any(List.class),
1104 any(UserHandle.class));
1105 return this;
1106 }
1107
1108 public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser(
1109 String publisherPackageName, UserHandle publisherUserHandle) {
1110 verify(mCallback, times(0)).onShortcutsChanged(
1111 eq(publisherPackageName),
1112 any(List.class),
1113 eq(publisherUserHandle));
1114 return this;
1115 }
1116
1117 public ShortcutListAsserter assertCallbackCalledForPackageAndUser(
1118 String publisherPackageName, UserHandle publisherUserHandle) {
1119 final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class);
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001120 verify(mCallback, atLeastOnce()).onShortcutsChanged(
Makoto Onukib08790c2016-06-23 14:05:46 -07001121 eq(publisherPackageName),
1122 shortcuts.capture(),
1123 eq(publisherUserHandle));
1124 return new ShortcutListAsserter(shortcuts.getValue());
1125 }
1126 }
1127
1128 public static LauncherCallbackAsserter assertForLauncherCallback(
1129 LauncherApps launcherApps, Runnable body) throws InterruptedException {
1130 final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter();
1131 launcherApps.registerCallback(asserter.getMockCallback(),
1132 new Handler(Looper.getMainLooper()));
1133
1134 body.run();
1135
1136 waitOnMainThread();
1137
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001138 // TODO unregister doesn't work well during unit tests. Figure out and fix it.
1139 // launcherApps.unregisterCallback(asserter.getMockCallback());
Makoto Onuki4e6cef42016-07-13 16:14:01 -07001140
Makoto Onukib08790c2016-06-23 14:05:46 -07001141 return asserter;
1142 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001143
Makoto Onukia01f4f02016-12-15 15:58:41 -08001144 public static LauncherCallbackAsserter assertForLauncherCallbackNoThrow(
1145 LauncherApps launcherApps, Runnable body) {
1146 try {
1147 return assertForLauncherCallback(launcherApps, body);
1148 } catch (InterruptedException e) {
1149 fail("Caught InterruptedException");
1150 return null; // Never happens.
1151 }
1152 }
1153
Makoto Onuki9c850012016-07-26 15:50:50 -07001154 public static void retryUntil(BooleanSupplier checker, String message) {
Makoto Onukid67bf6a2016-08-19 09:10:08 -07001155 retryUntil(checker, message, 30);
1156 }
1157
1158 public static void retryUntil(BooleanSupplier checker, String message, long timeoutSeconds) {
1159 final long timeOut = System.currentTimeMillis() + timeoutSeconds * 1000;
Makoto Onuki9c850012016-07-26 15:50:50 -07001160 while (!checker.getAsBoolean()) {
Makoto Onukia210ccf2016-07-27 14:08:48 -07001161 if (System.currentTimeMillis() > timeOut) {
1162 break;
1163 }
Makoto Onuki9c850012016-07-26 15:50:50 -07001164 try {
1165 Thread.sleep(200);
1166 } catch (InterruptedException ignore) {
1167 }
1168 }
1169 assertTrue(message, checker.getAsBoolean());
1170 }
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001171}