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