Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
feat(focus-mvp-android-device): updates vishualizations when elements…
Browse files Browse the repository at this point in the history
… not present (#88)
  • Loading branch information
waabid authored Mar 29, 2021
1 parent fb31dc3 commit b8b0cfc
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public AccessibilityEventDispatcher() {
public static List<Integer> redrawEventTypes =
Arrays.asList(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
AccessibilityEvent.TYPE_VIEW_SCROLLED,
AccessibilityEvent.TYPE_WINDOWS_CHANGED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FocusElementHighlight {
private HashMap<String, Paint> paints;
private Rect rect;
private View view;
private static final String TAG = "FocusElementHighlight";

public FocusElementHighlight(
AccessibilityNodeInfo eventSource,
Expand All @@ -33,23 +34,26 @@ public FocusElementHighlight(
this.radius = radius;
this.rect = new Rect();
this.paints = currentPaints;
this.updateWithNewCoordinates();
}

private void setCoordinates() {
if (this.eventSource == null) {
return;
}
if (!this.eventSource.refresh()) {
return;
}
this.eventSource.getBoundsInScreen(this.rect);
this.rect.offset(0, this.yOffset);
this.xCoordinate = rect.centerX();
this.yCoordinate = rect.centerY();
}

public void drawElementHighlight(Canvas canvas) {
if (this.eventSource == null) {
return;
}

if (!this.eventSource.refresh()) {
return;
}

this.updateWithNewCoordinates();

this.drawInnerCircle(
this.xCoordinate, this.yCoordinate, this.radius, this.paints.get("innerCircle"), canvas);
this.drawNumberInCircle(
Expand Down Expand Up @@ -85,7 +89,7 @@ public AccessibilityNodeInfo getEventSource() {
return this.eventSource;
}

public void updateWithNewCoordinates() {
private void updateWithNewCoordinates() {
this.yOffset = OffsetHelper.getYOffset(this.view);
this.setCoordinates();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ public FocusElementLine(
this.paints = Paints;
this.currentRect = new Rect();
this.prevRect = new Rect();
this.updateWithNewCoordinates();
}

public void drawLine(Canvas canvas) {
this.drawConnectingLine(
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("backgroundLine"), canvas);
this.drawConnectingLine(
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("foregroundLine"), canvas);
}

private void setCoordinates() {
if (this.eventSource == null || this.previousEventSource == null) {
return;
}

if (!this.eventSource.refresh() || !this.previousEventSource.refresh()) {
return;
}

this.updateWithNewCoordinates();
this.drawConnectingLine(
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("backgroundLine"), canvas);
this.drawConnectingLine(
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("foregroundLine"), canvas);
}

private void setCoordinates() {
this.eventSource.getBoundsInScreen(this.currentRect);
this.currentRect.offset(0, this.yOffset);

Expand All @@ -73,7 +74,7 @@ public void setPaint(HashMap<String, Paint> paints) {
this.paints = paints;
}

public void updateWithNewCoordinates() {
private void updateWithNewCoordinates() {
this.yOffset = OffsetHelper.getYOffset(this.view);
this.setCoordinates();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public FocusVisualizer(
}

public void refreshHighlights() {
this.updateDrawingsWithNewCoordinates();
this.focusVisualizationCanvas.redraw();
}

public void addNewFocusedElement(AccessibilityEvent event) {
Expand Down Expand Up @@ -96,14 +96,4 @@ private void setDrawItemsAndRedraw() {
this.focusVisualizationCanvas.setDrawItems(this.focusElementHighlights, this.focusElementLines);
this.focusVisualizationCanvas.redraw();
}

private void updateDrawingsWithNewCoordinates() {
for (int i = 0; i < this.focusElementHighlights.size(); i++) {
this.focusElementHighlights.get(i).updateWithNewCoordinates();
}
for (int i = 0; i < this.focusElementLines.size(); i++) {
this.focusElementLines.get(i).updateWithNewCoordinates();
}
this.focusVisualizationCanvas.redraw();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void onRedrawEventListenerFiresOnRedrawEvents() {
reset(eventMock);
});

verify(onRedrawEventListenerMock, times(3)).accept(eventMock);
verify(onRedrawEventListenerMock, times(4)).accept(eventMock);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

Expand Down Expand Up @@ -42,10 +41,11 @@ public class FocusElementHighlightTest {
@Mock Resources resourcesMock;
@Mock Rect rectMock;
@Mock Canvas canvasMock;
HashMap<String, Paint> paintsStub;

@Before
public void prepare() throws Exception {
HashMap<String, Paint> paintsStub = new HashMap<>();
paintsStub = new HashMap<>();
paintsStub.put("innerCircle", paintMock);
paintsStub.put("outerCircle", paintMock);
paintsStub.put("number", paintMock);
Expand All @@ -63,19 +63,6 @@ public void returnsNotNull() {
Assert.assertNotNull(testSubject);
}

@Test
public void followsCorrectStepsToUpdateCoordinates() throws Exception {
mockStatic(OffsetHelper.class);

FocusElementHighlight elementSpy = spy(testSubject);
elementSpy.updateWithNewCoordinates();

verifyStatic(OffsetHelper.class, times(1));
OffsetHelper.getYOffset(any(View.class));

verifyPrivate(elementSpy, times(1)).invoke("setCoordinates");
}

@Test
public void setPaintsWorksProperly() {
HashMap<String, Paint> testPaintsStub = new HashMap<>();
Expand All @@ -88,8 +75,23 @@ public void setPaintsWorksProperly() {
Assert.assertNull(resultingPaintsHashMap.get("innerCircle"));
}

@Test
public void drawElementHighlightDoesNothingWhenEventSourceIsNull() {
testSubject = new FocusElementHighlight(null, paintsStub, 10, 10, viewMock);
testSubject.drawElementHighlight(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void drawElementHighlightDoesNothingWhenEventSourceRefreshDoesNotWork() {
when(accessibilityNodeInfoMock.refresh()).thenReturn(false);
testSubject.drawElementHighlight(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void drawElementHighlightCallsAllRelevantDrawMethods() throws Exception {
when(accessibilityNodeInfoMock.refresh()).thenReturn(true);
FocusElementHighlight elementSpy = spy(testSubject);
elementSpy.drawElementHighlight(canvasMock);
verifyPrivate(elementSpy, times(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import android.content.res.Resources;
Expand All @@ -27,7 +26,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.verification.VerificationModeFactory;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
Expand All @@ -38,16 +36,18 @@ public class FocusElementLineTest {

FocusElementLine testSubject;

@Mock AccessibilityNodeInfo accessibilityNodeInfoMock;
@Mock AccessibilityNodeInfo eventSourceMock;
@Mock AccessibilityNodeInfo previousEventSourceMock;
@Mock Paint paintMock;
@Mock View viewMock;
@Mock Resources resourcesMock;
@Mock Rect rectMock;
@Mock Canvas canvasMock;
HashMap<String, Paint> paintsStub;

@Before
public void prepare() throws Exception {
HashMap<String, Paint> paintsStub = new HashMap<>();
paintsStub = new HashMap<>();
paintsStub.put("foregroundLine", paintMock);
paintsStub.put("backgroundLine", paintMock);

Expand All @@ -56,29 +56,19 @@ public void prepare() throws Exception {
doNothing().when(rectMock).offset(isA(Integer.class), isA(Integer.class));

testSubject =
new FocusElementLine(
accessibilityNodeInfoMock, accessibilityNodeInfoMock, paintsStub, viewMock);
new FocusElementLine(eventSourceMock, previousEventSourceMock, paintsStub, viewMock);
}

@Test
public void returnsNotNull() {
Assert.assertNotNull(testSubject);
}

@Test
public void followsCorrectStepsToUpdateCoordinates() throws Exception {
mockStatic(OffsetHelper.class);
FocusElementLine lineSpy = spy(testSubject);
lineSpy.updateWithNewCoordinates();

verifyStatic(OffsetHelper.class, VerificationModeFactory.times(1));
OffsetHelper.getYOffset(any(View.class));

verifyPrivate(lineSpy, times(1)).invoke("setCoordinates");
}

@Test
public void drawLineCallsCorrectPrivateMethod() throws Exception {
when(eventSourceMock.refresh()).thenReturn(true);
when(previousEventSourceMock.refresh()).thenReturn(true);

FocusElementLine lineSpy = spy(testSubject);
lineSpy.drawLine(canvasMock);
verifyPrivate(lineSpy, times(2))
Expand All @@ -92,6 +82,34 @@ public void drawLineCallsCorrectPrivateMethod() throws Exception {
any(Canvas.class));
}

@Test
public void drawLineDoesNothingWhenEventSourceIsNull() {
testSubject = new FocusElementLine(null, previousEventSourceMock, paintsStub, viewMock);
testSubject.drawLine(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void drawLineDoesNothingWhenPreviousEventSourceIsNull() {
testSubject = new FocusElementLine(eventSourceMock, null, paintsStub, viewMock);
testSubject.drawLine(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void drawLineDoesNothingWhenPreviousEventSourceDoesNotRefresh() {
when(previousEventSourceMock.refresh()).thenReturn(false);
testSubject.drawLine(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void drawLineDoesNothingWhenEventSourceDoesNotRefresh() {
when(eventSourceMock.refresh()).thenReturn(false);
testSubject.drawLine(canvasMock);
verifyNoInteractions(canvasMock);
}

@Test
public void setPaintWorksProperly() {
HashMap<String, Paint> paintsStub2 = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.accessibilityinsightsforandroidservice;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
Expand Down Expand Up @@ -108,4 +109,10 @@ public void resetVisualizationsDoesTheJob() {
Assert.assertEquals(resultingLineList.size(), 0);
Assert.assertEquals(resultingTabStopCount, 0);
}

@Test
public void refreshHighlightsCallsRedraw() {
testSubject.refreshHighlights();
verify(focusVisualizationCanvasMock).redraw();
}
}

0 comments on commit b8b0cfc

Please sign in to comment.