Permalink
Browse files

Moved React Native Android unit tests to open source

Reviewed By: mkonicek

Differential Revision: D2685799

fb-gh-sync-id: 56f061df58641c8cb13fc16bad5f87039f0c49fb
  • Loading branch information...
bestander authored and facebook-github-bot-5 committed Nov 26, 2015
1 parent a7e2059 commit c0f60d201839ce8ec572afb4da164c4773fd94e1
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2015-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.bridge;
+
+import com.facebook.react.bridge.queue.CatalystQueueConfiguration;
+import com.facebook.react.bridge.queue.CatalystQueueConfigurationImpl;
+import com.facebook.react.bridge.queue.CatalystQueueConfigurationSpec;
+import com.facebook.react.bridge.queue.MessageQueueThreadSpec;
+import com.facebook.react.bridge.queue.QueueThreadExceptionHandler;
+import com.facebook.react.uimanager.UIManagerModule;
+
+import org.robolectric.RuntimeEnvironment;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Utility for creating pre-configured instances of core react components for tests.
+ */
+public class CatalystTestHelper {
+
+ /**
+ * @return a ReactApplicationContext that has a CatalystInstance mock returned by
+ * {@link #createMockCatalystInstance}
+ */
+ public static ReactApplicationContext createCatalystContextForTest() {
+ ReactApplicationContext context =
+ new ReactApplicationContext(RuntimeEnvironment.application);
+ context.initializeWithInstance(createMockCatalystInstance());
+ return context;
+ }
+
+ /**
+ * @return a CatalystInstance mock that has a default working CatalystQueueConfiguration.
+ */
+ public static CatalystInstance createMockCatalystInstance() {
+ CatalystQueueConfigurationSpec spec = CatalystQueueConfigurationSpec.builder()
+ .setJSQueueThreadSpec(MessageQueueThreadSpec.mainThreadSpec())
+ .setNativeModulesQueueThreadSpec(MessageQueueThreadSpec.mainThreadSpec())
+ .build();
+ CatalystQueueConfiguration catalystQueueConfiguration = CatalystQueueConfigurationImpl.create(
+ spec,
+ new QueueThreadExceptionHandler() {
+ @Override
+ public void handleException(Exception e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ CatalystInstance reactInstance = mock(CatalystInstance.class);
+ when(reactInstance.getCatalystQueueConfiguration()).thenReturn(catalystQueueConfiguration);
+ when(reactInstance.getNativeModule(UIManagerModule.class))
+ .thenReturn(mock(UIManagerModule.class));
+
+ return reactInstance;
+ }
+}
@@ -0,0 +1,145 @@
+/**
+ * Copyright (c) 2015-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.bridge;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A simple read/write array that can be used in tests in place of {@link WritableNativeArray}.
+ */
+public class SimpleArray implements ReadableArray, WritableArray {
+
+ private final List mBackingList;
+
+ public static SimpleArray from(List list) {
+ return new SimpleArray(list);
+ }
+
+ public static SimpleArray of(Object... values) {
+ return new SimpleArray(values);
+ }
+
+ private SimpleArray(Object... values) {
+ mBackingList = Arrays.asList(values);
+ }
+
+ private SimpleArray(List list) {
+ mBackingList = new ArrayList(list);
+ }
+
+ public SimpleArray() {
+ mBackingList = new ArrayList();
+ }
+
+ @Override
+ public int size() {
+ return mBackingList.size();
+ }
+
+ @Override
+ public boolean isNull(int index) {
+ return mBackingList.get(index) == null;
+ }
+
+ @Override
+ public double getDouble(int index) {
+ return (Double) mBackingList.get(index);
+ }
+
+ @Override
+ public int getInt(int index) {
+ return (Integer) mBackingList.get(index);
+ }
+
+ @Override
+ public String getString(int index) {
+ return (String) mBackingList.get(index);
+ }
+
+ @Override
+ public SimpleArray getArray(int index) {
+ return (SimpleArray) mBackingList.get(index);
+ }
+
+ @Override
+ public boolean getBoolean(int index) {
+ return (Boolean) mBackingList.get(index);
+ }
+
+ @Override
+ public SimpleMap getMap(int index) {
+ return (SimpleMap) mBackingList.get(index);
+ }
+
+ @Override
+ public ReadableType getType(int index) {
+ return null;
+ }
+
+ @Override
+ public void pushBoolean(boolean value) {
+ mBackingList.add(value);
+ }
+
+ @Override
+ public void pushDouble(double value) {
+ mBackingList.add(value);
+ }
+
+ @Override
+ public void pushInt(int value) {
+ mBackingList.add(value);
+ }
+
+ @Override
+ public void pushString(String value) {
+ mBackingList.add(value);
+ }
+
+ @Override
+ public void pushArray(WritableArray array) {
+ mBackingList.add(array);
+ }
+
+ @Override
+ public void pushMap(WritableMap map) {
+ mBackingList.add(map);
+ }
+
+ @Override
+ public void pushNull() {
+ mBackingList.add(null);
+ }
+
+ @Override
+ public String toString() {
+ return mBackingList.toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ SimpleArray that = (SimpleArray) o;
+
+ if (mBackingList != null ? !mBackingList.equals(that.mBackingList) : that.mBackingList != null)
+ return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return mBackingList != null ? mBackingList.hashCode() : 0;
+ }
+}
@@ -0,0 +1,168 @@
+/**
+ * Copyright (c) 2015-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.bridge;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * A simple read/write map that can be used in tests in place of {@link WritableNativeMap}.
+ */
+public class SimpleMap implements ReadableMap, WritableMap {
+
+ private final Map mBackingMap;
+
+ public static SimpleMap of(Object... keysAndValues) {
+ return new SimpleMap(keysAndValues);
+ }
+
+ /**
+ * @param keysAndValues keys and values, interleaved
+ */
+ private SimpleMap(Object... keysAndValues) {
+ if (keysAndValues.length % 2 != 0) {
+ throw new IllegalArgumentException("You must provide the same number of keys and values");
+ }
+ mBackingMap = new HashMap();
+ for (int i = 0; i < keysAndValues.length; i += 2) {
+ mBackingMap.put(keysAndValues[i], keysAndValues[i + 1]);
+ }
+ }
+
+ public SimpleMap() {
+ mBackingMap = new HashMap();
+ }
+
+ @Override
+ public boolean hasKey(String name) {
+ return mBackingMap.containsKey(name);
+ }
+
+ @Override
+ public boolean isNull(String name) {
+ return mBackingMap.get(name) == null;
+ }
+
+ @Override
+ public boolean getBoolean(String name) {
+ return (Boolean) mBackingMap.get(name);
+ }
+
+ @Override
+ public double getDouble(String name) {
+ return (Double) mBackingMap.get(name);
+ }
+
+ @Override
+ public int getInt(String name) {
+ return (Integer) mBackingMap.get(name);
+ }
+
+ @Override
+ public String getString(String name) {
+ return (String) mBackingMap.get(name);
+ }
+
+ @Override
+ public SimpleMap getMap(String name) {
+ return (SimpleMap) mBackingMap.get(name);
+ }
+
+ @Override
+ public SimpleArray getArray(String name) {
+ return (SimpleArray) mBackingMap.get(name);
+ }
+
+ @Override
+ public ReadableType getType(String name) {
+ throw new UnsupportedOperationException("Method not implemented");
+ }
+
+ @Override
+ public ReadableMapKeySetIterator keySetIterator() {
+ return new ReadableMapKeySetIterator() {
+ Iterator<String> mIterator = mBackingMap.keySet().iterator();
+
+ @Override
+ public boolean hasNextKey() {
+ return mIterator.hasNext();
+ }
+
+ @Override
+ public String nextKey() {
+ return mIterator.next();
+ }
+ };
+ }
+
+ @Override
+ public void putBoolean(String key, boolean value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public void putDouble(String key, double value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public void putInt(String key, int value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public void putString(String key, String value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public void putNull(String key) {
+ mBackingMap.put(key, null);
+ }
+
+ @Override
+ public void putMap(String key, WritableMap value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public void merge(ReadableMap source) {
+ mBackingMap.putAll(((SimpleMap) source).mBackingMap);
+ }
+
+ @Override
+ public void putArray(String key, WritableArray value) {
+ mBackingMap.put(key, value);
+ }
+
+ @Override
+ public String toString() {
+ return mBackingMap.toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ SimpleMap that = (SimpleMap) o;
+
+ if (mBackingMap != null ? !mBackingMap.equals(that.mBackingMap) : that.mBackingMap != null)
+ return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return mBackingMap != null ? mBackingMap.hashCode() : 0;
+ }
+}
Oops, something went wrong.

1 comment on commit c0f60d2

@satya164

This comment has been minimized.

Show comment
Hide comment
@satya164

satya164 Nov 26, 2015

Collaborator

Yay!

Collaborator

satya164 commented on c0f60d2 Nov 26, 2015

Yay!

Please sign in to comment.