@Test public void givenPlacesWhenProximityAlertIsAddedThenTheStorePendingIntentRequestCodeIsUnique() { Place place = createDefaultGooglePlace(); Place place2 = new Place(); place2.setLatitude(place.getLatitude()+1); place2.setLongitude(place.getLongitude()); place2.setName("test 2"); place2.setPlaceId("test_id2"); List<Place> places = new ArrayList<Place>(); places.add(place); places.add(place2); groceryStoreManager.addProximityAlerts(places); List<com.groceryreminder.shadows.ShadowLocationManager.ProximityAlert> proximityAlerts = shadowLocationManager.getProximityAlerts(); assertEquals(places.size(), proximityAlerts.size()); ShadowPendingIntent shadowPendingIntent1 = Shadows.shadowOf(proximityAlerts.get(0).getPendingIntent()); ShadowPendingIntent shadowPendingIntent2 = Shadows.shadowOf(proximityAlerts.get(1).getPendingIntent()); assertNotEquals(shadowPendingIntent1.getRequestCode(), shadowPendingIntent2.getRequestCode()); }
private void verifyReturnsMainActivity() { // Tapping on the summary should go to MainActivity for (Notification notification : mNotificationManager.getAllNotifications()) { if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != Notification.FLAG_GROUP_SUMMARY) { continue; } ShadowPendingIntent pendingIntent = Shadows.shadowOf(notification.contentIntent); // We're launching an activity assertTrue(pendingIntent.isActivityIntent()); // There should be 1 intent. The root (MainActivity) assertEquals(1, pendingIntent.getSavedIntents().length); Intent mainActivity = pendingIntent.getSavedIntent(); assertEquals(MainActivity.class.getCanonicalName(), mainActivity.getComponent().getClassName()); } }
@Test public void setAddsTheAlarmIntent() { repo.set(alarm); ShadowAlarmManager.ScheduledAlarm nextAlarm = shadowAlarmManager.getNextScheduledAlarm(); ShadowPendingIntent shadowPendingIntent = shadowOf(nextAlarm.operation); Intent expectedIntent = new Intent(context, AlarmReceiver.class); expectedIntent.putExtra(Alarm.ALARM_ID_KEY, alarm.getId()); assertEquals(triggerTime.getMillis(), nextAlarm.triggerAtTime); assertTrue(expectedIntent.filterEquals(shadowPendingIntent.getSavedIntent())); assertEquals(PendingIntent.FLAG_UPDATE_CURRENT, shadowPendingIntent.getFlags()); }
@Test public void whenProximityAlertIsAddedThenThePendingIntentIsSetToBroadcast() { Place place = createDefaultGooglePlace(); List<Place> places = new ArrayList<Place>(); places.add(place); groceryStoreManager.addProximityAlerts(places); com.groceryreminder.shadows.ShadowLocationManager.ProximityAlert proximityAlert = shadowLocationManager.getProximityAlert(place.getLatitude(), place.getLongitude()); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(proximityAlert.getPendingIntent()); assertTrue(shadowPendingIntent.isBroadcastIntent()); }
@Test public void whenProximityAlertIsAddedThenTheStorePendingIntentIsSet() { Place place = createDefaultGooglePlace(); List<Place> places = new ArrayList<Place>(); places.add(place); groceryStoreManager.addProximityAlerts(places); com.groceryreminder.shadows.ShadowLocationManager.ProximityAlert proximityAlert = shadowLocationManager.getProximityAlert(place.getLatitude(), place.getLongitude()); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(proximityAlert.getPendingIntent()); ShadowIntent shadowIntent = Shadows.shadowOf(shadowPendingIntent.getSavedIntent()); assertEquals(GroceryReminderConstants.ACTION_STORE_PROXIMITY_EVENT, shadowIntent.getAction()); }
@Test public void whenProximityAlertIsAddedThenTheStorePendingIntentCancelsTheCurrentRequest() { Place place = createDefaultGooglePlace(); List<Place> places = new ArrayList<Place>(); places.add(place); groceryStoreManager.addProximityAlerts(places); com.groceryreminder.shadows.ShadowLocationManager.ProximityAlert proximityAlert = shadowLocationManager.getProximityAlert(place.getLatitude(), place.getLongitude()); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(proximityAlert.getPendingIntent()); assertEquals(PendingIntent.FLAG_CANCEL_CURRENT, shadowPendingIntent.getFlags()); }
@Test public void whenProximityAlertIsAddedThenTheStorePendingIntentContainsTheStoreName() { Place place = createDefaultGooglePlace(); List<Place> places = new ArrayList<Place>(); places.add(place); groceryStoreManager.addProximityAlerts(places); com.groceryreminder.shadows.ShadowLocationManager.ProximityAlert proximityAlert = shadowLocationManager.getProximityAlert(place.getLatitude(), place.getLongitude()); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(proximityAlert.getPendingIntent()); assertEquals(place.getName(), shadowPendingIntent.getSavedIntent().getStringExtra(ReminderContract.Locations.NAME)); }
@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()); }
private void verifyReturnsMessageActivity(long expectedThreadId) { for (Notification notification : mNotificationManager.getAllNotifications()) { ShadowPendingIntent pendingIntent = Shadows.shadowOf(notification.contentIntent); // We're launching an activity assertTrue(pendingIntent.isActivityIntent()); // There should be 2 intents. The root (MainActivity) and the child (MessageActivity). assertEquals(2, pendingIntent.getSavedIntents().length); Intent mainActivity = pendingIntent.getSavedIntents()[0]; Intent messageActivity = pendingIntent.getSavedIntents()[1]; assertEquals(MainActivity.class.getCanonicalName(), mainActivity.getComponent().getClassName()); assertEquals(MessageActivity.class.getCanonicalName(), messageActivity.getComponent().getClassName()); // MessageActivity should have extras for the thread we care about // We don't care about the implementation (thread vs id). String threadId = messageActivity.getStringExtra(MessageActivity.EXTRA_THREAD_ID); Thread thread = messageActivity.getParcelableExtra(MessageActivity.EXTRA_THREAD); if (threadId != null) { assertEquals(Long.toString(expectedThreadId), threadId); } else if (thread != null) { assertEquals(Long.toString(expectedThreadId), thread.getId()); } else { throw new IllegalStateException("No thread or thread id given"); } } }
@Test public void shouldRestartServiceUsingAlarmManagerWhenTaskRemoved() { when(mockedTimeProvider.getDate()).thenReturn(new Date(ANY_TIME)); startSmsInterceptorService(); smsRadarService.onTaskRemoved(ANY_INTENT); ArgumentCaptor<PendingIntent> pendingIntentArgumentCaptor = ArgumentCaptor.forClass(PendingIntent.class); verify(mockedAlarmManager).set(eq(AlarmManager.RTC_WAKEUP), eq(ANY_TIME + ONE_SECOND), pendingIntentArgumentCaptor.capture()); PendingIntent capturedPendingIntent = pendingIntentArgumentCaptor.getValue(); ShadowPendingIntent pendingIntent = Robolectric.shadowOf(capturedPendingIntent); ShadowIntent intent = Robolectric.shadowOf(pendingIntent.getSavedIntent()); assertEquals(SmsRadarService.class, intent.getIntentClass()); }
public static ShadowPendingIntent shadowOf(PendingIntent instance) { return (ShadowPendingIntent) shadowOf_(instance); }