ResultRequestFulfiller implements RequestFulfiller { public void fulfillRequest(RunnableFunction onRequestFulfilled) { screenshotController.getScreenshotWithMediaProjection( screenshot -> { try { AccessibilityNodeInfo source = eventHelper.claimLastSource(); AccessibilityNodeInfo rootNode = rootNodeFinder.getRootNodeFromSource(source); String content = getScanContent(rootNode, screenshot); responseWriter.writeSuccessfulResponse(content); if (rootNode != null && rootNode != source) { rootNode.recycle(); } if (source != null && !eventHelper.restoreLastSource(source)) { source.recycle(); } } catch (Exception e) { responseWriter.writeErrorResponse(e); } onRequestFulfilled.run(); }); } ResultRequestFulfiller(
      ResponseWriter responseWriter,
      RootNodeFinder rootNodeFinder,
      EventHelper eventHelper,
      AxeScanner axeScanner,
      ScreenshotController screenshotController); void fulfillRequest(RunnableFunction onRequestFulfilled);  }
@Test public void callsOnRequestFulfilled() { setupSuccessfulRequest(); testSubject.fulfillRequest(onRequestFulfilledMock); verifyOnRequestFulfilledCalled(); }
@Test public void callsGetScreenshotWithMediaProjection() { setupSuccessfulRequest(); testSubject.fulfillRequest(onRequestFulfilledMock); verify(screenshotController, times(1)).getScreenshotWithMediaProjection(any()); }
@Test public void requestHandledInsideGetScreenshotWithMediaProjection() { reset(screenshotController); testSubject.fulfillRequest(onRequestFulfilledMock); verifyZeroInteractions(responseWriter); verifyZeroInteractions(onRequestFulfilledMock); }
@Test public void writesSuccessfulResponse() { setupSuccessfulRequest(); testSubject.fulfillRequest(onRequestFulfilledMock); verify(responseWriter, times(1)).writeSuccessfulResponse(scanResultJson); }
@Test public void recyclesNodes() { setupSuccessfulRequest(); testSubject.fulfillRequest(onRequestFulfilledMock); verify(rootNode, times(1)).recycle(); verify(sourceNode, times(1)).recycle(); }
@Test public void recyclesNodeOnceIfRootEqualsSource() throws ViewChangedException { setupSuccessfulRequest(); reset(rootNodeFinder); reset(axeScanner); when(rootNodeFinder.getRootNodeFromSource(any())).thenReturn(sourceNode); when(axeScanner.scanWithAxe(eq(sourceNode), any())).thenReturn(axeResultMock); testSubject.fulfillRequest(onRequestFulfilledMock); verifyZeroInteractions(rootNode); verify(sourceNode, times(1)).recycle(); }
@Test public void writesErrorIfNoRootNode() { when(rootNodeFinder.getRootNodeFromSource(null)).thenReturn(null); testSubject.fulfillRequest(onRequestFulfilledMock); verify(responseWriter, times(1)) .writeErrorResponse(argThat((e) -> e.getMessage() == "Unable to locate root node to scan")); verifyOnRequestFulfilledCalled(); }
@Test public void writesErrorIfScanFailed() throws ViewChangedException { when(eventHelper.claimLastSource()).thenReturn(sourceNode); when(rootNodeFinder.getRootNodeFromSource(any())).thenReturn(rootNode); when(axeScanner.scanWithAxe(eq(rootNode), any())).thenReturn(null); testSubject.fulfillRequest(onRequestFulfilledMock); verify(responseWriter, times(1)) .writeErrorResponse(argThat((e) -> e.getMessage() == "Scanner returned no data")); verifyOnRequestFulfilledCalled(); }
@Test public void doesNotRecycleSourceIfRestoreLastSourceSucceeds() { setupSuccessfulRequest(); when(eventHelper.restoreLastSource(sourceNode)).thenReturn(true); testSubject.fulfillRequest(onRequestFulfilledMock); verify(rootNode, times(1)).recycle(); verify(sourceNode, never()).recycle(); }