Skip to content

Commit

Permalink
Modal: Setting resource-id from testID prop (#48313)
Browse files Browse the repository at this point in the history
Summary:
Follow up from #48271 and #48254, I noticed that the Modal component also doesn't map the `resource-id` from the `testID` on Android. This PR addresses that.

## Changelog:

[ANDROID] [FIXED] - Modal: Setting `resource-id` from `testID` prop

Pull Request resolved: #48313

Test Plan:
Alternatively do:
```
$ adb shell uiautomator dump
UI hierchary dumped to: /sdcard/window_dump.xml
$ adb pull /sdcard/window_dump.xml
/sdcard/window_dump.xml: 1 file pulled, 0 skipped. 1.1 MB/s (3505 bytes in 0.003s)
```
and check in XML: ` resource-id="playground-modal"  class="android.view.ViewGroup" `
-------
Using Appium, check that the `testID` prop passed from JS is mapped as `resource-id` in the rendered view group of the Modal.

<details>
<summary>Example of the code implementation in the RNTester Playground:</summary>

```tsx
function Playground() {
  const [modalVisible, setModalVisible] = React.useState(false);

  return (
    <>
      <Modal
        visible={modalVisible}
        testID="playground-modal">
        <Text testID="inner-text-test-id">Hello World!</Text>
      </Modal>

      <Button
        title="Open Modal"
        onPress={() => {
          setModalVisible(true);
        }}
      />
    </>
  );
}
```
</details>

<details>
<summary>Output in Appium Inspector:</summary>

<img width="913" alt="image" src="https://github.com/user-attachments/assets/514ae2b3-35a8-4a1a-8efc-1ca6bd73f189" />

</details>

Reviewed By: javache

Differential Revision: D67369350

Pulled By: alanleedev

fbshipit-source-id: a799ad5b974895a39d9287e3d76d1139a6ef6a83
  • Loading branch information
mateoguzmana authored and facebook-github-bot committed Dec 19, 2024
1 parent dd303b2 commit 52b6592
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6586,6 +6586,8 @@ public final class com/facebook/react/views/modal/ReactModalHostManager : com/fa
public fun setStatusBarTranslucent (Lcom/facebook/react/views/modal/ReactModalHostView;Z)V
public synthetic fun setSupportedOrientations (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setSupportedOrientations (Lcom/facebook/react/views/modal/ReactModalHostView;Lcom/facebook/react/bridge/ReadableArray;)V
public synthetic fun setTestId (Landroid/view/View;Ljava/lang/String;)V
public fun setTestId (Lcom/facebook/react/views/modal/ReactModalHostView;Ljava/lang/String;)V
public synthetic fun setTransparent (Landroid/view/View;Z)V
public fun setTransparent (Lcom/facebook/react/views/modal/ReactModalHostView;Z)V
public synthetic fun setVisible (Landroid/view/View;Z)V
Expand Down Expand Up @@ -6621,6 +6623,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView : android/v
public fun removeView (Landroid/view/View;)V
public fun removeViewAt (I)V
public final fun setAnimationType (Ljava/lang/String;)V
public final fun setDialogRootViewGroupTestId (Ljava/lang/String;)V
public final fun setEventDispatcher (Lcom/facebook/react/uimanager/events/EventDispatcher;)V
public final fun setHardwareAccelerated (Z)V
public fun setId (I)V
Expand All @@ -6638,6 +6641,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView$DialogRootV
public fun onChildEndedNativeGesture (Landroid/view/View;Landroid/view/MotionEvent;)V
public fun onChildStartedNativeGesture (Landroid/view/View;Landroid/view/MotionEvent;)V
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
public fun onInterceptHoverEvent (Landroid/view/MotionEvent;)Z
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public class ReactModalHostManager :
@ReactProp(name = "identifier")
public override fun setIdentifier(view: ReactModalHostView, value: Int): Unit = Unit

public override fun setTestId(view: ReactModalHostView, value: String?) {
super.setTestId(view, value)
view.setDialogRootViewGroupTestId(value)
}

protected override fun addEventEmitters(
reactContext: ThemedReactContext,
view: ReactModalHostView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.view.Window
import android.view.WindowInsetsController
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.FrameLayout
import androidx.annotation.UiThread
import com.facebook.common.logging.FLog
Expand Down Expand Up @@ -386,6 +387,15 @@ public class ReactModalHostView(context: ThemedReactContext) :
}
}

/**
* Sets the testID on the DialogRootViewGroup. Since the accessibility events are not triggered on
* the on the ReactModalHostView, the testID is forwarded to the DialogRootViewGroup to set the
* resource-id.
*/
public fun setDialogRootViewGroupTestId(testId: String?) {
dialogRootViewGroup.setTag(R.id.react_test_id, testId)
}

// This listener is called when the user presses KeyEvent.KEYCODE_BACK
// An event is then passed to JS which can either close or not close the Modal by setting the
// visible property
Expand Down Expand Up @@ -427,6 +437,15 @@ public class ReactModalHostView(context: ThemedReactContext) :
}
}

override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) {
super.onInitializeAccessibilityNodeInfo(info)

val testId = getTag(R.id.react_test_id) as String?
if (testId != null) {
info.viewIdResourceName = testId
}
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
viewWidth = w
Expand Down

0 comments on commit 52b6592

Please sign in to comment.