Skip to content

Commit

Permalink
Merge pull request #30 from carnivalmobile/add-geo-location-toggle
Browse files Browse the repository at this point in the history
Add geo location toggle
  • Loading branch information
igstewart3 authored Jul 4, 2019
2 parents 26fd347 + fe8a77f commit b57fb0a
Show file tree
Hide file tree
Showing 29 changed files with 1,253 additions and 42 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ You will then need replace the code that creates your RCTRootView with the code
...
id<RCTBridgeDelegate> moduleInitialiser = [[RNCarnivalBridge alloc]
initWithJSCodeLocation:jsCodeLocation // JS Code location used here should be same location used before
appKey:SDK_KEY // Obtain SDK key from your Carnival app settings
registerForPushNotifications:YES // Whether the SDK should handle push notification registration
displayInAppNotifications:YES]; // Whether the SDK should display in app notifications automatically
appKey:SDK_KEY]; // Obtain SDK key from your Carnival app settings

RCTBridge * bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:launchOptions];

Expand Down Expand Up @@ -88,8 +86,7 @@ public class MainApplication extends Application implements ReactApplication {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNCarnivalPackage(getApplicationContext(), // Should pass in application context
SDK_KEY, // Obtain SDK key from your Carnival app settings
true) // Whether SDK should display in app notifications automatically
SDK_KEY) // Obtain SDK key from your Carnival app settings
);
}
...
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.4.1'
}
}

