forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReactInstrumentationTest.java
132 lines (108 loc) · 4.3 KB
/
ReactInstrumentationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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.testing;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import com.facebook.react.ReactPackageTurboModuleManagerDelegate;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.testing.idledetection.IdleWaiter;
/**
* Base class for instrumentation tests that runs React based application.
*
* <p>This is similar to ReactAppInstrumentationTestCase except ReactInstrumentationTest allows
* optional rendering of components. A test case can render no components or render multiple
* components.
*/
public abstract class ReactInstrumentationTest
extends ActivityInstrumentationTestCase2<ReactAppTestActivity> implements IdleWaiter {
protected StringRecordingModule mRecordingModule;
@Nullable protected FabricUIManagerFactory mFabricUIManagerFactory = null;
@Nullable protected JavaScriptExecutorFactory mJavaScriptExecutorFactory = null;
public ReactInstrumentationTest() {
super(ReactAppTestActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
Intent intent = new Intent();
intent.putExtra(ReactAppTestActivity.EXTRA_IS_FABRIC_TEST, isFabricTest());
setActivityIntent(intent);
mRecordingModule = new StringRecordingModule();
final ReactAppTestActivity activity = getActivity();
activity.loadBundle(createReactInstanceSpecForTest(), getBundleName(), getEnableDevSupport());
}
/** Renders this component within this test's activity */
public void renderComponent(final String componentName) {
getActivity().renderComponent(componentName, null);
waitForBridgeAndUIIdle();
}
@Override
protected void tearDown() throws Exception {
ReactAppTestActivity activity = getActivity();
super.tearDown();
activity.waitForDestroy(5000);
}
public ViewGroup getRootView() {
return (ViewGroup) getActivity().getRootView();
}
public <T extends View> T getViewByTestId(String testID) {
return (T)
ReactTestHelper.getViewWithReactTestId((ViewGroup) getRootView().getParent(), testID);
}
public SingleTouchGestureGenerator createGestureGenerator() {
return new SingleTouchGestureGenerator(getRootView(), this);
}
public void waitForBridgeAndUIIdle() {
getActivity().waitForBridgeAndUIIdle();
}
public void waitForBridgeAndUIIdle(long timeoutMs) {
getActivity().waitForBridgeAndUIIdle(timeoutMs);
}
protected boolean getEnableDevSupport() {
return false;
}
protected boolean isFabricTest() {
return false;
}
protected <T extends JavaScriptModule> T getJSModule(Class<T> jsInterface) {
return getReactContext().getJSModule(jsInterface);
}
@Nullable
protected ReactPackageTurboModuleManagerDelegate.Builder
getReactPackageTurboModuleManagerDelegateBuilder() {
return null;
}
@Nullable
protected ReactInstanceSpecForTest.JSIModuleBuilder getJSIModuleBuilder() {
return null;
}
/** Override this method to provide extra native modules to be loaded before the app starts */
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
ReactInstanceSpecForTest reactInstanceSpecForTest =
new ReactInstanceSpecForTest().addNativeModule(mRecordingModule);
if (mJavaScriptExecutorFactory != null) {
reactInstanceSpecForTest.setJavaScriptExecutorFactory(mJavaScriptExecutorFactory);
}
if (mFabricUIManagerFactory != null) {
reactInstanceSpecForTest.setFabricUIManagerFactory(mFabricUIManagerFactory);
}
reactInstanceSpecForTest.setReactPackageTurboModuleManagerDelegateBuilder(
getReactPackageTurboModuleManagerDelegateBuilder());
reactInstanceSpecForTest.setJSIModuleBuilder(getJSIModuleBuilder());
return reactInstanceSpecForTest;
}
/** Implement this method to provide the bundle for this test */
protected abstract String getBundleName();
protected ReactContext getReactContext() {
return getActivity().getReactContext();
}
}