Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [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 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 17 | package com.android.internal.accessibility; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 18 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 19 | import static android.provider.Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 20 | import static android.provider.Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN; |
| 21 | import static android.provider.Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 22 | import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_SHORTCUT_KEY; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 23 | |
| 24 | import static junit.framework.Assert.assertEquals; |
| 25 | import static junit.framework.Assert.assertFalse; |
| 26 | import static junit.framework.Assert.assertTrue; |
| 27 | |
| 28 | import static org.junit.Assert.fail; |
| 29 | import static org.mockito.AdditionalMatchers.aryEq; |
| 30 | import static org.mockito.ArgumentMatchers.any; |
| 31 | import static org.mockito.ArgumentMatchers.anyLong; |
| 32 | import static org.mockito.Matchers.anyBoolean; |
| 33 | import static org.mockito.Matchers.anyInt; |
| 34 | import static org.mockito.Matchers.anyObject; |
| 35 | import static org.mockito.Matchers.eq; |
| 36 | import static org.mockito.Mockito.atLeastOnce; |
| 37 | import static org.mockito.Mockito.doAnswer; |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.doReturn; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 39 | import static org.mockito.Mockito.mock; |
| 40 | import static org.mockito.Mockito.times; |
| 41 | import static org.mockito.Mockito.verify; |
| 42 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 43 | import static org.mockito.Mockito.when; |
| 44 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 45 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 46 | import android.app.AlertDialog; |
| 47 | import android.content.ComponentName; |
| 48 | import android.content.Context; |
| 49 | import android.content.DialogInterface; |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 50 | import android.content.pm.ApplicationInfo; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 51 | import android.content.pm.PackageManager; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 52 | import android.content.pm.ResolveInfo; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 53 | import android.content.pm.ServiceInfo; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 54 | import android.content.res.Resources; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 55 | import android.media.Ringtone; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 56 | import android.os.Build; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 57 | import android.os.Handler; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 58 | import android.os.Message; |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 59 | import android.os.Vibrator; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 60 | import android.provider.Settings; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 61 | import android.speech.tts.TextToSpeech; |
| 62 | import android.speech.tts.Voice; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 63 | import android.test.mock.MockContentResolver; |
| 64 | import android.text.TextUtils; |
| 65 | import android.view.Window; |
| 66 | import android.view.WindowManager; |
| 67 | import android.view.accessibility.AccessibilityManager; |
| 68 | import android.view.accessibility.IAccessibilityManager; |
| 69 | import android.widget.Toast; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 70 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 71 | import androidx.test.runner.AndroidJUnit4; |
| 72 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 73 | import com.android.internal.R; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 74 | import com.android.internal.accessibility.AccessibilityShortcutController.FrameworkObjectProvider; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 75 | import com.android.internal.util.test.FakeSettingsProvider; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 76 | |
Dieter Hsu | f2da6b6 | 2019-07-16 19:59:16 +0800 | [diff] [blame] | 77 | import org.junit.AfterClass; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 78 | import org.junit.Before; |
| 79 | import org.junit.Test; |
| 80 | import org.junit.runner.RunWith; |
| 81 | import org.mockito.ArgumentCaptor; |
| 82 | import org.mockito.Mock; |
| 83 | import org.mockito.MockitoAnnotations; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 84 | import org.mockito.invocation.InvocationOnMock; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 85 | |
Paul Duffin | 192bb0b | 2017-03-09 18:49:41 +0000 | [diff] [blame] | 86 | import java.lang.reflect.Field; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 87 | import java.util.Collections; |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 88 | import java.util.HashSet; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 89 | import java.util.Map; |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 90 | import java.util.Set; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 91 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 92 | |
| 93 | @RunWith(AndroidJUnit4.class) |
| 94 | public class AccessibilityShortcutControllerTest { |
| 95 | private static final String SERVICE_NAME_STRING = "fake.package/fake.service.name"; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 96 | private static final String SERVICE_NAME_SUMMARY = "Summary"; |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 97 | private static final long VIBRATOR_PATTERN_1 = 100L; |
| 98 | private static final long VIBRATOR_PATTERN_2 = 150L; |
| 99 | private static final int[] VIBRATOR_PATTERN_INT = {(int) VIBRATOR_PATTERN_1, |
| 100 | (int) VIBRATOR_PATTERN_2}; |
| 101 | private static final long[] VIBRATOR_PATTERN_LONG = {VIBRATOR_PATTERN_1, VIBRATOR_PATTERN_2}; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 102 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 103 | // Convenience values for enabling/disabling to make code more readable |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 104 | private static final int ENABLED_EXCEPT_LOCK_SCREEN = 1; |
| 105 | private static final int ENABLED_INCLUDING_LOCK_SCREEN = 2; |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 106 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 107 | private @Mock Context mContext; |
| 108 | private @Mock FrameworkObjectProvider mFrameworkObjectProvider; |
| 109 | private @Mock IAccessibilityManager mAccessibilityManagerService; |
| 110 | private @Mock Handler mHandler; |
| 111 | private @Mock AlertDialog.Builder mAlertDialogBuilder; |
| 112 | private @Mock AlertDialog mAlertDialog; |
| 113 | private @Mock AccessibilityServiceInfo mServiceInfo; |
| 114 | private @Mock Resources mResources; |
| 115 | private @Mock Toast mToast; |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 116 | private @Mock Vibrator mVibrator; |
| 117 | private @Mock ApplicationInfo mApplicationInfo; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 118 | private @Mock PackageManager mPackageManager; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 119 | private @Mock TextToSpeech mTextToSpeech; |
| 120 | private @Mock Voice mVoice; |
| 121 | private @Mock Ringtone mRingtone; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 122 | |
| 123 | private MockContentResolver mContentResolver; |
Phil Weaver | 7192297 | 2017-02-14 12:36:47 -0800 | [diff] [blame] | 124 | private WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 125 | |
| 126 | @Before |
| 127 | public void setUp() throws Exception { |
| 128 | MockitoAnnotations.initMocks(this); |
| 129 | |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 130 | when(mVibrator.hasVibrator()).thenReturn(true); |
| 131 | |
| 132 | when(mContext.getResources()).thenReturn(mResources); |
| 133 | when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); |
| 134 | when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 135 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 136 | |
| 137 | // We're not checking the text. Just prevent us crashing when getting text. |
| 138 | when(mPackageManager.getText(any(), anyInt(), any())).thenReturn("text"); |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 139 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 140 | mContentResolver = new MockContentResolver(mContext); |
| 141 | mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); |
Dieter Hsu | f2da6b6 | 2019-07-16 19:59:16 +0800 | [diff] [blame] | 142 | FakeSettingsProvider.clearSettingsProvider(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 143 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 144 | |
| 145 | when(mAccessibilityManagerService.getInstalledAccessibilityServiceList(anyInt())) |
| 146 | .thenReturn(Collections.singletonList(mServiceInfo)); |
| 147 | |
| 148 | // Use the extra level of indirection in the object to mock framework objects |
| 149 | AccessibilityManager accessibilityManager = |
| 150 | new AccessibilityManager(mHandler, mAccessibilityManagerService, 0); |
| 151 | when(mFrameworkObjectProvider.getAccessibilityManagerInstance(mContext)) |
| 152 | .thenReturn(accessibilityManager); |
| 153 | when(mFrameworkObjectProvider.getAlertDialogBuilder(mContext)) |
| 154 | .thenReturn(mAlertDialogBuilder); |
| 155 | when(mFrameworkObjectProvider.makeToastFromText(eq(mContext), anyObject(), anyInt())) |
| 156 | .thenReturn(mToast); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 157 | when(mFrameworkObjectProvider.getSystemUiContext()).thenReturn(mContext); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 158 | when(mFrameworkObjectProvider.getTextToSpeech(eq(mContext), any())) |
| 159 | .thenReturn(mTextToSpeech); |
| 160 | when(mFrameworkObjectProvider.getRingtone(eq(mContext), any())).thenReturn(mRingtone); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 161 | |
| 162 | when(mResources.getString(anyInt())).thenReturn("Howdy %s"); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 163 | when(mResources.getString(R.string.config_defaultAccessibilityService)).thenReturn(null); |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 164 | when(mResources.getIntArray(anyInt())).thenReturn(VIBRATOR_PATTERN_INT); |
| 165 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 166 | ResolveInfo resolveInfo = mock(ResolveInfo.class); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 167 | resolveInfo.serviceInfo = mock(ServiceInfo.class); |
| 168 | resolveInfo.serviceInfo.applicationInfo = mApplicationInfo; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 169 | when(resolveInfo.loadLabel(anyObject())).thenReturn("Service name"); |
| 170 | when(mServiceInfo.getResolveInfo()).thenReturn(resolveInfo); |
| 171 | when(mServiceInfo.getComponentName()) |
| 172 | .thenReturn(ComponentName.unflattenFromString(SERVICE_NAME_STRING)); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 173 | when(mServiceInfo.loadSummary(any())).thenReturn(SERVICE_NAME_SUMMARY); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 174 | |
| 175 | when(mAlertDialogBuilder.setTitle(anyInt())).thenReturn(mAlertDialogBuilder); |
| 176 | when(mAlertDialogBuilder.setCancelable(anyBoolean())).thenReturn(mAlertDialogBuilder); |
| 177 | when(mAlertDialogBuilder.setMessage(anyObject())).thenReturn(mAlertDialogBuilder); |
| 178 | when(mAlertDialogBuilder.setPositiveButton(anyInt(), anyObject())) |
| 179 | .thenReturn(mAlertDialogBuilder); |
| 180 | when(mAlertDialogBuilder.setNegativeButton(anyInt(), anyObject())) |
| 181 | .thenReturn(mAlertDialogBuilder); |
| 182 | when(mAlertDialogBuilder.setOnCancelListener(anyObject())).thenReturn(mAlertDialogBuilder); |
| 183 | when(mAlertDialogBuilder.create()).thenReturn(mAlertDialog); |
| 184 | |
Phil Weaver | 7192297 | 2017-02-14 12:36:47 -0800 | [diff] [blame] | 185 | mLayoutParams.privateFlags = 0; |
| 186 | when(mToast.getWindowParams()).thenReturn(mLayoutParams); |
| 187 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 188 | Window window = mock(Window.class); |
Paul Duffin | 192bb0b | 2017-03-09 18:49:41 +0000 | [diff] [blame] | 189 | // Initialize the mWindowAttributes field which was not properly initialized during mock |
| 190 | // creation. |
| 191 | try { |
| 192 | Field field = Window.class.getDeclaredField("mWindowAttributes"); |
| 193 | field.setAccessible(true); |
| 194 | field.set(window, new WindowManager.LayoutParams()); |
| 195 | } catch (Exception e) { |
| 196 | throw new RuntimeException("Unable to set mWindowAttributes", e); |
| 197 | } |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 198 | when(mAlertDialog.getWindow()).thenReturn(window); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 199 | |
| 200 | when(mTextToSpeech.getVoice()).thenReturn(mVoice); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Dieter Hsu | f2da6b6 | 2019-07-16 19:59:16 +0800 | [diff] [blame] | 203 | @AfterClass |
| 204 | public static void cleanUpSettingsProvider() { |
| 205 | FakeSettingsProvider.clearSettingsProvider(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 209 | public void testShortcutAvailable_enabledButNoServiceWhenCreated_shouldReturnFalse() |
| 210 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 211 | configureNoShortcutService(); |
| 212 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 213 | assertFalse(getController().isAccessibilityShortcutAvailable(false)); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 217 | public void testShortcutAvailable_enabledWithValidServiceWhenCreated_shouldReturnTrue() |
| 218 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 219 | configureValidShortcutService(); |
| 220 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 221 | assertTrue(getController().isAccessibilityShortcutAvailable(false)); |
| 222 | } |
| 223 | |
| 224 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 225 | public void testShortcutAvailable_onLockScreenButDisabledThere_shouldReturnFalse() |
| 226 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 227 | configureValidShortcutService(); |
| 228 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 229 | assertFalse(getController().isAccessibilityShortcutAvailable(true)); |
| 230 | } |
| 231 | |
| 232 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 233 | public void testShortcutAvailable_onLockScreenAndEnabledThere_shouldReturnTrue() |
| 234 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 235 | configureValidShortcutService(); |
| 236 | configureShortcutEnabled(ENABLED_INCLUDING_LOCK_SCREEN); |
| 237 | assertTrue(getController().isAccessibilityShortcutAvailable(true)); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 241 | public void testShortcutAvailable_onLockScreenAndLockScreenPreferenceUnset() throws Exception { |
Phil Weaver | c5865d6 | 2018-03-02 16:00:43 -0800 | [diff] [blame] | 242 | // When the user hasn't specified a lock screen preference, we allow from the lock screen |
| 243 | // as long as the user has agreed to enable the shortcut |
| 244 | configureValidShortcutService(); |
| 245 | configureShortcutEnabled(ENABLED_INCLUDING_LOCK_SCREEN); |
| 246 | Settings.Secure.putString( |
| 247 | mContentResolver, ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, null); |
| 248 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 249 | assertFalse(getController().isAccessibilityShortcutAvailable(true)); |
| 250 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); |
| 251 | assertTrue(getController().isAccessibilityShortcutAvailable(true)); |
| 252 | } |
| 253 | |
| 254 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 255 | public void testShortcutAvailable_whenServiceIdBecomesNull_shouldReturnFalse() |
| 256 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 257 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 258 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 259 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 260 | configureNoShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 261 | accessibilityShortcutController.onSettingsChanged(); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 262 | assertFalse(accessibilityShortcutController.isAccessibilityShortcutAvailable(false)); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 266 | public void testShortcutAvailable_whenServiceIdBecomesNonNull_shouldReturnTrue() |
| 267 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 268 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 269 | configureNoShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 270 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 271 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 272 | accessibilityShortcutController.onSettingsChanged(); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 273 | assertTrue(accessibilityShortcutController.isAccessibilityShortcutAvailable(false)); |
| 274 | } |
| 275 | |
| 276 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 277 | public void testShortcutAvailable_whenShortcutBecomesEnabled_shouldReturnTrue() |
| 278 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 279 | configureValidShortcutService(); |
| 280 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 281 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 282 | accessibilityShortcutController.onSettingsChanged(); |
| 283 | assertTrue(accessibilityShortcutController.isAccessibilityShortcutAvailable(false)); |
| 284 | } |
| 285 | |
| 286 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 287 | public void testShortcutAvailable_whenLockscreenBecomesDisabled_shouldReturnFalse() |
| 288 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 289 | configureShortcutEnabled(ENABLED_INCLUDING_LOCK_SCREEN); |
| 290 | configureValidShortcutService(); |
| 291 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 292 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 293 | accessibilityShortcutController.onSettingsChanged(); |
| 294 | assertFalse(accessibilityShortcutController.isAccessibilityShortcutAvailable(true)); |
| 295 | } |
| 296 | |
| 297 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 298 | public void testShortcutAvailable_whenLockscreenBecomesEnabled_shouldReturnTrue() |
| 299 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 300 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 301 | configureValidShortcutService(); |
| 302 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 303 | configureShortcutEnabled(ENABLED_INCLUDING_LOCK_SCREEN); |
| 304 | accessibilityShortcutController.onSettingsChanged(); |
| 305 | assertTrue(accessibilityShortcutController.isAccessibilityShortcutAvailable(true)); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Test |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 309 | public void testOnAccessibilityShortcut_vibrates() { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 310 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 311 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 312 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 313 | verify(mVibrator).vibrate(aryEq(VIBRATOR_PATTERN_LONG), eq(-1), anyObject()); |
| 314 | } |
| 315 | |
| 316 | @Test |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 317 | public void testOnAccessibilityShortcut_firstTime_showsWarningDialog() |
| 318 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 319 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 320 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 321 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 322 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 323 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 324 | |
| 325 | assertEquals(1, Settings.Secure.getInt( |
| 326 | mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0)); |
| 327 | verify(mResources).getString(R.string.accessibility_shortcut_toogle_warning); |
| 328 | verify(mAlertDialog).show(); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 329 | verify(mAccessibilityManagerService, atLeastOnce()).getInstalledAccessibilityServiceList( |
| 330 | anyInt()); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 331 | verify(mAccessibilityManagerService, times(0)).performAccessibilityShortcut(null); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 332 | verify(mFrameworkObjectProvider, times(0)).getTextToSpeech(any(), any()); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Test |
| 336 | public void testOnAccessibilityShortcut_withDialogShowing_callsServer() |
| 337 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 338 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 339 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 340 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 341 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 342 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 343 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 344 | verify(mToast).show(); |
Bernardo Rufino | 0f64b3c | 2020-02-14 13:45:20 +0000 | [diff] [blame] | 345 | // TODO(b/149408635): Reintroduce assertion |
| 346 | // assertEquals(WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS, |
| 347 | // mLayoutParams.privateFlags |
| 348 | // & WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 349 | verify(mAccessibilityManagerService, times(1)).performAccessibilityShortcut(null); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Test |
| 353 | public void testOnAccessibilityShortcut_ifCanceledFirstTime_showsWarningDialog() |
| 354 | throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 355 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 356 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 357 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 358 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 359 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 360 | ArgumentCaptor<AlertDialog.OnCancelListener> cancelListenerCaptor = |
| 361 | ArgumentCaptor.forClass(AlertDialog.OnCancelListener.class); |
| 362 | verify(mAlertDialogBuilder).setOnCancelListener(cancelListenerCaptor.capture()); |
| 363 | // Call the cancel callback |
| 364 | cancelListenerCaptor.getValue().onCancel(null); |
| 365 | |
| 366 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 367 | verify(mAlertDialog, times(2)).show(); |
| 368 | } |
| 369 | |
| 370 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 371 | public void testClickingDisableButtonInDialog_shouldClearShortcutId() throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 372 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 373 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 374 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 375 | getController().performAccessibilityShortcut(); |
| 376 | |
| 377 | ArgumentCaptor<DialogInterface.OnClickListener> captor = |
| 378 | ArgumentCaptor.forClass(DialogInterface.OnClickListener.class); |
| 379 | verify(mAlertDialogBuilder).setNegativeButton(eq(R.string.disable_accessibility_shortcut), |
| 380 | captor.capture()); |
| 381 | // Call the button callback |
| 382 | captor.getValue().onClick(null, 0); |
| 383 | assertTrue(TextUtils.isEmpty( |
| 384 | Settings.Secure.getString(mContentResolver, ACCESSIBILITY_SHORTCUT_TARGET_SERVICE))); |
| 385 | } |
| 386 | |
| 387 | @Test |
| 388 | public void testClickingLeaveOnButtonInDialog_shouldLeaveShortcutReady() throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 389 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 390 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 391 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 392 | getController().performAccessibilityShortcut(); |
| 393 | |
| 394 | ArgumentCaptor<DialogInterface.OnClickListener> captor = |
| 395 | ArgumentCaptor.forClass(DialogInterface.OnClickListener.class); |
| 396 | verify(mAlertDialogBuilder).setPositiveButton(eq(R.string.leave_accessibility_shortcut_on), |
| 397 | captor.capture()); |
| 398 | // Call the button callback, if one exists |
| 399 | if (captor.getValue() != null) { |
| 400 | captor.getValue().onClick(null, 0); |
| 401 | } |
| 402 | assertEquals(SERVICE_NAME_STRING, |
| 403 | Settings.Secure.getString(mContentResolver, ACCESSIBILITY_SHORTCUT_TARGET_SERVICE)); |
| 404 | assertEquals(1, Settings.Secure.getInt( |
| 405 | mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN)); |
| 406 | } |
| 407 | |
| 408 | @Test |
| 409 | public void testOnAccessibilityShortcut_afterDialogShown_shouldCallServer() throws Exception { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 410 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 411 | configureValidShortcutService(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 412 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); |
| 413 | getController().performAccessibilityShortcut(); |
| 414 | |
| 415 | verifyZeroInteractions(mAlertDialogBuilder, mAlertDialog); |
| 416 | verify(mToast).show(); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 417 | verify(mAccessibilityManagerService).performAccessibilityShortcut(null); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 420 | @Test |
| 421 | public void getFrameworkFeatureMap_shouldBeNonNullAndUnmodifiable() { |
| 422 | Map<ComponentName, AccessibilityShortcutController.ToggleableFrameworkFeatureInfo> |
| 423 | frameworkFeatureMap = |
| 424 | AccessibilityShortcutController.getFrameworkShortcutFeaturesMap(); |
| 425 | assertTrue("Framework features not supported", frameworkFeatureMap.size() > 0); |
| 426 | |
| 427 | try { |
| 428 | frameworkFeatureMap.clear(); |
| 429 | fail("Framework feature map should be unmodifieable"); |
| 430 | } catch (UnsupportedOperationException e) { |
| 431 | // Expected |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | @Test |
Phil Weaver | 0f9aa4c | 2018-02-26 13:17:28 -0800 | [diff] [blame] | 436 | public void testOnAccessibilityShortcut_forServiceWithNoSummary_doesNotCrash() |
| 437 | throws Exception { |
| 438 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 439 | configureValidShortcutService(); |
| 440 | when(mServiceInfo.loadSummary(any())).thenReturn(null); |
| 441 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); |
| 442 | getController().performAccessibilityShortcut(); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 443 | verify(mAccessibilityManagerService).performAccessibilityShortcut(null); |
Phil Weaver | 0f9aa4c | 2018-02-26 13:17:28 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Test |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 447 | public void testOnAccessibilityShortcut_forFrameworkFeature_callsServiceWithNoToast() |
| 448 | throws Exception { |
| 449 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 450 | configureFirstFrameworkFeature(); |
| 451 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); |
| 452 | getController().performAccessibilityShortcut(); |
| 453 | |
| 454 | verifyZeroInteractions(mToast); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 455 | verify(mAccessibilityManagerService).performAccessibilityShortcut(null); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 458 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 459 | public void testOnAccessibilityShortcut_sdkGreaterThanQ_reqA11yButton_callsServiceWithNoToast() |
| 460 | throws Exception { |
| 461 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 462 | configureValidShortcutService(); |
| 463 | configureApplicationTargetSdkVersion(Build.VERSION_CODES.R); |
| 464 | configureRequestAccessibilityButton(); |
Rhed Jao | 8fcba03 | 2020-04-15 23:06:08 +0800 | [diff] [blame] | 465 | configureEnabledService(); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 466 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); |
| 467 | getController().performAccessibilityShortcut(); |
| 468 | |
| 469 | verifyZeroInteractions(mToast); |
Rhed Jao | f526c99 | 2019-11-29 22:49:11 +0800 | [diff] [blame] | 470 | verify(mAccessibilityManagerService).performAccessibilityShortcut(null); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | @Test |
| 474 | public void testOnAccessibilityShortcut_showsWarningDialog_shouldTtsSpokenPrompt() |
| 475 | throws Exception { |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 476 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 477 | configureValidShortcutService(); |
| 478 | configureTtsSpokenPromptEnabled(); |
| 479 | configureHandlerCallbackInvocation(); |
| 480 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 481 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 482 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 483 | |
| 484 | verify(mAlertDialog).show(); |
| 485 | ArgumentCaptor<TextToSpeech.OnInitListener> onInitCap = ArgumentCaptor.forClass( |
| 486 | TextToSpeech.OnInitListener.class); |
| 487 | verify(mFrameworkObjectProvider).getTextToSpeech(any(), onInitCap.capture()); |
| 488 | onInitCap.getValue().onInit(TextToSpeech.SUCCESS); |
| 489 | verify(mTextToSpeech).speak(any(), eq(TextToSpeech.QUEUE_FLUSH), any(), any()); |
| 490 | ArgumentCaptor<DialogInterface.OnDismissListener> onDismissCap = ArgumentCaptor.forClass( |
| 491 | DialogInterface.OnDismissListener.class); |
| 492 | verify(mAlertDialog).setOnDismissListener(onDismissCap.capture()); |
| 493 | onDismissCap.getValue().onDismiss(mAlertDialog); |
| 494 | verify(mTextToSpeech).shutdown(); |
| 495 | verify(mRingtone, times(0)).play(); |
| 496 | } |
| 497 | |
| 498 | @Test |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 499 | public void testOnAccessibilityShortcut_showsWarningDialog_ttsInitFail_noSpokenPrompt() |
| 500 | throws Exception { |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 501 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 502 | configureValidShortcutService(); |
| 503 | configureTtsSpokenPromptEnabled(); |
| 504 | configureHandlerCallbackInvocation(); |
| 505 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 506 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 507 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 508 | |
| 509 | verify(mAlertDialog).show(); |
| 510 | ArgumentCaptor<TextToSpeech.OnInitListener> onInitCap = ArgumentCaptor.forClass( |
| 511 | TextToSpeech.OnInitListener.class); |
| 512 | verify(mFrameworkObjectProvider).getTextToSpeech(any(), onInitCap.capture()); |
| 513 | onInitCap.getValue().onInit(TextToSpeech.ERROR); |
| 514 | verify(mTextToSpeech, times(0)).speak(any(), anyInt(), any(), any()); |
| 515 | verify(mRingtone).play(); |
| 516 | } |
| 517 | |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 518 | @Test |
| 519 | public void testOnAccessibilityShortcut_showsWarningDialog_ttsLongTimeInit_retrySpoken() |
| 520 | throws Exception { |
| 521 | configureShortcutEnabled(ENABLED_EXCEPT_LOCK_SCREEN); |
| 522 | configureValidShortcutService(); |
| 523 | configureTtsSpokenPromptEnabled(); |
| 524 | configureHandlerCallbackInvocation(); |
| 525 | AccessibilityShortcutController accessibilityShortcutController = getController(); |
| 526 | Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0); |
| 527 | Set<String> features = new HashSet<>(); |
| 528 | features.add(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED); |
| 529 | doReturn(features, Collections.emptySet()).when(mVoice).getFeatures(); |
| 530 | doReturn(TextToSpeech.LANG_NOT_SUPPORTED, TextToSpeech.LANG_AVAILABLE) |
| 531 | .when(mTextToSpeech).setLanguage(any()); |
| 532 | accessibilityShortcutController.performAccessibilityShortcut(); |
| 533 | |
| 534 | verify(mAlertDialog).show(); |
| 535 | ArgumentCaptor<TextToSpeech.OnInitListener> onInitCap = ArgumentCaptor.forClass( |
| 536 | TextToSpeech.OnInitListener.class); |
| 537 | verify(mFrameworkObjectProvider).getTextToSpeech(any(), onInitCap.capture()); |
| 538 | onInitCap.getValue().onInit(TextToSpeech.SUCCESS); |
| 539 | verify(mTextToSpeech).speak(any(), eq(TextToSpeech.QUEUE_FLUSH), any(), any()); |
| 540 | ArgumentCaptor<DialogInterface.OnDismissListener> onDismissCap = ArgumentCaptor.forClass( |
| 541 | DialogInterface.OnDismissListener.class); |
| 542 | verify(mAlertDialog).setOnDismissListener(onDismissCap.capture()); |
| 543 | onDismissCap.getValue().onDismiss(mAlertDialog); |
| 544 | verify(mTextToSpeech).shutdown(); |
| 545 | verify(mRingtone, times(0)).play(); |
| 546 | } |
| 547 | |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 548 | private void configureNoShortcutService() throws Exception { |
| 549 | when(mAccessibilityManagerService |
| 550 | .getAccessibilityShortcutTargets(ACCESSIBILITY_SHORTCUT_KEY)) |
| 551 | .thenReturn(Collections.emptyList()); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 552 | Settings.Secure.putString(mContentResolver, ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, ""); |
| 553 | } |
| 554 | |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 555 | private void configureValidShortcutService() throws Exception { |
| 556 | when(mAccessibilityManagerService |
| 557 | .getAccessibilityShortcutTargets(ACCESSIBILITY_SHORTCUT_KEY)) |
| 558 | .thenReturn(Collections.singletonList(SERVICE_NAME_STRING)); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 559 | Settings.Secure.putString( |
| 560 | mContentResolver, ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, SERVICE_NAME_STRING); |
| 561 | } |
| 562 | |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 563 | private void configureFirstFrameworkFeature() throws Exception { |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 564 | ComponentName featureComponentName = |
| 565 | (ComponentName) AccessibilityShortcutController.getFrameworkShortcutFeaturesMap() |
| 566 | .keySet().toArray()[0]; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 567 | when(mAccessibilityManagerService |
| 568 | .getAccessibilityShortcutTargets(ACCESSIBILITY_SHORTCUT_KEY)) |
| 569 | .thenReturn(Collections.singletonList(featureComponentName.flattenToString())); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 570 | Settings.Secure.putString(mContentResolver, ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, |
| 571 | featureComponentName.flattenToString()); |
| 572 | } |
| 573 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 574 | private void configureShortcutEnabled(int enabledValue) { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 575 | final boolean lockscreen; |
| 576 | |
| 577 | switch (enabledValue) { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 578 | case ENABLED_INCLUDING_LOCK_SCREEN: |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 579 | lockscreen = true; |
| 580 | break; |
| 581 | case ENABLED_EXCEPT_LOCK_SCREEN: |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 582 | lockscreen = false; |
| 583 | break; |
| 584 | default: |
| 585 | throw new IllegalArgumentException(); |
| 586 | } |
| 587 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 588 | Settings.Secure.putInt( |
| 589 | mContentResolver, ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, lockscreen ? 1 : 0); |
| 590 | } |
| 591 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 592 | private void configureTtsSpokenPromptEnabled() { |
| 593 | mServiceInfo.flags |= AccessibilityServiceInfo |
| 594 | .FLAG_REQUEST_SHORTCUT_WARNING_DIALOG_SPOKEN_FEEDBACK; |
| 595 | } |
| 596 | |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 597 | private void configureRequestAccessibilityButton() { |
| 598 | mServiceInfo.flags |= AccessibilityServiceInfo |
| 599 | .FLAG_REQUEST_ACCESSIBILITY_BUTTON; |
| 600 | } |
| 601 | |
| 602 | private void configureApplicationTargetSdkVersion(int versionCode) { |
| 603 | mApplicationInfo.targetSdkVersion = versionCode; |
| 604 | } |
| 605 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 606 | private void configureHandlerCallbackInvocation() { |
| 607 | doAnswer((InvocationOnMock invocation) -> { |
| 608 | Message m = (Message) invocation.getArguments()[0]; |
| 609 | m.getCallback().run(); |
| 610 | return true; |
| 611 | }).when(mHandler).sendMessageAtTime(any(), anyLong()); |
| 612 | } |
| 613 | |
Rhed Jao | 8fcba03 | 2020-04-15 23:06:08 +0800 | [diff] [blame] | 614 | private void configureEnabledService() throws Exception { |
| 615 | when(mAccessibilityManagerService.getEnabledAccessibilityServiceList(anyInt(), anyInt())) |
| 616 | .thenReturn(Collections.singletonList(mServiceInfo)); |
| 617 | } |
| 618 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 619 | private AccessibilityShortcutController getController() { |
| 620 | AccessibilityShortcutController accessibilityShortcutController = |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 621 | new AccessibilityShortcutController(mContext, mHandler, 0); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 622 | accessibilityShortcutController.mFrameworkObjectProvider = mFrameworkObjectProvider; |
| 623 | return accessibilityShortcutController; |
| 624 | } |
| 625 | } |