1 /*
2  * Copyright (C) 2010 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;
18 
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.anyInt;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.accessibilityservice.AccessibilityServiceInfo;
26 import android.os.UserHandle;
27 import android.test.AndroidTestCase;
28 import android.test.suitebuilder.annotation.LargeTest;
29 import android.test.suitebuilder.annotation.MediumTest;
30 import android.view.accessibility.AccessibilityEvent;
31 import android.view.accessibility.AccessibilityManager;
32 import android.view.accessibility.IAccessibilityManager;
33 import android.view.accessibility.IAccessibilityManagerClient;
34 
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * Tests for the AccessibilityManager which mocking the backing service.
43  */
44 public class AccessibilityManagerTest extends AndroidTestCase {
45 
46     /**
47      * Timeout required for pending Binder calls or event processing to
48      * complete.
49      */
50     public static final long TIMEOUT_BINDER_CALL = 50;
51 
52     @Mock
53     private IAccessibilityManager mMockService;
54 
55     @Override
setUp()56     public void setUp() throws Exception {
57         MockitoAnnotations.initMocks(this);
58     }
59 
createManager(boolean enabled)60     private AccessibilityManager createManager(boolean enabled) throws Exception {
61         if (enabled) {
62             when(mMockService.addClient(any(IAccessibilityManagerClient.class), anyInt()))
63                     .thenReturn(AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
64         } else {
65             when(mMockService.addClient(any(IAccessibilityManagerClient.class), anyInt()))
66                     .thenReturn(0);
67         }
68 
69         AccessibilityManager manager =
70                 new AccessibilityManager(mContext, mMockService, UserHandle.USER_CURRENT);
71 
72         verify(mMockService).addClient(any(IAccessibilityManagerClient.class), anyInt());
73 
74         return manager;
75     }
76 
77     @MediumTest
testGetAccessibilityServiceList()78     public void testGetAccessibilityServiceList() throws Exception {
79         // create a list of installed accessibility services the mock service returns
80         List<AccessibilityServiceInfo> expectedServices = new ArrayList<>();
81         AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
82         accessibilityServiceInfo.packageNames = new String[] { "foo.bar" };
83         expectedServices.add(accessibilityServiceInfo);
84 
85         // configure the mock service behavior
86         when(mMockService.getInstalledAccessibilityServiceList(anyInt()))
87                 .thenReturn(expectedServices);
88 
89         // invoke the method under test
90         AccessibilityManager manager = createManager(true);
91         List<AccessibilityServiceInfo> receivedServices =
92                 manager.getInstalledAccessibilityServiceList();
93 
94         verify(mMockService).getInstalledAccessibilityServiceList(UserHandle.USER_CURRENT);
95         // check expected result (list equals() compares it contents as well)
96         assertEquals("All expected services must be returned", expectedServices, receivedServices);
97     }
98 
99     @MediumTest
testInterrupt()100     public void testInterrupt() throws Exception {
101         AccessibilityManager manager = createManager(true);
102         manager.interrupt();
103 
104         verify(mMockService).interrupt(UserHandle.USER_CURRENT);
105     }
106 
107     @LargeTest
testIsEnabled()108     public void testIsEnabled() throws Exception {
109         // invoke the method under test
110         AccessibilityManager manager = createManager(true);
111         boolean isEnabledServiceEnabled = manager.isEnabled();
112 
113         // check expected result
114         assertTrue("Must be enabled since the mock service is enabled", isEnabledServiceEnabled);
115 
116         // disable accessibility
117         manager.getClient().setState(0);
118 
119         // wait for the asynchronous IBinder call to complete
120         Thread.sleep(TIMEOUT_BINDER_CALL);
121 
122         // invoke the method under test
123         boolean isEnabledServcieDisabled = manager.isEnabled();
124 
125         // check expected result
126         assertFalse("Must be disabled since the mock service is disabled",
127                 isEnabledServcieDisabled);
128     }
129 
130     @MediumTest
testSendAccessibilityEvent_AccessibilityEnabled()131     public void testSendAccessibilityEvent_AccessibilityEnabled() throws Exception {
132         AccessibilityEvent sentEvent = AccessibilityEvent.obtain();
133 
134         when(mMockService.sendAccessibilityEvent(eq(sentEvent), anyInt()))
135                 .thenReturn(true  /* should recycle event object */)
136                 .thenReturn(false /* should not recycle event object */);
137 
138         AccessibilityManager manager = createManager(true);
139         manager.sendAccessibilityEvent(sentEvent);
140 
141         assertSame("The event should be recycled.", sentEvent, AccessibilityEvent.obtain());
142 
143         manager.sendAccessibilityEvent(sentEvent);
144 
145         assertNotSame("The event should not be recycled.", sentEvent, AccessibilityEvent.obtain());
146     }
147 
148     @MediumTest
testSendAccessibilityEvent_AccessibilityDisabled()149     public void testSendAccessibilityEvent_AccessibilityDisabled() throws Exception {
150         AccessibilityEvent sentEvent = AccessibilityEvent.obtain();
151 
152         AccessibilityManager manager = createManager(false  /* disabled */);
153 
154         try {
155             manager.sendAccessibilityEvent(sentEvent);
156             fail("No accessibility events are sent if accessibility is disabled");
157         } catch (IllegalStateException ise) {
158             // check expected result
159             assertEquals("Accessibility off. Did you forget to check that?", ise.getMessage());
160         }
161     }
162 }
163