mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-06 17:52:57 +08:00
Introducing FallbackJSBundleLoader
Reviewed By: michalgr Differential Revision: D4386951 fbshipit-source-id: b1375deee9b3268d414e1b03fa79df50ac4d36cb
This commit is contained in:
committed by
Facebook Github Bot
parent
89d72c99be
commit
c3892fa871
18
ReactAndroid/src/test/java/com/facebook/react/cxxbridge/BUCK
Normal file
18
ReactAndroid/src/test/java/com/facebook/react/cxxbridge/BUCK
Normal file
@@ -0,0 +1,18 @@
|
||||
include_defs('//ReactAndroid/DEFS')
|
||||
|
||||
rn_robolectric_test(
|
||||
name = 'cxxbridge',
|
||||
# Please change the contact to the oncall of your team
|
||||
contacts = ['oncall+fbandroid_sheriff@xmail.facebook.com'],
|
||||
srcs = glob(['*Test.java']),
|
||||
deps = [
|
||||
react_native_dep('third-party/java/fest:fest'),
|
||||
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/cxxbridge:bridge'),
|
||||
],
|
||||
visibility = [
|
||||
'PUBLIC'
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* 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.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.fest.assertions.api.Assertions.fail;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class FallbackJSBundleLoaderTest {
|
||||
|
||||
private static final String UNRECOVERABLE;
|
||||
static {
|
||||
String prefix = FallbackJSBundleLoader.RECOVERABLE;
|
||||
char first = prefix.charAt(0);
|
||||
|
||||
UNRECOVERABLE = prefix.replace(first, (char) (first + 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstLoaderSucceeds() {
|
||||
JSBundleLoader delegates[] = new JSBundleLoader[] {
|
||||
successfulLoader("url1"),
|
||||
successfulLoader("url2")
|
||||
};
|
||||
|
||||
FallbackJSBundleLoader fallbackLoader =
|
||||
new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates)));
|
||||
|
||||
assertThat(fallbackLoader.loadScript(null)).isEqualTo("url1");
|
||||
|
||||
verify(delegates[0], times(1)).loadScript(null);
|
||||
verify(delegates[1], never()).loadScript(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fallingBackSuccessfully() {
|
||||
JSBundleLoader delegates[] = new JSBundleLoader[] {
|
||||
recoverableLoader("url1", "error1"),
|
||||
successfulLoader("url2"),
|
||||
successfulLoader("url3")
|
||||
};
|
||||
|
||||
FallbackJSBundleLoader fallbackLoader =
|
||||
new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates)));
|
||||
|
||||
assertThat(fallbackLoader.loadScript(null)).isEqualTo("url2");
|
||||
|
||||
verify(delegates[0], times(1)).loadScript(null);
|
||||
verify(delegates[1], times(1)).loadScript(null);
|
||||
verify(delegates[2], never()).loadScript(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fallingbackUnsuccessfully() {
|
||||
JSBundleLoader delegates[] = new JSBundleLoader[] {
|
||||
recoverableLoader("url1", "error1"),
|
||||
recoverableLoader("url2", "error2")
|
||||
};
|
||||
|
||||
FallbackJSBundleLoader fallbackLoader =
|
||||
new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates)));
|
||||
|
||||
try {
|
||||
fallbackLoader.loadScript(null);
|
||||
fail("expect throw");
|
||||
} catch (Exception e) {
|
||||
assertThat(e).isInstanceOf(RuntimeException.class);
|
||||
|
||||
Throwable cause = e.getCause();
|
||||
ArrayList<String> msgs = new ArrayList<>();
|
||||
while (cause != null) {
|
||||
msgs.add(cause.getMessage());
|
||||
cause = cause.getCause();
|
||||
}
|
||||
|
||||
assertThat(msgs).containsExactly(
|
||||
recoverableMsg("error1"),
|
||||
recoverableMsg("error2"));
|
||||
}
|
||||
|
||||
verify(delegates[0], times(1)).loadScript(null);
|
||||
verify(delegates[1], times(1)).loadScript(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unrecoverable() {
|
||||
JSBundleLoader delegates[] = new JSBundleLoader[] {
|
||||
fatalLoader("url1", "error1"),
|
||||
recoverableLoader("url2", "error2")
|
||||
};
|
||||
|
||||
FallbackJSBundleLoader fallbackLoader =
|
||||
new FallbackJSBundleLoader(new ArrayList(Arrays.asList(delegates)));
|
||||
|
||||
try {
|
||||
fallbackLoader.loadScript(null);
|
||||
fail("expect throw");
|
||||
} catch (Exception e) {
|
||||
assertThat(e.getMessage()).isEqualTo(fatalMsg("error1"));
|
||||
}
|
||||
|
||||
verify(delegates[0], times(1)).loadScript(null);
|
||||
verify(delegates[1], never()).loadScript(null);
|
||||
}
|
||||
|
||||
private static JSBundleLoader successfulLoader(String url) {
|
||||
JSBundleLoader loader = mock(JSBundleLoader.class);
|
||||
when(loader.loadScript(null)).thenReturn(url);
|
||||
|
||||
return loader;
|
||||
}
|
||||
|
||||
private static String recoverableMsg(String errMsg) {
|
||||
return FallbackJSBundleLoader.RECOVERABLE + errMsg;
|
||||
}
|
||||
|
||||
private static JSBundleLoader recoverableLoader(String url, String errMsg) {
|
||||
JSBundleLoader loader = mock(JSBundleLoader.class);
|
||||
when(loader.loadScript(null))
|
||||
.thenThrow(new RuntimeException(FallbackJSBundleLoader.RECOVERABLE + errMsg));
|
||||
|
||||
return loader;
|
||||
}
|
||||
|
||||
private static String fatalMsg(String errMsg) {
|
||||
return UNRECOVERABLE + errMsg;
|
||||
}
|
||||
|
||||
private static JSBundleLoader fatalLoader(String url, String errMsg) {
|
||||
JSBundleLoader loader = mock(JSBundleLoader.class);
|
||||
when(loader.loadScript(null))
|
||||
.thenThrow(new RuntimeException(UNRECOVERABLE + errMsg));
|
||||
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user