Evacuate unpacking logic from RN

Reviewed By: amnn

Differential Revision: D4186902

fbshipit-source-id: 02d8fe176940a678dc8f8d9c0bcf43732f45bde5
This commit is contained in:
Michał Gregorczyk
2016-11-17 03:01:11 -08:00
committed by Facebook Github Bot
parent 112bdc99dc
commit 32670371e4
8 changed files with 35 additions and 1001 deletions

View File

@@ -1,41 +0,0 @@
include_defs('//ReactAndroid/DEFS')
STANDARD_TEST_SRCS = [
'*Test.java',
]
android_library(
name = 'testhelpers',
srcs = glob(['*.java'], excludes = STANDARD_TEST_SRCS),
deps = [
react_native_dep('third-party/java/junit:junit'),
react_native_dep('third-party/java/mockito:mockito'),
],
visibility = [
'PUBLIC'
],
)
robolectric3_test(
name = 'bridge',
# Please change the contact to the oncall of your team
contacts = ['oncall+fbandroid_sheriff@xmail.facebook.com'],
srcs = glob(STANDARD_TEST_SRCS),
deps = [
':testhelpers',
react_native_dep('libraries/fbcore/src/test/java/com/facebook/powermock:powermock'),
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
react_native_dep('third-party/java/junit:junit'),
react_native_dep('third-party/java/mockito:mockito'),
react_native_dep('third-party/java/robolectric3/robolectric:robolectric'),
react_native_target('java/com/facebook/react/bridge:bridge'),
react_native_target('java/com/facebook/react/cxxbridge:bridge'),
],
visibility = [
'PUBLIC'
],
)
project_config(
test_target = ':bridge',
)

View File

