Example usage for android.support.v4.view.accessibility AccessibilityNodeInfoCompat obtain

List of usage examples for android.support.v4.view.accessibility AccessibilityNodeInfoCompat obtain

Introduction

In this page you can find the example usage for android.support.v4.view.accessibility AccessibilityNodeInfoCompat obtain.

Prototype

public static AccessibilityNodeInfoCompat obtain() 

Source Link

Document

Returns a cached instance if such is available otherwise a new one.

Usage

From source file:Main.java

/**
 * Determines if any of the provided {@link View}'s and {@link AccessibilityNodeInfoCompat}'s
 * ancestors can receive accessibility focus
 *
 * @param view The {@link View} to evaluate
 * @param node The {@link AccessibilityNodeInfoCompat} to evaluate
 * @return {@code true} if an ancestor of may receive accessibility focus
 *///w w  w.  j a  va  2  s  .co  m
public static boolean hasFocusableAncestor(@Nullable AccessibilityNodeInfoCompat node, @Nullable View view) {
    if (node == null || view == null) {
        return false;
    }

    ViewParent parentView = ViewCompat.getParentForAccessibility(view);
    if (!(parentView instanceof View)) {
        return false;
    }

    AccessibilityNodeInfoCompat parentNode = AccessibilityNodeInfoCompat.obtain();
    try {
        ViewCompat.onInitializeAccessibilityNodeInfo((View) parentView, parentNode);
        if (parentNode == null) {
            return false;
        }

        if (isAccessibilityFocusable(parentNode, (View) parentView)) {
            return true;
        }

        if (hasFocusableAncestor(parentNode, (View) parentView)) {
            return true;
        }
    } finally {
        parentNode.recycle();
    }
    return false;
}

From source file:Main.java

/**
 * Determines if the supplied {@link View} and {@link AccessibilityNodeInfoCompat} has any
 * children which are not independently accessibility focusable and also have a spoken
 * description.//  w w  w . j  a v a2 s . c  o m
 * <p>
 * NOTE: Accessibility services will include these children's descriptions in the closest
 * focusable ancestor.
 *
 * @param view The {@link View} to evaluate
 * @param node The {@link AccessibilityNodeInfoCompat} to evaluate
 * @return {@code true} if it has any non-actionable speaking descendants within its subtree
 */
public static boolean hasNonActionableSpeakingDescendants(@Nullable AccessibilityNodeInfoCompat node,
        @Nullable View view) {

    if (node == null || view == null || !(view instanceof ViewGroup)) {
        return false;
    }

    ViewGroup viewGroup = (ViewGroup) view;
    for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
        View childView = viewGroup.getChildAt(i);

        if (childView == null) {
            continue;
        }

        AccessibilityNodeInfoCompat childNode = AccessibilityNodeInfoCompat.obtain();
        try {
            ViewCompat.onInitializeAccessibilityNodeInfo(childView, childNode);

            if (isAccessibilityFocusable(childNode, childView)) {
                continue;
            }

            if (isSpeakingNode(childNode, childView)) {
                return true;
            }
        } finally {
            childNode.recycle();
        }
    }

    return false;
}

From source file:com.googlecode.eyesfree.brailleback.IMENavigationModeTest.java

/**
 * Tests the behaviour of the "inactive" mode.
 * This should be the mode used as long as input is not bound.
 *//*w w w .j  av  a  2s  . c  o  m*/
