AxeViewsFactory { public AxeView createAxeViews(AccessibilityNodeInfo rootNode) throws ViewChangedException { return buildAxeViewsWithRetries(rootNode, maxRetries); } AxeViewsFactory(
      NodeViewBuilderFactory nodeViewBuilderFactory,
      AccessibilityNodeInfoQueueBuilder queueBuilder); AxeView createAxeViews(AccessibilityNodeInfo rootNode);  }
@Test public void axeViewIsNotNull() throws ViewChangedException { enqueueNode(rootNodeMock); Assert.assertNotNull(testSubject.createAxeViews(rootNodeMock)); }
@Test public void createsAxeViewWithoutChildren() throws ViewChangedException { enqueueNode(rootNodeMock); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); verify(nodeViewBuilderFactoryMock, times(1)) .createNodeViewBuilder(eq(rootNodeMock), eq(new ArrayList<>()), eq(null)); }
@Test public void createsAxeViewWithChildNode() throws ViewChangedException { when(rootNodeMock.getChildCount()).thenReturn(1); when(rootNodeMock.getChild(0)).thenReturn(childNodeMock); enqueueNode(childNodeMock); enqueueNode(rootNodeMock); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); ArrayList<AxeView> children = new ArrayList<>(); children.add(childViewMock); verify(nodeViewBuilderFactoryMock, times(1)) .createNodeViewBuilder(eq(rootNodeMock), eq(children), eq(null)); verify(nodeViewBuilderFactoryMock, times(1)) .createNodeViewBuilder(eq(childNodeMock), eq(new ArrayList<>()), eq(null)); }
@Test public void createsAxeViewWithLabeledByNode() throws ViewChangedException { when(rootNodeMock.getLabeledBy()).thenReturn(labelNodeMock); enqueueNode(labelNodeMock); enqueueNode(rootNodeMock); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); verify(nodeViewBuilderFactoryMock, times(1)) .createNodeViewBuilder(eq(rootNodeMock), eq(new ArrayList<>()), eq(labelViewMock)); verify(nodeViewBuilderFactoryMock, times(1)) .createNodeViewBuilder(eq(labelNodeMock), eq(new ArrayList<>()), eq(null)); }
@Test public void refreshAndRetryIfViewChanged() throws ViewChangedException { setupViewChangedScenario(true, false); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); verify(rootNodeMock, times(1)).refresh(); verify(queueBuilderMock, times(2)).buildPriorityQueue(rootNodeMock); verify(rootNodeMock, times(2)).getChildCount(); verify(rootNodeMock, times(2)).getChild(0); }
@Test public void retriesFiveTimes() { setupViewChangedScenario(false, false); int numRetries = 5; try { testSubject.createAxeViews(rootNodeMock); Assert.fail("Expected createAxeViews to throw exception"); } catch (ViewChangedException e) { verify(rootNodeMock, times(numRetries)).refresh(); verify(queueBuilderMock, times(numRetries + 1)).buildPriorityQueue(rootNodeMock); verify(rootNodeMock, times(numRetries + 1)).getChildCount(); verify(rootNodeMock, times(numRetries + 1)).getChild(0); } }
@Test public void recyclesNodesOnSuccess() throws ViewChangedException { when(rootNodeMock.getChildCount()).thenReturn(1); when(rootNodeMock.getChild(0)).thenReturn(childNodeMock); when(rootNodeMock.getLabeledBy()).thenReturn(labelNodeMock); enqueueNode(labelNodeMock); enqueueNode(childNodeMock); enqueueNode(rootNodeMock); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); verify(childNodeMock, times(1)).recycle(); verify(labelNodeMock, times(1)).recycle(); verify(rootNodeMock, never()).recycle(); }
@Test public void recyclesNodesOnRetry() throws ViewChangedException { setupViewChangedScenario(true, true); AxeView axeView = testSubject.createAxeViews(rootNodeMock); Assert.assertNotNull(axeView); Assert.assertEquals(axeView, rootViewMock); verify(childNodeMock, times(2)).recycle(); verify(labelNodeMock, times(2)).recycle(); verify(rootNodeMock, never()).recycle(); }
@Test public void recyclesNodesOnRetryFailure() { setupViewChangedScenario(false, true); int numRetries = 5; try { testSubject.createAxeViews(rootNodeMock); Assert.fail("Expected createAxeViews to throw exception"); } catch (ViewChangedException e) { verify(childNodeMock, times(numRetries + 1)).recycle(); verify(labelNodeMock, times(numRetries + 1)).recycle(); verify(rootNodeMock, never()).recycle(); } }