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; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.mock; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.reset; |
| 31 | import static org.mockito.Mockito.times; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | |
| 34 | import android.app.Instrumentation; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 35 | import android.content.ComponentName; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.pm.LauncherApps; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 38 | import android.content.pm.LauncherApps.Callback; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 39 | import android.content.pm.ShortcutInfo; |
| 40 | import android.graphics.Bitmap; |
| 41 | import android.graphics.BitmapFactory; |
| 42 | import android.os.BaseBundle; |
| 43 | import android.os.Bundle; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 44 | import android.os.Handler; |
| 45 | import android.os.Looper; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 46 | import android.os.Parcel; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 47 | import android.os.ParcelFileDescriptor; |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 48 | import android.os.PersistableBundle; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 49 | import android.os.UserHandle; |
| 50 | import android.test.MoreAsserts; |
| 51 | import android.util.Log; |
| 52 | |
| 53 | import junit.framework.Assert; |
| 54 | |
| 55 | import org.hamcrest.BaseMatcher; |
| 56 | import org.hamcrest.Description; |
| 57 | import org.hamcrest.Matcher; |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 58 | import org.json.JSONException; |
| 59 | import org.json.JSONObject; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 60 | import org.mockito.ArgumentCaptor; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 61 | import org.mockito.Mockito; |
| 62 | |
| 63 | import java.io.BufferedReader; |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 64 | import java.io.File; |
| 65 | import java.io.FileNotFoundException; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 66 | import java.io.FileReader; |
| 67 | import java.io.IOException; |
| 68 | import java.util.ArrayList; |
| 69 | import java.util.Arrays; |
| 70 | import java.util.Collection; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 71 | import java.util.Collections; |
| 72 | import java.util.Comparator; |
| 73 | import java.util.LinkedHashSet; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 74 | import java.util.List; |
| 75 | import java.util.Set; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 76 | import java.util.SortedSet; |
| 77 | import java.util.TreeSet; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 78 | import java.util.concurrent.CountDownLatch; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 79 | import java.util.function.BooleanSupplier; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 80 | import java.util.function.Consumer; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 81 | import java.util.function.Function; |
| 82 | import java.util.function.Predicate; |
| 83 | |
Makoto Onuki | 51ab2b3 | 2016-06-02 11:03:51 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Common utility methods for ShortcutManager tests. This is used by both CTS and the unit tests. |
| 86 | * Because it's used by CTS too, it can only access the public APIs. |
| 87 | */ |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 88 | public class ShortcutManagerTestUtils { |
| 89 | private static final String TAG = "ShortcutManagerUtils"; |
| 90 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 91 | private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 92 | |
| 93 | private static final int STANDARD_TIMEOUT_SEC = 5; |
| 94 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 95 | private static final String[] EMPTY_STRINGS = new String[0]; |
| 96 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 97 | private ShortcutManagerTestUtils() { |
| 98 | } |
| 99 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 100 | public static List<String> readAll(File file) throws FileNotFoundException { |
| 101 | return readAll(ParcelFileDescriptor.open( |
| 102 | file.getAbsoluteFile(), ParcelFileDescriptor.MODE_READ_ONLY)); |
| 103 | } |
| 104 | |
| 105 | public static List<String> readAll(ParcelFileDescriptor pfd) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 106 | try { |
| 107 | try { |
| 108 | final ArrayList<String> ret = new ArrayList<>(); |
| 109 | try (BufferedReader r = new BufferedReader( |
| 110 | new FileReader(pfd.getFileDescriptor()))) { |
| 111 | String line; |
| 112 | while ((line = r.readLine()) != null) { |
| 113 | ret.add(line); |
| 114 | } |
| 115 | r.readLine(); |
| 116 | } |
| 117 | return ret; |
| 118 | } finally { |
| 119 | pfd.close(); |
| 120 | } |
| 121 | } catch (IOException e) { |
| 122 | throw new RuntimeException(e); |
| 123 | } |
| 124 | } |
| 125 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 126 | public static String concatResult(List<String> result) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 127 | final StringBuilder sb = new StringBuilder(); |
| 128 | for (String s : result) { |
| 129 | sb.append(s); |
| 130 | sb.append("\n"); |
| 131 | } |
| 132 | return sb.toString(); |
| 133 | } |
| 134 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 135 | public static boolean resultContains(List<String> result, String expected) { |
| 136 | for (String line : result) { |
| 137 | if (line.contains(expected)) { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | public static List<String> assertSuccess(List<String> result) { |
| 145 | if (!resultContains(result, "Success")) { |
| 146 | fail("Command failed. Result was:\n" + concatResult(result)); |
| 147 | } |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | public static List<String> assertContains(List<String> result, String expected) { |
| 152 | if (!resultContains(result, expected)) { |
| 153 | fail("Didn't contain expected string=" + expected |
| 154 | + "\nActual:\n" + concatResult(result)); |
| 155 | } |
| 156 | return result; |
| 157 | } |
| 158 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 159 | private static List<String> runCommand(Instrumentation instrumentation, String command) { |
| 160 | return runCommand(instrumentation, command, null); |
| 161 | } |
| 162 | private static List<String> runCommand(Instrumentation instrumentation, String command, |
| 163 | Predicate<List<String>> resultAsserter) { |
| 164 | Log.d(TAG, "Running command: " + command); |
| 165 | final List<String> result; |
| 166 | try { |
| 167 | result = readAll( |
| 168 | instrumentation.getUiAutomation().executeShellCommand(command)); |
| 169 | } catch (Exception e) { |
| 170 | throw new RuntimeException(e); |
| 171 | } |
| 172 | if (resultAsserter != null && !resultAsserter.test(result)) { |
| 173 | fail("Command '" + command + "' failed, output was:\n" + concatResult(result)); |
| 174 | } |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | private static void runCommandForNoOutput(Instrumentation instrumentation, String command) { |
| 179 | runCommand(instrumentation, command, result -> result.size() == 0); |
| 180 | } |
| 181 | |
| 182 | private static List<String> runShortcutCommand(Instrumentation instrumentation, String command, |
| 183 | Predicate<List<String>> resultAsserter) { |
| 184 | return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter); |
| 185 | } |
| 186 | |
| 187 | public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation, |
| 188 | String command) { |
| 189 | return runShortcutCommand(instrumentation, command, result -> result.contains("Success")); |
| 190 | } |
| 191 | |
| 192 | public static String getDefaultLauncher(Instrumentation instrumentation) { |
| 193 | final String PREFIX = "Launcher: ComponentInfo{"; |
| 194 | final String POSTFIX = "}"; |
| 195 | final List<String> result = runShortcutCommandForSuccess( |
| 196 | instrumentation, "get-default-launcher"); |
| 197 | for (String s : result) { |
| 198 | if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) { |
| 199 | return s.substring(PREFIX.length(), s.length() - POSTFIX.length()); |
| 200 | } |
| 201 | } |
| 202 | fail("Default launcher not found"); |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | public static void setDefaultLauncher(Instrumentation instrumentation, String component) { |
Makoto Onuki | 3bdbf98 | 2016-06-23 16:56:35 -0700 | [diff] [blame] | 207 | runCommand(instrumentation, "cmd package set-home-activity " + component, |
| 208 | result -> result.contains("Success")); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) { |
| 212 | setDefaultLauncher(instrumentation, packageContext.getPackageName() |
| 213 | + "/android.content.pm.cts.shortcutmanager.packages.Launcher"); |
| 214 | } |
| 215 | |
| 216 | public static void overrideConfig(Instrumentation instrumentation, String config) { |
| 217 | runShortcutCommandForSuccess(instrumentation, "override-config " + config); |
| 218 | } |
| 219 | |
| 220 | public static void resetConfig(Instrumentation instrumentation) { |
| 221 | runShortcutCommandForSuccess(instrumentation, "reset-config"); |
| 222 | } |
| 223 | |
| 224 | public static void resetThrottling(Instrumentation instrumentation) { |
| 225 | runShortcutCommandForSuccess(instrumentation, "reset-throttling"); |
| 226 | } |
| 227 | |
| 228 | public static void resetAllThrottling(Instrumentation instrumentation) { |
| 229 | runShortcutCommandForSuccess(instrumentation, "reset-all-throttling"); |
| 230 | } |
| 231 | |
| 232 | public static void clearShortcuts(Instrumentation instrumentation, int userId, |
| 233 | String packageName) { |
| 234 | runShortcutCommandForSuccess(instrumentation, "clear-shortcuts " |
| 235 | + " --user " + userId + " " + packageName); |
| 236 | } |
| 237 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 238 | public static void anyContains(List<String> result, String expected) { |
| 239 | for (String l : result) { |
| 240 | if (l.contains(expected)) { |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | fail("Result didn't contain '" + expected + "': was\n" + result); |
| 245 | } |
| 246 | |
| 247 | public static void enableComponent(Instrumentation instrumentation, ComponentName cn, |
| 248 | boolean enable) { |
| 249 | |
| 250 | final String word = (enable ? "enable" : "disable"); |
| 251 | runCommand(instrumentation, |
| 252 | "pm " + word + " " + cn.flattenToString() |
| 253 | , result ->concatResult(result).contains(word)); |
| 254 | } |
| 255 | |
| 256 | public static void appOps(Instrumentation instrumentation, String packageName, |
| 257 | String op, String mode) { |
| 258 | runCommand(instrumentation, "appops set " + packageName + " " + op + " " + mode); |
| 259 | } |
| 260 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 261 | public static void dumpsysShortcut(Instrumentation instrumentation) { |
| 262 | if (!ENABLE_DUMPSYS) { |
| 263 | return; |
| 264 | } |
Makoto Onuki | 3bdbf98 | 2016-06-23 16:56:35 -0700 | [diff] [blame] | 265 | Log.e(TAG, "Dumpsys shortcut"); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 266 | for (String s : runCommand(instrumentation, "dumpsys shortcut")) { |
| 267 | Log.e(TAG, s); |
| 268 | } |
| 269 | } |
| 270 | |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 271 | public static JSONObject getCheckinDump(Instrumentation instrumentation) throws JSONException { |
| 272 | return new JSONObject(concatResult(runCommand(instrumentation, "dumpsys shortcut -c"))); |
| 273 | } |
| 274 | |
| 275 | public static boolean isLowRamDevice(Instrumentation instrumentation) throws JSONException { |
| 276 | return getCheckinDump(instrumentation).getBoolean("lowRam"); |
| 277 | } |
| 278 | |
| 279 | public static int getIconSize(Instrumentation instrumentation) throws JSONException { |
| 280 | return getCheckinDump(instrumentation).getInt("iconSize"); |
| 281 | } |
| 282 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 283 | public static Bundle makeBundle(Object... keysAndValues) { |
| 284 | assertTrue((keysAndValues.length % 2) == 0); |
| 285 | |
| 286 | if (keysAndValues.length == 0) { |
| 287 | return null; |
| 288 | } |
| 289 | final Bundle ret = new Bundle(); |
| 290 | |
| 291 | for (int i = keysAndValues.length - 2; i >= 0; i -= 2) { |
| 292 | final String key = keysAndValues[i].toString(); |
| 293 | final Object value = keysAndValues[i + 1]; |
| 294 | |
| 295 | if (value == null) { |
| 296 | ret.putString(key, null); |
| 297 | } else if (value instanceof Integer) { |
| 298 | ret.putInt(key, (Integer) value); |
| 299 | } else if (value instanceof String) { |
| 300 | ret.putString(key, (String) value); |
| 301 | } else if (value instanceof Bundle) { |
| 302 | ret.putBundle(key, (Bundle) value); |
| 303 | } else { |
| 304 | fail("Type not supported yet: " + value.getClass().getName()); |
| 305 | } |
| 306 | } |
| 307 | return ret; |
| 308 | } |
| 309 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 310 | public static PersistableBundle makePersistableBundle(Object... keysAndValues) { |
| 311 | assertTrue((keysAndValues.length % 2) == 0); |
| 312 | |
| 313 | if (keysAndValues.length == 0) { |
| 314 | return null; |
| 315 | } |
| 316 | final PersistableBundle ret = new PersistableBundle(); |
| 317 | |
| 318 | for (int i = keysAndValues.length - 2; i >= 0; i -= 2) { |
| 319 | final String key = keysAndValues[i].toString(); |
| 320 | final Object value = keysAndValues[i + 1]; |
| 321 | |
| 322 | if (value == null) { |
| 323 | ret.putString(key, null); |
| 324 | } else if (value instanceof Integer) { |
| 325 | ret.putInt(key, (Integer) value); |
| 326 | } else if (value instanceof String) { |
| 327 | ret.putString(key, (String) value); |
| 328 | } else if (value instanceof PersistableBundle) { |
| 329 | ret.putPersistableBundle(key, (PersistableBundle) value); |
| 330 | } else { |
| 331 | fail("Type not supported yet: " + value.getClass().getName()); |
| 332 | } |
| 333 | } |
| 334 | return ret; |
| 335 | } |
| 336 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 337 | public static <T> List<T> list(T... array) { |
| 338 | return Arrays.asList(array); |
| 339 | } |
| 340 | |
| 341 | public static <T> Set<T> hashSet(Set<T> in) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 342 | return new LinkedHashSet<>(in); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | public static <T> Set<T> set(T... values) { |
| 346 | return set(v -> v, values); |
| 347 | } |
| 348 | |
| 349 | public static <T, V> Set<T> set(Function<V, T> converter, V... values) { |
| 350 | return set(converter, Arrays.asList(values)); |
| 351 | } |
| 352 | |
| 353 | 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] | 354 | final LinkedHashSet<T> ret = new LinkedHashSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 355 | for (V v : values) { |
| 356 | ret.add(converter.apply(v)); |
| 357 | } |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | public static void resetAll(Collection<?> mocks) { |
| 362 | for (Object o : mocks) { |
| 363 | reset(o); |
| 364 | } |
| 365 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 366 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 367 | public static <T extends Collection<?>> T assertEmpty(T collection) { |
| 368 | if (collection == null) { |
| 369 | return collection; // okay. |
| 370 | } |
| 371 | assertEquals(0, collection.size()); |
| 372 | return collection; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 375 | public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) { |
| 376 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(list); |
| 377 | ret.removeIf(si -> !p.test(si)); |
| 378 | return ret; |
| 379 | } |
| 380 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 381 | public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list, |
| 382 | ComponentName activity) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 383 | return filter(list, si -> |
| 384 | (si.getActivity().equals(activity) |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 385 | && (si.isDeclaredInManifest() || si.isDynamic()))); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) { |
| 389 | return filter(list, si -> si.getLastChangedTimestamp() >= time); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 392 | @FunctionalInterface |
| 393 | public interface ExceptionRunnable { |
| 394 | void run() throws Exception; |
| 395 | } |
| 396 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 397 | public static void assertExpectException(Class<? extends Throwable> expectedExceptionType, |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 398 | String expectedExceptionMessageRegex, ExceptionRunnable r) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 399 | assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r); |
| 400 | } |
| 401 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 402 | public static void assertCannotUpdateImmutable(Runnable r) { |
| 403 | assertExpectException( |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 404 | IllegalArgumentException.class, "may not be manipulated via APIs", r::run); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 407 | public static void assertDynamicShortcutCountExceeded(Runnable r) { |
| 408 | assertExpectException(IllegalArgumentException.class, |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 409 | "Max number of dynamic shortcuts exceeded", r::run); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | public static void assertExpectException(String message, |
| 413 | Class<? extends Throwable> expectedExceptionType, |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 414 | String expectedExceptionMessageRegex, ExceptionRunnable r) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 415 | try { |
| 416 | r.run(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 417 | } catch (Throwable e) { |
| 418 | Assert.assertTrue( |
| 419 | "Expected exception type was " + expectedExceptionType.getName() |
| 420 | + " but caught " + e + " (message=" + message + ")", |
| 421 | expectedExceptionType.isAssignableFrom(e.getClass())); |
| 422 | if (expectedExceptionMessageRegex != null) { |
| 423 | MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage()); |
| 424 | } |
Makoto Onuki | a1d38b3 | 2016-06-10 15:32:26 -0700 | [diff] [blame] | 425 | return; // Pass |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 426 | } |
Makoto Onuki | a1d38b3 | 2016-06-10 15:32:26 -0700 | [diff] [blame] | 427 | Assert.fail("Expected exception type " + expectedExceptionType.getName() |
| 428 | + " was not thrown"); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts, |
| 432 | String... expectedIds) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 433 | final SortedSet<String> expected = new TreeSet<>(list(expectedIds)); |
| 434 | final SortedSet<String> actual = new TreeSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 435 | for (ShortcutInfo s : actualShortcuts) { |
| 436 | actual.add(s.getId()); |
| 437 | } |
| 438 | |
| 439 | // Compare the sets. |
| 440 | assertEquals(expected, actual); |
| 441 | return actualShortcuts; |
| 442 | } |
| 443 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 444 | public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts, |
| 445 | String... expectedIds) { |
| 446 | final ArrayList<String> expected = new ArrayList<>(list(expectedIds)); |
| 447 | final ArrayList<String> actual = new ArrayList<>(); |
| 448 | for (ShortcutInfo s : actualShortcuts) { |
| 449 | actual.add(s.getId()); |
| 450 | } |
| 451 | assertEquals(expected, actual); |
| 452 | return actualShortcuts; |
| 453 | } |
| 454 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 455 | public static List<ShortcutInfo> assertAllHaveIntents( |
| 456 | List<ShortcutInfo> actualShortcuts) { |
| 457 | for (ShortcutInfo s : actualShortcuts) { |
| 458 | assertNotNull("ID " + s.getId(), s.getIntent()); |
| 459 | } |
| 460 | return actualShortcuts; |
| 461 | } |
| 462 | |
| 463 | public static List<ShortcutInfo> assertAllNotHaveIntents( |
| 464 | List<ShortcutInfo> actualShortcuts) { |
| 465 | for (ShortcutInfo s : actualShortcuts) { |
| 466 | assertNull("ID " + s.getId(), s.getIntent()); |
| 467 | } |
| 468 | return actualShortcuts; |
| 469 | } |
| 470 | |
| 471 | public static List<ShortcutInfo> assertAllHaveTitle( |
| 472 | List<ShortcutInfo> actualShortcuts) { |
| 473 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | eddbfec | 2016-05-31 17:04:34 -0700 | [diff] [blame] | 474 | assertNotNull("ID " + s.getId(), s.getShortLabel()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 475 | } |
| 476 | return actualShortcuts; |
| 477 | } |
| 478 | |
| 479 | public static List<ShortcutInfo> assertAllNotHaveTitle( |
| 480 | List<ShortcutInfo> actualShortcuts) { |
| 481 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | eddbfec | 2016-05-31 17:04:34 -0700 | [diff] [blame] | 482 | assertNull("ID " + s.getId(), s.getShortLabel()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 483 | } |
| 484 | return actualShortcuts; |
| 485 | } |
| 486 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 487 | public static List<ShortcutInfo> assertAllKeyFieldsOnly( |
| 488 | List<ShortcutInfo> actualShortcuts) { |
| 489 | for (ShortcutInfo s : actualShortcuts) { |
| 490 | assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly()); |
| 491 | } |
| 492 | return actualShortcuts; |
| 493 | } |
| 494 | |
| 495 | public static List<ShortcutInfo> assertAllNotKeyFieldsOnly( |
| 496 | List<ShortcutInfo> actualShortcuts) { |
| 497 | for (ShortcutInfo s : actualShortcuts) { |
| 498 | assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly()); |
| 499 | } |
| 500 | return actualShortcuts; |
| 501 | } |
| 502 | |
| 503 | public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) { |
| 504 | for (ShortcutInfo s : actualShortcuts) { |
| 505 | assertTrue("ID " + s.getId(), s.isDynamic()); |
| 506 | } |
| 507 | return actualShortcuts; |
| 508 | } |
| 509 | |
| 510 | public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) { |
| 511 | for (ShortcutInfo s : actualShortcuts) { |
| 512 | assertTrue("ID " + s.getId(), s.isPinned()); |
| 513 | } |
| 514 | return actualShortcuts; |
| 515 | } |
| 516 | |
| 517 | public static List<ShortcutInfo> assertAllDynamicOrPinned( |
| 518 | List<ShortcutInfo> actualShortcuts) { |
| 519 | for (ShortcutInfo s : actualShortcuts) { |
| 520 | assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned()); |
| 521 | } |
| 522 | return actualShortcuts; |
| 523 | } |
| 524 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 525 | public static List<ShortcutInfo> assertAllManifest( |
| 526 | List<ShortcutInfo> actualShortcuts) { |
| 527 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 528 | assertTrue("ID " + s.getId(), s.isDeclaredInManifest()); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 529 | } |
| 530 | return actualShortcuts; |
| 531 | } |
| 532 | |
| 533 | public static List<ShortcutInfo> assertAllNotManifest( |
| 534 | List<ShortcutInfo> actualShortcuts) { |
| 535 | for (ShortcutInfo s : actualShortcuts) { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 536 | assertFalse("ID " + s.getId(), s.isDeclaredInManifest()); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 537 | } |
| 538 | return actualShortcuts; |
| 539 | } |
| 540 | |
| 541 | public static List<ShortcutInfo> assertAllDisabled( |
| 542 | List<ShortcutInfo> actualShortcuts) { |
| 543 | for (ShortcutInfo s : actualShortcuts) { |
| 544 | assertTrue("ID " + s.getId(), !s.isEnabled()); |
| 545 | } |
| 546 | return actualShortcuts; |
| 547 | } |
| 548 | |
| 549 | public static List<ShortcutInfo> assertAllEnabled( |
| 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> assertAllImmutable( |
| 558 | List<ShortcutInfo> actualShortcuts) { |
| 559 | for (ShortcutInfo s : actualShortcuts) { |
| 560 | assertTrue("ID " + s.getId(), s.isImmutable()); |
| 561 | } |
| 562 | return actualShortcuts; |
| 563 | } |
| 564 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 565 | public static void assertDynamicOnly(ShortcutInfo si) { |
| 566 | assertTrue(si.isDynamic()); |
| 567 | assertFalse(si.isPinned()); |
| 568 | } |
| 569 | |
| 570 | public static void assertPinnedOnly(ShortcutInfo si) { |
| 571 | assertFalse(si.isDynamic()); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 572 | assertFalse(si.isDeclaredInManifest()); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 573 | assertTrue(si.isPinned()); |
| 574 | } |
| 575 | |
| 576 | public static void assertDynamicAndPinned(ShortcutInfo si) { |
| 577 | assertTrue(si.isDynamic()); |
| 578 | assertTrue(si.isPinned()); |
| 579 | } |
| 580 | |
| 581 | public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) { |
| 582 | assertEquals("width", expectedWidth, bitmap.getWidth()); |
| 583 | assertEquals("height", expectedHeight, bitmap.getHeight()); |
| 584 | } |
| 585 | |
| 586 | public static <T> void assertAllUnique(Collection<T> list) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 587 | final Set<Object> set = new LinkedHashSet<>(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 588 | for (T item : list) { |
| 589 | if (set.contains(item)) { |
| 590 | fail("Duplicate item found: " + item + " (in the list: " + list + ")"); |
| 591 | } |
| 592 | set.add(item); |
| 593 | } |
| 594 | } |
| 595 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 596 | public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) { |
| 597 | for (ShortcutInfo si : list) { |
| 598 | if (si.getId().equals(id)) { |
| 599 | return si; |
| 600 | } |
| 601 | } |
| 602 | fail("Shortcut " + id + " not found in the list"); |
| 603 | return null; |
| 604 | } |
| 605 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 606 | public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) { |
| 607 | assertNotNull(pfd); |
| 608 | try { |
| 609 | try { |
| 610 | return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor()); |
| 611 | } finally { |
| 612 | pfd.close(); |
| 613 | } |
| 614 | } catch (IOException e) { |
| 615 | throw new RuntimeException(e); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | public static void assertBundleEmpty(BaseBundle b) { |
| 620 | assertTrue(b == null || b.size() == 0); |
| 621 | } |
| 622 | |
| 623 | public static void assertCallbackNotReceived(LauncherApps.Callback mock) { |
| 624 | verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(), |
| 625 | any(UserHandle.class)); |
| 626 | } |
| 627 | |
| 628 | public static void assertCallbackReceived(LauncherApps.Callback mock, |
| 629 | UserHandle user, String packageName, String... ids) { |
| 630 | verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids), |
| 631 | eq(user)); |
| 632 | } |
| 633 | |
| 634 | public static boolean checkAssertSuccess(Runnable r) { |
| 635 | try { |
| 636 | r.run(); |
| 637 | return true; |
| 638 | } catch (AssertionError e) { |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | public static <T> T checkArgument(Predicate<T> checker, String description, |
| 644 | List<T> matchedCaptor) { |
| 645 | final Matcher<T> m = new BaseMatcher<T>() { |
| 646 | @Override |
| 647 | public boolean matches(Object item) { |
| 648 | if (item == null) { |
| 649 | return false; |
| 650 | } |
| 651 | final T value = (T) item; |
| 652 | if (!checker.test(value)) { |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | if (matchedCaptor != null) { |
| 657 | matchedCaptor.add(value); |
| 658 | } |
| 659 | return true; |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public void describeTo(Description d) { |
| 664 | d.appendText(description); |
| 665 | } |
| 666 | }; |
| 667 | return Mockito.argThat(m); |
| 668 | } |
| 669 | |
| 670 | public static List<ShortcutInfo> checkShortcutIds(String... ids) { |
| 671 | return checkArgument((List<ShortcutInfo> list) -> { |
| 672 | final Set<String> actualSet = set(si -> si.getId(), list); |
| 673 | return actualSet.equals(set(ids)); |
| 674 | |
| 675 | }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null); |
| 676 | } |
| 677 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 678 | public static ShortcutInfo parceled(ShortcutInfo si) { |
| 679 | Parcel p = Parcel.obtain(); |
| 680 | p.writeParcelable(si, 0); |
| 681 | p.setDataPosition(0); |
| 682 | ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader()); |
| 683 | p.recycle(); |
| 684 | return si2; |
| 685 | } |
| 686 | |
| 687 | public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) { |
| 688 | if (list == null) { |
| 689 | return null; |
| 690 | } |
| 691 | final List<ShortcutInfo> ret = new ArrayList<>(list.size()); |
| 692 | for (ShortcutInfo si : list) { |
| 693 | ret.add(parceled(si)); |
| 694 | } |
| 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 699 | private static final Comparator<ShortcutInfo> sRankComparator = |
| 700 | (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank()); |
| 701 | |
| 702 | public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) { |
| 703 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts); |
| 704 | Collections.sort(ret, sRankComparator); |
| 705 | return ret; |
| 706 | } |
| 707 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 708 | public static void waitUntil(String message, BooleanSupplier condition) { |
| 709 | waitUntil(message, condition, STANDARD_TIMEOUT_SEC); |
| 710 | } |
| 711 | |
| 712 | public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) { |
| 713 | final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L); |
| 714 | while (System.currentTimeMillis() < timeout) { |
| 715 | if (condition.getAsBoolean()) { |
| 716 | return; |
| 717 | } |
| 718 | try { |
| 719 | Thread.sleep(100); |
| 720 | } catch (InterruptedException e) { |
| 721 | throw new RuntimeException(e); |
| 722 | } |
| 723 | } |
| 724 | fail("Timed out for: " + message); |
| 725 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 726 | |
| 727 | public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) { |
| 728 | return new ShortcutListAsserter(list); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * New style assertion that allows chained calls. |
| 733 | */ |
| 734 | public static class ShortcutListAsserter { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 735 | private final ShortcutListAsserter mOriginal; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 736 | private final List<ShortcutInfo> mList; |
| 737 | |
| 738 | ShortcutListAsserter(List<ShortcutInfo> list) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 739 | this(null, list); |
| 740 | } |
| 741 | |
| 742 | private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 743 | mOriginal = (original == null) ? this : original; |
| 744 | mList = (list == null) ? new ArrayList<>(0) : new ArrayList<>(list); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 747 | public ShortcutListAsserter revertToOriginalList() { |
| 748 | return mOriginal; |
| 749 | } |
| 750 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 751 | public ShortcutListAsserter selectDynamic() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 752 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 753 | filter(mList, ShortcutInfo::isDynamic)); |
| 754 | } |
| 755 | |
| 756 | public ShortcutListAsserter selectManifest() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 757 | return new ShortcutListAsserter(this, |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 758 | filter(mList, ShortcutInfo::isDeclaredInManifest)); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | public ShortcutListAsserter selectPinned() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 762 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 763 | filter(mList, ShortcutInfo::isPinned)); |
| 764 | } |
| 765 | |
| 766 | public ShortcutListAsserter selectByActivity(ComponentName activity) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 767 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 768 | ShortcutManagerTestUtils.filterByActivity(mList, activity)); |
| 769 | } |
| 770 | |
| 771 | public ShortcutListAsserter selectByChangedSince(long time) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 772 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 773 | ShortcutManagerTestUtils.changedSince(mList, time)); |
| 774 | } |
| 775 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 776 | public ShortcutListAsserter selectByIds(String... ids) { |
| 777 | final Set<String> idSet = set(ids); |
| 778 | final ArrayList<ShortcutInfo> selected = new ArrayList<>(); |
| 779 | for (ShortcutInfo si : mList) { |
| 780 | if (idSet.contains(si.getId())) { |
| 781 | selected.add(si); |
| 782 | idSet.remove(si.getId()); |
| 783 | } |
| 784 | } |
| 785 | if (idSet.size() > 0) { |
| 786 | fail("Shortcuts not found for IDs=" + idSet); |
| 787 | } |
| 788 | |
| 789 | return new ShortcutListAsserter(this, selected); |
| 790 | } |
| 791 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 792 | public ShortcutListAsserter toSortByRank() { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 793 | return new ShortcutListAsserter(this, |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 794 | ShortcutManagerTestUtils.sortedByRank(mList)); |
| 795 | } |
| 796 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 797 | public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) { |
| 798 | c.accept(mList); |
| 799 | return this; |
| 800 | } |
| 801 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 802 | public ShortcutListAsserter haveIds(String... expectedIds) { |
| 803 | assertShortcutIds(mList, expectedIds); |
| 804 | return this; |
| 805 | } |
| 806 | |
| 807 | public ShortcutListAsserter haveIdsOrdered(String... expectedIds) { |
| 808 | assertShortcutIdsOrdered(mList, expectedIds); |
| 809 | return this; |
| 810 | } |
| 811 | |
| 812 | private ShortcutListAsserter haveSequentialRanks() { |
| 813 | for (int i = 0; i < mList.size(); i++) { |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 814 | final ShortcutInfo si = mList.get(i); |
| 815 | assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank()); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 816 | } |
| 817 | return this; |
| 818 | } |
| 819 | |
| 820 | public ShortcutListAsserter haveRanksInOrder(String... expectedIds) { |
| 821 | toSortByRank() |
| 822 | .haveSequentialRanks() |
| 823 | .haveIdsOrdered(expectedIds); |
| 824 | return this; |
| 825 | } |
| 826 | |
| 827 | public ShortcutListAsserter isEmpty() { |
| 828 | assertEquals(0, mList.size()); |
| 829 | return this; |
| 830 | } |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 831 | |
| 832 | public ShortcutListAsserter areAllDynamic() { |
| 833 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic())); |
| 834 | return this; |
| 835 | } |
| 836 | |
| 837 | public ShortcutListAsserter areAllNotDynamic() { |
| 838 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic())); |
| 839 | return this; |
| 840 | } |
| 841 | |
| 842 | public ShortcutListAsserter areAllPinned() { |
| 843 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned())); |
| 844 | return this; |
| 845 | } |
| 846 | |
| 847 | public ShortcutListAsserter areAllNotPinned() { |
| 848 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned())); |
| 849 | return this; |
| 850 | } |
| 851 | |
| 852 | public ShortcutListAsserter areAllManifest() { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 853 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDeclaredInManifest())); |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 854 | return this; |
| 855 | } |
| 856 | |
| 857 | public ShortcutListAsserter areAllNotManifest() { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 858 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDeclaredInManifest())); |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 859 | return this; |
| 860 | } |
| 861 | |
| 862 | public ShortcutListAsserter areAllImmutable() { |
| 863 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable())); |
| 864 | return this; |
| 865 | } |
| 866 | |
| 867 | public ShortcutListAsserter areAllMutable() { |
| 868 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable())); |
| 869 | return this; |
| 870 | } |
| 871 | |
| 872 | public ShortcutListAsserter areAllEnabled() { |
| 873 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled())); |
| 874 | return this; |
| 875 | } |
| 876 | |
| 877 | public ShortcutListAsserter areAllDisabled() { |
| 878 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled())); |
| 879 | return this; |
| 880 | } |
| 881 | |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 882 | public ShortcutListAsserter areAllWithKeyFieldsOnly() { |
| 883 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly())); |
| 884 | return this; |
| 885 | } |
| 886 | |
| 887 | public ShortcutListAsserter areAllNotWithKeyFieldsOnly() { |
| 888 | forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly())); |
| 889 | return this; |
| 890 | } |
| 891 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 892 | public ShortcutListAsserter areAllWithActivity(ComponentName activity) { |
| 893 | forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.getActivity().equals(activity))); |
| 894 | return this; |
| 895 | } |
| 896 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 897 | public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) { |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 898 | boolean found = false; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 899 | for (int i = 0; i < mList.size(); i++) { |
| 900 | final ShortcutInfo si = mList.get(i); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 901 | found = true; |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 902 | sa.accept(si); |
| 903 | } |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 904 | assertTrue("No shortcuts found.", found); |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 905 | return this; |
| 906 | } |
| 907 | |
| 908 | public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p, |
| 909 | Consumer<ShortcutInfo> sa) { |
| 910 | boolean found = false; |
| 911 | for (int i = 0; i < mList.size(); i++) { |
| 912 | final ShortcutInfo si = mList.get(i); |
| 913 | if (p.test(si)) { |
| 914 | found = true; |
| 915 | try { |
| 916 | sa.accept(si); |
| 917 | } catch (Throwable e) { |
| 918 | throw new AssertionError("Assertion failed for shortcut " + si.getId(), e); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | assertTrue("Shortcut with the given condition not found.", found); |
| 923 | return this; |
| 924 | } |
| 925 | |
| 926 | public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) { |
| 927 | forShortcut(si -> si.getId().equals(id), sa); |
| 928 | |
| 929 | return this; |
| 930 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 931 | } |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 932 | |
| 933 | public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) { |
| 934 | if (b1 == null && b2 == null) { |
| 935 | return; // pass |
| 936 | } |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 937 | assertNotNull("b1 is null but b2 is not", b1); |
| 938 | assertNotNull("b2 is null but b1 is not", b2); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 939 | |
| 940 | // HashSet makes the error message readable. |
| 941 | assertEquals(set(b1.keySet()), set(b2.keySet())); |
| 942 | |
| 943 | for (String key : b1.keySet()) { |
| 944 | final Object v1 = b1.get(key); |
| 945 | final Object v2 = b2.get(key); |
| 946 | if (v1 == null) { |
| 947 | if (v2 == null) { |
| 948 | return; |
| 949 | } |
| 950 | } |
| 951 | if (v1.equals(v2)) { |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | assertTrue("Only either value is null: key=" + key |
| 956 | + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null); |
| 957 | assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass()); |
| 958 | |
| 959 | if (v1 instanceof BaseBundle) { |
| 960 | assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2); |
| 961 | |
| 962 | } else if (v1 instanceof boolean[]) { |
| 963 | assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2)); |
| 964 | |
| 965 | } else if (v1 instanceof int[]) { |
| 966 | MoreAsserts.assertEquals((int[]) v1, (int[]) v2); |
| 967 | |
| 968 | } else if (v1 instanceof double[]) { |
| 969 | MoreAsserts.assertEquals((double[]) v1, (double[]) v2); |
| 970 | |
| 971 | } else if (v1 instanceof String[]) { |
| 972 | MoreAsserts.assertEquals((String[]) v1, (String[]) v2); |
| 973 | |
| 974 | } else if (v1 instanceof Double) { |
| 975 | if (((Double) v1).isNaN()) { |
| 976 | assertTrue(((Double) v2).isNaN()); |
| 977 | } else { |
| 978 | assertEquals(v1, v2); |
| 979 | } |
| 980 | |
| 981 | } else { |
| 982 | assertEquals(v1, v2); |
| 983 | } |
| 984 | } |
| 985 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 986 | |
| 987 | public static void waitOnMainThread() throws InterruptedException { |
| 988 | final CountDownLatch latch = new CountDownLatch(1); |
| 989 | |
| 990 | new Handler(Looper.getMainLooper()).post(() -> latch.countDown()); |
| 991 | |
| 992 | latch.await(); |
| 993 | } |
| 994 | |
| 995 | public static class LauncherCallbackAsserter { |
| 996 | private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class); |
| 997 | |
| 998 | private Callback getMockCallback() { |
| 999 | return mCallback; |
| 1000 | } |
| 1001 | |
| 1002 | public LauncherCallbackAsserter assertNoCallbackCalled() { |
| 1003 | verify(mCallback, times(0)).onShortcutsChanged( |
| 1004 | anyString(), |
| 1005 | any(List.class), |
| 1006 | any(UserHandle.class)); |
| 1007 | return this; |
| 1008 | } |
| 1009 | |
| 1010 | public LauncherCallbackAsserter assertNoCallbackCalledForPackage( |
| 1011 | String publisherPackageName) { |
| 1012 | verify(mCallback, times(0)).onShortcutsChanged( |
| 1013 | eq(publisherPackageName), |
| 1014 | any(List.class), |
| 1015 | any(UserHandle.class)); |
| 1016 | return this; |
| 1017 | } |
| 1018 | |
| 1019 | public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser( |
| 1020 | String publisherPackageName, UserHandle publisherUserHandle) { |
| 1021 | verify(mCallback, times(0)).onShortcutsChanged( |
| 1022 | eq(publisherPackageName), |
| 1023 | any(List.class), |
| 1024 | eq(publisherUserHandle)); |
| 1025 | return this; |
| 1026 | } |
| 1027 | |
| 1028 | public ShortcutListAsserter assertCallbackCalledForPackageAndUser( |
| 1029 | String publisherPackageName, UserHandle publisherUserHandle) { |
| 1030 | final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class); |
| 1031 | verify(mCallback, times(1)).onShortcutsChanged( |
| 1032 | eq(publisherPackageName), |
| 1033 | shortcuts.capture(), |
| 1034 | eq(publisherUserHandle)); |
| 1035 | return new ShortcutListAsserter(shortcuts.getValue()); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | public static LauncherCallbackAsserter assertForLauncherCallback( |
| 1040 | LauncherApps launcherApps, Runnable body) throws InterruptedException { |
| 1041 | final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter(); |
| 1042 | launcherApps.registerCallback(asserter.getMockCallback(), |
| 1043 | new Handler(Looper.getMainLooper())); |
| 1044 | |
| 1045 | body.run(); |
| 1046 | |
| 1047 | waitOnMainThread(); |
| 1048 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1049 | // TODO unregister doesn't work well during unit tests. Figure out and fix it. |
| 1050 | // launcherApps.unregisterCallback(asserter.getMockCallback()); |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 1051 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1052 | return asserter; |
| 1053 | } |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1054 | |
| 1055 | public static void retryUntil(BooleanSupplier checker, String message) { |
| 1056 | final long timeOut = System.currentTimeMillis() + 30 * 1000; // wait for 30 seconds. |
| 1057 | while (!checker.getAsBoolean()) { |
Makoto Onuki | a210ccf | 2016-07-27 14:08:48 -0700 | [diff] [blame^] | 1058 | if (System.currentTimeMillis() > timeOut) { |
| 1059 | break; |
| 1060 | } |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1061 | try { |
| 1062 | Thread.sleep(200); |
| 1063 | } catch (InterruptedException ignore) { |
| 1064 | } |
| 1065 | } |
| 1066 | assertTrue(message, checker.getAsBoolean()); |
| 1067 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 1068 | } |