|
| 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 to reference |
| 18 | + * 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.", viewWithTag); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void testViewListener() { |
| 67 | + assertTrue("OnViewFound callback was never invoked", mViewFound); |
| 68 | + } |
| 69 | + |
| 70 | + public void testFindView() { |
| 71 | + mViewFound = false; |
| 72 | + ReactFindViewUtil.findView( |
| 73 | + getActivity().getRootView(), |
| 74 | + new ReactFindViewUtil.OnViewFoundListener() { |
| 75 | + @Override |
| 76 | + public String getNativeId() { |
| 77 | + return viewTags.get(0); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onViewFound(View view) { |
| 82 | + mViewFound = true; |
| 83 | + } |
| 84 | + }); |
| 85 | + assertTrue( |
| 86 | + "OnViewFound callback should have successfully been invoked synchronously", mViewFound); |
| 87 | + } |
| 88 | +} |
0 commit comments