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

Commit b8b0cfc

Browse files
authored
feat(focus-mvp-android-device): updates vishualizations when elements not present (#88)
1 parent fb31dc3 commit b8b0cfc

File tree

8 files changed

+87
-64
lines changed

8 files changed

+87
-64
lines changed

AccessibilityInsightsForAndroidService/app/src/main/java/com/microsoft/accessibilityinsightsforandroidservice/AccessibilityEventDispatcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public AccessibilityEventDispatcher() {
2727
public static List<Integer> redrawEventTypes =
2828
Arrays.asList(
2929
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
30+
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
3031
AccessibilityEvent.TYPE_VIEW_SCROLLED,
3132
AccessibilityEvent.TYPE_WINDOWS_CHANGED);
3233

AccessibilityInsightsForAndroidService/app/src/main/java/com/microsoft/accessibilityinsightsforandroidservice/FocusElementHighlight.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class FocusElementHighlight {
2020
private HashMap<String, Paint> paints;
2121
private Rect rect;
2222
private View view;
23+
private static final String TAG = "FocusElementHighlight";
2324

2425
public FocusElementHighlight(
2526
AccessibilityNodeInfo eventSource,
@@ -33,23 +34,26 @@ public FocusElementHighlight(
3334
this.radius = radius;
3435
this.rect = new Rect();
3536
this.paints = currentPaints;
36-
this.updateWithNewCoordinates();
3737
}
3838

3939
private void setCoordinates() {
40-
if (this.eventSource == null) {
41-
return;
42-
}
43-
if (!this.eventSource.refresh()) {
44-
return;
45-
}
4640
this.eventSource.getBoundsInScreen(this.rect);
4741
this.rect.offset(0, this.yOffset);
4842
this.xCoordinate = rect.centerX();
4943
this.yCoordinate = rect.centerY();
5044
}
5145

5246
public void drawElementHighlight(Canvas canvas) {
47+
if (this.eventSource == null) {
48+
return;
49+
}
50+
51+
if (!this.eventSource.refresh()) {
52+
return;
53+
}
54+
55+
this.updateWithNewCoordinates();
56+
5357
this.drawInnerCircle(
5458
this.xCoordinate, this.yCoordinate, this.radius, this.paints.get("innerCircle"), canvas);
5559
this.drawNumberInCircle(
@@ -85,7 +89,7 @@ public AccessibilityNodeInfo getEventSource() {
8589
return this.eventSource;
8690
}
8791

88-
public void updateWithNewCoordinates() {
92+
private void updateWithNewCoordinates() {
8993
this.yOffset = OffsetHelper.getYOffset(this.view);
9094
this.setCoordinates();
9195
}

AccessibilityInsightsForAndroidService/app/src/main/java/com/microsoft/accessibilityinsightsforandroidservice/FocusElementLine.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,25 @@ public FocusElementLine(
3434
this.paints = Paints;
3535
this.currentRect = new Rect();
3636
this.prevRect = new Rect();
37-
this.updateWithNewCoordinates();
3837
}
3938

4039
public void drawLine(Canvas canvas) {
41-
this.drawConnectingLine(
42-
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("backgroundLine"), canvas);
43-
this.drawConnectingLine(
44-
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("foregroundLine"), canvas);
45-
}
46-
47-
private void setCoordinates() {
4840
if (this.eventSource == null || this.previousEventSource == null) {
4941
return;
5042
}
43+
5144
if (!this.eventSource.refresh() || !this.previousEventSource.refresh()) {
5245
return;
5346
}
5447

48+
this.updateWithNewCoordinates();
49+
this.drawConnectingLine(
50+
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("backgroundLine"), canvas);
51+
this.drawConnectingLine(
52+
this.xStart, this.yStart, this.xEnd, this.yEnd, this.paints.get("foregroundLine"), canvas);
53+
}
54+
55+
private void setCoordinates() {
5556
this.eventSource.getBoundsInScreen(this.currentRect);
5657
this.currentRect.offset(0, this.yOffset);
5758

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

76-
public void updateWithNewCoordinates() {
77+
private void updateWithNewCoordinates() {
7778
this.yOffset = OffsetHelper.getYOffset(this.view);
7879
this.setCoordinates();
7980
}

AccessibilityInsightsForAndroidService/app/src/main/java/com/microsoft/accessibilityinsightsforandroidservice/FocusVisualizer.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public FocusVisualizer(
2525
}
2626

2727
public void refreshHighlights() {
28-
this.updateDrawingsWithNewCoordinates();
28+
this.focusVisualizationCanvas.redraw();
2929
}
3030

3131
public void addNewFocusedElement(AccessibilityEvent event) {
@@ -96,14 +96,4 @@ private void setDrawItemsAndRedraw() {
9696
this.focusVisualizationCanvas.setDrawItems(this.focusElementHighlights, this.focusElementLines);
9797
this.focusVisualizationCanvas.redraw();
9898
}
99-
100-
private void updateDrawingsWithNewCoordinates() {
101-
for (int i = 0; i < this.focusElementHighlights.size(); i++) {
102-
this.focusElementHighlights.get(i).updateWithNewCoordinates();
103-
}
104-
for (int i = 0; i < this.focusElementLines.size(); i++) {
105-
this.focusElementLines.get(i).updateWithNewCoordinates();
106-
}
107-
this.focusVisualizationCanvas.redraw();
108-
}
10999
}

AccessibilityInsightsForAndroidService/app/src/test/java/com/microsoft/accessibilityinsightsforandroidservice/AccessibilityEventDispatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void onRedrawEventListenerFiresOnRedrawEvents() {
114114
reset(eventMock);
115115
});
116116

117-
verify(onRedrawEventListenerMock, times(3)).accept(eventMock);
117+
verify(onRedrawEventListenerMock, times(4)).accept(eventMock);
118118
}
119119

120120
@Test

AccessibilityInsightsForAndroidService/app/src/test/java/com/microsoft/accessibilityinsightsforandroidservice/FocusElementHighlightTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import static org.mockito.ArgumentMatchers.any;
77
import static org.mockito.ArgumentMatchers.anyInt;
88
import static org.mockito.ArgumentMatchers.isA;
9+
import static org.mockito.Mockito.verifyNoInteractions;
910
import static org.mockito.internal.verification.VerificationModeFactory.times;
1011
import static org.powermock.api.mockito.PowerMockito.doNothing;
11-
import static org.powermock.api.mockito.PowerMockito.mockStatic;
1212
import static org.powermock.api.mockito.PowerMockito.spy;
1313
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
14-
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
1514
import static org.powermock.api.mockito.PowerMockito.when;
1615
import static org.powermock.api.mockito.PowerMockito.whenNew;
1716

@@ -42,10 +41,11 @@ public class FocusElementHighlightTest {
4241
@Mock Resources resourcesMock;
4342
@Mock Rect rectMock;
4443
@Mock Canvas canvasMock;
44+
HashMap<String, Paint> paintsStub;
4545

4646
@Before
4747
public void prepare() throws Exception {
48-
HashMap<String, Paint> paintsStub = new HashMap<>();
48+
paintsStub = new HashMap<>();
4949
paintsStub.put("innerCircle", paintMock);
5050
paintsStub.put("outerCircle", paintMock);
5151
paintsStub.put("number", paintMock);
@@ -63,19 +63,6 @@ public void returnsNotNull() {
6363
Assert.assertNotNull(testSubject);
6464
}
6565

66-
@Test
67-
public void followsCorrectStepsToUpdateCoordinates() throws Exception {
68-
mockStatic(OffsetHelper.class);
69-
70-
FocusElementHighlight elementSpy = spy(testSubject);
71-
elementSpy.updateWithNewCoordinates();
72-
73-
verifyStatic(OffsetHelper.class, times(1));
74-
OffsetHelper.getYOffset(any(View.class));
75-
76-
verifyPrivate(elementSpy, times(1)).invoke("setCoordinates");
77-
}
78-
7966
@Test
8067
public void setPaintsWorksProperly() {
8168
HashMap<String, Paint> testPaintsStub = new HashMap<>();
@@ -88,8 +75,23 @@ public void setPaintsWorksProperly() {
8875
Assert.assertNull(resultingPaintsHashMap.get("innerCircle"));
8976
}
9077

78+
@Test
79+
public void drawElementHighlightDoesNothingWhenEventSourceIsNull() {
80+
testSubject = new FocusElementHighlight(null, paintsStub, 10, 10, viewMock);
81+
testSubject.drawElementHighlight(canvasMock);
82+
verifyNoInteractions(canvasMock);
83+
}
84+
85+
@Test
86+
public void drawElementHighlightDoesNothingWhenEventSourceRefreshDoesNotWork() {
87+
when(accessibilityNodeInfoMock.refresh()).thenReturn(false);
88+
testSubject.drawElementHighlight(canvasMock);
89+
verifyNoInteractions(canvasMock);
90+
}
91+
9192
@Test
9293
public void drawElementHighlightCallsAllRelevantDrawMethods() throws Exception {
94+
when(accessibilityNodeInfoMock.refresh()).thenReturn(true);
9395
FocusElementHighlight elementSpy = spy(testSubject);
9496
elementSpy.drawElementHighlight(canvasMock);
9597
verifyPrivate(elementSpy, times(1))

AccessibilityInsightsForAndroidService/app/src/test/java/com/microsoft/accessibilityinsightsforandroidservice/FocusElementLineTest.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import static org.mockito.ArgumentMatchers.anyInt;
88
import static org.mockito.ArgumentMatchers.isA;
99
import static org.mockito.Mockito.times;
10+
import static org.mockito.Mockito.verifyNoInteractions;
1011
import static org.mockito.Mockito.when;
1112
import static org.powermock.api.mockito.PowerMockito.doNothing;
12-
import static org.powermock.api.mockito.PowerMockito.mockStatic;
1313
import static org.powermock.api.mockito.PowerMockito.spy;
1414
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
15-
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
1615
import static org.powermock.api.mockito.PowerMockito.whenNew;
1716

1817
import android.content.res.Resources;
@@ -27,7 +26,6 @@
2726
import org.junit.Test;
2827
import org.junit.runner.RunWith;
2928
import org.mockito.Mock;
30-
import org.mockito.internal.verification.VerificationModeFactory;
3129
import org.powermock.core.classloader.annotations.PrepareForTest;
3230
import org.powermock.modules.junit4.PowerMockRunner;
3331
import org.powermock.reflect.Whitebox;
@@ -38,16 +36,18 @@ public class FocusElementLineTest {
3836

3937
FocusElementLine testSubject;
4038

41-
@Mock AccessibilityNodeInfo accessibilityNodeInfoMock;
39+
@Mock AccessibilityNodeInfo eventSourceMock;
40+
@Mock AccessibilityNodeInfo previousEventSourceMock;
4241
@Mock Paint paintMock;
4342
@Mock View viewMock;
4443
@Mock Resources resourcesMock;
4544
@Mock Rect rectMock;
4645
@Mock Canvas canvasMock;
46+
HashMap<String, Paint> paintsStub;
4747

4848
@Before
4949
public void prepare() throws Exception {
50-
HashMap<String, Paint> paintsStub = new HashMap<>();
50+
paintsStub = new HashMap<>();
5151
paintsStub.put("foregroundLine", paintMock);
5252
paintsStub.put("backgroundLine", paintMock);
5353

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

5858
testSubject =
59-
new FocusElementLine(
60-
accessibilityNodeInfoMock, accessibilityNodeInfoMock, paintsStub, viewMock);
59+
new FocusElementLine(eventSourceMock, previousEventSourceMock, paintsStub, viewMock);
6160
}
6261

6362
@Test
6463
public void returnsNotNull() {
6564
Assert.assertNotNull(testSubject);
6665
}
6766

68-
@Test
69-
public void followsCorrectStepsToUpdateCoordinates() throws Exception {
70-
mockStatic(OffsetHelper.class);
71-
FocusElementLine lineSpy = spy(testSubject);
72-
lineSpy.updateWithNewCoordinates();
73-
74-
verifyStatic(OffsetHelper.class, VerificationModeFactory.times(1));
75-
OffsetHelper.getYOffset(any(View.class));
76-
77-
verifyPrivate(lineSpy, times(1)).invoke("setCoordinates");
78-
}
79-
8067
@Test
8168
public void drawLineCallsCorrectPrivateMethod() throws Exception {
69+
when(eventSourceMock.refresh()).thenReturn(true);
70+
when(previousEventSourceMock.refresh()).thenReturn(true);
71+
8272
FocusElementLine lineSpy = spy(testSubject);
8373
lineSpy.drawLine(canvasMock);
8474
verifyPrivate(lineSpy, times(2))
@@ -92,6 +82,34 @@ public void drawLineCallsCorrectPrivateMethod() throws Exception {
9282
any(Canvas.class));
9383
}
9484

85+
@Test
86+
public void drawLineDoesNothingWhenEventSourceIsNull() {
87+
testSubject = new FocusElementLine(null, previousEventSourceMock, paintsStub, viewMock);
88+
testSubject.drawLine(canvasMock);
89+
verifyNoInteractions(canvasMock);
90+
}
91+
92+
@Test
93+
public void drawLineDoesNothingWhenPreviousEventSourceIsNull() {
94+
testSubject = new FocusElementLine(eventSourceMock, null, paintsStub, viewMock);
95+
testSubject.drawLine(canvasMock);
96+
verifyNoInteractions(canvasMock);
97+
}
98+
99+
@Test
100+
public void drawLineDoesNothingWhenPreviousEventSourceDoesNotRefresh() {
101+
when(previousEventSourceMock.refresh()).thenReturn(false);
102+
testSubject.drawLine(canvasMock);
103+
verifyNoInteractions(canvasMock);
104+
}
105+
106+
@Test
107+
public void drawLineDoesNothingWhenEventSourceDoesNotRefresh() {
108+
when(eventSourceMock.refresh()).thenReturn(false);
109+
testSubject.drawLine(canvasMock);
110+
verifyNoInteractions(canvasMock);
111+
}
112+
95113
@Test
96114
public void setPaintWorksProperly() {
97115
HashMap<String, Paint> paintsStub2 = new HashMap<>();

AccessibilityInsightsForAndroidService/app/src/test/java/com/microsoft/accessibilityinsightsforandroidservice/FocusVisualizerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.accessibilityinsightsforandroidservice;
55

66
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.verify;
78
import static org.mockito.internal.verification.VerificationModeFactory.times;
89
import static org.powermock.api.mockito.PowerMockito.spy;
910
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
@@ -108,4 +109,10 @@ public void resetVisualizationsDoesTheJob() {
108109
Assert.assertEquals(resultingLineList.size(), 0);
109110
Assert.assertEquals(resultingTabStopCount, 0);
110111
}
112+
113+
@Test
114+
public void refreshHighlightsCallsRedraw() {
115+
testSubject.refreshHighlights();
116+
verify(focusVisualizationCanvasMock).redraw();
117+
}
111118
}

0 commit comments

Comments
 (0)