@@ -1,85 +0,0 @@
/**
* 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.cxxbridge;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
@PrepareForTest({UnpackingJSBundleLoader.class})
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@RunWith(RobolectricTestRunner.class)
public class ContentCheckingUnpackerTest extends UnpackerTestBase {
@Rule
public PowerMockRule rule = new PowerMockRule();
private UnpackingJSBundleLoader.ContentCheckingUnpacker mUnpacker;
@Before
public void setUp() throws IOException {
super.setUp();
mUnpacker = new UnpackingJSBundleLoader.ContentCheckingUnpacker(
NAME_IN_APK,
DESTINATION_NAME);
mUnpacker.setDestinationDirectory(folder.getRoot());
}
@Test
public void testReconstructsIfFileDoesNotExist() throws IOException {
assertTrue(mUnpacker.shouldReconstructDir(mContext, mIOBuffer));
}
@Test
public void testReconstructsIfContentDoesNotMatch() throws IOException {
try (FileOutputStream fos = new FileOutputStream(mDestinationPath)) {
fos.write(ASSET_DATA, 0, ASSET_DATA.length - 1);
fos.write((byte) (ASSET_DATA[ASSET_DATA.length - 1] + 1));
}
assertTrue(mUnpacker.shouldReconstructDir(mContext, mIOBuffer));
}
@Test
public void testDoesNotReconstructIfContentMatches() throws IOException {
try (FileOutputStream fos = new FileOutputStream(mDestinationPath)) {
fos.write(ASSET_DATA);
}
assertFalse(mUnpacker.shouldReconstructDir(mContext, mIOBuffer));
}
@Test
public void testUnpacksFile() throws IOException {
mUnpacker.unpack(mContext, mIOBuffer);
assertTrue(mDestinationPath.exists());
try (InputStream is = new FileInputStream(mDestinationPath)) {
byte[] storedData = UnpackingJSBundleLoader.readBytes(is, mIOBuffer, Integer.MAX_VALUE);
assertArrayEquals(ASSET_DATA, storedData);
}
}
}

View File

@@ -1,78 +0,0 @@
/**
* 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.cxxbridge;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
@PrepareForTest({UnpackingJSBundleLoader.class})
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@RunWith(RobolectricTestRunner.class)
public class ExistenceCheckingUnpackerTest extends UnpackerTestBase {
@Rule
public PowerMockRule rule = new PowerMockRule();
private UnpackingJSBundleLoader.ExistenceCheckingUnpacker mUnpacker;
@Before
public void setUp() throws IOException {
super.setUp();
mUnpacker = new UnpackingJSBundleLoader.ExistenceCheckingUnpacker(
NAME_IN_APK,
DESTINATION_NAME);
mUnpacker.setDestinationDirectory(folder.getRoot());
}
@Test
public void testReconstructsIfFileDoesNotExist() {
assertTrue(mUnpacker.shouldReconstructDir(mContext, mIOBuffer));
}
@Test
public void testDoesNotReconstructIfFileExists() throws IOException {
mDestinationPath.createNewFile();
assertFalse(mUnpacker.shouldReconstructDir(mContext, mIOBuffer));
}
@Test
public void testUnpacksFile() throws IOException {
mUnpacker.unpack(mContext, mIOBuffer);
assertTrue(mDestinationPath.exists());
try (InputStream is = new FileInputStream(mDestinationPath)) {
byte[] storedData = UnpackingJSBundleLoader.readBytes(is, mIOBuffer, Integer.MAX_VALUE);
assertArrayEquals(ASSET_DATA, storedData);
}
}
@Test
public void testFsyncsAfterUnpacking() throws IOException {
mockStatic(UnpackingJSBundleLoader.class);
mUnpacker.finishUnpacking(mContext);
verifyStatic(times(1));
UnpackingJSBundleLoader.fsync(mDestinationPath);
}
}

View File

@@ -1,95 +0,0 @@
/**
* 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.cxxbridge;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class UnpackerTestBase {
static final String NAME_IN_APK = "nameInApk";
static final String DESTINATION_NAME = "destination";
static final byte[] ASSET_DATA = new byte[]{(byte) 1, (byte) 101, (byte) 50};
@Rule
public TemporaryFolder folder = new TemporaryFolder();
File mDestinationPath;
byte[] mIOBuffer;
Context mContext;
AssetManager mAssetManager;
public void setUp() throws IOException {
mDestinationPath = new File(folder.getRoot(), DESTINATION_NAME);
mIOBuffer = new byte[16 * 1024];
mContext = mock(Context.class);
mAssetManager = mock(AssetManager.class);
when(mContext.getAssets()).thenReturn(mAssetManager);
when(mAssetManager.open(eq(NAME_IN_APK), anyInt()))
.then(new Answer<FileInputStream>() {
@Override
public FileInputStream answer(InvocationOnMock invocation) throws Throwable {
final ByteArrayInputStream bais = new ByteArrayInputStream(ASSET_DATA);
final FileInputStream fis = mock(FileInputStream.class);
when(fis.read())
.then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return bais.read();
}
});
when(fis.read(any(byte[].class)))
.then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return bais.read((byte[]) invocation.getArguments()[0]);
}
});
when(fis.read(any(byte[].class), any(int.class), any(int.class)))
.then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return bais.read(
(byte[]) invocation.getArguments()[0],
(int) invocation.getArguments()[1],
(int) invocation.getArguments()[2]);
}
});
when(fis.available()).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return bais.available();
}
});
return fis;
}
});
}
}

View File

@@ -1,215 +0,0 @@
/**
* 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.cxxbridge;
import android.content.Context;
import com.facebook.soloader.SoLoader;
import java.io.File;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(RobolectricTestRunner.class)
public class UnpackingJSBundleLoaderTest {
static {
SoLoader.setInTestMode();
}
private static final String URL = "http://this.is.an.url";
private static final int MOCK_UNPACKERS_NUM = 2;
private static final int UNPACKER_TEST_FLAGS = 129;
@Rule
public TemporaryFolder folder = new TemporaryFolder();
private File mDestinationPath;
private File mFilesPath;
private UnpackingJSBundleLoader.Builder mBuilder;
private Context mContext;
private CatalystInstanceImpl mCatalystInstanceImpl;
private UnpackingJSBundleLoader.Unpacker[] mMockUnpackers;
private Runnable mCallback;
@Before
public void setUp() throws IOException {
mDestinationPath = folder.newFolder("destination");
mFilesPath = folder.newFolder("files");
mContext = mock(Context.class);
when(mContext.getFilesDir()).thenReturn(mFilesPath);
mCatalystInstanceImpl = mock(CatalystInstanceImpl.class);
mBuilder = UnpackingJSBundleLoader.newBuilder()
.setDestinationPath(mDestinationPath)
.setSourceURL(URL)
.setContext(mContext)
.setFinishOnBackgroundThread(false);
mMockUnpackers = new UnpackingJSBundleLoader.Unpacker[MOCK_UNPACKERS_NUM];
for (int i = 0; i < mMockUnpackers.length; ++i) {
mMockUnpackers[i] = mock(UnpackingJSBundleLoader.Unpacker.class);
}
mCallback = mock(Runnable.class);
}
private void addUnpackers() {
for (UnpackingJSBundleLoader.Unpacker unpacker : mMockUnpackers) {
mBuilder.addUnpacker(unpacker);
}
}
@Test
public void testGetSourceUrl() {
assertEquals(URL, mBuilder.build().getSourceUrl());
}
@Test
public void testCreatesDotUnpackedFile() throws IOException {
mBuilder.build().prepare();
assertTrue(new File(mDestinationPath, UnpackingJSBundleLoader.DOT_UNPACKED_FILE).exists());
}
@Test
public void testCreatesLockFile() throws IOException {
mBuilder.build().prepare();
assertTrue(new File(mFilesPath, UnpackingJSBundleLoader.LOCK_FILE).exists());
}
@Test
public void testCallsAppropriateInstanceMethod() throws IOException {
mBuilder.build().loadScript(mCatalystInstanceImpl);
verify(mCatalystInstanceImpl).loadScriptFromOptimizedBundle(
eq(mDestinationPath.getPath()),
eq(URL),
eq(0));
verifyNoMoreInteractions(mCatalystInstanceImpl);
}
@Test
public void testSetLoadFlags() throws IOException {
mBuilder.setLoadFlags(UNPACKER_TEST_FLAGS)
.build()
.loadScript(mCatalystInstanceImpl);
verify(mCatalystInstanceImpl).loadScriptFromOptimizedBundle(
eq(mDestinationPath.getPath()),
eq(URL),
eq(UNPACKER_TEST_FLAGS));
}
@Test
public void testLoadScriptUnpacks() {
mBuilder.build().loadScript(mCatalystInstanceImpl);
assertTrue(new File(mDestinationPath, UnpackingJSBundleLoader.DOT_UNPACKED_FILE).exists());
}
@Test
public void testPrepareCallDoesNotRecreateDirIfNotNecessary() throws IOException {
mBuilder.build().prepare();
addUnpackers();
mBuilder.build().prepare();
for (UnpackingJSBundleLoader.Unpacker unpacker : mMockUnpackers) {
verify(unpacker).setDestinationDirectory(mDestinationPath);
verify(unpacker).shouldReconstructDir(
same(mContext),
any(byte[].class));
verifyNoMoreInteractions(unpacker);
}
}
@Test
public void testShouldReconstructDirForcesRecreation() throws IOException {
mBuilder.build().prepare();
addUnpackers();
when(mMockUnpackers[0].shouldReconstructDir(
same(mContext),
any(byte[].class)))
.thenReturn(true);
mBuilder.build().prepare();
verify(mMockUnpackers[0]).shouldReconstructDir(
same(mContext),
any(byte[].class));
for (UnpackingJSBundleLoader.Unpacker unpacker : mMockUnpackers) {
verify(unpacker).setDestinationDirectory(mDestinationPath);
verify(unpacker).unpack(
same(mContext),
any(byte[].class));
verify(unpacker).finishUnpacking(same(mContext));
verifyNoMoreInteractions(unpacker);
}
}
@Test
public void testDirectoryReconstructionRemovesDir() throws IOException {
mBuilder.build().prepare();
final File aFile = new File(mDestinationPath, "a_file");
aFile.createNewFile();
when(mMockUnpackers[0].shouldReconstructDir(
same(mContext),
any(byte[].class)))
.thenReturn(true);
addUnpackers();
mBuilder.build().prepare();
assertFalse(aFile.exists());
}
@Test(expected = RuntimeException.class)
public void testDropsDirectoryOnException() throws IOException {
doThrow(new IOException("An expected IOException"))
.when(mMockUnpackers[0]).unpack(
same(mContext),
any(byte[].class));
try {
mBuilder.addUnpacker(mMockUnpackers[0]).build().prepare();
} finally {
assertFalse(mDestinationPath.exists());
}
}
@Test
public void testCallbackIsCalledAfterUnpack() {
mBuilder.setOnUnpackedCallback(mCallback).build().prepare();
verify(mCallback).run();
}
@Test
public void testCallbackIsNotCalledIfNothingIsUnpacked() {
mBuilder.build().prepare();
mBuilder.setOnUnpackedCallback(mCallback).build().prepare();
verifyNoMoreInteractions(mCallback);
}
}