summaryrefslogtreecommitdiffstats
path: root/services/tests/servicestests/src/com/android/server/AccessibilityManagerTest.java
blob: 1463d3055fdafde7c373128462403b1367a1a7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;

import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reportMatcher;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;

import org.easymock.IArgumentMatcher;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.pm.ServiceInfo;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.IAccessibilityManager;
import android.view.accessibility.IAccessibilityManagerClient;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests for the AccessibilityManager which mocking the backing service.
 */
public class AccessibilityManagerTest extends AndroidTestCase {

    /**
     * Timeout required for pending Binder calls or event processing to
     * complete.
     */
    public static final long TIMEOUT_BINDER_CALL = 50;

    /**
     * The reusable mock {@link IAccessibilityManager}.
     */
    private final IAccessibilityManager mMockServiceInterface =
        createStrictMock(IAccessibilityManager.class);

    @Override
    public void setUp() throws Exception {
        reset(mMockServiceInterface);
    }

    @MediumTest
    public void testGetAccessibilityServiceList() throws Exception {
        // create a list of installed accessibility services the mock service returns
        List<AccessibilityServiceInfo> expectedServices = new ArrayList<AccessibilityServiceInfo>();
        AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
        accessibilityServiceInfo.packageNames = new String[] { "foo.bar" };
        expectedServices.add(accessibilityServiceInfo);

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient())).andReturn(true);
        expect(mockServiceInterface.getInstalledAccessibilityServiceList()).andReturn(
                expectedServices);
        replay(mockServiceInterface);

        // invoke the method under test
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface);
        List<AccessibilityServiceInfo> receivedServices =
            manager.getInstalledAccessibilityServiceList();

        // check expected result (list equals() compares it contents as well)
        assertEquals("All expected services must be returned", receivedServices, expectedServices);

        // verify the mock service was properly called
        verify(mockServiceInterface);
    }

    @MediumTest
    public void testInterrupt() throws Exception {
        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient())).andReturn(true);
        mockServiceInterface.interrupt();
        replay(mockServiceInterface);

        // invoke the method under test
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface);
        manager.interrupt();

        // verify the mock service was properly called
        verify(mockServiceInterface);
    }

    @LargeTest
    public void testIsEnabled() throws Exception {
        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient())).andReturn(true);
        replay(mockServiceInterface);

        // invoke the method under test
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface);
        boolean isEnabledServiceEnabled = manager.isEnabled();

        // check expected result
        assertTrue("Must be enabled since the mock service is enabled", isEnabledServiceEnabled);

        // disable accessibility
        manager.getClient().setEnabled(false);

        // wait for the asynchronous IBinder call to complete
        Thread.sleep(TIMEOUT_BINDER_CALL);

        // invoke the method under test
        boolean isEnabledServcieDisabled = manager.isEnabled();

        // check expected result
        assertFalse("Must be disabled since the mock service is disabled",
                isEnabledServcieDisabled);

        // verify the mock service was properly called
        verify(mockServiceInterface);
    }

    @MediumTest
    public void testSendAccessibilityEvent_AccessibilityEnabled() throws Exception {
        // create an event to be dispatched
        AccessibilityEvent sentEvent = AccessibilityEvent.obtain();

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient())).andReturn(true);
        expect(mockServiceInterface.sendAccessibilityEvent(eqAccessibilityEvent(sentEvent)))
                .andReturn(true);
        expect(mockServiceInterface.sendAccessibilityEvent(eqAccessibilityEvent(sentEvent)))
                .andReturn(false);
        replay(mockServiceInterface);

        // invoke the method under test (manager and service in different processes)
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface);
        manager.sendAccessibilityEvent(sentEvent);

        // check expected result
        AccessibilityEvent nextEventDifferentProcesses = AccessibilityEvent.obtain();
        assertSame("The manager and the service are in different processes, so the event must be " +
                "recycled", sentEvent, nextEventDifferentProcesses);

        // invoke the method under test (manager and service in the same process)
        manager.sendAccessibilityEvent(sentEvent);

        // check expected result
        AccessibilityEvent nextEventSameProcess = AccessibilityEvent.obtain();
        assertNotSame("The manager and the service are in the same process, so the event must not" +
                "be recycled", sentEvent, nextEventSameProcess);

        // verify the mock service was properly called
        verify(mockServiceInterface);
    }

    @MediumTest
    public void testSendAccessibilityEvent_AccessibilityDisabled() throws Exception {
        // create an event to be dispatched
        AccessibilityEvent sentEvent = AccessibilityEvent.obtain();

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient())).andReturn(false);
        replay(mockServiceInterface);

        // invoke the method under test (accessibility disabled)
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface);
        try {
            manager.sendAccessibilityEvent(sentEvent);
            fail("No accessibility events are sent if accessibility is disabled");
        } catch (IllegalStateException ise) {
            // check expected result
            assertEquals("Accessibility off. Did you forget to check that?", ise.getMessage());
        }

        // verify the mock service was properly called
        verify(mockServiceInterface);
    }

    /**
     * Determines if an {@link AccessibilityEvent} passed as a method argument
     * matches expectations.
     *
     * @param matched The event to check.
     * @return True if expectations are matched.
     */
    private static AccessibilityEvent eqAccessibilityEvent(AccessibilityEvent matched) {
        reportMatcher(new AccessibilityEventMather(matched));
        return null;
    }

    /**
     * Determines if an {@link IAccessibilityManagerClient} passed as a method argument
     * matches expectations which in this case are that any instance is accepted.
     *
     * @return <code>null</code>.
     */
    private static IAccessibilityManagerClient anyIAccessibilityManagerClient() {
        reportMatcher(new AnyIAccessibilityManagerClientMather());
        return null;
    }

    /**
     * Matcher for {@link AccessibilityEvent}s.
     */
    private static class AccessibilityEventMather implements IArgumentMatcher {
        private AccessibilityEvent mExpectedEvent;

        public AccessibilityEventMather(AccessibilityEvent expectedEvent) {
            mExpectedEvent = expectedEvent;
        }

        public boolean matches(Object matched) {
            if (!(matched instanceof AccessibilityEvent)) {
                return false;
            }
            AccessibilityEvent receivedEvent = (AccessibilityEvent) matched;
            return mExpectedEvent.getEventType() == receivedEvent.getEventType();
        }

        public void appendTo(StringBuffer buffer) {
            buffer.append("sendAccessibilityEvent()");
            buffer.append(" with event type \"");
            buffer.append(mExpectedEvent.getEventType());
            buffer.append("\"");
        }
    }

    /**
     * Matcher for {@link IAccessibilityManagerClient}s.
     */
    private static class AnyIAccessibilityManagerClientMather implements IArgumentMatcher {
        public boolean matches(Object matched) {
            if (!(matched instanceof IAccessibilityManagerClient)) {
                return false;
            }
            return true;
        }

        public void appendTo(StringBuffer buffer) {
            buffer.append("addClient() with any IAccessibilityManagerClient");
        }
    }
}