|
5 | 5 | package io.flutter.view;
|
6 | 6 |
|
7 | 7 | import static org.junit.Assert.assertEquals;
|
| 8 | +import static org.mockito.Matchers.eq; |
8 | 9 | import static org.mockito.Mockito.mock;
|
| 10 | +import static org.mockito.Mockito.times; |
| 11 | +import static org.mockito.Mockito.verify; |
9 | 12 | import static org.mockito.Mockito.when;
|
10 | 13 |
|
11 | 14 | import android.content.ContentResolver;
|
12 | 15 | import android.content.Context;
|
13 | 16 | import android.view.View;
|
| 17 | +import android.view.ViewParent; |
| 18 | +import android.view.accessibility.AccessibilityEvent; |
14 | 19 | import android.view.accessibility.AccessibilityManager;
|
15 | 20 | import android.view.accessibility.AccessibilityNodeInfo;
|
16 | 21 | import io.flutter.embedding.engine.systemchannels.AccessibilityChannel;
|
17 | 22 | import io.flutter.plugin.platform.PlatformViewsAccessibilityDelegate;
|
18 | 23 | import java.nio.ByteBuffer;
|
19 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.List; |
20 | 26 | import org.junit.Test;
|
21 | 27 | import org.junit.runner.RunWith;
|
| 28 | +import org.mockito.ArgumentCaptor; |
22 | 29 | import org.robolectric.RobolectricTestRunner;
|
23 | 30 | import org.robolectric.annotation.Config;
|
24 | 31 |
|
@@ -73,24 +80,95 @@ public void itDoesNotContainADescriptionIfScopesRoute() {
|
73 | 80 | assertEquals(nodeInfo.getText(), null);
|
74 | 81 | }
|
75 | 82 |
|
76 |
| - AccessibilityBridge setUpBridge() { |
77 |
| - View view = mock(View.class); |
| 83 | + @Test |
| 84 | + public void itUnfocusesPlatformViewWhenPlatformViewGoesAway() { |
| 85 | + AccessibilityViewEmbedder mockViewEmbedder = mock(AccessibilityViewEmbedder.class); |
| 86 | + AccessibilityManager mockManager = mock(AccessibilityManager.class); |
| 87 | + View mockRootView = mock(View.class); |
78 | 88 | Context context = mock(Context.class);
|
79 |
| - when(view.getContext()).thenReturn(context); |
| 89 | + when(mockRootView.getContext()).thenReturn(context); |
80 | 90 | when(context.getPackageName()).thenReturn("test");
|
81 |
| - AccessibilityChannel accessibilityChannel = mock(AccessibilityChannel.class); |
82 |
| - AccessibilityManager accessibilityManager = mock(AccessibilityManager.class); |
83 |
| - ContentResolver contentResolver = mock(ContentResolver.class); |
84 |
| - PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate = |
85 |
| - mock(PlatformViewsAccessibilityDelegate.class); |
86 |
| - AccessibilityBridge accessibilityBridge = |
87 |
| - new AccessibilityBridge( |
88 |
| - view, |
| 91 | + AccessibilityBridge accessibilityBridge = setUpBridge(mockRootView, mockManager, mockViewEmbedder); |
| 92 | + |
| 93 | + // Sent a11y tree with platform view. |
| 94 | + TestSemanticsNode root = new TestSemanticsNode(); |
| 95 | + root.id = 0; |
| 96 | + TestSemanticsNode platformView = new TestSemanticsNode(); |
| 97 | + platformView.id = 1; |
| 98 | + platformView.platformViewId = 42; |
| 99 | + root.children.add(platformView); |
| 100 | + TestSemanticsUpdate testSemanticsUpdate = root.toUpdate(); |
| 101 | + accessibilityBridge.updateSemantics(testSemanticsUpdate.buffer, testSemanticsUpdate.strings); |
| 102 | + |
| 103 | + // Set a11y focus to platform view. |
| 104 | + View mockView = mock(View.class); |
| 105 | + AccessibilityEvent focusEvent = mock(AccessibilityEvent.class); |
| 106 | + when(mockViewEmbedder.requestSendAccessibilityEvent(mockView, mockView, focusEvent)).thenReturn(true); |
| 107 | + when(mockViewEmbedder.getRecordFlutterId(mockView, focusEvent)).thenReturn(42); |
| 108 | + when(focusEvent.getEventType()).thenReturn(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED); |
| 109 | + accessibilityBridge.externalViewRequestSendAccessibilityEvent(mockView, mockView, focusEvent); |
| 110 | + |
| 111 | + // Replace the platform view. |
| 112 | + TestSemanticsNode node = new TestSemanticsNode(); |
| 113 | + node.id = 2; |
| 114 | + root.children.clear(); |
| 115 | + root.children.add(node); |
| 116 | + testSemanticsUpdate = root.toUpdate(); |
| 117 | + when(mockManager.isEnabled()).thenReturn(true); |
| 118 | + ViewParent mockParent = mock(ViewParent.class); |
| 119 | + when(mockRootView.getParent()).thenReturn(mockParent); |
| 120 | + accessibilityBridge.updateSemantics(testSemanticsUpdate.buffer, testSemanticsUpdate.strings); |
| 121 | + |
| 122 | + // Check that unfocus event was sent. |
| 123 | + ArgumentCaptor<AccessibilityEvent> eventCaptor = ArgumentCaptor.forClass(AccessibilityEvent.class); |
| 124 | + verify(mockParent, times(2)).requestSendAccessibilityEvent(eq(mockRootView), eventCaptor.capture()); |
| 125 | + AccessibilityEvent event = eventCaptor.getAllValues().get(0); |
| 126 | + assertEquals(event.getEventType(), AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED); |
| 127 | + } |
| 128 | + |
| 129 | + AccessibilityBridge setUpBridge() { |
| 130 | + return setUpBridge(null, null, null, null, null, null); |
| 131 | + } |
| 132 | + |
| 133 | + AccessibilityBridge setUpBridge(View rootAccessibilityView, AccessibilityManager accessibilityManager, AccessibilityViewEmbedder accessibilityViewEmbedder) { |
| 134 | + return setUpBridge(rootAccessibilityView, null, accessibilityManager, null, accessibilityViewEmbedder, null); |
| 135 | + } |
| 136 | + |
| 137 | + AccessibilityBridge setUpBridge( |
| 138 | + View rootAccessibilityView, |
| 139 | + AccessibilityChannel accessibilityChannel, |
| 140 | + AccessibilityManager accessibilityManager, |
| 141 | + ContentResolver contentResolver, |
| 142 | + AccessibilityViewEmbedder accessibilityViewEmbedder, |
| 143 | + PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate |
| 144 | + ) { |
| 145 | + if (rootAccessibilityView == null) { |
| 146 | + rootAccessibilityView = mock(View.class); |
| 147 | + Context context = mock(Context.class); |
| 148 | + when(rootAccessibilityView.getContext()).thenReturn(context); |
| 149 | + when(context.getPackageName()).thenReturn("test"); |
| 150 | + } |
| 151 | + if (accessibilityChannel == null) { |
| 152 | + accessibilityChannel = mock(AccessibilityChannel.class); |
| 153 | + } |
| 154 | + if (accessibilityManager == null) { |
| 155 | + accessibilityManager = mock(AccessibilityManager.class); |
| 156 | + } |
| 157 | + if (contentResolver == null) { |
| 158 | + contentResolver = mock(ContentResolver.class); |
| 159 | + } |
| 160 | + if (accessibilityViewEmbedder == null) { |
| 161 | + accessibilityViewEmbedder = mock(AccessibilityViewEmbedder.class); |
| 162 | + } |
| 163 | + if (platformViewsAccessibilityDelegate == null) { |
| 164 | + platformViewsAccessibilityDelegate = mock(PlatformViewsAccessibilityDelegate.class); |
| 165 | + } |
| 166 | + return new AccessibilityBridge( |
| 167 | + rootAccessibilityView, |
89 | 168 | accessibilityChannel,
|
90 | 169 | accessibilityManager,
|
91 | 170 | contentResolver,
|
92 |
| - platformViewsAccessibilityDelegate); |
93 |
| - return accessibilityBridge; |
| 171 | + accessibilityViewEmbedder, platformViewsAccessibilityDelegate); |
94 | 172 | }
|
95 | 173 |
|
96 | 174 | /// The encoding for semantics is described in platform_view_android.cc
|
@@ -136,11 +214,18 @@ void addFlag(AccessibilityBridge.Flag flag) {
|
136 | 214 | float top = 0.0f;
|
137 | 215 | float right = 0.0f;
|
138 | 216 | float bottom = 0.0f;
|
139 |
| - // children and custom actions not supported. |
| 217 | + final List<TestSemanticsNode> children = new ArrayList<TestSemanticsNode>(); |
| 218 | + //custom actions not supported. |
140 | 219 |
|
141 | 220 | TestSemanticsUpdate toUpdate() {
|
142 | 221 | ArrayList<String> strings = new ArrayList<String>();
|
143 | 222 | ByteBuffer bytes = ByteBuffer.allocate(1000);
|
| 223 | + addToBuffer(bytes, strings); |
| 224 | + bytes.flip(); |
| 225 | + return new TestSemanticsUpdate(bytes, strings.toArray(new String[strings.size()])); |
| 226 | + } |
| 227 | + |
| 228 | + protected void addToBuffer(ByteBuffer bytes, ArrayList<String> strings) { |
144 | 229 | bytes.putInt(id);
|
145 | 230 | bytes.putInt(flags);
|
146 | 231 | bytes.putInt(actions);
|
@@ -169,11 +254,20 @@ TestSemanticsUpdate toUpdate() {
|
169 | 254 | bytes.putFloat(0);
|
170 | 255 | }
|
171 | 256 | // children in traversal order.
|
172 |
| - bytes.putInt(0); |
| 257 | + bytes.putInt(children.size()); |
| 258 | + for (TestSemanticsNode node : children) { |
| 259 | + bytes.putInt(node.id); |
| 260 | + } |
| 261 | + // children in hit test order. |
| 262 | + for (TestSemanticsNode node : children) { |
| 263 | + bytes.putInt(node.id); |
| 264 | + } |
173 | 265 | // custom actions
|
174 | 266 | bytes.putInt(0);
|
175 |
| - bytes.flip(); |
176 |
| - return new TestSemanticsUpdate(bytes, strings.toArray(new String[strings.size()])); |
| 267 | + // child nodes |
| 268 | + for (TestSemanticsNode node : children) { |
| 269 | + node.addToBuffer(bytes, strings); |
| 270 | + } |
177 | 271 | }
|
178 | 272 | }
|
179 | 273 |
|
|
0 commit comments