public void testInactiveMode() {
    mIMENavMode.onActivate();
    verify(mSelfBrailleManager).setImeOpen(false);
    mIMENavMode.onCreateIME();
    verify(mNext).onActivate();

    assertEquals(mBrailleTranslator, mIMENavMode.getBrailleTranslator());
    assertNull(mIMENavMode.getDisplayManager());
    assertEquals(mFeedbackManager, mIMENavMode.getFeedbackManager());

    AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onObserveAccessibilityEvent(accessibilityEvent);
        verify(mNext).onObserveAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onAccessibilityEvent(accessibilityEvent);
        verify(mNext).onAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    try {
        mIMENavMode.onInvalidateAccessibilityNode(node);
        verify(mNext).onInvalidateAccessibilityNode(node);
    } finally {
        node.recycle();
    }

    DisplayManager.Content content = new DisplayManager.Content("");
    mIMENavMode.onPanLeftOverflow(content);
    verify(mNext).onPanLeftOverflow(content);
    mIMENavMode.onPanRightOverflow(content);
    verify(mNext).onPanRightOverflow(content);

    BrailleInputEvent inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_ENTER, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendAndroidKey(anyInt());

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_NAV_ITEM_NEXT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).moveCursor(anyInt(), anyInt());

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ACTIVATE_CURRENT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendDefaultAction();

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ROUTE, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).route(anyInt(), any(DisplayManager.Content.class));

    mIMENavMode.onDestroyIME();
    verify(mSelfBrailleManager, never()).setImeOpen(true);
    Mockito.reset(mSelfBrailleManager);

    // Deactivate, but make sure it didn't happen too early.
    verify(mNext, never()).onDeactivate();
    mIMENavMode.onDeactivate();
    verify(mNext).onDeactivate();
    verify(mSelfBrailleManager).setImeOpen(false);
}

From source file:com.android.yijiang.kzx.widget.betterpickers.TouchExplorationHelper.java

@Override
public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) {
    if (virtualViewId == View.NO_ID) {
        return getNodeForParent();
    }//from  w  ww  .jav a  2s.c  om

    final T item = getItemForId(virtualViewId);
    if (item == null) {
        return null;
    }

    final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    populateNodeForItemInternal(item, node);
    return node;
}

From source file:com.googlecode.eyesfree.brailleback.IMENavigationModeTest.java

/**
 * Tests the behaviour of the "text only" mode.
 * Used when there's an input connection, but input is not started.
 *///from  w  ww  .  j av  a  2 s  . c  o  m
public void testTextOnlyMode() {
    mIMENavMode.onActivate();
    mIMENavMode.onCreateIME();
    verify(mNext).onActivate();
    mIMENavMode.onBindInput();

    assertEquals(mBrailleTranslator, mIMENavMode.getBrailleTranslator());
    assertNull(mIMENavMode.getDisplayManager());
    assertEquals(mFeedbackManager, mIMENavMode.getFeedbackManager());

    AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onObserveAccessibilityEvent(accessibilityEvent);
        verify(mNext).onObserveAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onAccessibilityEvent(accessibilityEvent);
        verify(mNext).onAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    try {
        mIMENavMode.onInvalidateAccessibilityNode(node);
        verify(mNext).onInvalidateAccessibilityNode(node);
    } finally {
        node.recycle();
    }

    DisplayManager.Content content = new DisplayManager.Content("");
    mIMENavMode.onPanLeftOverflow(content);
    verify(mNext).onPanLeftOverflow(content);
    mIMENavMode.onPanRightOverflow(content);
    verify(mNext).onPanRightOverflow(content);

    BrailleInputEvent inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_ENTER, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_ENTER);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_DEL, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_DEL);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_BRAILLE_KEY, 0x1b, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).handleBrailleKey(0x1b);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_NAV_ITEM_NEXT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).moveCursor(anyInt(), anyInt());

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ACTIVATE_CURRENT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendDefaultAction();

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ROUTE, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).route(anyInt(), any(DisplayManager.Content.class));

    mIMENavMode.onUnbindInput();
    mIMENavMode.onDestroyIME();
    verify(mSelfBrailleManager, atLeastOnce()).setImeOpen(false);
    verify(mSelfBrailleManager, never()).setImeOpen(true);

    // Deactivate, but make sure it didn't happen too early.
    verify(mNext, never()).onDeactivate();
    mIMENavMode.onDeactivate();
    verify(mNext).onDeactivate();
}

From source file:com.facebook.stetho.inspector.elements.android.AccessibilityNodeInfoWrapper.java

