Skip to content

Commit

Permalink
Fix testID support for TextInput, Slider and ScrollView component (#3…
Browse files Browse the repository at this point in the history
…1865)

Summary:
With the advent of #29610, we are now able to use the `testID` view prop on Android in black-box testing framework through the  view's `resource-id`.

But after testing it, I noticed that on the `TextInput`, `Slider` and `ScrollView` components, the `testID` prop was not exposed as  the `resource-id` properly. The main issue was that those component was using the `AccessibilityDelegateCompat` instead of the `ReactAccessibilityDelegate`.

## Changelog

[Android] [Fixed] - Fix `testID` prop for `TextInput`, `Slider` and `ScrollView` components

Pull Request resolved: #31865

Test Plan: ![test-screenshot](https://user-images.githubusercontent.com/69216913/125802180-c0791a8c-a740-4657-a44f-42b1885eee39.png)

Reviewed By: mdvacca

Differential Revision: D29765333

Pulled By: yungsters

fbshipit-source-id: 2b8e362257e3e5fdcd20330280c588dabb44f28a
  • Loading branch information
hubastard authored and facebook-github-bot committed Jul 23, 2021
1 parent 41f45a7 commit efb359f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ rn_android_library(
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
react_native_target("java/com/facebook/react/views/view:view"),
react_native_target("res:uimanager"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.OverScroller;
import android.widget.ScrollView;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.R;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
Expand Down Expand Up @@ -117,6 +119,20 @@ public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener)
setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
}

@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);

// Expose the testID prop as the resource-id name of the view. Black-box E2E/UI testing
// frameworks, which interact with the UI through the accessibility framework, do not have
// access to view tags. This allows developers/testers to avoid polluting the
// content-description with test identifiers.
final String testId = (String) this.getTag(R.id.react_test_id);
if (testId != null) {
info.setViewIdResourceName(testId);
}
}

@Nullable
private OverScroller getOverScrollerFromParent() {
OverScroller scroller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import android.view.ViewGroup;
import android.widget.SeekBar;
import androidx.annotation.Nullable;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactAccessibilityDelegate;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerHelper;
Expand Down Expand Up @@ -278,7 +278,7 @@ protected ViewManagerDelegate<ReactSlider> getDelegate() {
return mDelegate;
}

protected static class ReactSliderAccessibilityDelegate extends AccessibilityDelegateCompat {
protected static class ReactSliderAccessibilityDelegate extends ReactAccessibilityDelegate {
private static boolean isSliderAction(int action) {
return (action == AccessibilityActionCompat.ACTION_SCROLL_FORWARD.getId())
|| (action == AccessibilityActionCompat.ACTION_SCROLL_BACKWARD.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactSoftException;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.uimanager.FabricViewStateManager;
import com.facebook.react.uimanager.ReactAccessibilityDelegate;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.text.CustomLetterSpacingSpan;
Expand Down Expand Up @@ -155,7 +155,7 @@ public ReactEditText(Context context) {

ViewCompat.setAccessibilityDelegate(
this,
new AccessibilityDelegateCompat() {
new ReactAccessibilityDelegate() {
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
if (action == AccessibilityNodeInfo.ACTION_CLICK) {
Expand Down

0 comments on commit efb359f

Please sign in to comment.