FileDocCategorySizeDatePackage
AccessibilityManagerTest.javaAPI DocAndroid 5.1 API11179Thu Mar 12 22:22:42 GMT 2015com.android.server

AccessibilityManagerTest

public class AccessibilityManagerTest extends android.test.AndroidTestCase
Tests for the AccessibilityManager which mocking the backing service.

Fields Summary
public static final long
TIMEOUT_BINDER_CALL
Timeout required for pending Binder calls or event processing to complete.
private final android.view.accessibility.IAccessibilityManager
mMockServiceInterface
The reusable mock {@link IAccessibilityManager}.
Constructors Summary
Methods Summary
private static android.view.accessibility.IAccessibilityManagerClientanyIAccessibilityManagerClient()
Determines if an {@link IAccessibilityManagerClient} passed as a method argument matches expectations which in this case are that any instance is accepted.

return
null.

        reportMatcher(new AnyIAccessibilityManagerClientMather());
        return null;
    
private static android.view.accessibility.AccessibilityEventeqAccessibilityEvent(android.view.accessibility.AccessibilityEvent matched)
Determines if an {@link AccessibilityEvent} passed as a method argument matches expectations.

param
matched The event to check.
return
True if expectations are matched.

        reportMatcher(new AccessibilityEventMather(matched));
        return null;
    
public voidsetUp()


    
         
        reset(mMockServiceInterface);
    
public voidtestGetAccessibilityServiceList()

        // 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(),
                UserHandle.USER_OWNER)).andReturn(
                AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
        expect(mockServiceInterface.getInstalledAccessibilityServiceList(UserHandle.USER_OWNER))
                .andReturn(expectedServices);
        replay(mockServiceInterface);

        // invoke the method under test
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
                UserHandle.USER_OWNER);
        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);
    
public voidtestInterrupt()

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
                UserHandle.USER_OWNER)).andReturn(
                        AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
        mockServiceInterface.interrupt(UserHandle.USER_OWNER);
        replay(mockServiceInterface);

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

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

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
                UserHandle.USER_OWNER)).andReturn(
                        AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED);
        replay(mockServiceInterface);

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

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

        // disable accessibility
        manager.getClient().setState(0);

        // 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);
    
public voidtestSendAccessibilityEvent_AccessibilityDisabled()

        // create an event to be dispatched
        AccessibilityEvent sentEvent = AccessibilityEvent.obtain();

        // configure the mock service behavior
        IAccessibilityManager mockServiceInterface = mMockServiceInterface;
        expect(mockServiceInterface.addClient(anyIAccessibilityManagerClient(),
                UserHandle.USER_OWNER)).andReturn(0);
        replay(mockServiceInterface);

        // invoke the method under test (accessibility disabled)
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
                UserHandle.USER_OWNER);
        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);
    
public voidtestSendAccessibilityEvent_AccessibilityEnabled()

        // create an event to be dispatched
        AccessibilityEvent sentEvent = AccessibilityEvent.obtain();

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

        // invoke the method under test (manager and service in different processes)
        AccessibilityManager manager = new AccessibilityManager(mContext, mockServiceInterface,
                UserHandle.USER_OWNER);
        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);