@Nullable
public static CharSequence getDescription(AccessibilityNodeInfoCompat node, View view) {
    CharSequence contentDescription = node.getContentDescription();
    CharSequence nodeText = node.getText();

    boolean hasNodeText = !TextUtils.isEmpty(nodeText);
    boolean isEditText = view instanceof EditText;

    // EditText's prioritize their own text content over a contentDescription
    if (!TextUtils.isEmpty(contentDescription) && (!isEditText || !hasNodeText)) {
        return contentDescription;
    }// ww w  .  j a v a 2s . com

    if (hasNodeText) {
        return nodeText;
    }

    // If there are child views and no contentDescription the text of all non-focusable children,
    // comma separated, becomes the description.
    if (view instanceof ViewGroup) {
        final StringBuilder concatChildDescription = new StringBuilder();
        final String separator = ", ";
        ViewGroup viewGroup = (ViewGroup) view;

        for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
            final View child = viewGroup.getChildAt(i);

            AccessibilityNodeInfoCompat childNodeInfo = AccessibilityNodeInfoCompat.obtain();
            ViewCompat.onInitializeAccessibilityNodeInfo(child, childNodeInfo);

            CharSequence childNodeDescription = null;
            if (AccessibilityUtil.isSpeakingNode(childNodeInfo, child)
                    && !AccessibilityUtil.isAccessibilityFocusable(childNodeInfo, child)) {
                childNodeDescription = getDescription(childNodeInfo, child);
            }

            if (!TextUtils.isEmpty(childNodeDescription)) {
                if (concatChildDescription.length() > 0) {
                    concatChildDescription.append(separator);
                }
                concatChildDescription.append(childNodeDescription);
            }
            childNodeInfo.recycle();
        }

        return concatChildDescription.length() > 0 ? concatChildDescription.toString() : null;
    }

    return null;
}

From source file:com.googlecode.eyesfree.brailleback.IMENavigationModeTest.java

/**
 * Tests the behaviour of the "text only" mode.
 * Also used when input is started, but the field is not handled
 * specially (as with, for example, a focused EditText).
 *///from   w  w  w.  j a v a 2 s  . c  o  m
public void testTextOnlyModeInputStarted() {
    EditorInfo ei = new EditorInfo();

    // Mock out the AccessibilityNodeInfo.
    // The class actually uses the compat variant, but on recent API
    // releases (including the test environment) they should call through.
    AccessibilityNodeInfo rawNode = mock(AccessibilityNodeInfo.class);
    when(mAccessibilityService.getRootInActiveWindow()).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)).thenReturn(rawNode);
    when(rawNode.getClassName()).thenReturn("com.example.UnknownWidget");

    mIMENavMode.onActivate();
    mIMENavMode.onCreateIME();
    verify(mNext).onActivate();
    mIMENavMode.onBindInput();
    mIMENavMode.onStartInput(ei, false /* restarting */);
    Mockito.reset(mSelfBrailleManager);
    mIMENavMode.onStartInputView(ei, false /* restarting */);
    verify(mSelfBrailleManager).setImeOpen(true);

    assertEquals(mBrailleTranslator, mIMENavMode.getBrailleTranslator());
    assertNull(mIMENavMode.getDisplayManager());
    assertEquals(mFeedbackManager, mIMENavMode.getFeedbackManager());

    AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onObserveAccessibilityEvent(accessibilityEvent);
        verify(mNext).onObserveAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onAccessibilityEvent(accessibilityEvent);
        verify(mNext).onAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    try {
        mIMENavMode.onInvalidateAccessibilityNode(node);
        verify(mNext).onInvalidateAccessibilityNode(node);
    } finally {
        node.recycle();
    }

    DisplayManager.Content content = new DisplayManager.Content("");
    mIMENavMode.onPanLeftOverflow(content);
    verify(mNext).onPanLeftOverflow(content);
    mIMENavMode.onPanRightOverflow(content);
    verify(mNext).onPanRightOverflow(content);

    BrailleInputEvent inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_ENTER, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_ENTER);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_DEL, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_DEL);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_BRAILLE_KEY, 0x1b, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).handleBrailleKey(0x1b);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_NAV_ITEM_NEXT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).moveCursor(anyInt(), anyInt());

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ACTIVATE_CURRENT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendDefaultAction();

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ROUTE, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).route(anyInt(), any(DisplayManager.Content.class));

    verify(mSelfBrailleManager, never()).setImeOpen(false);
    Mockito.reset(mSelfBrailleManager);

    mIMENavMode.onFinishInputView(true);
    mIMENavMode.onFinishInput();
    mIMENavMode.onUnbindInput();
    mIMENavMode.onDestroyIME();

    verify(mSelfBrailleManager, never()).setImeOpen(true);
    verify(mSelfBrailleManager, atLeastOnce()).setImeOpen(false);

    // Deactivate, but make sure it didn't happen too early.
    verify(mNext, never()).onDeactivate();
    mIMENavMode.onDeactivate();
    verify(mNext).onDeactivate();
}