Expand Down Expand Up @@ -48,7 +48,7 @@ repositories {

dependencies {
api 'com.facebook.react:react-native:+'
api 'com.carnival.sdk:carnival:7.0.2'
api 'com.carnival.sdk:carnival:8.1.0'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-all.zip
15 changes: 15 additions & 0 deletions android/src/main/java/com/reactlibrary/RNCarnivalModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,21 @@ public void setGeoIPTrackingEnabled(boolean enabled) {
Carnival.setGeoIpTrackingEnabled(enabled);
}

@ReactMethod
public void setGeoIPTrackingEnabled(boolean enabled, final Promise promise) {
Carnival.setGeoIpTrackingEnabled(enabled, new Carnival.CarnivalHandler<Void>() {
@Override
public void onSuccess(Void aVoid) {
promise.resolve(true);
}

@Override
public void onFailure(Error error) {
promise.reject(ERROR_CODE_DEVICE, error.getMessage());
}
});
}

@ReactMethod
public void setCrashHandlersEnabled(boolean enabled) {
// noop. It's here to share signatures with iOS.
Expand Down
90 changes: 88 additions & 2 deletions android/src/main/java/com/reactlibrary/RNCarnivalPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class RNCarnivalPackage implements ReactPackage {
protected ReactApplicationContext reactApplicationContext;
protected boolean displayInAppNotifications;
protected boolean displayInAppNotifications = true;

/**
* Default constructor - should not be used
Expand All @@ -25,18 +25,37 @@ protected RNCarnivalPackage() {
throw new UnsupportedOperationException("RNCarnivalPackage constructor with parameters must be used");
}

/**
* Constructor for the RNCarnivalPackage.
*
* @param context the application context
* @param appKey the app key provided when you registered your application.
*/
public RNCarnivalPackage(Context context, String appKey) {
Carnival.startEngine(context, appKey);
}

/**
* Constructor for the RNCarnivalPackage.
*
* @param context the application context
* @param appKey the app key provided when you registered your application.
* @param displayInAppNotifications whether the SDK should handle displaying in app notifications
* @deprecated Use {@link #RNCarnivalPackage(Context, String)} or the Builder pattern to specify
* additional options
*/
@Deprecated
public RNCarnivalPackage(Context context, String appKey, boolean displayInAppNotifications) {
Carnival.startEngine(context, appKey);
this.displayInAppNotifications = displayInAppNotifications;
}

protected RNCarnivalPackage(Builder builder) {
Carnival.setGeoIpTrackingDefault(builder.geoIPTrackingDefault);
Carnival.startEngine(builder.context, builder.appKey);
this.displayInAppNotifications = builder.displayInAppNotifications;
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
reactApplicationContext = reactContext;
Expand All @@ -53,4 +72,71 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}

/**
* Builder for the RNCarnivalPackage class. Use this specify additional options
* in the RNCarnivalPackage class such as whether to display in app messages.
*/
public static class Builder {
protected Context context;
protected String appKey;
protected boolean displayInAppNotifications = true;
protected boolean geoIPTrackingDefault = true;

/**
* Default constructor - should not be used
*/
private Builder() {
}

/**
* Create an instance of the RNCarnivalPackage builder.
*
* @param context the application context
* @param appKey the app key provided when you registered your application.
*/
private Builder(Context context, String appKey) {
this.context = context;
this.appKey = appKey;
}

/**
* Get an instance of the RNCarnivalPackage builder.
*
* @param context the application context
* @param appKey the app key provided when you registered your application.
*/
public static Builder createInstance(Context context, String appKey) {
return new Builder(context, appKey);
}

/**
* Set whether the SDK should display in app notifications.
* @param displayInAppNotifications Whether the SDK should display in app notifications
* @return the Builder instance
*/
public Builder setDisplayInAppNotifications(boolean displayInAppNotifications) {
this.displayInAppNotifications = displayInAppNotifications;
return this;
}

/**
* Set the default geo IP tracking value. Defaults to true. Set to false to have geo IP tracking
* disabled on initial device creation.
* @param geoIPTrackingDefault Whether geo IP tracking should be enabled by default.
* @return the Builder instance
*/
public Builder setGeoIPTrackingDefault(boolean geoIPTrackingDefault) {
this.geoIPTrackingDefault = geoIPTrackingDefault;
return this;
}

/**
* Create the RNCarnivalPackage instance.
* @return new RNCarnivalPackage instance
*/
public RNCarnivalPackage build() {
return new RNCarnivalPackage(this);
}
}
}
31 changes: 31 additions & 0 deletions android/src/test/java/com/reactlibrary/RNCarnivalModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,37 @@ public void testSetGeoIPTrackingEnabled() throws Exception {
Carnival.setGeoIpTrackingEnabled(enabled);
}

@Test
public void testSeGeoIPTrackingEnabledWithPromise() throws Exception {
// Create input
Promise promise = mock(Promise.class);
Error error = mock(Error.class);

// Mock methods
PowerMockito.doNothing().when(Carnival.class, "setGeoIpTrackingEnabled", anyBoolean(), any(Carnival.CarnivalHandler.class));

// Initiate test
rnCarnivalModule.setGeoIPTrackingEnabled(false, promise);

// Verify result
ArgumentCaptor<Carnival.CarnivalHandler> argumentCaptor = ArgumentCaptor.forClass(Carnival.CarnivalHandler.class);
PowerMockito.verifyStatic();
Carnival.setGeoIpTrackingEnabled(eq(false), argumentCaptor.capture());
Carnival.CarnivalHandler clearHandler = argumentCaptor.getValue();

// Test success handler
clearHandler.onSuccess(null);
verify(promise).resolve(true);

// Setup error
String errorMessage = "error message";
when(error.getMessage()).thenReturn(errorMessage);

// Test error handler
clearHandler.onFailure(error);
verify(promise).reject(RNCarnivalModule.ERROR_CODE_DEVICE, errorMessage);
}

@Test
public void testClearDevice() throws Exception {
// Create input
Expand Down
90 changes: 81 additions & 9 deletions android/src/test/java/com/reactlibrary/RNCarnivalPackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,125 @@ public class RNCarnivalPackageTest {

private RNCarnivalPackage rnCarnivalPackage;
private String appKey = "App Key";
private boolean displayInAppNotifications = false;

@Before
public void setup() throws Exception {
public void setup() {

}

private void stubCarnival() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(Carnival.class);
PowerMockito.doNothing().when(Carnival.class, "startEngine", any(), any());
}

rnCarnivalPackage = new RNCarnivalPackage(mockContext, appKey, displayInAppNotifications);
private void setupDefaultPackage() throws Exception {
stubCarnival();
rnCarnivalPackage = new RNCarnivalPackage(mockContext, appKey);
}

@Test
public void testConstructor() throws Exception {
setupDefaultPackage();
PowerMockito.verifyStatic();
Carnival.startEngine(mockContext, appKey);
}

@Test
public void testConstructor() {
public void testDisplayInAppConstructor() throws Exception {
stubCarnival();
RNCarnivalPackage carnivalPackage = new RNCarnivalPackage(mockContext, appKey, false);
PowerMockito.verifyStatic();
Carnival.startEngine(mockContext, appKey);
Assert.assertFalse(rnCarnivalPackage.displayInAppNotifications);
Assert.assertFalse(carnivalPackage.displayInAppNotifications);
}

@Test
public void testBuilderConstructor() throws Exception {
stubCarnival();
RNCarnivalPackage.Builder builder = RNCarnivalPackage.Builder.createInstance(mockContext, appKey)
.setDisplayInAppNotifications(false)
.setGeoIPTrackingDefault(false);
RNCarnivalPackage carnivalPackage = new RNCarnivalPackage(builder);

PowerMockito.verifyStatic();
Carnival.setGeoIpTrackingDefault(false);
PowerMockito.verifyStatic();
Carnival.startEngine(mockContext, appKey);
Assert.assertFalse(carnivalPackage.displayInAppNotifications);
}

@Test
public void testDefaultConstructor() {
boolean exceptionThrown = false;
try {
RNCarnivalPackage rnCarnivalPackage = new RNCarnivalPackage();
new RNCarnivalPackage();
} catch (UnsupportedOperationException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
}

@Test
public void testCreateNativeModules() {
public void testCreateNativeModules() throws Exception {
setupDefaultPackage();
List<NativeModule> nativeModules = rnCarnivalPackage.createNativeModules(reactApplicationContext);
Assert.assertEquals(1, nativeModules.size());
NativeModule nativeModule = nativeModules.get(0);
Assert.assertTrue(nativeModule instanceof RNCarnivalModule);
}

@Test
public void testCreateJSModules() {
public void testCreateJSModules() throws Exception {
setupDefaultPackage();
List<Class<? extends JavaScriptModule>> jsModules = rnCarnivalPackage.createJSModules();
Assert.assertTrue(jsModules.isEmpty());
}

@Test
public void testCreateViewManagers() {
public void testCreateViewManagers() throws Exception {
setupDefaultPackage();
List<com.facebook.react.uimanager.ViewManager> viewManagers = rnCarnivalPackage.createViewManagers(reactApplicationContext);
Assert.assertTrue(viewManagers.isEmpty());
}


// Builder Tests

@Test
public void testBuilderCreateInstance() {
RNCarnivalPackage.Builder builder = RNCarnivalPackage.Builder.createInstance(mockContext, appKey);
Assert.assertEquals(mockContext, builder.context);
Assert.assertEquals(appKey, builder.appKey);
}

@Test
public void testBuilderSetDisplayInAppNotifications() {
RNCarnivalPackage.Builder builder = RNCarnivalPackage.Builder.createInstance(mockContext, appKey)
.setDisplayInAppNotifications(false);
Assert.assertFalse(builder.displayInAppNotifications);
}

@Test
public void testBuilderSetGeoIPTrackingDefault() {
RNCarnivalPackage.Builder builder = RNCarnivalPackage.Builder.createInstance(mockContext, appKey)
.setGeoIPTrackingDefault(false);
Assert.assertFalse(builder.geoIPTrackingDefault);
}

@Test
public void testBuilderBuild() throws Exception {
stubCarnival();
RNCarnivalPackage carnivalPackage = RNCarnivalPackage.Builder.createInstance(mockContext, appKey)
.setDisplayInAppNotifications(false)
.setGeoIPTrackingDefault(false)
.build();

PowerMockito.verifyStatic();
Carnival.setGeoIpTrackingDefault(false);
PowerMockito.verifyStatic();
Carnival.startEngine(mockContext, appKey);

Assert.assertFalse(carnivalPackage.displayInAppNotifications);
}
}
Binary file modified ios/Carnival.framework/Carnival
Binary file not shown.
Loading

0 comments on commit b57fb0a

Please sign in to comment.