Thread safe renderComponent

Reviewed By: mdvacca

Differential Revision: D7528307

fbshipit-source-id: 1f22898c17f10b883965b03d5c95bbb3c39209c4
This commit is contained in:
Andrew Chen (Eng) 2018-04-06 12:31:02 -07:00 committed by Facebook Github Bot
parent 159869d250
commit c2b9be08f8
3 changed files with 51 additions and 53 deletions

View File

@ -37,21 +37,10 @@ public abstract class ReactAppInstrumentationTestCase extends
intent.putExtra(ReactAppTestActivity.EXTRA_IS_FABRIC_TEST, isFabricTest());
setActivityIntent(intent);
final ReactAppTestActivity activity = getActivity();
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
activity.loadApp(
getReactApplicationKeyUnderTest(),
createReactInstanceSpecForTest(),
getEnableDevSupport());
}
});
} catch (Throwable t) {
throw new Exception("Unable to load react app", t);
}
waitForBridgeAndUIIdle();
assertTrue("Layout never occurred!", activity.waitForLayout(5000));
activity.loadApp(
getReactApplicationKeyUnderTest(),
createReactInstanceSpecForTest(),
getEnableDevSupport());
waitForBridgeAndUIIdle();
}

View File

@ -11,6 +11,7 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
@ -180,17 +181,32 @@ public class ReactAppTestActivity extends FragmentActivity
renderComponent(appKey, initialProps);
}
public void renderComponent(String appKey, @Nullable Bundle initialProps) {
public void renderComponent(String appKey) {
renderComponent(appKey, null);
}
public void renderComponent(final String appKey, final @Nullable Bundle initialProps) {
final CountDownLatch currentLayoutEvent = mLayoutEvent = new CountDownLatch(1);
Assertions.assertNotNull(mReactRootView).getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
currentLayoutEvent.countDown();
}
});
Assertions.assertNotNull(mReactRootView)
.startReactApplication(mReactInstanceManager, appKey, initialProps);
runOnUiThread(new Runnable() {
@Override
public void run() {
Assertions.assertNotNull(mReactRootView).getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
currentLayoutEvent.countDown();
}
});
Assertions.assertNotNull(mReactRootView)
.startReactApplication(mReactInstanceManager, appKey, initialProps);
}
});
try {
waitForBridgeAndUIIdle();
waitForLayout(5000);
} catch (InterruptedException e) {
throw new RuntimeException("Layout never occurred for component " + appKey, e);
}
}
public void loadBundle(
@ -208,7 +224,7 @@ public class ReactAppTestActivity extends FragmentActivity
mBridgeIdleSignaler = new ReactBridgeIdleSignaler();
ReactInstanceManagerBuilder builder =
final ReactInstanceManagerBuilder builder =
ReactTestHelper.getReactTestFactory()
.getReactInstanceManagerBuilder()
.setApplication(getApplication())
@ -259,8 +275,21 @@ public class ReactAppTestActivity extends FragmentActivity
}})
.setUIImplementationProvider(uiImplementationProvider);
mReactInstanceManager = builder.build();
mReactInstanceManager.onHostResume(this, this);
final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
mReactInstanceManager = builder.build();
mReactInstanceManager.onHostResume(ReactAppTestActivity.this, ReactAppTestActivity.this);
latch.countDown();
}
});
try {
latch.await(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(
"ReactInstanceManager never finished initializing " + bundleName, e);
}
}
private ReactInstanceManager getReactInstanceManager() {

View File

@ -37,37 +37,17 @@ public abstract class ReactInstrumentationTest extends
intent.putExtra(ReactAppTestActivity.EXTRA_IS_FABRIC_TEST, isFabricTest());
setActivityIntent(intent);
final ReactAppTestActivity activity = getActivity();
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
activity.loadBundle(
createReactInstanceSpecForTest(),
getBundleName(),
getEnableDevSupport());
}
});
} catch (Throwable t) {
throw new Exception("Unable to load react bundle " + getBundleName(), t);
}
activity.loadBundle(
createReactInstanceSpecForTest(),
getBundleName(),
getEnableDevSupport());
}
/**
* Renders this component within this test's activity
*/
public void renderComponent(final String componentName) throws Exception {
final ReactAppTestActivity activity = getActivity();
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
activity.renderComponent(componentName, null);
}
});
} catch (Throwable t) {
throw new Exception("Unable to render component " + componentName, t);
}
assertTrue("Layout never occurred!", activity.waitForLayout(5000));
getActivity().renderComponent(componentName, null);
waitForBridgeAndUIIdle();
}