From e29eaaddc86965fd3cc3124fb7d37b1ef54a90c3 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Tue, 12 Jan 2016 04:51:17 -0800 Subject: [PATCH] WebWorkers: Add simple integration test Summary: public Adds a simple integration test that verifies that a message can be posted to/from the worker andthat it's torn down properly. Reviewed By: lexs Differential Revision: D2815244 fb-gh-sync-id: 00c0d8933d785b2913d378c0589b2fbabf0d0edb --- .../react/testing/IntRecordingModule.java | 56 +++++++++++++++++++ .../testing/ReactIntegrationTestCase.java | 3 + 2 files changed, 59 insertions(+) create mode 100644 ReactAndroid/src/androidTest/java/com/facebook/react/testing/IntRecordingModule.java diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/IntRecordingModule.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/IntRecordingModule.java new file mode 100644 index 000000000..5cf7e4373 --- /dev/null +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/IntRecordingModule.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.testing; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import com.facebook.react.bridge.BaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +/** + * Native module provides single method {@link #record} which records its single int argument + * in calls array + */ +public class IntRecordingModule extends BaseJavaModule { + + private final List mCalls = new ArrayList<>(); + private final CountDownLatch mCountDownLatch = new CountDownLatch(1); + + @Override + public String getName() { + return "Recording"; + } + + @ReactMethod + public void record(int i) { + mCalls.add(i); + mCountDownLatch.countDown(); + } + + public void reset() { + mCalls.clear(); + } + + public List getCalls() { + return mCalls; + } + + public void waitForFirstCall() { + try { + if (!mCountDownLatch.await(15000, TimeUnit.MILLISECONDS)) { + throw new RuntimeException("Timed out waiting for first call"); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactIntegrationTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactIntegrationTestCase.java index e70a8428b..58173647f 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactIntegrationTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactIntegrationTestCase.java @@ -14,6 +14,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import android.app.Application; import android.support.test.InstrumentationRegistry; import android.test.AndroidTestCase; import android.view.View; @@ -27,6 +28,7 @@ import com.facebook.react.bridge.CatalystInstanceImpl; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.SoftAssertions; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.common.ApplicationHolder; import com.facebook.react.common.futures.SimpleSettableFuture; import com.facebook.react.modules.core.Timing; @@ -155,6 +157,7 @@ public abstract class ReactIntegrationTestCase extends AndroidTestCase { mBridgeIdleSignaler = new ReactBridgeIdleSignaler(); mInstance.addBridgeIdleDebugListener(mBridgeIdleSignaler); getContext().initializeWithInstance(mInstance); + ApplicationHolder.setApplication((Application) getContext().getApplicationContext()); setupEvent.occur(); } });