diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/IdTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/IdTestCase.java new file mode 100644 index 00000000000000..665635f3275974 --- /dev/null +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/IdTestCase.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.tests; + +import android.view.View; +import com.facebook.react.testing.ReactAppInstrumentationTestCase; +import com.facebook.react.uimanager.util.ReactFindViewUtil; +import java.util.Arrays; +import java.util.List; + +/** + * Tests that the 'id' property can be set on various views. The 'id' property is used + * to reference react managed views from native code. + */ +public class IdTestCase extends ReactAppInstrumentationTestCase { + + @Override + protected String getReactApplicationKeyUnderTest() { + return "IdTestApp"; + } + + private final List viewTags = + Arrays.asList( + "Image", + "Text", + "TouchableBounce", + "TouchableHighlight", + "TouchableOpacity", + "TouchableWithoutFeedback", + "TextInput", + "View"); + + private boolean mViewFound; + + @Override + protected void setUp() throws Exception { + mViewFound = false; + ReactFindViewUtil.addViewListener( + new ReactFindViewUtil.OnViewFoundListener() { + @Override + public String getNativeId() { + return viewTags.get(0); + } + + @Override + public void onViewFound(View view) { + mViewFound = true; + } + }); + super.setUp(); + } + + public void testPropertyIsSetForViews() { + for (String nativeId : viewTags) { + View viewWithTag = ReactFindViewUtil.findView(getActivity().getRootView(), nativeId); + assertNotNull( + "View with id " + nativeId + " was not found. Check IdTestModule.js.", + viewWithTag); + } + } + + public void testViewListener() { + assertTrue("OnViewFound callback was never invoked", mViewFound); + } + + public void testFindView() { + mViewFound = false; + ReactFindViewUtil.findView( + getActivity().getRootView(), + new ReactFindViewUtil.OnViewFoundListener() { + @Override + public String getNativeId() { + return viewTags.get(0); + } + + @Override + public void onViewFound(View view) { + mViewFound = true; + } + }); + assertTrue( + "OnViewFound callback should have successfully been invoked synchronously", mViewFound); + } +} diff --git a/ReactAndroid/src/androidTest/js/IdTestModule.js b/ReactAndroid/src/androidTest/js/IdTestModule.js new file mode 100644 index 00000000000000..278670f35274af --- /dev/null +++ b/ReactAndroid/src/androidTest/js/IdTestModule.js @@ -0,0 +1,76 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow strict-local + */ + +'use strict'; + +const React = require('react'); +const TouchableBounce = require('react-native/Libraries/Components/Touchable/TouchableBounce'); + +const { + Image, + StyleSheet, + Text, + TextInput, + TouchableHighlight, + TouchableOpacity, + TouchableWithoutFeedback, + View, +} = require('react-native'); + +/** + * All the views implemented on Android, each with the id property set. + * We test that: + * - The app renders fine + * - The id property is passed to the native views via the nativeID property + */ +class IdTestApp extends React.Component<{...}> { + render(): React.Node { + const uri = + 'data:image/gif;base64,' + + 'R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapy' + + 'uvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/' + + 'TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5' + + 'iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97V' + + 'riy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7'; + return ( + + + text + + + TouchableBounce + + + TouchableHighlight + + + TouchableOpacity + + + + TouchableWithoutFeedback + + + + + ); + } +} + +const styles = StyleSheet.create({ + base: { + width: 150, + height: 50, + }, +}); + +module.exports = { + IdTestApp, +}; diff --git a/ReactAndroid/src/androidTest/js/TestApps.js b/ReactAndroid/src/androidTest/js/TestApps.js index 4659e2c4f86322..208b6cdf91eb0a 100644 --- a/ReactAndroid/src/androidTest/js/TestApps.js +++ b/ReactAndroid/src/androidTest/js/TestApps.js @@ -48,6 +48,10 @@ const apps = [ component: () => require('./ScrollViewTestModule').HorizontalScrollViewTestApp, }, + { + appKey: 'IdTestApp', + component: () => require('./IdTestModule').IdTestApp, + }, { appKey: 'ImageOverlayColorTestApp', component: () => require('./ImageOverlayColorTestApp'),