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