use of org.robolectric.annotation.Config in project OneSignal-Android-SDK by OneSignal.
the class MainOneSignalClassRunner method testLocationFromSyncAlarm.
@Test
@Config(shadows = { ShadowGoogleApiClientBuilder.class, ShadowGoogleApiClientCompatProxy.class, ShadowFusedLocationApiWrapper.class })
public void testLocationFromSyncAlarm() throws Exception {
ShadowFusedLocationApiWrapper.lat = 1.0d;
ShadowFusedLocationApiWrapper.log = 2.0d;
ShadowFusedLocationApiWrapper.accuracy = 3.0f;
ShadowFusedLocationApiWrapper.time = 12345L;
OneSignalInit();
threadAndTaskWait();
StaticResetHelper.restSetStaticFields();
AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
shadowOf(alarmManager).getScheduledAlarms().clear();
ShadowOneSignalRestClient.lastPost = null;
ShadowApplication.getInstance().grantPermissions("android.permission.ACCESS_COARSE_LOCATION");
Intent intent = new Intent();
// Sync
intent.putExtra("task", 1);
Robolectric.buildService(SyncService.class, intent).startCommand(0, 0);
threadAndTaskWait();
Assert.assertEquals(1.0, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
Assert.assertEquals(2.0, ShadowOneSignalRestClient.lastPost.optDouble("long"));
Assert.assertEquals(3.0, ShadowOneSignalRestClient.lastPost.optDouble("loc_acc"));
Assert.assertEquals(0, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
Assert.assertEquals(true, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
// Checking make sure an update is scheduled.
alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
Assert.assertEquals(1, shadowOf(alarmManager).getScheduledAlarms().size());
intent = shadowOf(shadowOf(alarmManager).getNextScheduledAlarm().operation).getSavedIntent();
Assert.assertEquals(SyncService.class, shadowOf(intent).getIntentClass());
shadowOf(alarmManager).getScheduledAlarms().clear();
}
use of org.robolectric.annotation.Config in project OneSignal-Android-SDK by OneSignal.
the class GenerateNotificationRunner method shouldPreventOtherGCMReceiversWhenSettingEnabled.
@Test
@Config(shadows = { ShadowGcmBroadcastReceiver.class })
public void shouldPreventOtherGCMReceiversWhenSettingEnabled() throws Exception {
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.InAppAlert);
OneSignal.startInit(blankActivity).filterOtherGCMReceivers(true).init();
threadAndTaskWait();
Intent intentGcm = new Intent();
intentGcm.setAction("com.google.android.c2dm.intent.RECEIVE");
intentGcm.putExtra("message_type", "gcm");
Bundle bundle = getBaseNotifBundle();
bundle.putString("o", "[{\"n\": \"text1\", \"i\": \"id1\"}]");
intentGcm.putExtras(bundle);
GcmBroadcastReceiver gcmBroadcastReceiver = new GcmBroadcastReceiver();
gcmBroadcastReceiver.onReceive(blankActivity, intentGcm);
Assert.assertNull(ShadowGcmBroadcastReceiver.lastResultCode);
Assert.assertTrue(ShadowGcmBroadcastReceiver.calledAbortBroadcast);
}
use of org.robolectric.annotation.Config 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());
}
use of org.robolectric.annotation.Config in project OneSignal-Android-SDK by OneSignal.
the class MainOneSignalClassRunner method testLocationSchedule.
@Test
@Config(shadows = { ShadowGoogleApiClientBuilder.class, ShadowGoogleApiClientCompatProxy.class, ShadowFusedLocationApiWrapper.class })
public void testLocationSchedule() throws Exception {
ShadowApplication.getInstance().grantPermissions("android.permission.ACCESS_FINE_LOCATION");
ShadowFusedLocationApiWrapper.lat = 1.0d;
ShadowFusedLocationApiWrapper.log = 2.0d;
ShadowFusedLocationApiWrapper.accuracy = 3.0f;
ShadowFusedLocationApiWrapper.time = 12345L;
// location if we have permission
OneSignalInit();
threadAndTaskWait();
Assert.assertEquals(1.0, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
Assert.assertEquals(2.0, ShadowOneSignalRestClient.lastPost.optDouble("long"));
Assert.assertEquals(3.0, ShadowOneSignalRestClient.lastPost.optDouble("loc_acc"));
Assert.assertEquals(1, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
// Checking make sure an update is scheduled.
AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
Assert.assertEquals(1, shadowOf(alarmManager).getScheduledAlarms().size());
Intent intent = shadowOf(shadowOf(alarmManager).getNextScheduledAlarm().operation).getSavedIntent();
Assert.assertEquals(SyncService.class, shadowOf(intent).getIntentClass());
// Setting up a new point and testing it is sent
ShadowFusedLocationApiWrapper.lat = 1.1d;
ShadowFusedLocationApiWrapper.log = 2.2d;
ShadowFusedLocationApiWrapper.accuracy = 3.3f;
ShadowFusedLocationApiWrapper.time = 12346L;
Robolectric.buildService(SyncService.class, intent).startCommand(0, 0);
threadAndTaskWait();
Assert.assertEquals(1.1d, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
Assert.assertEquals(2.2d, ShadowOneSignalRestClient.lastPost.optDouble("long"));
Assert.assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
Assert.assertEquals(false, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
Assert.assertEquals("11111111-2222-3333-4444-555555555555", ShadowOneSignalRestClient.lastPost.opt("ad_id"));
// Testing loc_bg
blankActivityController.pause();
threadAndTaskWait();
ShadowFusedLocationApiWrapper.time = 12347L;
Robolectric.buildService(SyncService.class, intent).startCommand(0, 0);
threadAndTaskWait();
Assert.assertEquals(1.1d, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
Assert.assertEquals(2.2d, ShadowOneSignalRestClient.lastPost.optDouble("long"));
Assert.assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
Assert.assertEquals(true, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
Assert.assertEquals(1, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
Assert.assertEquals("11111111-2222-3333-4444-555555555555", ShadowOneSignalRestClient.lastPost.opt("ad_id"));
}
use of org.robolectric.annotation.Config in project OneSignal-Android-SDK by OneSignal.
the class GenerateNotificationRunner method shouldCancelNotificationAndUpdateSummary.
@Test
@Config(shadows = { ShadowNotificationRestorer.class })
public void shouldCancelNotificationAndUpdateSummary() throws Exception {
// Setup - Init
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
threadAndTaskWait();
// Setup - Display 3 notifications that will be grouped together.
Bundle bundle = getBaseNotifBundle("UUID1");
bundle.putString("grp", "test1");
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
bundle = getBaseNotifBundle("UUID2");
bundle.putString("grp", "test1");
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
bundle = getBaseNotifBundle("UUID3");
bundle.putString("grp", "test1");
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
// Test - 3 notifis + 1 summary
Assert.assertEquals(4, postedNotifs.size());
// Test - First notification should be the summary
PostedNotification postedSummaryNotification = postedNotifsIterator.next().getValue();
Assert.assertEquals("3 new messages", postedSummaryNotification.getShadow().getContentText());
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
// Setup - Let's cancel a child notification.
PostedNotification postedNotification = postedNotifsIterator.next().getValue();
OneSignal.cancelNotification(postedNotification.id);
// Test - It should update summary text to say 2 notifications
postedNotifs = ShadowRoboNotificationManager.notifications;
// 2 notifis + 1 summary
Assert.assertEquals(3, postedNotifs.size());
postedNotifsIterator = postedNotifs.entrySet().iterator();
postedSummaryNotification = postedNotifsIterator.next().getValue();
Assert.assertEquals("2 new messages", postedSummaryNotification.getShadow().getContentText());
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
// Setup - Let's cancel a 2nd child notification.
postedNotification = postedNotifsIterator.next().getValue();
OneSignal.cancelNotification(postedNotification.id);
// Test - It should update summary notification to be the text of the last remaining one.
postedNotifs = ShadowRoboNotificationManager.notifications;
// 1 notifis + 1 summary
Assert.assertEquals(2, postedNotifs.size());
postedNotifsIterator = postedNotifs.entrySet().iterator();
postedSummaryNotification = postedNotifsIterator.next().getValue();
Assert.assertEquals(notifMessage, postedSummaryNotification.getShadow().getContentText());
Assert.assertEquals(Notification.FLAG_GROUP_SUMMARY, postedSummaryNotification.notif.flags & Notification.FLAG_GROUP_SUMMARY);
// Test - Let's make sure we will have our last notification too
postedNotification = postedNotifsIterator.next().getValue();
Assert.assertEquals(notifMessage, postedNotification.getShadow().getContentText());
// Setup - Let's cancel our 3rd and last child notification.
OneSignal.cancelNotification(postedNotification.id);
// Test - No more notifications! :)
postedNotifs = ShadowRoboNotificationManager.notifications;
Assert.assertEquals(0, postedNotifs.size());
}
Aggregations