Skip to content

Commit dc2bbfd

Browse files
test: Add id prop android tests
1 parent fc4233d commit dc2bbfd

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.tests;
9+
10+
import android.view.View;
11+
import com.facebook.react.testing.ReactAppInstrumentationTestCase;
12+
import com.facebook.react.uimanager.util.ReactFindViewUtil;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
/**
17+
* Tests that the 'id' property can be set on various views. The 'id' property is used
18+
* to reference react managed views from native code.
19+
*/
20+
public class IdTestCase extends ReactAppInstrumentationTestCase {
21+
22+
@Override
23+
protected String getReactApplicationKeyUnderTest() {
24+
return "IdTestApp";
25+
}
26+
27+
private final List<String> viewTags =
28+
Arrays.asList(
29+
"Image",
30+
"Text",
31+
"TouchableBounce",
32+
"TouchableHighlight",
33+
"TouchableOpacity",
34+
"TouchableWithoutFeedback",
35+
"TextInput",
36+
"View");
37+
38+
private boolean mViewFound;
39+
40+
@Override
41+
protected void setUp() throws Exception {
42+
mViewFound = false;
43+
ReactFindViewUtil.addViewListener(
44+
new ReactFindViewUtil.OnViewFoundListener() {
45+
@Override
46+
public String getNativeId() {
47+
return viewTags.get(0);
48+
}
49+
50+
@Override
51+
public void onViewFound(View view) {
52+
mViewFound = true;
53+
}
54+
});
55+
super.setUp();
56+
}
57+
58+
public void testPropertyIsSetForViews() {
59+
for (String nativeId : viewTags) {
60+
View viewWithTag = ReactFindViewUtil.findView(getActivity().getRootView(), nativeId);
61+
assertNotNull(
62+
"View with id " + nativeId + " was not found. Check IdTestModule.js.",
63+
viewWithTag);
64+
}
65+
}
66+
67+
public void testViewListener() {
68+
assertTrue("OnViewFound callback was never invoked", mViewFound);
69+
}
70+
71+
public void testFindView() {
72+
mViewFound = false;
73+
ReactFindViewUtil.findView(
74+
getActivity().getRootView(),
75+
new ReactFindViewUtil.OnViewFoundListener() {
76+
@Override
77+
public String getNativeId() {
78+
return viewTags.get(0);
79+
}
80+
81+
@Override
82+
public void onViewFound(View view) {
83+
mViewFound = true;
84+
}
85+
});
86+
assertTrue(
87+
"OnViewFound callback should have successfully been invoked synchronously", mViewFound);
88+
}
89+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict-local
9+
*/
10+
11+
'use strict';
12+
13+
const React = require('react');
14+
const TouchableBounce = require('react-native/Libraries/Components/Touchable/TouchableBounce');
15+
16+
const {
17+
Image,
18+
StyleSheet,
19+
Text,
20+
TextInput,
21+
TouchableHighlight,
22+
TouchableOpacity,
23+
TouchableWithoutFeedback,
24+
View,
25+
} = require('react-native');
26+
27+
/**
28+
* All the views implemented on Android, each with the id property set.
29+
* We test that:
30+
* - The app renders fine
31+
* - The id property is passed to the native views via the nativeID property
32+
*/
33+
class IdTestApp extends React.Component<{...}> {
34+
render(): React.Node {
35+
const uri =
36+
'data:image/gif;base64,' +
37+
'R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapy' +
38+
'uvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/' +
39+
'TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5' +
40+
'iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97V' +
41+
'riy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7';
42+
return (
43+
<View>
44+
<Image id="Image" source={{uri: uri}} style={styles.base} />
45+
<Text id="Text">text</Text>
46+
<TextInput id="TextInput" value="Text input" />
47+
<TouchableBounce id="TouchableBounce">
48+
<Text>TouchableBounce</Text>
49+
</TouchableBounce>
50+
<TouchableHighlight id="TouchableHighlight">
51+
<Text>TouchableHighlight</Text>
52+
</TouchableHighlight>
53+
<TouchableOpacity id="TouchableOpacity">
54+
<Text>TouchableOpacity</Text>
55+
</TouchableOpacity>
56+
<TouchableWithoutFeedback id="TouchableWithoutFeedback">
57+
<View>
58+
<Text>TouchableWithoutFeedback</Text>
59+
</View>
60+
</TouchableWithoutFeedback>
61+
<View id="View" />
62+
</View>
63+
);
64+
}
65+
}
66+
67+
const styles = StyleSheet.create({
68+
base: {
69+
width: 150,
70+
height: 50,
71+
},
72+
});
73+
74+
module.exports = {
75+
IdTestApp,
76+
};

ReactAndroid/src/androidTest/js/TestApps.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ const apps = [
4848
component: () =>
4949
require('./ScrollViewTestModule').HorizontalScrollViewTestApp,
5050
},
51+
{
52+
appKey: 'IdTestApp',
53+
component: () => require('./IdTestModule').IdTestApp,
54+
},
5155
{
5256
appKey: 'ImageOverlayColorTestApp',
5357
component: () => require('./ImageOverlayColorTestApp'),

0 commit comments

Comments
 (0)