1 /* 2 * Copyright (C) 2023 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 17 package com.android.systemui.accessibility.utils; 18 19 import static com.android.internal.accessibility.common.ShortcutConstants.SERVICES_SEPARATOR; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.content.ComponentName; 25 import android.os.SystemClock; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 29 import com.android.systemui.util.settings.SecureSettings; 30 31 import java.util.Set; 32 import java.util.StringJoiner; 33 import java.util.function.BooleanSupplier; 34 35 public class TestUtils { 36 private static final ComponentName TEST_COMPONENT_A = new ComponentName("pkg", "A"); 37 private static final ComponentName TEST_COMPONENT_B = new ComponentName("pkg", "B"); 38 public static final String[] TEST_BUTTON_TARGETS = { 39 TEST_COMPONENT_A.flattenToString(), TEST_COMPONENT_B.flattenToString()}; 40 public static long DEFAULT_CONDITION_DURATION = 5_000; 41 42 /** 43 * Waits an amount of time specified by {@link TestUtils#DEFAULT_CONDITION_DURATION} 44 * for a condition to become true. 45 * On failure, throws a {@link RuntimeException} with a custom message. 46 * 47 * @param c Condition which must return true to proceed. 48 * @param message Message to print on failure. 49 */ waitForCondition(BooleanSupplier condition, String message)50 public static void waitForCondition(BooleanSupplier condition, String message) { 51 waitForCondition(condition, message, DEFAULT_CONDITION_DURATION); 52 } 53 54 /** 55 * Waits up to a specified amount of time for a condition to become true. 56 * On failure, throws a {@link RuntimeException} with a custom message. 57 * 58 * @param c Condition which must return true to proceed. 59 * @param message Message to print on failure. 60 * @param duration Amount of time permitted to wait. 61 */ waitForCondition(BooleanSupplier condition, String message, long duration)62 public static void waitForCondition(BooleanSupplier condition, String message, long duration) { 63 long deadline = SystemClock.uptimeMillis() + duration; 64 long sleepMs = 50; 65 while (!condition.getAsBoolean()) { 66 if (SystemClock.uptimeMillis() > deadline) { 67 throw new RuntimeException(message); 68 } 69 // Reduce frequency of checks as more checks occur 70 sleepMs *= 2; 71 SystemClock.sleep(sleepMs); 72 } 73 } 74 75 /** 76 * Returns a mock secure settings configured to return information needed for tests. 77 * Currently, this only includes button targets. 78 */ mockSecureSettings()79 public static SecureSettings mockSecureSettings() { 80 SecureSettings secureSettings = mock(SecureSettings.class); 81 82 final String targets = getShortcutTargets( 83 Set.of(TEST_COMPONENT_A, TEST_COMPONENT_B)); 84 when(secureSettings.getStringForUser( 85 Settings.Secure.ACCESSIBILITY_BUTTON_TARGETS, 86 UserHandle.USER_CURRENT)).thenReturn(targets); 87 88 return secureSettings; 89 } 90 getShortcutTargets(Set<ComponentName> components)91 private static String getShortcutTargets(Set<ComponentName> components) { 92 final StringJoiner stringJoiner = new StringJoiner(String.valueOf(SERVICES_SEPARATOR)); 93 for (ComponentName target : components) { 94 stringJoiner.add(target.flattenToString()); 95 } 96 return stringJoiner.toString(); 97 } 98 } 99