Search in sources :

Example 1 with OSNotificationReceivedResult

use of com.onesignal.OSNotificationReceivedResult in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method shouldFireNotificationExtenderService.

@Test
@Config(shadows = { ShadowOneSignal.class })
public void shouldFireNotificationExtenderService() throws Exception {
    // Test that GCM receiver starts the NotificationExtenderServiceTest when it is in the AndroidManifest.xml
    Bundle bundle = getBaseNotifBundle();
    Intent serviceIntent = new Intent();
    serviceIntent.setPackage("com.onesignal.example");
    serviceIntent.setAction("com.onesignal.NotificationExtender");
    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.serviceInfo = new ServiceInfo();
    resolveInfo.serviceInfo.name = "com.onesignal.example.NotificationExtenderServiceTest";
    RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent(serviceIntent, resolveInfo);
    boolean ret = OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle(blankActivity, bundle);
    Assert.assertTrue(ret);
    Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
    Assert.assertEquals("com.onesignal.NotificationExtender", intent.getAction());
    // Test that all options are set.
    NotificationExtenderServiceTest service = startNotificationExtender(createInternalPayloadBundle(getBundleWithAllOptionsSet()), NotificationExtenderServiceTest.class);
    OSNotificationReceivedResult notificationReceived = service.notification;
    OSNotificationPayload notificationPayload = notificationReceived.payload;
    Assert.assertEquals("Test H", notificationPayload.title);
    Assert.assertEquals("Test B", notificationPayload.body);
    Assert.assertEquals("9764eaeb-10ce-45b1-a66d-8f95938aaa51", notificationPayload.notificationID);
    Assert.assertEquals(0, notificationPayload.lockScreenVisibility);
    Assert.assertEquals("FF0000FF", notificationPayload.smallIconAccentColor);
    Assert.assertEquals("703322744261", notificationPayload.fromProjectNumber);
    Assert.assertEquals("FFFFFF00", notificationPayload.ledColor);
    Assert.assertEquals("big_picture", notificationPayload.bigPicture);
    Assert.assertEquals("large_icon", notificationPayload.largeIcon);
    Assert.assertEquals("small_icon", notificationPayload.smallIcon);
    Assert.assertEquals("test_sound", notificationPayload.sound);
    Assert.assertEquals("You test $[notif_count] MSGs!", notificationPayload.groupMessage);
    Assert.assertEquals("http://google.com", notificationPayload.launchURL);
    Assert.assertEquals(10, notificationPayload.priority);
    Assert.assertEquals("a_key", notificationPayload.collapseId);
    Assert.assertEquals("id1", notificationPayload.actionButtons.get(0).id);
    Assert.assertEquals("button1", notificationPayload.actionButtons.get(0).text);
    Assert.assertEquals("ic_menu_share", notificationPayload.actionButtons.get(0).icon);
    Assert.assertEquals("id2", notificationPayload.actionButtons.get(1).id);
    Assert.assertEquals("button2", notificationPayload.actionButtons.get(1).text);
    Assert.assertEquals("ic_menu_send", notificationPayload.actionButtons.get(1).icon);
    Assert.assertEquals("test_image_url", notificationPayload.backgroundImageLayout.image);
    Assert.assertEquals("FF000000", notificationPayload.backgroundImageLayout.titleTextColor);
    Assert.assertEquals("FFFFFFFF", notificationPayload.backgroundImageLayout.bodyTextColor);
    JSONObject additionalData = notificationPayload.additionalData;
    Assert.assertEquals("myValue", additionalData.getString("myKey"));
    Assert.assertEquals("nValue", additionalData.getJSONObject("nested").getString("nKey"));
    Assert.assertNotSame(-1, service.notificationId);
    // Test a basic notification without anything special.
    startNotificationExtender(createInternalPayloadBundle(getBaseNotifBundle()), NotificationExtenderServiceTest.class);
    Assert.assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
    // Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
    NotificationExtenderServiceTest.throwInAppCode = true;
    startNotificationExtender(createInternalPayloadBundle(getBaseNotifBundle("NewUUID1")), NotificationExtenderServiceTest.class);
    Assert.assertTrue(ShadowOneSignal.messages.contains("onNotificationProcessing throw an exception"));
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    Assert.assertEquals(3, postedNotifs.size());
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ServiceInfo(android.content.pm.ServiceInfo) JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Intent(android.content.Intent) OSNotificationPayload(com.onesignal.OSNotificationPayload) OSNotificationReceivedResult(com.onesignal.OSNotificationReceivedResult) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test) BuildConfig(com.onesignal.BuildConfig) Config(org.robolectric.annotation.Config)

Aggregations

Intent (android.content.Intent)1 ResolveInfo (android.content.pm.ResolveInfo)1 ServiceInfo (android.content.pm.ServiceInfo)1 Bundle (android.os.Bundle)1 BuildConfig (com.onesignal.BuildConfig)1 OSNotificationPayload (com.onesignal.OSNotificationPayload)1 OSNotificationReceivedResult (com.onesignal.OSNotificationReceivedResult)1 OneSignalPackagePrivateHelper.createInternalPayloadBundle (com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle)1 PostedNotification (com.onesignal.ShadowRoboNotificationManager.PostedNotification)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 Config (org.robolectric.annotation.Config)1