@Test public void whenANotificationIsSentThenTheTheNotificationIncludesTheStoreName() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); long currentTime = System.currentTimeMillis(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), currentTime); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); ShadowNotification shadowNotification = (ShadowNotification)Shadows.shadowOf(notification); assertTrue(((String) shadowNotification.getContentTitle()).contains(ARBITRARY_STORE_NAME)); }
@Test public void givenAStoreNotificationHasBeenStoredWhenARequestToSendANotificationWithTheTheSameStoreIsReceivedBeforeTheMinimumUpdateTimeForTheSameStoreThenTheNotificationIsNotSent() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); SharedPreferences sharedPreferences = shadowApplication.getSharedPreferences(shadowApplication.getString(R.string.reminder_pref_key), Context.MODE_PRIVATE); sharedPreferences.edit().putString(GroceryReminderConstants.LAST_NOTIFIED_STORE_KEY, ARBITRARY_STORE_NAME) .putLong(GroceryReminderConstants.LAST_NOTIFICATION_TIME_FOR_SAME_STORE, System.currentTimeMillis() - 1) .commit(); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNull(notification); }
@Test public void givenAStoreNotificationHasBeenStoredWhenARequestToSendANotificationWithTheTheSameStoreAfterTheMinimumUpdateTimeForTheSameStoreIsReceivedThenTheNotificationIsSent() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); SharedPreferences sharedPreferences = shadowApplication.getSharedPreferences(shadowApplication.getString(R.string.reminder_pref_key), Context.MODE_PRIVATE); sharedPreferences.edit() .putString(GroceryReminderConstants.LAST_NOTIFIED_STORE_KEY, ARBITRARY_STORE_NAME) .putLong(GroceryReminderConstants.LAST_NOTIFICATION_TIME_FOR_SAME_STORE, GroceryReminderConstants.MIN_LOCATION_UPDATE_TIME_FOR_SAME_STORE_MILLIS + 1) .commit(); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNotNull(notification); }
@Test public void givenAStoreNotificationHasBeenSentWhenARequestToSendANotificationIsReceivedUnderTheMinimumLocationUpdateTimeThenTheNotificationIsNotSent() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); SharedPreferences sharedPreferences = shadowApplication.getSharedPreferences(shadowApplication.getString(R.string.reminder_pref_key), Context.MODE_PRIVATE); sharedPreferences.edit() .putString(GroceryReminderConstants.LAST_NOTIFIED_STORE_KEY, ARBITRARY_STORE_NAME + 1) .putLong(GroceryReminderConstants.LAST_NOTIFICATION_TIME, System.currentTimeMillis()) .commit(); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNull(notification); }
@Test public void whenANotificationIsSentThenTheNotificationShouldBeCreated() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNotNull(notification); }
@Test public void whenANotificationIsSentThenTheSmallIconIsSet() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertEquals(R.drawable.ic_stat_maps_local_grocery_store, notification.icon); }
@Test public void whenANotificationIsSentThenTheContentTitleIsSet() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); assertEquals(RuntimeEnvironment.application.getString(R.string.app_name) + ": " + ARBITRARY_STORE_NAME, notification.getContentTitle()); }
@Test public void whenANotificationIsSentThenTheContentTextIsSet() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); assertEquals(RuntimeEnvironment.application.getString(R.string.reminder_notification), notification.getContentText()); }
@Test public void whenANotificationIsSentThenTheVibrationIsSet() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); assertArrayEquals(GroceryReminderConstants.PROXIMITY_VIBRATION_PATTERN, notification.getRealNotification().vibrate); }
@Test public void whenANotificationIsSentThenTheDefaultNotificationSoundPlays() { Intent intent = buildIntentToListenFor(); groceryStoreNotificationManager.sendNotification(intent); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, notification.getRealNotification().sound); }
@Test public void whenThereAreNoStoresThenTheNotificationIsNotSent() { ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); shadowApplication.getContentResolver().delete(ReminderContract.Locations.CONTENT_URI, null, null); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNull(notification); }
@Test public void givenAStoreNotificationHasNotBeenSentWhenARequestToSendANotificationIsReceivedThenTheIsSent() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNotNull(notification); }
@Test public void whenNoRemindersExistThenNoNotificationIsSent() { ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); shadowApplication.getContentResolver().delete(ReminderContract.Reminders.CONTENT_URI, null, null); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNull(notification); }
@Test public void givenANotificationIsSentWhenTheNotificationIsActedOnThenTheRemindersActivityIsLaunched() { groceryStoreNotificationManager.sendNotification(buildIntentToListenFor()); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(notification.getRealNotification().contentIntent); ShadowIntent shadowIntent = Shadows.shadowOf(shadowPendingIntent.getSavedIntent()); assertEquals(RemindersActivity.class.getName(), shadowIntent.getComponent().getClassName()); }
@Test public void givenANotificationIsSentWhenTheNotificationIsActedOnThenTheTheNotificationIsDismissed() { groceryStoreNotificationManager.sendNotification(buildIntentToListenFor()); ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowNotification notification = Shadows.shadowOf(shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT)); assertTrue((notification.getRealNotification().flags & Notification.FLAG_AUTO_CANCEL) == Notification.FLAG_AUTO_CANCEL); }
@Test public void testShowBackgroundRunningNotification() { HijackingNotification hijackingNotification = new HijackingNotification(RuntimeEnvironment.application); hijackingNotification.show(); ShadowNotificationManager nm = shadowOf((NotificationManager) RuntimeEnvironment.application .getSystemService(Context.NOTIFICATION_SERVICE)); Assert.assertNotNull(nm.getNotification(HijackingNotification.NOTIFICATION_ID)); }
@Test public void testDismissBackgroundRunningNotification() { HijackingNotification hijackingNotification = new HijackingNotification(RuntimeEnvironment.application); hijackingNotification.show(); hijackingNotification.dismiss(); ShadowNotificationManager nm = shadowOf((NotificationManager) RuntimeEnvironment.application .getSystemService(Context.NOTIFICATION_SERVICE)); Assert.assertNull(nm.getNotification(HijackingNotification.NOTIFICATION_ID)); }
@Test public void shouldCreatePersistentNotification() throws Exception { mnc.createNewNotification("title", "content"); NotificationManager notificationManager = (NotificationManager) ACTIVITY.getSystemService(Robolectric.application.NOTIFICATION_SERVICE); ShadowNotificationManager shadowManager = Robolectric.shadowOf(notificationManager); Notification notification = shadowManager.getAllNotifications().get(0); assertThat(Robolectric.shadowOf(notification).isOngoing()).isTrue(); }
@Test public void testNotification() throws IOException { Call<HackerNewsItem> call = mock(Call.class); when(call.execute()).thenReturn(Response.success(new TestHnItem(1L) { @Override public boolean isStoryType() { return true; } @Override public String getRawUrl() { return "http://example.com"; } @Override public long[] getKids() { return new long[]{2L, 3L}; } })); when(TestRestServiceFactory.hnRestService.cachedItem(eq("1"))).thenReturn(call); Call<HackerNewsItem> kid1Call = mock(Call.class); when(kid1Call.execute()).thenReturn(Response.success(new TestHnItem(2L) { @Override public boolean isStoryType() { return false; } })); when(TestRestServiceFactory.hnRestService.cachedItem(eq("2"))).thenReturn(kid1Call); Call<HackerNewsItem> kid2Call = mock(Call.class); when(kid2Call.execute()).thenThrow(IOException.class); when(TestRestServiceFactory.hnRestService.cachedItem(eq("3"))).thenReturn(kid2Call); when(TestRestServiceFactory.hnRestService.networkItem(eq("3"))).thenReturn(kid2Call); PreferenceManager.getDefaultSharedPreferences(service) .edit() .putBoolean(service.getString(R.string.pref_offline_notification), true) .apply(); syncScheduler.scheduleSync(service, "1"); adapter.onPerformSync(mock(Account.class), getLastSyncExtras(), null, null, null); verify(readabilityClient).parse(any(), eq("http://example.com"), readabilityCallbackCaptor.capture()); readabilityCallbackCaptor.getValue().onResponse(""); ShadowNotificationManager notificationManager = shadowOf((NotificationManager) service .getSystemService(Context.NOTIFICATION_SERVICE)); ProgressBar progress = shadowOf(notificationManager.getNotification(1)) .getProgressBar(); assertThat(progress.getProgress()).isEqualTo(3); // self + kid 1 + readability assertThat(progress.getMax()).isEqualTo(104); // self + 2 kids + readability + web shadowOf(adapter.syncDelegate.mWebView).getWebChromeClient() .onProgressChanged(adapter.syncDelegate.mWebView, 100); verify(kid2Call).enqueue(callbackCapture.capture()); callbackCapture.getValue().onFailure(null, null); assertThat(notificationManager.getAllNotifications()).isEmpty(); }
private ShadowNotificationManager getShadowNotificationManager() { return Shadows.shadowOf((NotificationManager) RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE)); }
@Test public void whenThereAreMultipleStoresNearbyThenOnlyOneNotificationIsSent() { ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); List<Notification> notifications = shadowNotificationManager.getAllNotifications(); assertEquals(1, notifications.size()); }
public static ShadowNotificationManager shadowOf(NotificationManager other) { return (ShadowNotificationManager) Robolectric.shadowOf_(other); }
private ShadowNotification getRoutingNotification() { NotificationManager manager = (NotificationManager) act.getSystemService( Context.NOTIFICATION_SERVICE); ShadowNotificationManager sManager = shadowOf(manager); return shadowOf(sManager.getAllNotifications().get(0)); }
@Test public void givenThereIsAStoreNearbyThenTheNotificationIsSent() { ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS}); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNotNull(notification); }
@Test public void givenThereAreStoresButNotNearbyThenTheNotificationIsNotSent() { ShadowNotificationManager shadowNotificationManager = getShadowNotificationManager(); ShadowApplication shadowApplication = (ShadowApplication) Shadows.shadowOf(RuntimeEnvironment.application); insertStoreLocation(shadowApplication); ShadowLocation.setDistanceBetween(new float[] {(float)GroceryReminderConstants.LOCATION_GEOFENCE_RADIUS_METERS + 1}); groceryStoreNotificationManager.sendPotentialNotification(new Location(LocationManager.GPS_PROVIDER), System.currentTimeMillis()); Notification notification = shadowNotificationManager.getNotification(GroceryReminderConstants.NOTIFICATION_PROXIMITY_ALERT); assertNull(notification); }