RootNodeFinder { public AccessibilityNodeInfo getRootNodeFromSource(AccessibilityNodeInfo source) { AccessibilityNodeInfo rootNode = null; if (source != null) { AccessibilityNodeInfo currentNode = source; while (true) { AccessibilityNodeInfo parent = currentNode.getParent(); if (parent == null) { rootNode = currentNode; break; } if (source != currentNode) { currentNode.recycle(); } currentNode = parent; } } return rootNode; }  AccessibilityNodeInfo getRootNodeFromSource(AccessibilityNodeInfo source);  }
@Test public void returnNullIfSourceIsNull() { Assert.assertNull(testSubject.getRootNodeFromSource(null)); }
@Test public void rootNodeExistsIfSourceExists() { Assert.assertNotNull(testSubject.getRootNodeFromSource(sourceMock)); }
@Test public void rootNodeIsSource() { AccessibilityNodeInfo rootNode = testSubject.getRootNodeFromSource(sourceMock); Assert.assertEquals(rootNode, sourceMock); }
@Test public void rootNodeIsSourceParent() { when(sourceMock.getParent()).thenReturn(parentMock); AccessibilityNodeInfo rootNode = testSubject.getRootNodeFromSource(sourceMock); Assert.assertEquals(rootNode, parentMock); }
@Test public void rootNodeIsSourceAncestor() { when(sourceMock.getParent()).thenReturn(parentMock); when(parentMock.getParent()).thenReturn(grandparentMock); AccessibilityNodeInfo rootNode = testSubject.getRootNodeFromSource(sourceMock); Assert.assertEquals(rootNode, grandparentMock); }
@Test public void uneededNodesGetRecycled() { when(sourceMock.getParent()).thenReturn(parentMock); when(parentMock.getParent()).thenReturn(grandparentMock); AccessibilityNodeInfo rootNode = testSubject.getRootNodeFromSource(sourceMock); verify(sourceMock, never()).recycle(); verify(rootNode, never()).recycle(); verify(parentMock, times(1)).recycle(); }