mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-03 22:48:25 +08:00
Add LazyReactPackage
Summary: LazyReactPackage is an extension of ReactPackage that allows us to lazily construct native modules. It's a separate class to avoid breaking existing packages both internally and in open source. Reviewed By: astreet Differential Revision: D3334258 fbshipit-source-id: e090e146adc4e8e156cae217689e2258ab9837aa
This commit is contained in:
committed by
Facebook Github Bot 3
parent
dba1ce46bf
commit
1feb462f44
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* 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 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.core.classloader.annotations.SuppressStaticInitializationFor;
|
||||
import org.powermock.modules.junit4.rule.PowerMockRule;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import com.facebook.react.bridge.annotations.ReactModule;
|
||||
import com.facebook.react.common.build.ReactBuildConfig;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
|
||||
@SuppressStaticInitializationFor("com.facebook.react.common.build.ReactBuildConfig")
|
||||
@PrepareForTest({ReactBuildConfig.class})
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ModuleSpecTest {
|
||||
@Rule
|
||||
public PowerMockRule rule = new PowerMockRule();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSimpleFailFast() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", true);
|
||||
ModuleSpec.simple(ComplexModule.class, mock(ReactApplicationContext.class));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSimpleFailFastDefault() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", true);
|
||||
ModuleSpec.simple(ComplexModule.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleNoFailFastRelease() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", false);
|
||||
ModuleSpec.simple(ComplexModule.class, mock(ReactApplicationContext.class));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testSimpleFailLateRelease() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", false);
|
||||
ModuleSpec spec = ModuleSpec.simple(ComplexModule.class, mock(ReactApplicationContext.class));
|
||||
spec.getProvider().get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleDefaultConstructor() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", true);
|
||||
ModuleSpec spec = ModuleSpec.simple(SimpleModule.class);
|
||||
assertThat(spec.getProvider().get()).isInstanceOf(SimpleModule.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleContextConstructor() {
|
||||
Whitebox.setInternalState(ReactBuildConfig.class, "DEBUG", true);
|
||||
ReactApplicationContext context = mock(ReactApplicationContext.class);
|
||||
ModuleSpec spec = ModuleSpec.simple(SimpleContextModule.class, context);
|
||||
|
||||
NativeModule module = spec.getProvider().get();
|
||||
assertThat(module).isInstanceOf(SimpleContextModule.class);
|
||||
SimpleContextModule contextModule = (SimpleContextModule) module;
|
||||
assertThat(contextModule.getReactApplicationContext()).isSameAs(context);
|
||||
}
|
||||
|
||||
@ReactModule(name = "ComplexModule")
|
||||
public static class ComplexModule extends BaseJavaModule {
|
||||
public ComplexModule(int a, int b) {
|
||||
}
|
||||
}
|
||||
|
||||
@ReactModule(name = "SimpleModule")
|
||||
public static class SimpleModule extends BaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactModule(name = "SimpleContextModule")
|
||||
public static class SimpleContextModule extends ReactContextBaseJavaModule {
|
||||
public SimpleContextModule(ReactApplicationContext context) {
|
||||
super(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user