diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppInstrumentationTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppInstrumentationTestCase.java index 863e249cbfe..b01d0542af4 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppInstrumentationTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppInstrumentationTestCase.java @@ -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(); } diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java index 341c8781494..de18fad93e2 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java @@ -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() { diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstrumentationTest.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstrumentationTest.java index 3f0a7e250bf..20dcc3ef516 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstrumentationTest.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstrumentationTest.java @@ -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(); }