Skip to content

Commit 52b6592

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Modal: Setting resource-id from testID prop (#48313)
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
1 parent dd303b2 commit 52b6592

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6586,6 +6586,8 @@ public final class com/facebook/react/views/modal/ReactModalHostManager : com/fa
65866586
public fun setStatusBarTranslucent (Lcom/facebook/react/views/modal/ReactModalHostView;Z)V
65876587
public synthetic fun setSupportedOrientations (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V
65886588
public fun setSupportedOrientations (Lcom/facebook/react/views/modal/ReactModalHostView;Lcom/facebook/react/bridge/ReadableArray;)V
6589+
public synthetic fun setTestId (Landroid/view/View;Ljava/lang/String;)V
6590+
public fun setTestId (Lcom/facebook/react/views/modal/ReactModalHostView;Ljava/lang/String;)V
65896591
public synthetic fun setTransparent (Landroid/view/View;Z)V
65906592
public fun setTransparent (Lcom/facebook/react/views/modal/ReactModalHostView;Z)V
65916593
public synthetic fun setVisible (Landroid/view/View;Z)V
@@ -6621,6 +6623,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView : android/v
66216623
public fun removeView (Landroid/view/View;)V
66226624
public fun removeViewAt (I)V
66236625
public final fun setAnimationType (Ljava/lang/String;)V
6626+
public final fun setDialogRootViewGroupTestId (Ljava/lang/String;)V
66246627
public final fun setEventDispatcher (Lcom/facebook/react/uimanager/events/EventDispatcher;)V
66256628
public final fun setHardwareAccelerated (Z)V
66266629
public fun setId (I)V
@@ -6638,6 +6641,7 @@ public final class com/facebook/react/views/modal/ReactModalHostView$DialogRootV
66386641
public fun onChildEndedNativeGesture (Landroid/view/View;Landroid/view/MotionEvent;)V
66396642
public fun onChildStartedNativeGesture (Landroid/view/View;Landroid/view/MotionEvent;)V
66406643
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
6644+
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
66416645
public fun onInterceptHoverEvent (Landroid/view/MotionEvent;)Z
66426646
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
66436647
public fun onTouchEvent (Landroid/view/MotionEvent;)Z

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public class ReactModalHostManager :
9393
@ReactProp(name = "identifier")
9494
public override fun setIdentifier(view: ReactModalHostView, value: Int): Unit = Unit
9595

96+
public override fun setTestId(view: ReactModalHostView, value: String?) {
97+
super.setTestId(view, value)
98+
view.setDialogRootViewGroupTestId(value)
99+
}
100+
96101
protected override fun addEventEmitters(
97102
reactContext: ThemedReactContext,
98103
view: ReactModalHostView

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.view.Window
2424
import android.view.WindowInsetsController
2525
import android.view.WindowManager
2626
import android.view.accessibility.AccessibilityEvent
27+
import android.view.accessibility.AccessibilityNodeInfo
2728
import android.widget.FrameLayout
2829
import androidx.annotation.UiThread
2930
import com.facebook.common.logging.FLog
@@ -386,6 +387,15 @@ public class ReactModalHostView(context: ThemedReactContext) :
386387
}
387388
}
388389

390+
/**
391+
* Sets the testID on the DialogRootViewGroup. Since the accessibility events are not triggered on
392+
* the on the ReactModalHostView, the testID is forwarded to the DialogRootViewGroup to set the
393+
* resource-id.
394+
*/
395+
public fun setDialogRootViewGroupTestId(testId: String?) {
396+
dialogRootViewGroup.setTag(R.id.react_test_id, testId)
397+
}
398+
389399
// This listener is called when the user presses KeyEvent.KEYCODE_BACK
390400
// An event is then passed to JS which can either close or not close the Modal by setting the
391401
// visible property
@@ -427,6 +437,15 @@ public class ReactModalHostView(context: ThemedReactContext) :
427437
}
428438
}
429439

440+
override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) {
441+
super.onInitializeAccessibilityNodeInfo(info)
442+
443+
val testId = getTag(R.id.react_test_id) as String?
444+
if (testId != null) {
445+
info.viewIdResourceName = testId
446+
}
447+
}
448+
430449
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
431450
super.onSizeChanged(w, h, oldw, oldh)
432451
viewWidth = w

0 commit comments

Comments
 (0)