blob: d3a8deee799ecea421bdc4395007ff8bebbace33 [file] [log] [blame]
Phil Weaver015847a2017-07-28 08:43:39 -07001/*
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
17package com.android.server.accessibility;
18
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.ArgumentMatchers.eq;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.accessibilityservice.AccessibilityServiceInfo;
27import android.content.ComponentName;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ResolveInfo;
32import android.content.pm.ServiceInfo;
33import android.os.Handler;
34import android.os.IBinder;
35import android.os.Looper;
36import android.os.RemoteException;
37import android.os.UserHandle;
38import android.view.WindowManagerInternal;
39
40import org.junit.Before;
41import org.junit.BeforeClass;
42import org.junit.Test;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46import java.util.Arrays;
47import java.util.HashSet;
48
49
50/**
51 * Tests for AccessibilityServiceConnection
52 */
53public 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}