5
5
import android .support .annotation .NonNull ;
6
6
7
7
import com .onesignal .NotificationOpenedActivityHMS ;
8
+ import com .onesignal .OSNotificationOpenResult ;
8
9
import com .onesignal .OneSignal ;
9
10
import com .onesignal .OneSignalPackagePrivateHelper .UserState ;
10
11
import com .onesignal .ShadowCustomTabsClient ;
17
18
import com .onesignal .StaticResetHelper ;
18
19
import com .onesignal .example .BlankActivity ;
19
20
21
+ import org .json .JSONException ;
20
22
import org .json .JSONObject ;
21
23
import org .junit .Before ;
22
24
import org .junit .BeforeClass ;
30
32
31
33
import java .util .UUID ;
32
34
35
+ import static com .onesignal .OneSignalPackagePrivateHelper .GenerateNotification .BUNDLE_KEY_ACTION_ID ;
33
36
import static com .onesignal .InAppMessagingHelpers .ONESIGNAL_APP_ID ;
34
37
import static com .test .onesignal .RestClientAsserts .assertNotificationOpenAtIndex ;
35
38
import static com .test .onesignal .TestHelpers .fastColdRestartApp ;
36
39
import static com .test .onesignal .TestHelpers .threadAndTaskWait ;
37
40
import static junit .framework .Assert .assertEquals ;
38
41
import static org .robolectric .Shadows .shadowOf ;
39
42
40
-
41
43
@ Config (
42
44
packageName = "com.onesignal.example" ,
43
45
shadows = {
@@ -68,27 +70,50 @@ public void beforeEachTest() throws Exception {
68
70
ShadowOSUtils .supportsHMS (true );
69
71
}
70
72
71
- private static Intent helper_baseHMSOpenIntent () {
73
+ private static @ NonNull Intent helper_baseHMSOpenIntent () {
72
74
return new Intent ()
73
75
.setFlags (Intent .FLAG_ACTIVITY_NO_HISTORY | Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_MULTIPLE_TASK )
74
76
.setAction ("android.intent.action.VIEW" );
75
77
}
76
78
77
- private static void helper_startHMSOpenActivity (@ NonNull Intent intent ) {
78
- Robolectric .buildActivity (NotificationOpenedActivityHMS .class , intent ).create ();
79
+ private static @ NonNull Intent helper_basicOSHMSOpenIntent () throws JSONException {
80
+ return helper_baseHMSOpenIntent ()
81
+ .putExtra (
82
+ "custom" ,
83
+ new JSONObject () {{
84
+ put ("i" , UUID .randomUUID ().toString ());
85
+ }}.toString ()
86
+ );
79
87
}
80
88
81
- private static void helper_initSDKAndFireHMSNotificationOpenIntent () throws Exception {
82
- OneSignal .init (RuntimeEnvironment .application , "123456789" , ONESIGNAL_APP_ID );
83
- fastColdRestartApp ();
84
-
85
- Intent intent = helper_baseHMSOpenIntent ()
89
+ private static @ NonNull Intent helper_basicOSHMSOpenIntentWithActionId (final @ NonNull String actionId ) throws JSONException {
90
+ return helper_baseHMSOpenIntent ()
86
91
.putExtra (
87
92
"custom" ,
88
93
new JSONObject () {{
89
94
put ("i" , UUID .randomUUID ().toString ());
95
+ put (BUNDLE_KEY_ACTION_ID , actionId );
90
96
}}.toString ()
91
97
);
98
+ }
99
+
100
+ private static void helper_startHMSOpenActivity (@ NonNull Intent intent ) {
101
+ Robolectric .buildActivity (NotificationOpenedActivityHMS .class , intent ).create ();
102
+ }
103
+
104
+ private static void helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent () throws Exception {
105
+ Intent intent = helper_basicOSHMSOpenIntent ();
106
+ helper_initSDKAndFireHMSNotificationOpenWithIntent (intent );
107
+ }
108
+
109
+ private static void helper_initSDKAndFireHMSNotificationActionButtonTapIntent (@ NonNull String actionId ) throws Exception {
110
+ Intent intent = helper_basicOSHMSOpenIntentWithActionId (actionId );
111
+ helper_initSDKAndFireHMSNotificationOpenWithIntent (intent );
112
+ }
113
+
114
+ private static void helper_initSDKAndFireHMSNotificationOpenWithIntent (@ NonNull Intent intent ) throws Exception {
115
+ OneSignal .init (RuntimeEnvironment .application , "123456789" , ONESIGNAL_APP_ID );
116
+ fastColdRestartApp ();
92
117
93
118
helper_startHMSOpenActivity (intent );
94
119
}
@@ -102,18 +127,34 @@ public void emptyIntent_doesNotThrow() {
102
127
103
128
@ Test
104
129
public void barebonesOSPayload_startsMainActivity () throws Exception {
105
- helper_initSDKAndFireHMSNotificationOpenIntent ();
130
+ helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent ();
106
131
107
132
Intent startedActivity = shadowOf (RuntimeEnvironment .application ).getNextStartedActivity ();
108
133
assertEquals (startedActivity .getComponent ().getClassName (), BlankActivity .class .getName ());
109
134
}
110
135
111
136
@ Test
112
137
public void barebonesOSPayload_makesNotificationOpenRequest () throws Exception {
113
- helper_initSDKAndFireHMSNotificationOpenIntent ();
138
+ helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent ();
114
139
assertNotificationOpenAtIndex (1 , UserState .DEVICE_TYPE_HUAWEI );
115
140
}
116
141
142
+ private static final String TEST_ACTION_ID = "myTestActionId" ;
143
+ private static String lastActionId ;
144
+ @ Test
145
+ public void firesOSNotificationOpenCallbackWithActionId () throws Exception {
146
+ helper_initSDKAndFireHMSNotificationActionButtonTapIntent (TEST_ACTION_ID );
147
+
148
+ OneSignal .startInit (RuntimeEnvironment .application ).setNotificationOpenedHandler (new OneSignal .NotificationOpenedHandler () {
149
+ @ Override
150
+ public void notificationOpened (OSNotificationOpenResult result ) {
151
+ lastActionId = result .action .actionID ;
152
+ }
153
+ }).init ();
154
+
155
+ assertEquals (TEST_ACTION_ID , lastActionId );
156
+ }
157
+
117
158
@ Test
118
159
public void osIAMPreview_showsPreview () throws Exception {
119
160
Activity activity = Robolectric .buildActivity (BlankActivity .class ).create ().get ();
0 commit comments