Commit dcdbe3f4 authored by d4vidi's avatar d4vidi

Add tesing + more refactoring

parent c569ebcc
package com.wix.reactnativenotifications.core; package com.wix.reactnativenotifications.core;
import com.facebook.react.bridge.ReactContext;
public interface AppLifecycleFacade { public interface AppLifecycleFacade {
interface AppVisibilityListener { interface AppVisibilityListener {
...@@ -8,6 +10,7 @@ public interface AppLifecycleFacade { ...@@ -8,6 +10,7 @@ public interface AppLifecycleFacade {
} }
boolean isReactInitialized(); boolean isReactInitialized();
ReactContext getRunningReactContext();
boolean isAppVisible(); boolean isAppVisible();
void addVisibilityListener(AppVisibilityListener listener); void addVisibilityListener(AppVisibilityListener listener);
void removeVisibilityListener(AppVisibilityListener listener); void removeVisibilityListener(AppVisibilityListener listener);
......
package com.wix.reactnativenotifications.core;
import android.os.Bundle;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class JsIOHelper {
public boolean sendEventToJS(String eventName, Bundle data, ReactContext reactContext) {
if (reactContext != null) {
sendEventToJS(eventName, Arguments.fromBundle(data), reactContext);
return true;
}
return false;
}
public boolean sendEventToJS(String eventName, WritableMap data, ReactContext reactContext) {
if (reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
return true;
}
return false;
}
}
...@@ -80,7 +80,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements ...@@ -80,7 +80,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
@ReactMethod @ReactMethod
public void cancelLocalNotification(int notificationId) { public void cancelLocalNotification(int notificationId) {
IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onNotificationClear(notificationId); notificationsDrawer.onNotificationClearRequest(notificationId);
} }
@Override @Override
......
...@@ -54,6 +54,16 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade { ...@@ -54,6 +54,16 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade {
return mReactContext.hasActiveCatalystInstance(); return mReactContext.hasActiveCatalystInstance();
} }
@Override
public ReactContext getRunningReactContext() {
ReactContext reactContext = mReactContext;
if (reactContext == null) {
return null;
}
return mReactContext;
}
@Override @Override
public boolean isAppVisible() { public boolean isAppVisible() {
return mIsVisible; return mIsVisible;
......
package com.wix.reactnativenotifications.core;
import android.content.Context;
import android.os.Bundle;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class ReactContextAdapter {
public ReactContext getRunningReactContext(Context context) {
final ReactNativeHost rnHost = ((ReactApplication) context.getApplicationContext()).getReactNativeHost();
if (!rnHost.hasInstance()) {
return null;
}
final ReactInstanceManager instanceManager = rnHost.getReactInstanceManager();
final ReactContext reactContext = instanceManager.getCurrentReactContext();
if (reactContext == null || !reactContext.hasActiveCatalystInstance()) {
return null;
}
return reactContext;
}
public void sendEventToJS(String eventName, Bundle data, Context context) {
final ReactContext reactContext = getRunningReactContext(context);
if (reactContext != null) {
sendEventToJS(eventName, data, reactContext);
}
}
public void sendEventToJS(String eventName, WritableMap data, Context context) {
final ReactContext reactContext = getRunningReactContext(context);
if (reactContext != null) {
sendEventToJS(eventName, data, reactContext);
}
}
public void sendEventToJS(String eventName, Bundle data, ReactContext reactContext) {
sendEventToJS(eventName, Arguments.fromBundle(data), reactContext);
}
public void sendEventToJS(String eventName, WritableMap data, ReactContext reactContext) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
}
}
...@@ -14,7 +14,7 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityLis ...@@ -14,7 +14,7 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityLis
import com.wix.reactnativenotifications.core.InitialNotification; import com.wix.reactnativenotifications.core.InitialNotification;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter; import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
import com.wix.reactnativenotifications.core.ProxyService; import com.wix.reactnativenotifications.core.ProxyService;
import com.wix.reactnativenotifications.core.ReactContextAdapter; import com.wix.reactnativenotifications.core.JsIOHelper;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME;
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME; import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME;
...@@ -24,7 +24,7 @@ public class PushNotification implements IPushNotification { ...@@ -24,7 +24,7 @@ public class PushNotification implements IPushNotification {
final protected Context mContext; final protected Context mContext;
final protected AppLifecycleFacade mAppLifecycleFacade; final protected AppLifecycleFacade mAppLifecycleFacade;
final protected AppLaunchHelper mAppLaunchHelper; final protected AppLaunchHelper mAppLaunchHelper;
final protected ReactContextAdapter mReactContextAdapter; final protected JsIOHelper mJsIOHelper;
final protected PushNotificationProps mNotificationProps; final protected PushNotificationProps mNotificationProps;
final protected AppVisibilityListener mAppVisibilityListener = new AppVisibilityListener() { final protected AppVisibilityListener mAppVisibilityListener = new AppVisibilityListener() {
@Override @Override
...@@ -47,14 +47,14 @@ public class PushNotification implements IPushNotification { ...@@ -47,14 +47,14 @@ public class PushNotification implements IPushNotification {
if (appContext instanceof INotificationsApplication) { if (appContext instanceof INotificationsApplication) {
return ((INotificationsApplication) appContext).getPushNotification(context, bundle, facade, appLaunchHelper); return ((INotificationsApplication) appContext).getPushNotification(context, bundle, facade, appLaunchHelper);
} }
return new PushNotification(context, bundle, facade, appLaunchHelper, new ReactContextAdapter()); return new PushNotification(context, bundle, facade, appLaunchHelper, new JsIOHelper());
} }
protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, ReactContextAdapter reactContextAdapter) { protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper JsIOHelper) {
mContext = context; mContext = context;
mAppLifecycleFacade = appLifecycleFacade; mAppLifecycleFacade = appLifecycleFacade;
mAppLaunchHelper = appLaunchHelper; mAppLaunchHelper = appLaunchHelper;
mReactContextAdapter = reactContextAdapter; mJsIOHelper = JsIOHelper;
mNotificationProps = createProps(bundle); mNotificationProps = createProps(bundle);
} }
...@@ -92,7 +92,7 @@ public class PushNotification implements IPushNotification { ...@@ -92,7 +92,7 @@ public class PushNotification implements IPushNotification {
return; return;
} }
final ReactContext reactContext = mReactContextAdapter.getRunningReactContext(mContext); final ReactContext reactContext = mAppLifecycleFacade.getRunningReactContext();
if (reactContext.getCurrentActivity() == null) { if (reactContext.getCurrentActivity() == null) {
setAsInitialNotification(); setAsInitialNotification();
} }
...@@ -159,15 +159,15 @@ public class PushNotification implements IPushNotification { ...@@ -159,15 +159,15 @@ public class PushNotification implements IPushNotification {
} }
protected int createNotificationId(Notification notification) { protected int createNotificationId(Notification notification) {
return (int) System.currentTimeMillis(); return (int) System.nanoTime();
} }
private void notifyReceivedToJS() { private void notifyReceivedToJS() {
mReactContextAdapter.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mContext); mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
} }
private void notifyOpenedToJS() { private void notifyOpenedToJS() {
mReactContextAdapter.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, mNotificationProps.asBundle(), mContext); mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
} }
protected void launchOrResumeApp() { protected void launchOrResumeApp() {
......
...@@ -8,5 +8,5 @@ public interface IPushNotificationsDrawer { ...@@ -8,5 +8,5 @@ public interface IPushNotificationsDrawer {
void onNewActivity(Activity activity); void onNewActivity(Activity activity);
void onNotificationOpened(); void onNotificationOpened();
void onNotificationClear(int id); void onNotificationClearRequest(int id);
} }
...@@ -54,7 +54,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer { ...@@ -54,7 +54,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer {
} }
@Override @Override
public void onNotificationClear(int id) { public void onNotificationClearRequest(int id) {
final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id); notificationManager.cancel(id);
} }
......
package com.wix.reactnativenotifications.core.notificationdrawer;
import android.app.NotificationManager;
import android.content.Context;
import com.facebook.react.bridge.ReactContext;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class PushNotificationsDrawerTest {
@Mock private ReactContext mReactContext;
@Mock private Context mContext;
@Mock private NotificationManager mNotificationManager;
@Mock private AppLaunchHelper mAppLaunchHelper;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(mNotificationManager);
}
@Test
public void onAppInit_clearAllNotifications() throws Exception {
createUUT().onAppInit();
verify(mNotificationManager).cancelAll();
}
@Test
public void onAppVisible_clearAllNotifications() throws Exception {
createUUT().onAppVisible();
verify(mNotificationManager).cancelAll();
}
@Test
public void onNotificationOpened_clearAllNotifications() throws Exception {
createUUT().onNotificationOpened();
verify(mNotificationManager).cancelAll();
}
@Test
public void onNotificationClearRequest_clearSpecificNotification() throws Exception {
createUUT().onNotificationClearRequest(666);
verify(mNotificationManager).cancel(eq(666));
verify(mNotificationManager, never()).cancelAll();
}
protected PushNotificationsDrawer createUUT() {
return new PushNotificationsDrawer(mContext, mAppLaunchHelper);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment