Skip to content

Commit

Permalink
android accessibility bridge performSetText also removes string attri…
Browse files Browse the repository at this point in the history
…butes (flutter#33217)
  • Loading branch information
chunhtai authored May 12, 2022
1 parent 8f604da commit 5f3119a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1297,9 +1297,10 @@ private boolean performSetText(SemanticsNode node, int virtualViewId, @NonNull B
newText = arguments.getString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE);
}
accessibilityChannel.dispatchSemanticsAction(virtualViewId, Action.SET_TEXT, newText);
// The voice access expects the semantics node to update immediately. We update the semantics
// The voice access expects the semantics node to update immediately. Update the semantics
// node based on prediction. If the result is incorrect, it will be updated in the next frame.
node.value = newText;
node.valueAttributes = null;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannedString;
import android.text.style.LocaleSpan;
import android.text.style.TtsSpan;
Expand Down Expand Up @@ -721,6 +722,65 @@ public void itBuildsAttributedString() {
assertEquals(actual.getSpanEnd(spellOutSpan), 9);
}

@TargetApi(21)
@Test
public void itSetsTextCorrectly() {
AccessibilityChannel mockChannel = mock(AccessibilityChannel.class);
AccessibilityViewEmbedder mockViewEmbedder = mock(AccessibilityViewEmbedder.class);
AccessibilityManager mockManager = mock(AccessibilityManager.class);
View mockRootView = mock(View.class);
Context context = mock(Context.class);
when(mockRootView.getContext()).thenReturn(context);
when(context.getPackageName()).thenReturn("test");
AccessibilityBridge accessibilityBridge =
setUpBridge(
/*rootAccessibilityView=*/ mockRootView,
/*accessibilityChannel=*/ mockChannel,
/*accessibilityManager=*/ mockManager,
/*contentResolver=*/ null,
/*accessibilityViewEmbedder=*/ mockViewEmbedder,
/*platformViewsAccessibilityDelegate=*/ null);

ViewParent mockParent = mock(ViewParent.class);
when(mockRootView.getParent()).thenReturn(mockParent);
when(mockManager.isEnabled()).thenReturn(true);

TestSemanticsNode root = new TestSemanticsNode();
root.id = 0;
root.value = "value";
TestStringAttribute attribute = new TestStringAttributeSpellOut();
attribute.start = 1;
attribute.end = 2;
attribute.type = TestStringAttributeType.SPELLOUT;
root.valueAttributes = new ArrayList<>();
root.valueAttributes.add(attribute);

TestSemanticsUpdate testSemanticsUpdate = root.toUpdate();
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
SpannableString actual = (SpannableString) nodeInfo.getContentDescription();
assertEquals(actual.toString(), "value");
Object[] objectSpans = actual.getSpans(0, actual.length(), Object.class);
assertEquals(objectSpans.length, 1);
TtsSpan spellOutSpan = (TtsSpan) objectSpans[0];
assertEquals(spellOutSpan.getType(), TtsSpan.TYPE_VERBATIM);
assertEquals(actual.getSpanStart(spellOutSpan), 1);
assertEquals(actual.getSpanEnd(spellOutSpan), 2);

// Perform a set text action.
Bundle bundle = new Bundle();
String expectedText = "a";
bundle.putString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, expectedText);
accessibilityBridge.performAction(0, AccessibilityNodeInfo.ACTION_SET_TEXT, bundle);

// The action should remove the string attributes.
nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
actual = (SpannableString) nodeInfo.getContentDescription();
assertEquals(actual.toString(), expectedText);
objectSpans = actual.getSpans(0, actual.length(), Object.class);
assertEquals(objectSpans.length, 0);
}

@TargetApi(28)
@Test
public void itSetsTooltipCorrectly() {
Expand Down

0 comments on commit 5f3119a

Please sign in to comment.