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