blob: ad49c2ff78e8f982c6b65d5a2e38d55fbf8b9d2d [file] [log] [blame]
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.pm.shortcutmanagertest;
17
18import static junit.framework.Assert.assertEquals;
19import static junit.framework.Assert.assertFalse;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
22import static junit.framework.Assert.assertTrue;
23import static junit.framework.Assert.fail;
24
25import static org.mockito.Matchers.any;
26import static org.mockito.Matchers.anyList;
27import static org.mockito.Matchers.anyString;
28import static org.mockito.Matchers.eq;
29import static org.mockito.Mockito.reset;
30import static org.mockito.Mockito.times;
31import static org.mockito.Mockito.verify;
32
33import android.app.Instrumentation;
34import android.content.Context;
35import android.content.pm.LauncherApps;
36import android.content.pm.ShortcutInfo;
37import android.graphics.Bitmap;
38import android.graphics.BitmapFactory;
39import android.os.BaseBundle;
40import android.os.Bundle;
41import android.os.ParcelFileDescriptor;
42import android.os.UserHandle;
43import android.test.MoreAsserts;
44import android.util.Log;
45
46import junit.framework.Assert;
47
48import org.hamcrest.BaseMatcher;
49import org.hamcrest.Description;
50import org.hamcrest.Matcher;
51import org.mockito.Mockito;
52
53import java.io.BufferedReader;
54import java.io.FileReader;
55import java.io.IOException;
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.Collection;
59import java.util.HashSet;
60import java.util.List;
61import java.util.Set;
62import java.util.function.BooleanSupplier;
63import java.util.function.Function;
64import java.util.function.Predicate;
65
66public class ShortcutManagerTestUtils {
67 private static final String TAG = "ShortcutManagerUtils";
68
69 private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true
70
71 private static final int STANDARD_TIMEOUT_SEC = 5;
72
73 private ShortcutManagerTestUtils() {
74 }
75
76 private static List<String> readAll(ParcelFileDescriptor pfd) {
77 try {
78 try {
79 final ArrayList<String> ret = new ArrayList<>();
80 try (BufferedReader r = new BufferedReader(
81 new FileReader(pfd.getFileDescriptor()))) {
82 String line;
83 while ((line = r.readLine()) != null) {
84 ret.add(line);
85 }
86 r.readLine();
87 }
88 return ret;
89 } finally {
90 pfd.close();
91 }
92 } catch (IOException e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 private static String concatResult(List<String> result) {
98 final StringBuilder sb = new StringBuilder();
99 for (String s : result) {
100 sb.append(s);
101 sb.append("\n");
102 }
103 return sb.toString();
104 }
105
106 private static List<String> runCommand(Instrumentation instrumentation, String command) {
107 return runCommand(instrumentation, command, null);
108 }
109 private static List<String> runCommand(Instrumentation instrumentation, String command,
110 Predicate<List<String>> resultAsserter) {
111 Log.d(TAG, "Running command: " + command);
112 final List<String> result;
113 try {
114 result = readAll(
115 instrumentation.getUiAutomation().executeShellCommand(command));
116 } catch (Exception e) {
117 throw new RuntimeException(e);
118 }
119 if (resultAsserter != null && !resultAsserter.test(result)) {
120 fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
121 }
122 return result;
123 }
124
125 private static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
126 runCommand(instrumentation, command, result -> result.size() == 0);
127 }
128
129 private static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
130 Predicate<List<String>> resultAsserter) {
131 return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
132 }
133
134 public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
135 String command) {
136 return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
137 }
138
139 public static String getDefaultLauncher(Instrumentation instrumentation) {
140 final String PREFIX = "Launcher: ComponentInfo{";
141 final String POSTFIX = "}";
142 final List<String> result = runShortcutCommandForSuccess(
143 instrumentation, "get-default-launcher");
144 for (String s : result) {
145 if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
146 return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
147 }
148 }
149 fail("Default launcher not found");
150 return null;
151 }
152
153 public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
154 runCommandForNoOutput(instrumentation, "cmd package set-home-activity " + component);
155 }
156
157 public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
158 setDefaultLauncher(instrumentation, packageContext.getPackageName()
159 + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
160 }
161
162 public static void overrideConfig(Instrumentation instrumentation, String config) {
163 runShortcutCommandForSuccess(instrumentation, "override-config " + config);
164 }
165
166 public static void resetConfig(Instrumentation instrumentation) {
167 runShortcutCommandForSuccess(instrumentation, "reset-config");
168 }
169
170 public static void resetThrottling(Instrumentation instrumentation) {
171 runShortcutCommandForSuccess(instrumentation, "reset-throttling");
172 }
173
174 public static void resetAllThrottling(Instrumentation instrumentation) {
175 runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
176 }
177
178 public static void clearShortcuts(Instrumentation instrumentation, int userId,
179 String packageName) {
180 runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
181 + " --user " + userId + " " + packageName);
182 }
183
184 public static void dumpsysShortcut(Instrumentation instrumentation) {
185 if (!ENABLE_DUMPSYS) {
186 return;
187 }
188 for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
189 Log.e(TAG, s);
190 }
191 }
192
193 public static Bundle makeBundle(Object... keysAndValues) {
194 assertTrue((keysAndValues.length % 2) == 0);
195
196 if (keysAndValues.length == 0) {
197 return null;
198 }
199 final Bundle ret = new Bundle();
200
201 for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
202 final String key = keysAndValues[i].toString();
203 final Object value = keysAndValues[i + 1];
204
205 if (value == null) {
206 ret.putString(key, null);
207 } else if (value instanceof Integer) {
208 ret.putInt(key, (Integer) value);
209 } else if (value instanceof String) {
210 ret.putString(key, (String) value);
211 } else if (value instanceof Bundle) {
212 ret.putBundle(key, (Bundle) value);
213 } else {
214 fail("Type not supported yet: " + value.getClass().getName());
215 }
216 }
217 return ret;
218 }
219
220 public static <T> List<T> list(T... array) {
221 return Arrays.asList(array);
222 }
223
224 public static <T> Set<T> hashSet(Set<T> in) {
225 return new HashSet<T>(in);
226 }
227
228 public static <T> Set<T> set(T... values) {
229 return set(v -> v, values);
230 }
231
232 public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
233 return set(converter, Arrays.asList(values));
234 }
235
236 public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
237 final HashSet<T> ret = new HashSet<>();
238 for (V v : values) {
239 ret.add(converter.apply(v));
240 }
241 return ret;
242 }
243
244 public static void resetAll(Collection<?> mocks) {
245 for (Object o : mocks) {
246 reset(o);
247 }
248 }
249 public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
250 String expectedExceptionMessageRegex, Runnable r) {
251 assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
252 }
253
254 public static void assertDynamicShortcutCountExceeded(Runnable r) {
255 assertExpectException(IllegalArgumentException.class,
256 "Max number of dynamic shortcuts exceeded", r);
257 }
258
259 public static void assertExpectException(String message,
260 Class<? extends Throwable> expectedExceptionType,
261 String expectedExceptionMessageRegex, Runnable r) {
262 try {
263 r.run();
264 Assert.fail("Expected exception type " + expectedExceptionType.getName()
265 + " was not thrown (message=" + message + ")");
266 } catch (Throwable e) {
267 Assert.assertTrue(
268 "Expected exception type was " + expectedExceptionType.getName()
269 + " but caught " + e + " (message=" + message + ")",
270 expectedExceptionType.isAssignableFrom(e.getClass()));
271 if (expectedExceptionMessageRegex != null) {
272 MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
273 }
274 }
275 }
276
277 public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
278 String... expectedIds) {
279 final HashSet<String> expected = new HashSet<>(list(expectedIds));
280 final HashSet<String> actual = new HashSet<>();
281 for (ShortcutInfo s : actualShortcuts) {
282 actual.add(s.getId());
283 }
284
285 // Compare the sets.
286 assertEquals(expected, actual);
287 return actualShortcuts;
288 }
289
290 public static List<ShortcutInfo> assertAllHaveIntents(
291 List<ShortcutInfo> actualShortcuts) {
292 for (ShortcutInfo s : actualShortcuts) {
293 assertNotNull("ID " + s.getId(), s.getIntent());
294 }
295 return actualShortcuts;
296 }
297
298 public static List<ShortcutInfo> assertAllNotHaveIntents(
299 List<ShortcutInfo> actualShortcuts) {
300 for (ShortcutInfo s : actualShortcuts) {
301 assertNull("ID " + s.getId(), s.getIntent());
302 }
303 return actualShortcuts;
304 }
305
306 public static List<ShortcutInfo> assertAllHaveTitle(
307 List<ShortcutInfo> actualShortcuts) {
308 for (ShortcutInfo s : actualShortcuts) {
309 assertNotNull("ID " + s.getId(), s.getTitle());
310 }
311 return actualShortcuts;
312 }
313
314 public static List<ShortcutInfo> assertAllNotHaveTitle(
315 List<ShortcutInfo> actualShortcuts) {
316 for (ShortcutInfo s : actualShortcuts) {
317 assertNull("ID " + s.getId(), s.getTitle());
318 }
319 return actualShortcuts;
320 }
321
322 public static List<ShortcutInfo> assertAllHaveIconResId(
323 List<ShortcutInfo> actualShortcuts) {
324 for (ShortcutInfo s : actualShortcuts) {
325 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
326 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
327 }
328 return actualShortcuts;
329 }
330
331 public static List<ShortcutInfo> assertAllHaveIconFile(
332 List<ShortcutInfo> actualShortcuts) {
333 for (ShortcutInfo s : actualShortcuts) {
334 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
335 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
336 }
337 return actualShortcuts;
338 }
339
340 public static List<ShortcutInfo> assertAllHaveIcon(
341 List<ShortcutInfo> actualShortcuts) {
342 for (ShortcutInfo s : actualShortcuts) {
343 assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource());
344 }
345 return actualShortcuts;
346 }
347
348 public static List<ShortcutInfo> assertAllKeyFieldsOnly(
349 List<ShortcutInfo> actualShortcuts) {
350 for (ShortcutInfo s : actualShortcuts) {
351 assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
352 }
353 return actualShortcuts;
354 }
355
356 public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
357 List<ShortcutInfo> actualShortcuts) {
358 for (ShortcutInfo s : actualShortcuts) {
359 assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
360 }
361 return actualShortcuts;
362 }
363
364 public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
365 for (ShortcutInfo s : actualShortcuts) {
366 assertTrue("ID " + s.getId(), s.isDynamic());
367 }
368 return actualShortcuts;
369 }
370
371 public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
372 for (ShortcutInfo s : actualShortcuts) {
373 assertTrue("ID " + s.getId(), s.isPinned());
374 }
375 return actualShortcuts;
376 }
377
378 public static List<ShortcutInfo> assertAllDynamicOrPinned(
379 List<ShortcutInfo> actualShortcuts) {
380 for (ShortcutInfo s : actualShortcuts) {
381 assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
382 }
383 return actualShortcuts;
384 }
385
386 public static void assertDynamicOnly(ShortcutInfo si) {
387 assertTrue(si.isDynamic());
388 assertFalse(si.isPinned());
389 }
390
391 public static void assertPinnedOnly(ShortcutInfo si) {
392 assertFalse(si.isDynamic());
393 assertTrue(si.isPinned());
394 }
395
396 public static void assertDynamicAndPinned(ShortcutInfo si) {
397 assertTrue(si.isDynamic());
398 assertTrue(si.isPinned());
399 }
400
401 public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
402 assertEquals("width", expectedWidth, bitmap.getWidth());
403 assertEquals("height", expectedHeight, bitmap.getHeight());
404 }
405
406 public static <T> void assertAllUnique(Collection<T> list) {
407 final Set<Object> set = new HashSet<>();
408 for (T item : list) {
409 if (set.contains(item)) {
410 fail("Duplicate item found: " + item + " (in the list: " + list + ")");
411 }
412 set.add(item);
413 }
414 }
415
Makoto Onuki39686e82016-04-13 18:03:00 -0700416 public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
417 for (ShortcutInfo si : list) {
418 if (si.getId().equals(id)) {
419 return si;
420 }
421 }
422 fail("Shortcut " + id + " not found in the list");
423 return null;
424 }
425
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700426 public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
427 assertNotNull(pfd);
428 try {
429 try {
430 return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
431 } finally {
432 pfd.close();
433 }
434 } catch (IOException e) {
435 throw new RuntimeException(e);
436 }
437 }
438
439 public static void assertBundleEmpty(BaseBundle b) {
440 assertTrue(b == null || b.size() == 0);
441 }
442
443 public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
444 verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
445 any(UserHandle.class));
446 }
447
448 public static void assertCallbackReceived(LauncherApps.Callback mock,
449 UserHandle user, String packageName, String... ids) {
450 verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
451 eq(user));
452 }
453
454 public static boolean checkAssertSuccess(Runnable r) {
455 try {
456 r.run();
457 return true;
458 } catch (AssertionError e) {
459 return false;
460 }
461 }
462
463 public static <T> T checkArgument(Predicate<T> checker, String description,
464 List<T> matchedCaptor) {
465 final Matcher<T> m = new BaseMatcher<T>() {
466 @Override
467 public boolean matches(Object item) {
468 if (item == null) {
469 return false;
470 }
471 final T value = (T) item;
472 if (!checker.test(value)) {
473 return false;
474 }
475
476 if (matchedCaptor != null) {
477 matchedCaptor.add(value);
478 }
479 return true;
480 }
481
482 @Override
483 public void describeTo(Description d) {
484 d.appendText(description);
485 }
486 };
487 return Mockito.argThat(m);
488 }
489
490 public static List<ShortcutInfo> checkShortcutIds(String... ids) {
491 return checkArgument((List<ShortcutInfo> list) -> {
492 final Set<String> actualSet = set(si -> si.getId(), list);
493 return actualSet.equals(set(ids));
494
495 }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
496 }
497
498 public static void waitUntil(String message, BooleanSupplier condition) {
499 waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
500 }
501
502 public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
503 final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
504 while (System.currentTimeMillis() < timeout) {
505 if (condition.getAsBoolean()) {
506 return;
507 }
508 try {
509 Thread.sleep(100);
510 } catch (InterruptedException e) {
511 throw new RuntimeException(e);
512 }
513 }
514 fail("Timed out for: " + message);
515 }
516}