Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.server.accessibility; |
| 18 | |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.any; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 20 | import static org.mockito.Mockito.doThrow; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.never; |
| 23 | import static org.mockito.Mockito.verify; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 24 | import static org.mockito.Mockito.when; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 25 | |
| 26 | import android.Manifest; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 27 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 28 | import android.accessibilityservice.IAccessibilityServiceClient; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 29 | import android.app.PendingIntent; |
| 30 | import android.app.RemoteAction; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 31 | import android.content.ComponentName; |
| 32 | import android.content.Context; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 33 | import android.content.Intent; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 34 | import android.content.pm.ApplicationInfo; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
| 37 | import android.content.pm.ServiceInfo; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 38 | import android.graphics.drawable.Icon; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 39 | import android.os.IBinder; |
| 40 | import android.os.UserHandle; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 41 | import android.test.AndroidTestCase; |
| 42 | import android.test.suitebuilder.annotation.SmallTest; |
| 43 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 44 | |
| 45 | import androidx.test.InstrumentationRegistry; |
| 46 | |
| 47 | import com.android.server.LocalServices; |
| 48 | import com.android.server.accessibility.AccessibilityManagerService.AccessibilityDisplayListener; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 49 | import com.android.server.accessibility.test.MessageCapturingHandler; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 50 | import com.android.server.wm.ActivityTaskManagerInternal; |
| 51 | import com.android.server.wm.WindowManagerInternal; |
| 52 | |
| 53 | import org.mockito.Mock; |
| 54 | import org.mockito.MockitoAnnotations; |
| 55 | |
| 56 | /** |
| 57 | * APCT tests for {@link AccessibilityManagerService}. |
| 58 | */ |
| 59 | public class AccessibilityManagerServiceTest extends AndroidTestCase { |
| 60 | private static final String TAG = "A11Y_MANAGER_SERVICE_TEST"; |
| 61 | private static final int ACTION_ID = 20; |
| 62 | private static final String LABEL = "label"; |
| 63 | private static final String INTENT_ACTION = "TESTACTION"; |
| 64 | private static final String DESCRIPTION = "description"; |
| 65 | private static final PendingIntent TEST_PENDING_INTENT = PendingIntent.getBroadcast( |
| 66 | InstrumentationRegistry.getTargetContext(), 0, new Intent(INTENT_ACTION), 0); |
| 67 | private static final RemoteAction TEST_ACTION = new RemoteAction( |
| 68 | Icon.createWithContentUri("content://test"), |
| 69 | LABEL, |
| 70 | DESCRIPTION, |
| 71 | TEST_PENDING_INTENT); |
| 72 | private static final AccessibilityAction NEW_ACCESSIBILITY_ACTION = |
| 73 | new AccessibilityAction(ACTION_ID, LABEL); |
| 74 | |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 75 | static final ComponentName COMPONENT_NAME = new ComponentName( |
| 76 | "com.android.server.accessibility", "AccessibilityManagerServiceTest"); |
| 77 | static final int SERVICE_ID = 42; |
| 78 | |
| 79 | @Mock private Context mMockContext; |
| 80 | @Mock private AccessibilityServiceInfo mMockServiceInfo; |
| 81 | @Mock private ResolveInfo mMockResolveInfo; |
| 82 | @Mock private AbstractAccessibilityServiceConnection.SystemSupport mMockSystemSupport; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 83 | @Mock private PackageManager mMockPackageManager; |
| 84 | @Mock private WindowManagerInternal mMockWindowManagerService; |
| 85 | @Mock private AccessibilitySecurityPolicy mMockSecurityPolicy; |
| 86 | @Mock private SystemActionPerformer mMockSystemActionPerformer; |
| 87 | @Mock private AccessibilityWindowManager mMockA11yWindowManager; |
| 88 | @Mock private AccessibilityDisplayListener mMockA11yDisplayListener; |
| 89 | @Mock private ActivityTaskManagerInternal mMockActivityTaskManagerInternal; |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 90 | @Mock private IBinder mMockBinder; |
| 91 | @Mock private IAccessibilityServiceClient mMockServiceClient; |
| 92 | private AccessibilityUserState mUserState; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 93 | |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 94 | private MessageCapturingHandler mHandler = new MessageCapturingHandler(null); |
| 95 | private AccessibilityServiceConnection mAccessibilityServiceConnection; |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 96 | private AccessibilityManagerService mA11yms; |
| 97 | |
| 98 | @Override |
| 99 | protected void setUp() throws Exception { |
| 100 | MockitoAnnotations.initMocks(this); |
| 101 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 102 | LocalServices.removeServiceForTest(ActivityTaskManagerInternal.class); |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 103 | LocalServices.addService( |
| 104 | WindowManagerInternal.class, mMockWindowManagerService); |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 105 | LocalServices.addService( |
| 106 | ActivityTaskManagerInternal.class, mMockActivityTaskManagerInternal); |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 107 | |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 108 | mA11yms = new AccessibilityManagerService( |
| 109 | InstrumentationRegistry.getContext(), |
| 110 | mMockPackageManager, |
| 111 | mMockSecurityPolicy, |
| 112 | mMockSystemActionPerformer, |
| 113 | mMockA11yWindowManager, |
| 114 | mMockA11yDisplayListener); |
| 115 | } |
| 116 | |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 117 | private void setupAccessibilityServiceConnection() { |
| 118 | when(mMockContext.getSystemService(Context.DISPLAY_SERVICE)).thenReturn( |
| 119 | InstrumentationRegistry.getContext().getSystemService( |
| 120 | Context.DISPLAY_SERVICE)); |
| 121 | mUserState = new AccessibilityUserState(UserHandle.USER_SYSTEM, mMockContext, mA11yms); |
| 122 | |
| 123 | when(mMockServiceInfo.getResolveInfo()).thenReturn(mMockResolveInfo); |
| 124 | mMockResolveInfo.serviceInfo = mock(ServiceInfo.class); |
| 125 | mMockResolveInfo.serviceInfo.applicationInfo = mock(ApplicationInfo.class); |
| 126 | |
| 127 | when(mMockBinder.queryLocalInterface(any())).thenReturn(mMockServiceClient); |
| 128 | mAccessibilityServiceConnection = new AccessibilityServiceConnection( |
| 129 | mUserState, |
| 130 | mMockContext, |
| 131 | COMPONENT_NAME, |
| 132 | mMockServiceInfo, |
| 133 | SERVICE_ID, |
| 134 | mHandler, |
| 135 | new Object(), |
| 136 | mMockSecurityPolicy, |
| 137 | mMockSystemSupport, |
| 138 | mMockWindowManagerService, |
| 139 | mMockSystemActionPerformer, |
| 140 | mMockA11yWindowManager, |
| 141 | mMockActivityTaskManagerInternal); |
| 142 | mAccessibilityServiceConnection.bindLocked(); |
| 143 | mAccessibilityServiceConnection.onServiceConnected(COMPONENT_NAME, mMockBinder); |
| 144 | } |
| 145 | |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 146 | @SmallTest |
| 147 | public void testRegisterSystemActionWithoutPermission() throws Exception { |
| 148 | doThrow(SecurityException.class).when(mMockSecurityPolicy).enforceCallingPermission( |
| 149 | Manifest.permission.MANAGE_ACCESSIBILITY, |
| 150 | AccessibilityManagerService.FUNCTION_REGISTER_SYSTEM_ACTION); |
| 151 | |
| 152 | try { |
| 153 | mA11yms.registerSystemAction(TEST_ACTION, ACTION_ID); |
| 154 | fail(); |
| 155 | } catch (SecurityException expected) { |
| 156 | } |
| 157 | verify(mMockSystemActionPerformer, never()).registerSystemAction(ACTION_ID, TEST_ACTION); |
| 158 | } |
| 159 | |
| 160 | @SmallTest |
| 161 | public void testRegisterSystemAction() throws Exception { |
| 162 | mA11yms.registerSystemAction(TEST_ACTION, ACTION_ID); |
| 163 | verify(mMockSystemActionPerformer).registerSystemAction(ACTION_ID, TEST_ACTION); |
| 164 | } |
| 165 | |
| 166 | @SmallTest |
| 167 | public void testUnregisterSystemActionWithoutPermission() throws Exception { |
| 168 | doThrow(SecurityException.class).when(mMockSecurityPolicy).enforceCallingPermission( |
| 169 | Manifest.permission.MANAGE_ACCESSIBILITY, |
| 170 | AccessibilityManagerService.FUNCTION_UNREGISTER_SYSTEM_ACTION); |
| 171 | |
| 172 | try { |
| 173 | mA11yms.unregisterSystemAction(ACTION_ID); |
| 174 | fail(); |
| 175 | } catch (SecurityException expected) { |
| 176 | } |
| 177 | verify(mMockSystemActionPerformer, never()).unregisterSystemAction(ACTION_ID); |
| 178 | } |
| 179 | |
| 180 | @SmallTest |
| 181 | public void testUnregisterSystemAction() throws Exception { |
| 182 | mA11yms.unregisterSystemAction(ACTION_ID); |
| 183 | verify(mMockSystemActionPerformer).unregisterSystemAction(ACTION_ID); |
| 184 | } |
Hongming Jin | 8302c6c | 2020-01-10 17:45:18 -0800 | [diff] [blame] | 185 | |
| 186 | @SmallTest |
| 187 | public void testOnSystemActionsChanged() throws Exception { |
| 188 | setupAccessibilityServiceConnection(); |
| 189 | mA11yms.notifySystemActionsChangedLocked(mUserState); |
| 190 | verify(mMockServiceClient).onSystemActionsChanged(); |
| 191 | } |
Hongming Jin | bc76112 | 2019-12-10 10:32:58 -0800 | [diff] [blame] | 192 | } |