Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-notifications
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
冷佳娟
react-native-notifications
Commits
dcdbe3f4
Commit
dcdbe3f4
authored
Nov 29, 2016
by
d4vidi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tesing + more refactoring
parent
c569ebcc
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
267 additions
and
80 deletions
+267
-80
android/src/main/java/com/wix/reactnativenotifications/core/AppLifecycleFacade.java
...wix/reactnativenotifications/core/AppLifecycleFacade.java
+3
-0
android/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java
...ava/com/wix/reactnativenotifications/core/JsIOHelper.java
+26
-0
android/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java
.../reactnativenotifications/core/RNNotificationsModule.java
+1
-1
android/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java
...eactnativenotifications/core/ReactAppLifecycleFacade.java
+10
-0
android/src/main/java/com/wix/reactnativenotifications/core/ReactContextAdapter.java
...ix/reactnativenotifications/core/ReactContextAdapter.java
+0
-51
android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
...tivenotifications/core/notification/PushNotification.java
+9
-9
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java
...ons/core/notificationdrawer/IPushNotificationsDrawer.java
+1
-1
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java
...ions/core/notificationdrawer/PushNotificationsDrawer.java
+1
-1
android/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java
...notifications/core/notification/PushNotificationTest.java
+152
-17
android/src/test/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawerTest.java
.../core/notificationdrawer/PushNotificationsDrawerTest.java
+64
-0
No files found.
android/src/main/java/com/wix/reactnativenotifications/core/AppLifecycleFacade.java
View file @
dcdbe3f4
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
);
...
...
android/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java
0 → 100644
View file @
dcdbe3f4
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
;
}
}
android/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java
View file @
dcdbe3f4
...
@@ -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
.
onNotificationClear
Request
(
notificationId
);
}
}
@Override
@Override
...
...
android/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java
View file @
dcdbe3f4
...
@@ -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
;
...
...
android/src/main/java/com/wix/reactnativenotifications/core/ReactContextAdapter.java
deleted
100644 → 0
View file @
c569ebcc
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
);
}
}
android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
View file @
dcdbe3f4
...
@@ -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.
ReactContextAdapt
er
;
import
com.wix.reactnativenotifications.core.
JsIOHelp
er
;
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
mReactContextAdapt
er
;
final
protected
JsIOHelper
mJsIOHelp
er
;
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
ReactContextAdapt
er
());
return
new
PushNotification
(
context
,
bundle
,
facade
,
appLaunchHelper
,
new
JsIOHelp
er
());
}
}
protected
PushNotification
(
Context
context
,
Bundle
bundle
,
AppLifecycleFacade
appLifecycleFacade
,
AppLaunchHelper
appLaunchHelper
,
ReactContextAdapter
reactContextAdapt
er
)
{
protected
PushNotification
(
Context
context
,
Bundle
bundle
,
AppLifecycleFacade
appLifecycleFacade
,
AppLaunchHelper
appLaunchHelper
,
JsIOHelper
JsIOHelp
er
)
{
mContext
=
context
;
mContext
=
context
;
mAppLifecycleFacade
=
appLifecycleFacade
;
mAppLifecycleFacade
=
appLifecycleFacade
;
mAppLaunchHelper
=
appLaunchHelper
;
mAppLaunchHelper
=
appLaunchHelper
;
m
ReactContextAdapter
=
reactContextAdapt
er
;
m
JsIOHelper
=
JsIOHelp
er
;
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
=
m
ReactContextAdapter
.
getRunningReactContext
(
mContext
);
final
ReactContext
reactContext
=
m
AppLifecycleFacade
.
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
()
{
m
ReactContextAdapter
.
sendEventToJS
(
NOTIFICATION_RECEIVED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mContext
);
m
JsIOHelper
.
sendEventToJS
(
NOTIFICATION_RECEIVED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mAppLifecycleFacade
.
getRunningReactContext
()
);
}
}
private
void
notifyOpenedToJS
()
{
private
void
notifyOpenedToJS
()
{
m
ReactContextAdapter
.
sendEventToJS
(
NOTIFICATION_OPENED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mContext
);
m
JsIOHelper
.
sendEventToJS
(
NOTIFICATION_OPENED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mAppLifecycleFacade
.
getRunningReactContext
()
);
}
}
protected
void
launchOrResumeApp
()
{
protected
void
launchOrResumeApp
()
{
...
...
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java
View file @
dcdbe3f4
...
@@ -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
onNotificationClear
Request
(
int
id
);
}
}
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java
View file @
dcdbe3f4
...
@@ -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
onNotificationClear
Request
(
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
);
}
}
...
...
android/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java
View file @
dcdbe3f4
package
com.wix.reactnativenotifications.core.notification
;
package
com.wix.reactnativenotifications.core.notification
;
import
android.app.Notification
;
import
android.app.NotificationManager
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.content.pm.ApplicationInfo
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
com.facebook.react.bridge.ReactContext
;
import
com.facebook.react.bridge.ReactContext
;
import
com.wix.reactnativenotifications.core.AppLaunchHelper
;
import
com.wix.reactnativenotifications.core.AppLaunchHelper
;
import
com.wix.reactnativenotifications.core.AppLifecycleFacade
;
import
com.wix.reactnativenotifications.core.AppLifecycleFacade
;
import
com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener
;
import
com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener
;
import
com.wix.reactnativenotifications.core.
ReactContextAdapt
er
;
import
com.wix.reactnativenotifications.core.
JsIOHelp
er
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -16,12 +19,18 @@ import org.junit.runner.RunWith;
...
@@ -16,12 +19,18 @@ import org.junit.runner.RunWith;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.Mock
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.mockito.MockitoAnnotations
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.Shadows
;
import
org.robolectric.shadows.ShadowNotification
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotEquals
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyInt
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
@@ -31,25 +40,33 @@ public class PushNotificationTest {
...
@@ -31,25 +40,33 @@ public class PushNotificationTest {
private
static
final
String
NOTIFICATION_OPENED_EVENT_NAME
=
"notificationOpened"
;
private
static
final
String
NOTIFICATION_OPENED_EVENT_NAME
=
"notificationOpened"
;
private
static
final
String
NOTIFICATION_RECEIVED_EVENT_NAME
=
"notificationReceived"
;
private
static
final
String
NOTIFICATION_RECEIVED_EVENT_NAME
=
"notificationReceived"
;
private
static
final
String
DEFAULT_NOTIFICATION_TITLE
=
"Notification-title"
;
private
static
final
String
DEFAULT_NOTIFICATION_BODY
=
"Notification-body"
;
@Mock
private
ReactContext
mReactContext
;
@Mock
private
ReactContext
mReactContext
;
@Mock
private
Context
mContext
;
@Mock
private
Context
mContext
;
@Mock
private
NotificationManager
mNotificationManager
;
@Mock
private
Bundle
mDefaultBundle
;
@Mock
private
Bundle
mDefaultBundle
;
@Mock
private
Intent
mLaunchIntent
;
@Mock
private
Intent
mLaunchIntent
;
@Mock
private
AppLifecycleFacade
mAppLifecycleFacade
;
@Mock
private
AppLifecycleFacade
mAppLifecycleFacade
;
@Mock
private
AppLaunchHelper
mAppLaunchHelper
;
@Mock
private
AppLaunchHelper
mAppLaunchHelper
;
@Mock
private
ReactContextAdapter
mReactContextAdapt
er
;
@Mock
private
JsIOHelper
mJsIOHelp
er
;
@Before
@Before
public
void
setup
()
throws
Exception
{
public
void
setup
()
throws
Exception
{
MockitoAnnotations
.
initMocks
(
this
);
MockitoAnnotations
.
initMocks
(
this
);
when
(
mDefaultBundle
.
getString
(
eq
(
"title"
))).
thenReturn
(
"Notification-title"
);
when
(
mDefaultBundle
.
getString
(
eq
(
"title"
))).
thenReturn
(
DEFAULT_NOTIFICATION_TITLE
);
when
(
mDefaultBundle
.
getString
(
eq
(
"body"
))).
thenReturn
(
"Notification-body"
);
when
(
mDefaultBundle
.
getString
(
eq
(
"body"
))).
thenReturn
(
DEFAULT_NOTIFICATION_BODY
);
when
(
mDefaultBundle
.
clone
()).
thenReturn
(
mDefaultBundle
);
when
(
mDefaultBundle
.
clone
()).
thenReturn
(
mDefaultBundle
);
when
(
mAppLaunchHelper
.
getLaunchIntent
(
eq
(
mContext
))).
thenReturn
(
mLaunchIntent
);
when
(
mAppLaunchHelper
.
getLaunchIntent
(
eq
(
mContext
))).
thenReturn
(
mLaunchIntent
);
when
(
mReactContextAdapter
.
getRunningReactContext
(
mContext
)).
thenReturn
(
mReactContext
);
ApplicationInfo
ai
=
mock
(
ApplicationInfo
.
class
);
when
(
mContext
.
getApplicationInfo
()).
thenReturn
(
ai
);
when
(
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)).
thenReturn
(
mNotificationManager
);
}
}
@Test
@Test
...
@@ -60,12 +77,16 @@ public class PushNotificationTest {
...
@@ -60,12 +77,16 @@ public class PushNotificationTest {
uut
.
onOpened
();
uut
.
onOpened
();
verify
(
mContext
).
startActivity
(
eq
(
mLaunchIntent
));
verify
(
mContext
).
startActivity
(
eq
(
mLaunchIntent
));
// The unit shouldn't wait for visibility in this case cause we dont make the extra effort of
// notifying the notification upon app launch completion (simply cause we dont know when in completes).
// Instead, the user is expected to use getInitialNotification().
verify
(
mAppLifecycleFacade
,
never
()).
addVisibilityListener
(
any
(
AppVisibilityListener
.
class
));
}
}
@Test
@Test
public
void
onOpened_appInvisible_resumeAppWaitForVisibility
()
throws
Exception
{
public
void
onOpened_appInvisible_resumeAppWaitForVisibility
()
throws
Exception
{
when
(
mAppLifecycleFacade
.
isReactInitialized
()).
thenReturn
(
true
);
setUpBackgroundApp
();
when
(
mAppLifecycleFacade
.
isAppVisible
()).
thenReturn
(
false
);
final
PushNotification
uut
=
createUUT
();
final
PushNotification
uut
=
createUUT
();
uut
.
onOpened
();
uut
.
onOpened
();
...
@@ -79,8 +100,7 @@ public class PushNotificationTest {
...
@@ -79,8 +100,7 @@ public class PushNotificationTest {
// Arrange
// Arrange
when
(
mAppLifecycleFacade
.
isReactInitialized
()).
thenReturn
(
true
);
setUpBackgroundApp
();
when
(
mAppLifecycleFacade
.
isAppVisible
()).
thenReturn
(
false
);
// Act
// Act
...
@@ -95,30 +115,145 @@ public class PushNotificationTest {
...
@@ -95,30 +115,145 @@ public class PushNotificationTest {
// Assert
// Assert
verify
(
m
ReactContextAdapter
).
sendEventToJS
(
eq
(
NOTIFICATION_OPENED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
m
Context
));
verify
(
m
JsIOHelper
).
sendEventToJS
(
eq
(
NOTIFICATION_OPENED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mReact
Context
));
}
}
@Test
@Test
public
void
onOpened_appVisible_notifyJS
()
throws
Exception
{
public
void
onOpened_appVisible_notifyJS
()
throws
Exception
{
when
(
mAppLifecycleFacade
.
isReactInitialized
()).
thenReturn
(
true
);
setUpForegroundApp
();
when
(
mAppLifecycleFacade
.
isAppVisible
()).
thenReturn
(
true
);
final
PushNotification
uut
=
createUUT
();
final
PushNotification
uut
=
createUUT
();
uut
.
onOpened
();
uut
.
onOpened
();
verify
(
mContext
,
never
()).
startActivity
(
any
(
Intent
.
class
));
verify
(
mContext
,
never
()).
startActivity
(
any
(
Intent
.
class
));
verify
(
mReactContextAdapter
).
sendEventToJS
(
eq
(
NOTIFICATION_OPENED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mContext
));
verify
(
mJsIOHelper
).
sendEventToJS
(
eq
(
NOTIFICATION_OPENED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mReactContext
));
}
@Test
public
void
onReceived_validData_postNotificationAndNotifyJS
()
throws
Exception
{
// Arrange
setUpForegroundApp
();
// Act
final
PushNotification
uut
=
createUUT
();
uut
.
onReceived
();
// Assert
ArgumentCaptor
<
Notification
>
notificationCaptor
=
ArgumentCaptor
.
forClass
(
Notification
.
class
);
verify
(
mNotificationManager
).
notify
(
anyInt
(),
notificationCaptor
.
capture
());
verifyNotification
(
notificationCaptor
.
getValue
());
verify
(
mJsIOHelper
).
sendEventToJS
(
eq
(
NOTIFICATION_RECEIVED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mReactContext
));
}
@Test
public
void
onReceived_validDataForBackgroundApp_postNotificationAndNotifyJs
()
throws
Exception
{
// Arrange
setUpForegroundApp
();
// Act
final
PushNotification
uut
=
createUUT
();
uut
.
onReceived
();
// Assert
ArgumentCaptor
<
Notification
>
notificationCaptor
=
ArgumentCaptor
.
forClass
(
Notification
.
class
);
verify
(
mNotificationManager
).
notify
(
anyInt
(),
notificationCaptor
.
capture
());
verifyNotification
(
notificationCaptor
.
getValue
());
verify
(
mJsIOHelper
).
sendEventToJS
(
eq
(
NOTIFICATION_RECEIVED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mReactContext
));
}
@Test
public
void
onReceived_validDataForDeadApp_postNotificationDontNotifyJS
()
throws
Exception
{
final
PushNotification
uut
=
createUUT
();
uut
.
onReceived
();
ArgumentCaptor
<
Notification
>
notificationCaptor
=
ArgumentCaptor
.
forClass
(
Notification
.
class
);
verify
(
mNotificationManager
).
notify
(
anyInt
(),
notificationCaptor
.
capture
());
verifyNotification
(
notificationCaptor
.
getValue
());
verify
(
mJsIOHelper
,
never
()).
sendEventToJS
(
eq
(
NOTIFICATION_RECEIVED_EVENT_NAME
),
any
(
Bundle
.
class
),
any
(
ReactContext
.
class
));
}
}
@Test
@Test
public
void
onPostRequest_withValidDataButNoId_postNotifAndNotifyJS
()
throws
Exception
{
public
void
onPostRequest_withValidDataButNoId_postNotifications
()
throws
Exception
{
// Arrange
setUpForegroundApp
();
// Act
final
PushNotification
uut
=
createUUT
();
final
PushNotification
uut
=
createUUT
();
uut
.
onPostRequest
(
null
);
uut
.
onPostRequest
(
null
);
verify
(
mReactContextAdapter
).
sendEventToJS
(
eq
(
NOTIFICATION_RECEIVED_EVENT_NAME
),
eq
(
mDefaultBundle
),
eq
(
mContext
));
// Assert
ArgumentCaptor
<
Notification
>
notificationCaptor
=
ArgumentCaptor
.
forClass
(
Notification
.
class
);
verify
(
mNotificationManager
).
notify
(
anyInt
(),
notificationCaptor
.
capture
());
verifyNotification
(
notificationCaptor
.
getValue
());
// Shouldn't notify an event on an explicit call to notification posting
verify
(
mJsIOHelper
,
never
()).
sendEventToJS
(
eq
(
NOTIFICATION_RECEIVED_EVENT_NAME
),
any
(
Bundle
.
class
),
any
(
ReactContext
.
class
));
}
@Test
public
void
onPostRequest_withValidDataButNoId_idsShouldBeUnique
()
throws
Exception
{
createUUT
().
onPostRequest
(
null
);
createUUT
().
onPostRequest
(
null
);
ArgumentCaptor
<
Integer
>
idsCaptor
=
ArgumentCaptor
.
forClass
(
Integer
.
class
);
verify
(
mNotificationManager
,
times
(
2
)).
notify
(
idsCaptor
.
capture
(),
any
(
Notification
.
class
));
assertNotEquals
(
idsCaptor
.
getAllValues
().
get
(
0
),
idsCaptor
.
getAllValues
().
get
(
1
));
}
@Test
public
void
onPostRequest_withValidDataAndExplicitId_postNotification
()
throws
Exception
{
final
int
id
=
666
;
final
PushNotification
uut
=
createUUT
();
uut
.
onPostRequest
(
id
);
verify
(
mNotificationManager
).
notify
(
eq
(
id
),
any
(
Notification
.
class
));
}
@Test
public
void
onPostRequest_emptyData_postNotification
()
throws
Exception
{
PushNotification
uut
=
createUUT
(
new
Bundle
());
uut
.
onPostRequest
(
null
);
verify
(
mNotificationManager
).
notify
(
anyInt
(),
any
(
Notification
.
class
));
}
}
protected
PushNotification
createUUT
()
{
protected
PushNotification
createUUT
()
{
return
new
PushNotification
(
mContext
,
mDefaultBundle
,
mAppLifecycleFacade
,
mAppLaunchHelper
,
mReactContextAdapter
);
return
createUUT
(
mDefaultBundle
);
}
protected
PushNotification
createUUT
(
Bundle
bundle
)
{
return
new
PushNotification
(
mContext
,
bundle
,
mAppLifecycleFacade
,
mAppLaunchHelper
,
mJsIOHelper
);
}
protected
void
setUpBackgroundApp
()
{
when
(
mAppLifecycleFacade
.
isReactInitialized
()).
thenReturn
(
true
);
when
(
mAppLifecycleFacade
.
getRunningReactContext
()).
thenReturn
(
mReactContext
);
when
(
mAppLifecycleFacade
.
isAppVisible
()).
thenReturn
(
false
);
}
protected
void
setUpForegroundApp
()
{
when
(
mAppLifecycleFacade
.
isReactInitialized
()).
thenReturn
(
true
);
when
(
mAppLifecycleFacade
.
getRunningReactContext
()).
thenReturn
(
mReactContext
);
when
(
mAppLifecycleFacade
.
isAppVisible
()).
thenReturn
(
true
);
}
protected
void
verifyNotification
(
Notification
notification
)
{
ShadowNotification
shadowNotification
=
Shadows
.
shadowOf
(
notification
);
assertEquals
(
shadowNotification
.
getContentText
(),
DEFAULT_NOTIFICATION_BODY
);
assertEquals
(
shadowNotification
.
getContentTitle
(),
DEFAULT_NOTIFICATION_TITLE
);
}
}
}
}
android/src/test/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawerTest.java
0 → 100644
View file @
dcdbe3f4
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment