Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.pm.shortcutmanagertest; |
| 17 | |
| 18 | import static junit.framework.Assert.assertEquals; |
| 19 | import static junit.framework.Assert.assertFalse; |
| 20 | import static junit.framework.Assert.assertNotNull; |
| 21 | import static junit.framework.Assert.assertNull; |
| 22 | import static junit.framework.Assert.assertTrue; |
| 23 | import static junit.framework.Assert.fail; |
| 24 | |
| 25 | import static org.mockito.Matchers.any; |
| 26 | import static org.mockito.Matchers.anyList; |
| 27 | import static org.mockito.Matchers.anyString; |
| 28 | import static org.mockito.Matchers.eq; |
| 29 | import static org.mockito.Mockito.reset; |
| 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | |
| 33 | import android.app.Instrumentation; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 35 | import android.content.Context; |
| 36 | import android.content.pm.LauncherApps; |
| 37 | import android.content.pm.ShortcutInfo; |
| 38 | import android.graphics.Bitmap; |
| 39 | import android.graphics.BitmapFactory; |
| 40 | import android.os.BaseBundle; |
| 41 | import android.os.Bundle; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 42 | import android.os.Parcel; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 43 | import android.os.ParcelFileDescriptor; |
| 44 | import android.os.UserHandle; |
| 45 | import android.test.MoreAsserts; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 46 | import android.util.ArraySet; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 47 | import android.util.Log; |
| 48 | |
| 49 | import junit.framework.Assert; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 50 | import junit.framework.AssertionFailedError; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 51 | |
| 52 | import org.hamcrest.BaseMatcher; |
| 53 | import org.hamcrest.Description; |
| 54 | import org.hamcrest.Matcher; |
| 55 | import org.mockito.Mockito; |
| 56 | |
| 57 | import java.io.BufferedReader; |
| 58 | import java.io.FileReader; |
| 59 | import java.io.IOException; |
| 60 | import java.util.ArrayList; |
| 61 | import java.util.Arrays; |
| 62 | import java.util.Collection; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 63 | import java.util.Collections; |
| 64 | import java.util.Comparator; |
| 65 | import java.util.LinkedHashSet; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 66 | import java.util.List; |
| 67 | import java.util.Set; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 68 | import java.util.SortedSet; |
| 69 | import java.util.TreeSet; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 70 | import java.util.function.BooleanSupplier; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 71 | import java.util.function.Consumer; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 72 | import java.util.function.Function; |
| 73 | import java.util.function.Predicate; |
| 74 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Common utility methods for ShortcutManager tests. This is used by both CTS and the unit tests. |
| 77 | * Because it's used by CTS too, it can only access the public APIs. |
| 78 | */ |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 79 | public class ShortcutManagerTestUtils { |
| 80 | private static final String TAG = "ShortcutManagerUtils"; |
| 81 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 82 | private static final boolean ENABLE_DUMPSYS = false; // DO NOT SUBMIT WITH true |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 83 | |
| 84 | private static final int STANDARD_TIMEOUT_SEC = 5; |
| 85 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 86 | private static final String[] EMPTY_STRINGS = new String[0]; |
| 87 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 88 | private ShortcutManagerTestUtils() { |
| 89 | } |
| 90 | |
| 91 | private static List<String> readAll(ParcelFileDescriptor pfd) { |
| 92 | try { |
| 93 | try { |
| 94 | final ArrayList<String> ret = new ArrayList<>(); |
| 95 | try (BufferedReader r = new BufferedReader( |
| 96 | new FileReader(pfd.getFileDescriptor()))) { |
| 97 | String line; |
| 98 | while ((line = r.readLine()) != null) { |
| 99 | ret.add(line); |
| 100 | } |
| 101 | r.readLine(); |
| 102 | } |
| 103 | return ret; |
| 104 | } finally { |
| 105 | pfd.close(); |
| 106 | } |
| 107 | } catch (IOException e) { |
| 108 | throw new RuntimeException(e); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private static String concatResult(List<String> result) { |
| 113 | final StringBuilder sb = new StringBuilder(); |
| 114 | for (String s : result) { |
| 115 | sb.append(s); |
| 116 | sb.append("\n"); |
| 117 | } |
| 118 | return sb.toString(); |
| 119 | } |
| 120 | |
| 121 | private static List<String> runCommand(Instrumentation instrumentation, String command) { |
| 122 | return runCommand(instrumentation, command, null); |
| 123 | } |
| 124 | private static List<String> runCommand(Instrumentation instrumentation, String command, |
| 125 | Predicate<List<String>> resultAsserter) { |
| 126 | Log.d(TAG, "Running command: " + command); |
| 127 | final List<String> result; |
| 128 | try { |
| 129 | result = readAll( |
| 130 | instrumentation.getUiAutomation().executeShellCommand(command)); |
| 131 | } catch (Exception e) { |
| 132 | throw new RuntimeException(e); |
| 133 | } |
| 134 | if (resultAsserter != null && !resultAsserter.test(result)) { |
| 135 | fail("Command '" + command + "' failed, output was:\n" + concatResult(result)); |
| 136 | } |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | private static void runCommandForNoOutput(Instrumentation instrumentation, String command) { |
| 141 | runCommand(instrumentation, command, result -> result.size() == 0); |
| 142 | } |
| 143 | |
| 144 | private static List<String> runShortcutCommand(Instrumentation instrumentation, String command, |
| 145 | Predicate<List<String>> resultAsserter) { |
| 146 | return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter); |
| 147 | } |
| 148 | |
| 149 | public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation, |
| 150 | String command) { |
| 151 | return runShortcutCommand(instrumentation, command, result -> result.contains("Success")); |
| 152 | } |
| 153 | |
| 154 | public static String getDefaultLauncher(Instrumentation instrumentation) { |
| 155 | final String PREFIX = "Launcher: ComponentInfo{"; |
| 156 | final String POSTFIX = "}"; |
| 157 | final List<String> result = runShortcutCommandForSuccess( |
| 158 | instrumentation, "get-default-launcher"); |
| 159 | for (String s : result) { |
| 160 | if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) { |
| 161 | return s.substring(PREFIX.length(), s.length() - POSTFIX.length()); |
| 162 | } |
| 163 | } |
| 164 | fail("Default launcher not found"); |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | public static void setDefaultLauncher(Instrumentation instrumentation, String component) { |
| 169 | runCommandForNoOutput(instrumentation, "cmd package set-home-activity " + component); |
| 170 | } |
| 171 | |
| 172 | public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) { |
| 173 | setDefaultLauncher(instrumentation, packageContext.getPackageName() |
| 174 | + "/android.content.pm.cts.shortcutmanager.packages.Launcher"); |
| 175 | } |
| 176 | |
| 177 | public static void overrideConfig(Instrumentation instrumentation, String config) { |
| 178 | runShortcutCommandForSuccess(instrumentation, "override-config " + config); |
| 179 | } |
| 180 | |
| 181 | public static void resetConfig(Instrumentation instrumentation) { |
| 182 | runShortcutCommandForSuccess(instrumentation, "reset-config"); |
| 183 | } |
| 184 | |
| 185 | public static void resetThrottling(Instrumentation instrumentation) { |
| 186 | runShortcutCommandForSuccess(instrumentation, "reset-throttling"); |
| 187 | } |
| 188 | |
| 189 | public static void resetAllThrottling(Instrumentation instrumentation) { |
| 190 | runShortcutCommandForSuccess(instrumentation, "reset-all-throttling"); |
| 191 | } |
| 192 | |
| 193 | public static void clearShortcuts(Instrumentation instrumentation, int userId, |
| 194 | String packageName) { |
| 195 | runShortcutCommandForSuccess(instrumentation, "clear-shortcuts " |
| 196 | + " --user " + userId + " " + packageName); |
| 197 | } |
| 198 | |
| 199 | public static void dumpsysShortcut(Instrumentation instrumentation) { |
| 200 | if (!ENABLE_DUMPSYS) { |
| 201 | return; |
| 202 | } |
| 203 | for (String s : runCommand(instrumentation, "dumpsys shortcut")) { |
| 204 | Log.e(TAG, s); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public static Bundle makeBundle(Object... keysAndValues) { |
| 209 | assertTrue((keysAndValues.length % 2) == 0); |
| 210 | |
| 211 | if (keysAndValues.length == 0) { |
| 212 | return null; |
| 213 | } |
| 214 | final Bundle ret = new Bundle(); |
| 215 | |
| 216 | for (int i = keysAndValues.length - 2; i >= 0; i -= 2) { |
| 217 | final String key = keysAndValues[i].toString(); |
| 218 | final Object value = keysAndValues[i + 1]; |
| 219 | |
| 220 | if (value == null) { |
| 221 | ret.putString(key, null); |
| 222 | } else if (value instanceof Integer) { |
| 223 | ret.putInt(key, (Integer) value); |
| 224 | } else if (value instanceof String) { |
| 225 | ret.putString(key, (String) value); |
| 226 | } else if (value instanceof Bundle) { |
| 227 | ret.putBundle(key, (Bundle) value); |
| 228 | } else { |
| 229 | fail("Type not supported yet: " + value.getClass().getName()); |
| 230 | } |
| 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | public static <T> List<T> list(T... array) { |
| 236 | return Arrays.asList(array); |
| 237 | } |
| 238 | |
| 239 | public static <T> Set<T> hashSet(Set<T> in) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 240 | return new LinkedHashSet<>(in); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | public static <T> Set<T> set(T... values) { |
| 244 | return set(v -> v, values); |
| 245 | } |
| 246 | |
| 247 | public static <T, V> Set<T> set(Function<V, T> converter, V... values) { |
| 248 | return set(converter, Arrays.asList(values)); |
| 249 | } |
| 250 | |
| 251 | public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 252 | final LinkedHashSet<T> ret = new LinkedHashSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 253 | for (V v : values) { |
| 254 | ret.add(converter.apply(v)); |
| 255 | } |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | public static void resetAll(Collection<?> mocks) { |
| 260 | for (Object o : mocks) { |
| 261 | reset(o); |
| 262 | } |
| 263 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 264 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 265 | public static <T extends Collection<?>> T assertEmpty(T collection) { |
| 266 | if (collection == null) { |
| 267 | return collection; // okay. |
| 268 | } |
| 269 | assertEquals(0, collection.size()); |
| 270 | return collection; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 273 | public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) { |
| 274 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(list); |
| 275 | ret.removeIf(si -> !p.test(si)); |
| 276 | return ret; |
| 277 | } |
| 278 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 279 | public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list, |
| 280 | ComponentName activity) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 281 | return filter(list, si -> |
| 282 | (si.getActivity().equals(activity) |
| 283 | && (si.isManifestShortcut() || si.isDynamic()))); |
| 284 | } |
| 285 | |
| 286 | public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) { |
| 287 | return filter(list, si -> si.getLastChangedTimestamp() >= time); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 290 | public static void assertExpectException(Class<? extends Throwable> expectedExceptionType, |
| 291 | String expectedExceptionMessageRegex, Runnable r) { |
| 292 | assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r); |
| 293 | } |
| 294 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 295 | public static void assertCannotUpdateImmutable(Runnable r) { |
| 296 | assertExpectException( |
| 297 | IllegalArgumentException.class, "may not be manipulated via APIs", r); |
| 298 | } |
| 299 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 300 | public static void assertDynamicShortcutCountExceeded(Runnable r) { |
| 301 | assertExpectException(IllegalArgumentException.class, |
| 302 | "Max number of dynamic shortcuts exceeded", r); |
| 303 | } |
| 304 | |
| 305 | public static void assertExpectException(String message, |
| 306 | Class<? extends Throwable> expectedExceptionType, |
| 307 | String expectedExceptionMessageRegex, Runnable r) { |
| 308 | try { |
| 309 | r.run(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 310 | } catch (Throwable e) { |
| 311 | Assert.assertTrue( |
| 312 | "Expected exception type was " + expectedExceptionType.getName() |
| 313 | + " but caught " + e + " (message=" + message + ")", |
| 314 | expectedExceptionType.isAssignableFrom(e.getClass())); |
| 315 | if (expectedExceptionMessageRegex != null) { |
| 316 | MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage()); |
| 317 | } |
Makoto Onuki | a1d38b3 | 2016-06-10 15:32:26 -0700 | [diff] [blame] | 318 | return; // Pass |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 319 | } |
Makoto Onuki | a1d38b3 | 2016-06-10 15:32:26 -0700 | [diff] [blame] | 320 | Assert.fail("Expected exception type " + expectedExceptionType.getName() |
| 321 | + " was not thrown"); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts, |
| 325 | String... expectedIds) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 326 | final SortedSet<String> expected = new TreeSet<>(list(expectedIds)); |
| 327 | final SortedSet<String> actual = new TreeSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 328 | for (ShortcutInfo s : actualShortcuts) { |
| 329 | actual.add(s.getId()); |
| 330 | } |
| 331 | |
| 332 | // Compare the sets. |
| 333 | assertEquals(expected, actual); |
| 334 | return actualShortcuts; |
| 335 | } |
| 336 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 337 | public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts, |
| 338 | String... expectedIds) { |
| 339 | final ArrayList<String> expected = new ArrayList<>(list(expectedIds)); |
| 340 | final ArrayList<String> actual = new ArrayList<>(); |
| 341 | for (ShortcutInfo s : actualShortcuts) { |
| 342 | actual.add(s.getId()); |
| 343 | } |
| 344 | assertEquals(expected, actual); |
| 345 | return actualShortcuts; |
| 346 | } |
| 347 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 348 | public static List<ShortcutInfo> assertAllHaveIntents( |
| 349 | List<ShortcutInfo> actualShortcuts) { |
| 350 | for (ShortcutInfo s : actualShortcuts) { |
| 351 | assertNotNull("ID " + s.getId(), s.getIntent()); |
| 352 | } |
| 353 | return actualShortcuts; |
| 354 | } |
| 355 | |
| 356 | public static List<ShortcutInfo> assertAllNotHaveIntents( |
| 357 | List<ShortcutInfo> actualShortcuts) { |
| 358 | for (ShortcutInfo s : actualShortcuts) { |
| 359 | assertNull("ID " + s.getId(), s.getIntent()); |
| 360 | } |
| 361 | return actualShortcuts; |
| 362 | } |
| 363 | |
| 364 | public static List<ShortcutInfo> assertAllHaveTitle( |
| 365 | List<ShortcutInfo> actualShortcuts) { |
| 366 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | eddbfec | 2016-05-31 17:04:34 -0700 | [diff] [blame] | 367 | assertNotNull("ID " + s.getId(), s.getShortLabel()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 368 | } |
| 369 | return actualShortcuts; |
| 370 | } |
| 371 | |
| 372 | public static List<ShortcutInfo> assertAllNotHaveTitle( |
| 373 | List<ShortcutInfo> actualShortcuts) { |
| 374 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | eddbfec | 2016-05-31 17:04:34 -0700 | [diff] [blame] | 375 | assertNull("ID " + s.getId(), s.getShortLabel()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 376 | } |
| 377 | return actualShortcuts; |
| 378 | } |
| 379 | |
| 380 | public static List<ShortcutInfo> assertAllHaveIconResId( |
| 381 | List<ShortcutInfo> actualShortcuts) { |
| 382 | for (ShortcutInfo s : actualShortcuts) { |
| 383 | assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource()); |
| 384 | assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile()); |
| 385 | } |
| 386 | return actualShortcuts; |
| 387 | } |
| 388 | |
| 389 | public static List<ShortcutInfo> assertAllHaveIconFile( |
| 390 | List<ShortcutInfo> actualShortcuts) { |
| 391 | for (ShortcutInfo s : actualShortcuts) { |
| 392 | assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource()); |
| 393 | assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile()); |
| 394 | } |
| 395 | return actualShortcuts; |
| 396 | } |
| 397 | |
| 398 | public static List<ShortcutInfo> assertAllHaveIcon( |
| 399 | List<ShortcutInfo> actualShortcuts) { |
| 400 | for (ShortcutInfo s : actualShortcuts) { |
| 401 | assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource()); |
| 402 | } |
| 403 | return actualShortcuts; |
| 404 | } |
| 405 | |
| 406 | public static List<ShortcutInfo> assertAllKeyFieldsOnly( |
| 407 | List<ShortcutInfo> actualShortcuts) { |
| 408 | for (ShortcutInfo s : actualShortcuts) { |
| 409 | assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly()); |
| 410 | } |
| 411 | return actualShortcuts; |
| 412 | } |
| 413 | |
| 414 | public static List<ShortcutInfo> assertAllNotKeyFieldsOnly( |
| 415 | List<ShortcutInfo> actualShortcuts) { |
| 416 | for (ShortcutInfo s : actualShortcuts) { |
| 417 | assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly()); |
| 418 | } |
| 419 | return actualShortcuts; |
| 420 | } |
| 421 | |
| 422 | public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) { |
| 423 | for (ShortcutInfo s : actualShortcuts) { |
| 424 | assertTrue("ID " + s.getId(), s.isDynamic()); |
| 425 | } |
| 426 | return actualShortcuts; |
| 427 | } |
| 428 | |
| 429 | public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) { |
| 430 | for (ShortcutInfo s : actualShortcuts) { |
| 431 | assertTrue("ID " + s.getId(), s.isPinned()); |
| 432 | } |
| 433 | return actualShortcuts; |
| 434 | } |
| 435 | |
| 436 | public static List<ShortcutInfo> assertAllDynamicOrPinned( |
| 437 | List<ShortcutInfo> actualShortcuts) { |
| 438 | for (ShortcutInfo s : actualShortcuts) { |
| 439 | assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned()); |
| 440 | } |
| 441 | return actualShortcuts; |
| 442 | } |
| 443 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 444 | public static List<ShortcutInfo> assertAllManifest( |
| 445 | List<ShortcutInfo> actualShortcuts) { |
| 446 | for (ShortcutInfo s : actualShortcuts) { |
| 447 | assertTrue("ID " + s.getId(), s.isManifestShortcut()); |
| 448 | } |
| 449 | return actualShortcuts; |
| 450 | } |
| 451 | |
| 452 | public static List<ShortcutInfo> assertAllNotManifest( |
| 453 | List<ShortcutInfo> actualShortcuts) { |
| 454 | for (ShortcutInfo s : actualShortcuts) { |
| 455 | assertFalse("ID " + s.getId(), s.isManifestShortcut()); |
| 456 | } |
| 457 | return actualShortcuts; |
| 458 | } |
| 459 | |
| 460 | public static List<ShortcutInfo> assertAllDisabled( |
| 461 | List<ShortcutInfo> actualShortcuts) { |
| 462 | for (ShortcutInfo s : actualShortcuts) { |
| 463 | assertTrue("ID " + s.getId(), !s.isEnabled()); |
| 464 | } |
| 465 | return actualShortcuts; |
| 466 | } |
| 467 | |
| 468 | public static List<ShortcutInfo> assertAllEnabled( |
| 469 | List<ShortcutInfo> actualShortcuts) { |
| 470 | for (ShortcutInfo s : actualShortcuts) { |
| 471 | assertTrue("ID " + s.getId(), s.isEnabled()); |
| 472 | } |
| 473 | return actualShortcuts; |
| 474 | } |
| 475 | |
| 476 | public static List<ShortcutInfo> assertAllImmutable( |
| 477 | List<ShortcutInfo> actualShortcuts) { |
| 478 | for (ShortcutInfo s : actualShortcuts) { |
| 479 | assertTrue("ID " + s.getId(), s.isImmutable()); |
| 480 | } |
| 481 | return actualShortcuts; |
| 482 | } |
| 483 | |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 484 | public static List<ShortcutInfo> assertAllStringsResolved( |
| 485 | List<ShortcutInfo> actualShortcuts) { |
| 486 | for (ShortcutInfo s : actualShortcuts) { |
| 487 | assertTrue("ID " + s.getId(), s.hasStringResourcesResolved()); |
| 488 | } |
| 489 | return actualShortcuts; |
| 490 | } |
| 491 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 492 | public static void assertDynamicOnly(ShortcutInfo si) { |
| 493 | assertTrue(si.isDynamic()); |
| 494 | assertFalse(si.isPinned()); |
| 495 | } |
| 496 | |
| 497 | public static void assertPinnedOnly(ShortcutInfo si) { |
| 498 | assertFalse(si.isDynamic()); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 499 | assertFalse(si.isManifestShortcut()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 500 | assertTrue(si.isPinned()); |
| 501 | } |
| 502 | |
| 503 | public static void assertDynamicAndPinned(ShortcutInfo si) { |
| 504 | assertTrue(si.isDynamic()); |
| 505 | assertTrue(si.isPinned()); |
| 506 | } |
| 507 | |
| 508 | public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) { |
| 509 | assertEquals("width", expectedWidth, bitmap.getWidth()); |
| 510 | assertEquals("height", expectedHeight, bitmap.getHeight()); |
| 511 | } |
| 512 | |
| 513 | public static <T> void assertAllUnique(Collection<T> list) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 514 | final Set<Object> set = new LinkedHashSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 515 | for (T item : list) { |
| 516 | if (set.contains(item)) { |
| 517 | fail("Duplicate item found: " + item + " (in the list: " + list + ")"); |
| 518 | } |
| 519 | set.add(item); |
| 520 | } |
| 521 | } |
| 522 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 523 | public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) { |
| 524 | for (ShortcutInfo si : list) { |
| 525 | if (si.getId().equals(id)) { |
| 526 | return si; |
| 527 | } |
| 528 | } |
| 529 | fail("Shortcut " + id + " not found in the list"); |
| 530 | return null; |
| 531 | } |
| 532 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 533 | public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) { |
| 534 | assertNotNull(pfd); |
| 535 | try { |
| 536 | try { |
| 537 | return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor()); |
| 538 | } finally { |
| 539 | pfd.close(); |
| 540 | } |
| 541 | } catch (IOException e) { |
| 542 | throw new RuntimeException(e); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | public static void assertBundleEmpty(BaseBundle b) { |
| 547 | assertTrue(b == null || b.size() == 0); |
| 548 | } |
| 549 | |
| 550 | public static void assertCallbackNotReceived(LauncherApps.Callback mock) { |
| 551 | verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(), |
| 552 | any(UserHandle.class)); |
| 553 | } |
| 554 | |
| 555 | public static void assertCallbackReceived(LauncherApps.Callback mock, |
| 556 | UserHandle user, String packageName, String... ids) { |
| 557 | verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids), |
| 558 | eq(user)); |
| 559 | } |
| 560 | |
| 561 | public static boolean checkAssertSuccess(Runnable r) { |
| 562 | try { |
| 563 | r.run(); |
| 564 | return true; |
| 565 | } catch (AssertionError e) { |
| 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | public static <T> T checkArgument(Predicate<T> checker, String description, |
| 571 | List<T> matchedCaptor) { |
| 572 | final Matcher<T> m = new BaseMatcher<T>() { |
| 573 | @Override |
| 574 | public boolean matches(Object item) { |
| 575 | if (item == null) { |
| 576 | return false; |
| 577 | } |
| 578 | final T value = (T) item; |
| 579 | if (!checker.test(value)) { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | if (matchedCaptor != null) { |
| 584 | matchedCaptor.add(value); |
| 585 | } |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public void describeTo(Description d) { |
| 591 | d.appendText(description); |
| 592 | } |
| 593 | }; |
| 594 | return Mockito.argThat(m); |
| 595 | } |
| 596 | |
| 597 | public static List<ShortcutInfo> checkShortcutIds(String... ids) { |
| 598 | return checkArgument((List<ShortcutInfo> list) -> { |
| 599 | final Set<String> actualSet = set(si -> si.getId(), list); |
| 600 | return actualSet.equals(set(ids)); |
| 601 | |
| 602 | }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null); |
| 603 | } |
| 604 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 605 | public static ShortcutInfo parceled(ShortcutInfo si) { |
| 606 | Parcel p = Parcel.obtain(); |
| 607 | p.writeParcelable(si, 0); |
| 608 | p.setDataPosition(0); |
| 609 | ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader()); |
| 610 | p.recycle(); |
| 611 | return si2; |
| 612 | } |
| 613 | |
| 614 | public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) { |
| 615 | if (list == null) { |
| 616 | return null; |
| 617 | } |
| 618 | final List<ShortcutInfo> ret = new ArrayList<>(list.size()); |
| 619 | for (ShortcutInfo si : list) { |
| 620 | ret.add(parceled(si)); |
| 621 | } |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 626 | private static final Comparator<ShortcutInfo> sRankComparator = |
| 627 | (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank()); |
| 628 | |
| 629 | public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) { |
| 630 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts); |
| 631 | Collections.sort(ret, sRankComparator); |
| 632 | return ret; |
| 633 | } |
| 634 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 635 | public static void waitUntil(String message, BooleanSupplier condition) { |
| 636 | waitUntil(message, condition, STANDARD_TIMEOUT_SEC); |
| 637 | } |
| 638 | |
| 639 | public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) { |
| 640 | final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L); |
| 641 | while (System.currentTimeMillis() < timeout) { |
| 642 | if (condition.getAsBoolean()) { |
| 643 | return; |
| 644 | } |
| 645 | try { |
| 646 | Thread.sleep(100); |
| 647 | } catch (InterruptedException e) { |
| 648 | throw new RuntimeException(e); |
| 649 | } |
| 650 | } |
| 651 | fail("Timed out for: " + message); |
| 652 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 653 | |
| 654 | public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) { |
| 655 | return new ShortcutListAsserter(list); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * New style assertion that allows chained calls. |
| 660 | */ |
| 661 | public static class ShortcutListAsserter { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 662 | private final ShortcutListAsserter mOriginal; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 663 | private final List<ShortcutInfo> mList; |
| 664 | |
| 665 | ShortcutListAsserter(List<ShortcutInfo> list) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 666 | this(null, list); |
| 667 | } |
| 668 | |
| 669 | private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) { |
| 670 | mOriginal = original == null ? this : original; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 671 | mList = new ArrayList<>(list); |
| 672 | } |
| 673 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 674 | public ShortcutListAsserter revertToOriginalList() { |
| 675 | return mOriginal; |
| 676 | } |
| 677 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 678 | public ShortcutListAsserter selectDynamic() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 679 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 680 | filter(mList, ShortcutInfo::isDynamic)); |
| 681 | } |
| 682 | |
| 683 | public ShortcutListAsserter selectManifest() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 684 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 685 | filter(mList, ShortcutInfo::isManifestShortcut)); |
| 686 | } |
| 687 | |
| 688 | public ShortcutListAsserter selectPinned() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 689 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 690 | filter(mList, ShortcutInfo::isPinned)); |
| 691 | } |
| 692 | |
| 693 | public ShortcutListAsserter selectByActivity(ComponentName activity) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 694 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 695 | ShortcutManagerTestUtils.filterByActivity(mList, activity)); |
| 696 | } |
| 697 | |
| 698 | public ShortcutListAsserter selectByChangedSince(long time) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 699 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 700 | ShortcutManagerTestUtils.changedSince(mList, time)); |
| 701 | } |
| 702 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 703 | public ShortcutListAsserter selectByIds(String... ids) { |
| 704 | final Set<String> idSet = set(ids); |
| 705 | final ArrayList<ShortcutInfo> selected = new ArrayList<>(); |
| 706 | for (ShortcutInfo si : mList) { |
| 707 | if (idSet.contains(si.getId())) { |
| 708 | selected.add(si); |
| 709 | idSet.remove(si.getId()); |
| 710 | } |
| 711 | } |
| 712 | if (idSet.size() > 0) { |
| 713 | fail("Shortcuts not found for IDs=" + idSet); |
| 714 | } |
| 715 | |
| 716 | return new ShortcutListAsserter(this, selected); |
| 717 | } |
| 718 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 719 | public ShortcutListAsserter toSortByRank() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 720 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 721 | ShortcutManagerTestUtils.sortedByRank(mList)); |
| 722 | } |
| 723 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 724 | public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) { |
| 725 | c.accept(mList); |
| 726 | return this; |
| 727 | } |
| 728 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 729 | public ShortcutListAsserter haveIds(String... expectedIds) { |
| 730 | assertShortcutIds(mList, expectedIds); |
| 731 | return this; |
| 732 | } |
| 733 | |
| 734 | public ShortcutListAsserter haveIdsOrdered(String... expectedIds) { |
| 735 | assertShortcutIdsOrdered(mList, expectedIds); |
| 736 | return this; |
| 737 | } |
| 738 | |
| 739 | private ShortcutListAsserter haveSequentialRanks() { |
| 740 | for (int i = 0; i < mList.size(); i++) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 741 | final ShortcutInfo si = mList.get(i); |
| 742 | assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank()); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 743 | } |
| 744 | return this; |
| 745 | } |
| 746 | |
| 747 | public ShortcutListAsserter haveRanksInOrder(String... expectedIds) { |
| 748 | toSortByRank() |
| 749 | .haveSequentialRanks() |
| 750 | .haveIdsOrdered(expectedIds); |
| 751 | return this; |
| 752 | } |
| 753 | |
| 754 | public ShortcutListAsserter isEmpty() { |
| 755 | assertEquals(0, mList.size()); |
| 756 | return this; |
| 757 | } |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 758 | |
| 759 | public ShortcutListAsserter areAllDynamic() { |
| 760 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic())); |
| 761 | return this; |
| 762 | } |
| 763 | |
| 764 | public ShortcutListAsserter areAllNotDynamic() { |
| 765 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic())); |
| 766 | return this; |
| 767 | } |
| 768 | |
| 769 | public ShortcutListAsserter areAllPinned() { |
| 770 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned())); |
| 771 | return this; |
| 772 | } |
| 773 | |
| 774 | public ShortcutListAsserter areAllNotPinned() { |
| 775 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned())); |
| 776 | return this; |
| 777 | } |
| 778 | |
| 779 | public ShortcutListAsserter areAllManifest() { |
| 780 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isManifestShortcut())); |
| 781 | return this; |
| 782 | } |
| 783 | |
| 784 | public ShortcutListAsserter areAllNotManifest() { |
| 785 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isManifestShortcut())); |
| 786 | return this; |
| 787 | } |
| 788 | |
| 789 | public ShortcutListAsserter areAllImmutable() { |
| 790 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable())); |
| 791 | return this; |
| 792 | } |
| 793 | |
| 794 | public ShortcutListAsserter areAllMutable() { |
| 795 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable())); |
| 796 | return this; |
| 797 | } |
| 798 | |
| 799 | public ShortcutListAsserter areAllEnabled() { |
| 800 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled())); |
| 801 | return this; |
| 802 | } |
| 803 | |
| 804 | public ShortcutListAsserter areAllDisabled() { |
| 805 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled())); |
| 806 | return this; |
| 807 | } |
| 808 | |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame^] | 809 | public ShortcutListAsserter areAllWithKeyFieldsOnly() { |
| 810 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly())); |
| 811 | return this; |
| 812 | } |
| 813 | |
| 814 | public ShortcutListAsserter areAllNotWithKeyFieldsOnly() { |
| 815 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly())); |
| 816 | return this; |
| 817 | } |
| 818 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 819 | public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) { |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame^] | 820 | boolean found = false; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 821 | for (int i = 0; i < mList.size(); i++) { |
| 822 | final ShortcutInfo si = mList.get(i); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame^] | 823 | found = true; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 824 | sa.accept(si); |
| 825 | } |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame^] | 826 | assertTrue("No shortcuts found.", found); |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 827 | return this; |
| 828 | } |
| 829 | |
| 830 | public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p, |
| 831 | Consumer<ShortcutInfo> sa) { |
| 832 | boolean found = false; |
| 833 | for (int i = 0; i < mList.size(); i++) { |
| 834 | final ShortcutInfo si = mList.get(i); |
| 835 | if (p.test(si)) { |
| 836 | found = true; |
| 837 | try { |
| 838 | sa.accept(si); |
| 839 | } catch (Throwable e) { |
| 840 | throw new AssertionError("Assertion failed for shortcut " + si.getId(), e); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | assertTrue("Shortcut with the given condition not found.", found); |
| 845 | return this; |
| 846 | } |
| 847 | |
| 848 | public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) { |
| 849 | forShortcut(si -> si.getId().equals(id), sa); |
| 850 | |
| 851 | return this; |
| 852 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 853 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 854 | } |