32
32
import android .content .Intent ;
33
33
import android .content .pm .ActivityInfo ;
34
34
import android .content .pm .ResolveInfo ;
35
+ import android .content .pm .ServiceInfo ;
35
36
import android .database .Cursor ;
36
37
import android .database .sqlite .SQLiteDatabase ;
37
38
import android .os .Bundle ;
38
39
39
40
import com .onesignal .BuildConfig ;
40
- import com .onesignal .NotificationBundleProcessor ;
41
+ import com .onesignal .NotificationExtenderService ;
41
42
import com .onesignal .NotificationOpenedProcessor ;
43
+ import com .onesignal .OSNotificationDisplayedResult ;
44
+ import com .onesignal .OSNotificationPayload ;
42
45
import com .onesignal .OneSignalDbHelper ;
46
+ import com .onesignal .OneSignalPackagePrivateHelper ;
43
47
import com .onesignal .ShadowBadgeCountUpdater ;
44
48
import com .onesignal .ShadowOneSignalRestClient ;
45
49
import com .onesignal .ShadowRoboNotificationManager ;
49
53
50
54
import junit .framework .Assert ;
51
55
56
+ import org .json .JSONObject ;
52
57
import org .junit .Before ;
53
58
import org .junit .BeforeClass ;
54
59
import org .junit .Test ;
55
60
import org .junit .runner .RunWith ;
56
61
import org .robolectric .Robolectric ;
57
62
import org .robolectric .RuntimeEnvironment ;
63
+ import org .robolectric .Shadows ;
58
64
import org .robolectric .annotation .Config ;
59
65
import org .robolectric .shadows .ShadowLog ;
60
66
import org .robolectric .shadows .ShadowSystemClock ;
67
+ import org .robolectric .util .ServiceController ;
61
68
62
69
import java .util .List ;
63
70
@@ -116,7 +123,7 @@ public static Bundle getBaseNotifBundle(String id) {
116
123
static Intent createOpenIntent (int notifId , Bundle bundle ) {
117
124
return new Intent ()
118
125
.putExtra ("notificationId" , notifId )
119
- .putExtra ("onesignal_data" , NotificationBundleProcessor .bundleAsJSONObject (bundle ).toString ());
126
+ .putExtra ("onesignal_data" , OneSignalPackagePrivateHelper .bundleAsJSONObject (bundle ).toString ());
120
127
}
121
128
122
129
private Intent createOpenIntent (Bundle bundle ) {
@@ -127,14 +134,14 @@ private Intent createOpenIntent(Bundle bundle) {
127
134
public void shouldSetTitleCorrectly () throws Exception {
128
135
// Should use app's Title by default
129
136
Bundle bundle = getBaseNotifBundle ();
130
- NotificationBundleProcessor . Process (blankActivity , bundle );
137
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
131
138
Assert .assertEquals ("UnitTestApp" , ShadowRoboNotificationManager .lastNotif .getContentTitle ());
132
139
Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
133
140
134
141
// Should allow title from GCM payload.
135
142
bundle = getBaseNotifBundle ("UUID2" );
136
143
bundle .putString ("title" , "title123" );
137
- NotificationBundleProcessor . Process (blankActivity , bundle );
144
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
138
145
Assert .assertEquals ("title123" , ShadowRoboNotificationManager .lastNotif .getContentTitle ());
139
146
Assert .assertEquals (2 , ShadowBadgeCountUpdater .lastCount );
140
147
}
@@ -143,7 +150,7 @@ public void shouldSetTitleCorrectly() throws Exception {
143
150
public void shouldHandleBasicNotifications () throws Exception {
144
151
// Make sure the notification got posted and the content is correct.
145
152
Bundle bundle = getBaseNotifBundle ();
146
- NotificationBundleProcessor . Process (blankActivity , bundle );
153
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
147
154
Assert .assertEquals (notifMessage , ShadowRoboNotificationManager .lastNotif .getContentText ());
148
155
Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
149
156
@@ -165,14 +172,14 @@ public void shouldHandleBasicNotifications() throws Exception {
165
172
int firstNotifId = cursor .getInt (1 );
166
173
167
174
// Should not display a duplicate notification, count should still be 1
168
- NotificationBundleProcessor . Process (blankActivity , bundle );
175
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
169
176
cursor = readableDb .query (NotificationTable .TABLE_NAME , null , null , null , null , null , null );
170
177
Assert .assertEquals (1 , cursor .getCount ());
171
178
Assert .assertEquals (0 , ShadowBadgeCountUpdater .lastCount );
172
179
173
180
// Display a second notification
174
181
bundle = getBaseNotifBundle ("UUID2" );
175
- NotificationBundleProcessor . Process (blankActivity , bundle );
182
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
176
183
cursor = readableDb .query (NotificationTable .TABLE_NAME , new String [] { "android_notification_id" }, "android_notification_id <> " + firstNotifId , null , null , null , null );
177
184
cursor .moveToFirst ();
178
185
int secondNotifId = cursor .getInt (0 );
@@ -184,28 +191,29 @@ public void shouldHandleBasicNotifications() throws Exception {
184
191
// Should of been added for a total of 2 records now.
185
192
// First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
186
193
bundle = getBaseNotifBundle ("UUID3" );
187
- NotificationBundleProcessor . Process (blankActivity , bundle );
194
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
188
195
cursor = readableDb .query (NotificationTable .TABLE_NAME , new String [] { "android_notification_id" }, null , null , null , null , null );
189
196
Assert .assertEquals (2 , cursor .getCount ());
190
197
Assert .assertEquals (2 , ShadowBadgeCountUpdater .lastCount );
191
198
192
199
cursor .moveToFirst ();
193
- boolean foundSecond = true ;
200
+ boolean foundSecond = false ;
194
201
do {
195
202
Assert .assertTrue (cursor .getInt (0 ) != firstNotifId );
196
203
if (cursor .getInt (0 ) == secondNotifId )
197
204
foundSecond = true ;
198
205
} while (cursor .moveToNext ());
199
206
200
207
Assert .assertTrue (foundSecond );
208
+ cursor .close ();
201
209
}
202
210
203
211
@ Test
204
212
public void shouldGenerate2BasicGroupNotifications () throws Exception {
205
213
// Make sure the notification got posted and the content is correct.
206
214
Bundle bundle = getBaseNotifBundle ();
207
215
bundle .putString ("grp" , "test1" );
208
- NotificationBundleProcessor . Process (blankActivity , bundle );
216
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
209
217
210
218
List <PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
211
219
Assert .assertEquals (2 , postedNotifs .size ());
@@ -233,7 +241,7 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
233
241
bundle .putString ("alert" , "Notif test 2" );
234
242
bundle .putString ("custom" , "{\" i\" : \" UUID2\" }" );
235
243
bundle .putString ("grp" , "test1" );
236
- NotificationBundleProcessor . Process (blankActivity , bundle );
244
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
237
245
238
246
postedNotifs = ShadowRoboNotificationManager .notifications ;
239
247
Assert .assertEquals (2 , postedNotifs .size ());
@@ -263,10 +271,121 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
263
271
bundle .putString ("alert" , "Notif test 3" );
264
272
bundle .putString ("custom" , "{\" i\" : \" UUID3\" }" );
265
273
bundle .putString ("grp" , "test1" );
266
- NotificationBundleProcessor . Process (blankActivity , bundle );
274
+ OneSignalPackagePrivateHelper . NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
267
275
268
276
Assert .assertEquals ("Notif test 3" , postedNotifs .get (0 ).notif .getContentText ());
269
277
Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotifs .get (0 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
270
278
Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
279
+ cursor .close ();
280
+ }
281
+
282
+ @ Test
283
+ public void shouldFireNotificationExtenderService () throws Exception {
284
+ Bundle bundle = getBaseNotifBundle ();
285
+
286
+ Intent serviceIntent = new Intent ();
287
+ serviceIntent .setPackage ("com.onesignal.example" );
288
+ serviceIntent .setAction ("com.onesignal.NotificationExtender" );
289
+ ResolveInfo resolveInfo = new ResolveInfo ();
290
+ resolveInfo .serviceInfo = new ServiceInfo ();
291
+ resolveInfo .serviceInfo .name = "com.onesignal.example.NotificationExtenderServiceTest" ;
292
+ RuntimeEnvironment .getRobolectricPackageManager ().addResolveInfoForIntent (serviceIntent , resolveInfo );
293
+
294
+ boolean ret = OneSignalPackagePrivateHelper .GcmBroadcastReceiver_processBundle (blankActivity , bundle );
295
+ Assert .assertEquals (true , ret );
296
+
297
+ Intent intent = Shadows .shadowOf (blankActivity ).getNextStartedService ();
298
+ Assert .assertEquals ("com.onesignal.NotificationExtender" , intent .getAction ());
299
+
300
+ ServiceController <NotificationExtenderServiceTest > controller = Robolectric .buildService (NotificationExtenderServiceTest .class );
301
+ NotificationExtenderServiceTest service = controller .attach ().create ().get ();
302
+ Intent testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
303
+ testIntent .putExtras (getBundleWithAllOptionsSet ());
304
+ controller .withIntent (testIntent ).startCommand (0 , 0 );
305
+
306
+ OSNotificationPayload notification = service .notification ;
307
+ Assert .assertEquals ("Test H" , notification .title );
308
+ Assert .assertEquals ("Test B" , notification .message );
309
+ Assert .assertEquals ("9764eaeb-10ce-45b1-a66d-8f95938aaa51" , notification .notificationId );
310
+
311
+ Assert .assertEquals (true , notification .backgroundData );
312
+ Assert .assertEquals (0 , notification .visibility );
313
+ Assert .assertEquals ("FF0000FF" , notification .backgroundColor );
314
+ Assert .assertEquals ("703322744261" , notification .fromProjectNumber );
315
+ Assert .assertEquals ("FFFFFF00" , notification .ledColor );
316
+ Assert .assertEquals ("big_picture" , notification .bigPicture );
317
+ Assert .assertEquals ("large_icon" , notification .largeIcon );
318
+ Assert .assertEquals ("small_icon" , notification .smallIcon );
319
+ Assert .assertEquals ("test_sound" , notification .sound );
320
+ Assert .assertEquals ("You test $[notif_count] MSGs!" , notification .groupMessage );
321
+ Assert .assertEquals ("http://google.com" , notification .launchUrl );
322
+
323
+ Assert .assertEquals ("id1" , notification .actionButtons .get (0 ).id );
324
+ Assert .assertEquals ("button1" , notification .actionButtons .get (0 ).text );
325
+ Assert .assertEquals ("ic_menu_share" , notification .actionButtons .get (0 ).icon );
326
+ Assert .assertEquals ("id2" , notification .actionButtons .get (1 ).id );
327
+ Assert .assertEquals ("button2" , notification .actionButtons .get (1 ).text );
328
+ Assert .assertEquals ("ic_menu_send" , notification .actionButtons .get (1 ).icon );
329
+
330
+ JSONObject additionalData = notification .additionalData ;
331
+ Assert .assertEquals ("myValue" , additionalData .getString ("myKey" ));
332
+ Assert .assertEquals ("nValue" , additionalData .getJSONObject ("nested" ).getString ("nKey" ));
333
+
334
+ Assert .assertNotSame (-1 , service .notificationId );
335
+ }
336
+
337
+ private static Bundle getBundleWithAllOptionsSet () {
338
+ Bundle bundle = new Bundle ();
339
+
340
+ bundle .putString ("title" , "Test H" );
341
+ bundle .putString ("alert" , "Test B" );
342
+ bundle .putString ("bgn" , "1" );
343
+ bundle .putString ("vis" , "0" );
344
+ bundle .putString ("bgac" , "FF0000FF" );
345
+ bundle .putString ("from" , "703322744261" );
346
+ bundle .putString ("ledc" , "FFFFFF00" );
347
+ bundle .putString ("bicon" , "big_picture" );
348
+ bundle .putString ("licon" , "large_icon" );
349
+ bundle .putString ("sicon" , "small_icon" );
350
+ bundle .putString ("sound" , "test_sound" );
351
+ bundle .putString ("grp_msg" , "You test $[notif_count] MSGs!" );
352
+ bundle .putString ("collapse_key" , "do_not_collapse" );
353
+
354
+
355
+ bundle .putString ("custom" ,
356
+ "{\" a\" : {" +
357
+ " \" myKey\" : \" myValue\" ," +
358
+ " \" nested\" : {\" nKey\" : \" nValue\" }," +
359
+ " \" actionButtons\" : [{\" id\" : \" id1\" , \" text\" : \" button1\" , \" icon\" : \" ic_menu_share\" }," +
360
+ " {\" id\" : \" id2\" , \" text\" : \" button2\" , \" icon\" : \" ic_menu_send\" }" +
361
+ " ]," +
362
+ " \" actionSelected\" : \" __DEFAULT__\" " +
363
+ " }," +
364
+ "\" u\" :\" http://google.com\" ," +
365
+ "\" i\" :\" 9764eaeb-10ce-45b1-a66d-8f95938aaa51\" " +
366
+ "}" );
367
+
368
+ return bundle ;
369
+ }
370
+
371
+
372
+ public static class NotificationExtenderServiceTest extends NotificationExtenderService {
373
+ public OSNotificationPayload notification ;
374
+ public int notificationId = -1 ;
375
+
376
+ // Override onStart to manually call onHandleIntent on the main thread.
377
+ @ Override
378
+ public void onStart (Intent intent , int startId ) {
379
+ onHandleIntent (intent );
380
+ stopSelf (startId );
381
+ }
382
+
383
+ @ Override
384
+ protected boolean onNotificationProcessing (OSNotificationPayload notification ) {
385
+ this .notification = notification ;
386
+ notificationId = displayNotification (new OverrideSettings ()).notificationId ;
387
+
388
+ return true ;
389
+ }
271
390
}
272
391
}
0 commit comments