From source file:com.android.utils.ExploreByTouchHelper.java

/**
 * Constructs and returns an {@link AccessibilityNodeInfoCompat} for the
 * specified item. Automatically manages accessibility focus actions.
 * <p>/*from w w w .  jav a2  s .  co m*/
 * Allows the implementing class to specify most node properties, but
 * overrides the following:
 * <ul>
 * <li>{@link AccessibilityNodeInfoCompat#setPackageName}
 * <li>{@link AccessibilityNodeInfoCompat#setClassName}
 * <li>{@link AccessibilityNodeInfoCompat#setParent(View)}
 * <li>{@link AccessibilityNodeInfoCompat#setSource(View, int)}
 * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
 * <li>{@link AccessibilityNodeInfoCompat#setBoundsInScreen(Rect)}
 * </ul>
 * <p>
 * Uses the bounds of the parent view and the parent-relative bounding
 * rectangle specified by
 * {@link AccessibilityNodeInfoCompat#getBoundsInParent} to automatically
 * update the following properties:
 * <ul>
 * <li>{@link AccessibilityNodeInfoCompat#setVisibleToUser}
 * <li>{@link AccessibilityNodeInfoCompat#setBoundsInParent(Rect)}
 * </ul>
 *
 * @param virtualViewId The virtual view id for item for which to construct
 *            a node.
 * @return An {@link AccessibilityNodeInfoCompat} for the specified item.
 */
private AccessibilityNodeInfoCompat getNodeForVirtualViewId(int virtualViewId) {
    final AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();

    // Ensure the client has good defaults.
    node.setEnabled(true);
    node.setClassName(mHost.getClass().getName() + DEFAULT_CLASS_NAME);

    // Allow the client to populate the node.
    populateNodeForVirtualViewId(virtualViewId, node);

    if (TextUtils.isEmpty(node.getText()) && TextUtils.isEmpty(node.getContentDescription())) {
        throw new RuntimeException(
                "You must add text or a content description in populateNodeForVirtualViewId()");
    }

    // Don't allow the client to override these properties.
    node.setPackageName(mHost.getContext().getPackageName());
    node.setParent(mHost, ROOT_ID);
    node.setSource(mHost, virtualViewId);

    // Manage internal accessibility focus state.
    if (mFocusedVirtualViewId == virtualViewId) {
        node.setAccessibilityFocused(true);
        node.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
    } else {
        node.setAccessibilityFocused(false);
        node.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
    }

    node.getBoundsInParent(mTempParentRect);
    if (mTempParentRect.isEmpty()) {
        throw new RuntimeException("You must set parent bounds in populateNodeForVirtualViewId()");
    }

    // Set the visibility based on the parent bound.
    if (intersectVisibleToUser(mTempParentRect)) {
        node.setVisibleToUser(true);
        node.setBoundsInParent(mTempParentRect);
    }

    // Calculate screen-relative bound.
    mHost.getLocationOnScreen(mTempGlobalRect);
    final int offsetX = mTempGlobalRect[0];
    final int offsetY = mTempGlobalRect[1];
    mTempScreenRect.set(mTempParentRect);
    mTempScreenRect.offset(offsetX, offsetY);
    node.setBoundsInScreen(mTempScreenRect);

    return node;
}

From source file:com.googlecode.eyesfree.brailleback.IMENavigationModeTest.java

/**
 * Tests that text and navigation mode is not triggered when the input view
 * is not shown, even if input is started. This distinction is needed to
 * handle Chrome correctly./*from www .j  av  a 2 s . co  m*/
 */
public void testTextAndNavigationModeRequiresStartInputView() {
    EditorInfo ei = new EditorInfo();

    // Mock out the AccessibilityNodeInfo.
    // The class actually uses the compat variant, but on recent API
    // releases (including the test environment) they should call through.
    AccessibilityNodeInfo rawNode = mock(AccessibilityNodeInfo.class);
    when(mAccessibilityService.getRootInActiveWindow()).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)).thenReturn(rawNode);
    when(rawNode.getClassName()).thenReturn("com.example.ExampleWebView");
    when(mSelfBrailleManager.hasContentForNode(compatWrapperForNode(rawNode))).thenReturn(true);

    mIMENavMode.onActivate();
    mIMENavMode.onCreateIME();
    verify(mNext).onActivate();
    mIMENavMode.onBindInput();
    mIMENavMode.onStartInput(ei, false /* restarting */);

    assertEquals(mBrailleTranslator, mIMENavMode.getBrailleTranslator());
    assertNull(mIMENavMode.getDisplayManager());
    assertEquals(mFeedbackManager, mIMENavMode.getFeedbackManager());
    verify(mSelfBrailleManager, atLeastOnce()).setImeOpen(false);

    AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onObserveAccessibilityEvent(accessibilityEvent);
        verify(mNext).onObserveAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onAccessibilityEvent(accessibilityEvent);
        verify(mNext).onAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    try {
        mIMENavMode.onInvalidateAccessibilityNode(node);
        verify(mNext).onInvalidateAccessibilityNode(node);
    } finally {
        node.recycle();
    }

    DisplayManager.Content content = new DisplayManager.Content("");
    mIMENavMode.onPanLeftOverflow(content);
    verify(mNext).onPanLeftOverflow(content);
    mIMENavMode.onPanRightOverflow(content);
    verify(mNext).onPanRightOverflow(content);

    BrailleInputEvent inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_ENTER, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_ENTER);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_DEL, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_DEL);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_BRAILLE_KEY, 0x1b, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).handleBrailleKey(0x1b);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_NAV_ITEM_NEXT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).moveCursor(anyInt(), anyInt());

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ACTIVATE_CURRENT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendDefaultAction();

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ROUTE, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).route(anyInt(), any(DisplayManager.Content.class));

    mIMENavMode.onFinishInput();
    mIMENavMode.onUnbindInput();
    mIMENavMode.onDestroyIME();

    verify(mSelfBrailleManager, never()).setImeOpen(true);

    // Deactivate, but make sure it didn't happen too early.
    verify(mNext, never()).onDeactivate();
    mIMENavMode.onDeactivate();
    verify(mNext).onDeactivate();
}

From source file:com.googlecode.eyesfree.brailleback.IMENavigationModeTest.java

/**
 * Tests the behaviour of the "text and navigation" mode.
 *//* w w w  .  j  a v  a2 s. c o  m*/
public void testTextAndNavigationMode() {
    EditorInfo ei = new EditorInfo();

    // Mock out the AccessibilityNodeInfo.
    // The class actually uses the compat variant, but on recent API
    // releases (including the test environment) they should call through.
    AccessibilityNodeInfo rawNode = mock(AccessibilityNodeInfo.class);
    when(mAccessibilityService.getRootInActiveWindow()).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)).thenReturn(rawNode);
    when(rawNode.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)).thenReturn(rawNode);
    when(rawNode.getClassName()).thenReturn("com.example.ExampleWebView");
    when(mSelfBrailleManager.hasContentForNode(compatWrapperForNode(rawNode))).thenReturn(true);

    mIMENavMode.onActivate();
    mIMENavMode.onCreateIME();
    verify(mNext).onActivate();
    mIMENavMode.onBindInput();
    mIMENavMode.onStartInput(ei, false /* restarting */);
    verify(mSelfBrailleManager, atLeastOnce()).setImeOpen(false);
    verify(mSelfBrailleManager, never()).setImeOpen(true);
    mIMENavMode.onStartInputView(ei, false /* restarting */);

    assertEquals(mBrailleTranslator, mIMENavMode.getBrailleTranslator());
    assertNull(mIMENavMode.getDisplayManager());
    assertEquals(mFeedbackManager, mIMENavMode.getFeedbackManager());
    verify(mSelfBrailleManager, atLeastOnce()).setImeOpen(true);

    AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onObserveAccessibilityEvent(accessibilityEvent);
        verify(mNext).onObserveAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    accessibilityEvent = AccessibilityEvent.obtain();
    try {
        mIMENavMode.onAccessibilityEvent(accessibilityEvent);
        verify(mNext).onAccessibilityEvent(accessibilityEvent);
    } finally {
        accessibilityEvent.recycle();
    }

    AccessibilityNodeInfoCompat node = AccessibilityNodeInfoCompat.obtain();
    try {
        mIMENavMode.onInvalidateAccessibilityNode(node);
        verify(mNext).onInvalidateAccessibilityNode(node);
    } finally {
        node.recycle();
    }

    DisplayManager.Content content = new DisplayManager.Content("");
    mIMENavMode.onPanLeftOverflow(content);
    verify(mNext).onPanLeftOverflow(content);
    mIMENavMode.onPanRightOverflow(content);
    verify(mNext).onPanRightOverflow(content);

    BrailleInputEvent inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_ENTER, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_ENTER);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_KEY_DEL, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).sendAndroidKey(KeyEvent.KEYCODE_DEL);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_BRAILLE_KEY, 0x1b, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext, never()).onMappedInputEvent(inputEvent, content);
    verify(mIME).handleBrailleKey(0x1b);

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ACTIVATE_CURRENT, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).sendDefaultAction();

    inputEvent = new BrailleInputEvent(BrailleInputEvent.CMD_ROUTE, 0, 0);
    mIMENavMode.onMappedInputEvent(inputEvent, content);
    verify(mNext).onMappedInputEvent(inputEvent, content);
    verify(mIME, never()).route(anyInt(), any(DisplayManager.Content.class));

    // Nothing above this point should have triggered an action on the
    // accessibility node.
    verify(rawNode, never()).performAction(anyInt(), any(Bundle.class));
    verifyEventCausesNavigation(BrailleInputEvent.CMD_NAV_ITEM_NEXT, ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
            MOVEMENT_GRANULARITY_CHARACTER, rawNode, content);
    verifyEventCausesNavigation(BrailleInputEvent.CMD_NAV_ITEM_PREVIOUS,
            ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, MOVEMENT_GRANULARITY_CHARACTER, rawNode, content);
    verifyEventCausesNavigation(BrailleInputEvent.CMD_NAV_LINE_NEXT, ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
            MOVEMENT_GRANULARITY_LINE, rawNode, content);
    verifyEventCausesNavigation(BrailleInputEvent.CMD_NAV_LINE_PREVIOUS,
            ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, MOVEMENT_GRANULARITY_LINE, rawNode, content);

    Mockito.reset(mSelfBrailleManager);

    mIMENavMode.onFinishInputView(true);
    verify(mSelfBrailleManager).setImeOpen(false);
    mIMENavMode.onFinishInput();
    mIMENavMode.onUnbindInput();
    mIMENavMode.onDestroyIME();
    verify(mSelfBrailleManager, never()).setImeOpen(true);

    // Deactivate, but make sure it didn't happen too early.
    verify(mNext, never()).onDeactivate();
    mIMENavMode.onDeactivate();
    verify(mNext).onDeactivate();
}