Fix Android RN server snapshot tests

Summary: Trying to see what it takes to get server snapshot tests working on android. This will be landed after fixing few things.

Reviewed By: achen1

Differential Revision: D8237948

fbshipit-source-id: 926555ba752171dac4e5814f5c8e5c2c173a82c7
This commit is contained in:
Himabindu Gadupudi 2018-07-18 16:57:36 -07:00 committed by Facebook Github Bot
parent 165dd40bea
commit 7ff7572e5c
1 changed files with 85 additions and 81 deletions

View File

@ -1,16 +1,16 @@
/** /**
* Copyright (c) 2014-present, Facebook, Inc. * Copyright (c) 2014-present, Facebook, Inc.
* *
* This source code is licensed under the MIT license found in the * <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* LICENSE file in the root directory of this source tree. * directory of this source tree.
*/ */
package com.facebook.react.testing; package com.facebook.react.testing;
import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread; import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.view.View; import android.view.View;
@ -47,6 +47,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class ReactAppTestActivity extends FragmentActivity public class ReactAppTestActivity extends FragmentActivity
implements DefaultHardwareBackBtnHandler, PermissionAwareActivity { implements DefaultHardwareBackBtnHandler, PermissionAwareActivity {
@ -56,11 +57,10 @@ public class ReactAppTestActivity extends FragmentActivity
private static final int ROOT_VIEW_ID = 8675309; private static final int ROOT_VIEW_ID = 8675309;
// we need a bigger timeout for CI builds because they run on a slow emulator // we need a bigger timeout for CI builds because they run on a slow emulator
private static final long IDLE_TIMEOUT_MS = 120000; private static final long IDLE_TIMEOUT_MS = 120000;
private final CountDownLatch mDestroyCountDownLatch = new CountDownLatch(1);
private CountDownLatch mLayoutEvent = new CountDownLatch(1); private CountDownLatch mLayoutEvent = new CountDownLatch(1);
private @Nullable ReactBridgeIdleSignaler mBridgeIdleSignaler; private @Nullable ReactBridgeIdleSignaler mBridgeIdleSignaler;
private ScreenshotingFrameLayout mScreenshotingFrameLayout; private ScreenshotingFrameLayout mScreenshotingFrameLayout;
private final CountDownLatch mDestroyCountDownLatch = new CountDownLatch(1);
private @Nullable ReactInstanceManager mReactInstanceManager; private @Nullable ReactInstanceManager mReactInstanceManager;
private @Nullable ReactRootView mReactRootView; private @Nullable ReactRootView mReactRootView;
private LifecycleState mLifecycleState = LifecycleState.BEFORE_RESUME; private LifecycleState mLifecycleState = LifecycleState.BEFORE_RESUME;
@ -138,10 +138,7 @@ public class ReactAppTestActivity extends FragmentActivity
loadApp(appKey, spec, null, DEFAULT_BUNDLE_NAME, enableDevSupport); loadApp(appKey, spec, null, DEFAULT_BUNDLE_NAME, enableDevSupport);
} }
public void loadApp( public void loadApp(String appKey, ReactInstanceSpecForTest spec, String bundleName) {
String appKey,
ReactInstanceSpecForTest spec,
String bundleName) {
loadApp(appKey, spec, null, bundleName, false /* = useDevSupport */); loadApp(appKey, spec, null, bundleName, false /* = useDevSupport */);
} }
@ -175,14 +172,20 @@ public class ReactAppTestActivity extends FragmentActivity
public void renderComponent(final String appKey, final @Nullable Bundle initialProps) { public void renderComponent(final String appKey, final @Nullable Bundle initialProps) {
final CountDownLatch currentLayoutEvent = mLayoutEvent = new CountDownLatch(1); final CountDownLatch currentLayoutEvent = mLayoutEvent = new CountDownLatch(1);
runOnUiThread(new Runnable() { runOnUiThread(
new Runnable() {
@Override @Override
public void run() { public void run() {
Assertions.assertNotNull(mReactRootView).getViewTreeObserver().addOnGlobalLayoutListener( Assertions.assertNotNull(mReactRootView)
.getViewTreeObserver()
.addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() { new ViewTreeObserver.OnGlobalLayoutListener() {
@Override @Override
public void onGlobalLayout() { public void onGlobalLayout() {
currentLayoutEvent.countDown(); currentLayoutEvent.countDown();
Assertions.assertNotNull(mReactRootView)
.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} }
}); });
Assertions.assertNotNull(mReactRootView) Assertions.assertNotNull(mReactRootView)
@ -193,14 +196,10 @@ public class ReactAppTestActivity extends FragmentActivity
waitForBridgeAndUIIdle(); waitForBridgeAndUIIdle();
waitForLayout(5000); waitForLayout(5000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException("Layout never occurred for component " + appKey, e); throw new RuntimeException("Layout never occurred for component " + appKey, e);}
}
} }
public void loadBundle( public void loadBundle(ReactInstanceSpecForTest spec, String bundleName, boolean useDevSupport) {
ReactInstanceSpecForTest spec,
String bundleName,
boolean useDevSupport) {
mBridgeIdleSignaler = new ReactBridgeIdleSignaler(); mBridgeIdleSignaler = new ReactBridgeIdleSignaler();
@ -242,31 +241,42 @@ public class ReactAppTestActivity extends FragmentActivity
@Override @Override
public FabricUIManager get() { public FabricUIManager get() {
List<ViewManager> viewManagers = List<ViewManager> viewManagers =
mReactInstanceManager.getOrCreateViewManagers(reactApplicationContext); mReactInstanceManager.getOrCreateViewManagers(
reactApplicationContext);
EventDispatcher eventDispatcher = EventDispatcher eventDispatcher =
reactApplicationContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); reactApplicationContext
.getNativeModule(UIManagerModule.class)
.getEventDispatcher();
FabricUIManager fabricUIManager = FabricUIManager fabricUIManager =
new FabricUIManager(reactApplicationContext, new ViewManagerRegistry(viewManagers), jsContext, eventDispatcher); new FabricUIManager(
reactApplicationContext,
new ViewManagerRegistry(viewManagers),
jsContext,
eventDispatcher);
new FabricJSCBinding().installFabric(jsContext, fabricUIManager); new FabricJSCBinding().installFabric(jsContext, fabricUIManager);
return fabricUIManager; return fabricUIManager;
} }
}; };
} }
}); });
}}); }
});
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() { runOnUiThread(
new Runnable() {
@Override @Override
public void run() { public void run() {
mReactInstanceManager = builder.build(); mReactInstanceManager = builder.build();
mReactInstanceManager.onHostResume(ReactAppTestActivity.this, ReactAppTestActivity.this); mReactInstanceManager.onHostResume(
ReactAppTestActivity.this, ReactAppTestActivity.this);
latch.countDown(); latch.countDown();
} }
}); });
try { try {
latch.await(1000, TimeUnit.MILLISECONDS); latch.await(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException( throw new RuntimeException(
"ReactInstanceManager never finished initializing " + bundleName, e); "ReactInstanceManager never finished initializing " + bundleName, e);
} }
@ -286,9 +296,7 @@ public class ReactAppTestActivity extends FragmentActivity
public void waitForBridgeAndUIIdle(long timeoutMs) { public void waitForBridgeAndUIIdle(long timeoutMs) {
ReactIdleDetectionUtil.waitForBridgeAndUIIdle( ReactIdleDetectionUtil.waitForBridgeAndUIIdle(
Assertions.assertNotNull(mBridgeIdleSignaler), Assertions.assertNotNull(mBridgeIdleSignaler), getReactContext(), timeoutMs);
getReactContext(),
timeoutMs);
} }
public View getRootView() { public View getRootView() {
@ -323,9 +331,8 @@ public class ReactAppTestActivity extends FragmentActivity
} }
/** /**
* Does not ensure that this is run on the UI thread or that the UI Looper is idle like * Does not ensure that this is run on the UI thread or that the UI Looper is idle like {@link
* {@link ReactAppInstrumentationTestCase#getScreenshot()}. You probably want to use that * ReactAppInstrumentationTestCase#getScreenshot()}. You probably want to use that instead.
* instead.
*/ */
public Bitmap getCurrentScreenshot() { public Bitmap getCurrentScreenshot() {
return mScreenshotingFrameLayout.getLastDrawnBitmap(); return mScreenshotingFrameLayout.getLastDrawnBitmap();
@ -346,10 +353,7 @@ public class ReactAppTestActivity extends FragmentActivity
@Override @Override
public void onRequestPermissionsResult( public void onRequestPermissionsResult(
int requestCode, int requestCode, String[] permissions, int[] grantResults) {}
String[] permissions,
int[] grantResults) {
}
@Override @Override
public void requestPermissions( public void requestPermissions(