@Test
  public void lastDescentWithChildren_shouldReturnTrueAndSetItselfForLastChildren() {
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);
    parentNode.setContentDescription("Parent");
    AccessibilityNodeInfo child1Node = AccessibilityNodeInfo.obtain();
    child1Node.setVisibleToUser(true);
    child1Node.setContentDescription("Child1");
    AccessibilityNodeInfo child2Node = AccessibilityNodeInfo.obtain();
    child2Node.setVisibleToUser(true);
    child2Node.setContentDescription("Child2");

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child1Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child2Node);

    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(parentCompat);
    System.out.println("lastDescentWithChildren_shouldReturnTrueAndSetItselfForLastChildren");
    assertTrue(ref.lastDescendant());
    assertEquals(child2Node, ref.get().getInfo());
    parentNode.recycle();
    child1Node.recycle();
    child2Node.recycle();
    ref.recycle();
  }
  @Before
  public void setUp() {
    ShadowAccessibilityNodeInfo.resetObtainedInstances();
    mCompat1 = new SwitchAccessNodeCompat(AccessibilityNodeInfo.obtain());
    mShadowInfo1 =
        (ShadowAccessibilityNodeInfo)
            ShadowExtractor.extract((AccessibilityNodeInfo) mCompat1.getInfo());
    mCompat2 = new SwitchAccessNodeCompat(AccessibilityNodeInfo.obtain());
    mShadowInfo2 =
        (ShadowAccessibilityNodeInfo)
            ShadowExtractor.extract((AccessibilityNodeInfo) mCompat2.getInfo());
    mSharedPreferences.edit().clear().commit();
    MockitoAnnotations.initMocks(this);
    mCompat1.setBoundsInScreen(NODE_BOUNDS_1);
    mCompat2.setBoundsInScreen(NODE_BOUNDS_2);
    mActionNode1 =
        new AccessibilityNodeActionNode(
            mCompat1,
            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                AccessibilityNodeInfoCompat.ACTION_CLICK, "label1"));
    mActionNode2 =
        new AccessibilityNodeActionNode(
            mCompat2,
            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                AccessibilityNodeInfoCompat.ACTION_CLICK, "label2"));
    mSelectionNode = new OptionScanSelectionNode(mActionNode1, mActionNode2);

    when(mOverlayController.getContext()).thenReturn(mContext);
    mOptionManager = new OptionManager(mOverlayController);
  }
 @Test
 public void testScrollWithParent_shouldScrollAndClearFocus() {
   AccessibilityNodeInfoCompat parent = AccessibilityNodeInfoCompat.obtain();
   ((ShadowAccessibilityNodeInfoCompat) ShadowExtractor.extract(parent)).addChild(mCompat1);
   ShadowAccessibilityNodeInfo shadowParent =
       (ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parent.getInfo());
   parent.setScrollable(true);
   mActionNode1.recycle();
   mActionNode1 =
       new AccessibilityNodeActionNode(
           mCompat1,
           new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
               AccessibilityNodeInfoCompat.ACTION_CLICK, "label1"));
   mSelectionNode = new OptionScanSelectionNode(mActionNode1, mActionNode2);
   mOptionManager.clearFocusIfNewTree(mSelectionNode);
   mOptionManager.selectOption(0);
   mOptionManager.performScrollAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
   assertEquals(0, mShadowInfo1.getPerformedActions().size());
   List<Integer> parentActions = shadowParent.getPerformedActions();
   assertEquals(1, parentActions.size());
   assertEquals(
       new Integer(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD), parentActions.get(0));
   verify(mOverlayController, times(3)).clearOverlay();
   parent.recycle();
 }
  @Override
  @Implementation
  public boolean equals(Object object) {
    if (!(object instanceof AccessibilityEvent)) {
      return false;
    }

    final AccessibilityEvent event = (AccessibilityEvent) object;
    final ShadowAccessibilityEvent otherShadow =
        (ShadowAccessibilityEvent) ShadowExtractor.extract(event);

    boolean areEqual = (eventType == otherShadow.eventType);
    areEqual &= (enabled == otherShadow.enabled);
    areEqual &= TextUtils.equals(contentDescription, otherShadow.contentDescription);
    areEqual &= TextUtils.equals(packageName, otherShadow.packageName);
    areEqual &= TextUtils.equals(className, otherShadow.className);
    boolean parcelableDataEqual = false;
    if (getParcelableData() == null && otherShadow.getParcelableData() == null) {
      parcelableDataEqual = true;
    } else if (getParcelableData().equals(otherShadow.getParcelableData())) {
      parcelableDataEqual = true;
    }
    areEqual &= parcelableDataEqual;

    return areEqual;
  }
  @Test
  public void parentWithSelfReferencedParent_shouldReturnFalse() {
    AccessibilityNodeInfo curNode = AccessibilityNodeInfo.obtain();
    curNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    curNode.setContentDescription("Current");
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).addChild(curNode);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).setVisibleToUser(true);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(curNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertFalse(ref.parent());
    parentNode.recycle();
    curNode.recycle();
    ref.recycle();
  }
 @Implementation
 public boolean invert(Matrix inverse) {
   final SimpleMatrix inverseMatrix = mMatrix.invert();
   if (inverseMatrix != null) {
     if (inverse != null) {
       final ShadowMatrix shadowInverse = (ShadowMatrix) ShadowExtractor.extract(inverse);
       shadowInverse.mMatrix = inverseMatrix;
     }
     return true;
   }
   return false;
 }
  @Implementation
  public static AccessibilityEvent obtain(AccessibilityEvent event) {
    ShadowAccessibilityEvent shadowEvent =
        ((ShadowAccessibilityEvent) ShadowExtractor.extract(event));
    AccessibilityEvent obtainedInstance = shadowEvent.getClone();

    sAllocationCount++;
    StrictEqualityEventWrapper wrapper = new StrictEqualityEventWrapper(obtainedInstance);
    obtainedInstances.put(wrapper, Thread.currentThread().getStackTrace());
    orderedInstances.put(sAllocationCount, wrapper);
    return obtainedInstance;
  }
  @Test
  public void lastDescentWithLoop_shouldReturnFalse() {
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);
    AccessibilityNodeInfo child1Node = AccessibilityNodeInfo.obtain();
    child1Node.setVisibleToUser(true);
    AccessibilityNodeInfo child2Node = AccessibilityNodeInfo.obtain();
    child2Node.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child1Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(child2Node);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(child2Node)).addChild(parentNode);

    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(parentCompat);
    assertFalse(ref.lastDescendant());
    parentNode.recycle();
    child1Node.recycle();
    child2Node.recycle();
    ref.recycle();
  }
  @Test
  public void parentWithLoopedParents_shouldReturnFalse() {
    AccessibilityNodeInfo curNode = AccessibilityNodeInfo.obtain();
    curNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(curNode);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).addChild(parentNode);

    /* Set nodes to be invisible to force the the code to traverse the loop */
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).setVisibleToUser(false);
    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(curNode)).setVisibleToUser(false);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(curNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertFalse(ref.parent());
    parentNode.recycle();
    curNode.recycle();
    ref.recycle();
  }
  @Implementation
  public static AccessibilityEvent obtain(int eventType) {
    // We explicitly avoid allocating the AccessibilityEvent from the actual pool by using
    // the private constructor. Not doing so affects test suites which use both shadow and
    // non-shadow objects.
    final AccessibilityEvent obtainedInstance =
        ReflectionHelpers.callConstructor(AccessibilityEvent.class);
    final ShadowAccessibilityEvent shadowObtained =
        ((ShadowAccessibilityEvent) ShadowExtractor.extract(obtainedInstance));

    sAllocationCount++;
    StrictEqualityEventWrapper wrapper = new StrictEqualityEventWrapper(obtainedInstance);
    obtainedInstances.put(wrapper, Thread.currentThread().getStackTrace());
    orderedInstances.put(sAllocationCount, wrapper);
    shadowObtained.eventType = eventType;
    return obtainedInstance;
  }
  /** @return A shallow copy. */
  private AccessibilityEvent getClone() {
    // We explicitly avoid allocating the AccessibilityEvent from the actual pool by using
    // the private constructor. Not doing so affects test suites which use both shadow and
    // non-shadow objects.
    final AccessibilityEvent newEvent = ReflectionHelpers.callConstructor(AccessibilityEvent.class);
    final ShadowAccessibilityEvent newShadow =
        (ShadowAccessibilityEvent) ShadowExtractor.extract(newEvent);

    newShadow.eventType = eventType;
    newShadow.contentDescription = contentDescription;
    newShadow.packageName = packageName;
    newShadow.className = className;
    newShadow.enabled = enabled;
    newShadow.setParcelableData(getParcelableData());

    return newEvent;
  }
  /**
   * Check for leaked objects that were {@code obtain}ed but never {@code recycle}d.
   *
   * @param printUnrecycledEventsToSystemErr - if true, stack traces of calls to {@code obtain} that
   *     lack matching calls to {@code recycle} are dumped to System.err.
   * @return {@code true} if there are unrecycled events
   */
  public static boolean areThereUnrecycledEvents(boolean printUnrecycledEventsToSystemErr) {
    if (printUnrecycledEventsToSystemErr) {
      for (final StrictEqualityEventWrapper wrapper : obtainedInstances.keySet()) {
        final ShadowAccessibilityEvent shadow =
            ((ShadowAccessibilityEvent) ShadowExtractor.extract(wrapper.mEvent));

        System.err.println(
            String.format(
                "Leaked AccessibilityEvent. Stack trace of allocation:",
                shadow.getContentDescription()));
        for (final StackTraceElement stackTraceElement : obtainedInstances.get(wrapper)) {
          System.err.println(stackTraceElement.toString());
        }
      }
    }

    return (obtainedInstances.size() != 0);
  }
  @Test
  public void parentWithParents_shouldReturnTrueAndSetItselfForParent() {
    AccessibilityNodeInfo currentNode = AccessibilityNodeInfo.obtain();
    currentNode.setVisibleToUser(true);
    AccessibilityNodeInfo parentNode = AccessibilityNodeInfo.obtain();
    parentNode.setVisibleToUser(true);

    ((ShadowAccessibilityNodeInfo) ShadowExtractor.extract(parentNode)).addChild(currentNode);

    AccessibilityNodeInfoCompat currentCompat = new AccessibilityNodeInfoCompat(currentNode);
    AccessibilityNodeInfoCompat parentCompat = new AccessibilityNodeInfoCompat(parentNode);

    AccessibilityNodeInfoRef ref = AccessibilityNodeInfoRef.obtain(currentCompat);
    assertTrue(ref.parent());
    assertEquals(parentCompat, ref.get());
    parentNode.recycle();
    currentNode.recycle();
    ref.recycle();
  }
 private static ShadowDialog shadowOf(Dialog dialog) {
   return (ShadowDialog) ShadowExtractor.extract(dialog);
 }
Beispiel #15
0
 private static SimpleMatrix getSimpleMatrix(Matrix matrix) {
   final ShadowMatrix otherMatrix = (ShadowMatrix) ShadowExtractor.extract(matrix);
   return otherMatrix.mMatrix;
 }
 public void attachToRecyclerView(RecyclerView recyclerView) {
   ((ShadowRecyclerView) ShadowExtractor.extract(recyclerView))
       .setItemTouchHelperCallback(this.callback);
 }