PendingIntentFactory { @Nullable static PendingIntent getPendingIntent(@Nullable Context context, int pendingIntentType, int flags) { if (context == null) { return null; } int broadcastSenderRequestCode; Intent intent; switch (pendingIntentType) { case PENDING_INTENT_LOCATION: intent = new Intent(context.getApplicationContext(), CTLocationUpdateReceiver.class); intent.setAction(CTGeofenceConstants.ACTION_LOCATION_RECEIVER); broadcastSenderRequestCode = 10100111; break; case PENDING_INTENT_GEOFENCE: intent = new Intent(context.getApplicationContext(), CTGeofenceReceiver.class); intent.setAction(CTGeofenceConstants.ACTION_GEOFENCE_RECEIVER); broadcastSenderRequestCode = 1001001; break; default: throw new IllegalArgumentException("invalid pendingIntentType"); } return PendingIntent.getBroadcast(context.getApplicationContext(), broadcastSenderRequestCode, intent, flags); }    }
@Test public void testGetPendingIntentForFlagNoCreate() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_NO_CREATE); assertNull(actual); }
@Test public void testGetPendingIntentForFlagUpdateCurrent() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); assertNotNull(actual); }
@Test public void testGetPendingIntentForGeofence() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(actual); assertTrue(shadowPendingIntent.isBroadcastIntent()); assertEquals(1001001, shadowPendingIntent.getRequestCode()); assertEquals(PendingIntent.FLAG_UPDATE_CURRENT, shadowPendingIntent.getFlags()); ComponentName actualComponentName = new ComponentName(application, CTGeofenceReceiver.class); assertEquals(CTGeofenceConstants.ACTION_GEOFENCE_RECEIVER, shadowPendingIntent.getSavedIntent().getAction()); assertEquals(actualComponentName, shadowPendingIntent.getSavedIntent().getComponent()); }
@Test(expected = IllegalArgumentException.class) public void testGetPendingIntentForInvalidType() { PendingIntentFactory.getPendingIntent(application, 9, PendingIntent.FLAG_UPDATE_CURRENT); }
@Test public void testGetPendingIntentForLocation() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_LOCATION, PendingIntent.FLAG_UPDATE_CURRENT); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(actual); assertTrue(shadowPendingIntent.isBroadcastIntent()); assertEquals(10100111, shadowPendingIntent.getRequestCode()); assertEquals(PendingIntent.FLAG_UPDATE_CURRENT, shadowPendingIntent.getFlags()); ComponentName actualComponentName = new ComponentName(application, CTLocationUpdateReceiver.class); assertEquals(CTGeofenceConstants.ACTION_LOCATION_RECEIVER, shadowPendingIntent.getSavedIntent().getAction()); assertEquals(actualComponentName, shadowPendingIntent.getSavedIntent().getComponent()); }
@Test public void testGetPendingIntentWhenContextIsNull() { PendingIntent actual = PendingIntentFactory.getPendingIntent(null, PendingIntentFactory.PENDING_INTENT_LOCATION, PendingIntent.FLAG_NO_CREATE); assertNull(actual); }
@Test public void testGetPendingIntentWhenInstanceAlreadyExist() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent actual1 = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_NO_CREATE); assertSame(actual, actual1); }
@Test public void testGetPendingIntentWhenInstanceAlreadyExist1() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent actual1 = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); assertSame(actual, actual1); }
@Test public void testGetPendingIntentWhenInstanceCancel() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); actual.cancel(); PendingIntent actual1 = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_NO_CREATE); assertNotSame(actual, actual1); assertNull(actual1); }
@Test public void testGetPendingIntentWhenInstanceCancel1() { PendingIntent actual = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); actual.cancel(); PendingIntent actual1 = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_GEOFENCE, PendingIntent.FLAG_UPDATE_CURRENT); assertNotSame(actual, actual1); }