Phil Weaver | 015847a | 2017-07-28 08:43:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2017, 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 | |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | import static org.mockito.ArgumentMatchers.eq; |
| 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.content.pm.ServiceInfo; |
| 33 | import android.os.Handler; |
| 34 | import android.os.IBinder; |
| 35 | import android.os.Looper; |
| 36 | import android.os.RemoteException; |
| 37 | import android.os.UserHandle; |
| 38 | import android.view.WindowManagerInternal; |
| 39 | |
| 40 | import org.junit.Before; |
| 41 | import org.junit.BeforeClass; |
| 42 | import org.junit.Test; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | |
| 46 | import java.util.Arrays; |
| 47 | import java.util.HashSet; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Tests for AccessibilityServiceConnection |
| 52 | */ |
| 53 | public class AccessibilityServiceConnectionTest { |
| 54 | static final ComponentName COMPONENT_NAME = new ComponentName( |
| 55 | "com.android.server.accessibility", "AccessibilityServiceConnectionTest"); |
| 56 | static final int SERVICE_ID = 42; |
| 57 | |
| 58 | AccessibilityServiceConnection mConnection; |
| 59 | @Mock AccessibilityManagerService.UserState mMockUserState; |
| 60 | @Mock Context mMockContext; |
| 61 | @Mock AccessibilityServiceInfo mMockServiceInfo; |
| 62 | @Mock ResolveInfo mMockResolveInfo; |
| 63 | @Mock AccessibilityManagerService.SecurityPolicy mMockSecurityPolicy; |
| 64 | @Mock AccessibilityClientConnection.SystemSupport mMockSystemSupport; |
| 65 | @Mock WindowManagerInternal mMockWindowManagerInternal; |
| 66 | @Mock GlobalActionPerformer mMockGlobalActionPerformer; |
| 67 | @Mock KeyEventDispatcher mMockKeyEventDispatcher; |
| 68 | |
| 69 | |
| 70 | @BeforeClass |
| 71 | public static void oneTimeInitialization() { |
| 72 | if (Looper.myLooper() == null) { |
| 73 | Looper.prepare(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Before |
| 78 | public void setup() { |
| 79 | MockitoAnnotations.initMocks(this); |
| 80 | when(mMockSystemSupport.getKeyEventDispatcher()).thenReturn(mMockKeyEventDispatcher); |
| 81 | when(mMockServiceInfo.getResolveInfo()).thenReturn(mMockResolveInfo); |
| 82 | mMockResolveInfo.serviceInfo = mock(ServiceInfo.class); |
| 83 | mMockResolveInfo.serviceInfo.applicationInfo = mock(ApplicationInfo.class); |
| 84 | |
| 85 | mConnection = new AccessibilityServiceConnection(mMockUserState, mMockContext, |
| 86 | COMPONENT_NAME, mMockServiceInfo, SERVICE_ID, new Handler(), new Object(), |
| 87 | mMockSecurityPolicy, mMockSystemSupport, mMockWindowManagerInternal, |
| 88 | mMockGlobalActionPerformer); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void bind_requestsContextToBindService() { |
| 93 | mConnection.bindLocked(); |
| 94 | verify(mMockContext).bindServiceAsUser(any(Intent.class), eq(mConnection), |
| 95 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE), |
| 96 | any(UserHandle.class)); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void unbind_requestsContextToUnbindService() { |
| 101 | mConnection.unbindLocked(); |
| 102 | verify(mMockContext).unbindService(mConnection); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void bindConnectUnbind_linksAndUnlinksToServiceDeath() throws RemoteException { |
| 107 | IBinder mockBinder = mock(IBinder.class); |
| 108 | when(mMockUserState.getBindingServicesLocked()) |
| 109 | .thenReturn(new HashSet<>(Arrays.asList(COMPONENT_NAME))); |
| 110 | mConnection.bindLocked(); |
| 111 | mConnection.onServiceConnected(COMPONENT_NAME, mockBinder); |
| 112 | verify(mockBinder).linkToDeath(eq(mConnection), anyInt()); |
| 113 | mConnection.unbindLocked(); |
| 114 | verify(mockBinder).unlinkToDeath(eq(mConnection), anyInt()); |
| 115 | } |
| 116 | } |