EventTracker { boolean shouldTrack(Activity activity) { if(wifiOnly && !utils.isOnWifi(context)){ return false; } else if(activity!=null){ Class<? extends Activity> clazz = activity.getClass(); if(clazz.isAnnotationPresent(FoamDontTrack.class)){ return false; } else { for (Method method : clazz.getMethods()) { if(method.isAnnotationPresent(FoamDontTrack.class)){ return false; } } } } return true; } EventTracker(Context context, List<EventTrackingService> services, boolean wifiOnly); void start();  }
@Test public void testShouldTrackSuccess() { Context mockContext = mock(Context.class); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(true); EventTracker eventTracker = new EventTracker(mockContext, null, false); eventTracker.utils = mockUtils; Activity activity = new Activity(); assertTrue(eventTracker.shouldTrack(activity)); }
@Test public void testShouldTrackNoWifi() { Context mockContext = mock(Context.class); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(false); EventTracker eventTracker = new EventTracker(mockContext, null, true); eventTracker.utils = mockUtils; Activity activity = new Activity(); assertFalse(eventTracker.shouldTrack(activity)); }
@Test public void testShouldTrackDontTrackClassAnnotation() { Context mockContext = mock(Context.class); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(true); EventTracker eventTracker = new EventTracker(mockContext, null, false); eventTracker.utils = mockUtils; Activity activity = new DontTrackActivity(); assertFalse(eventTracker.shouldTrack(activity)); }
@Test public void testShouldTrackDontTrackMethodAnnotation() { Context mockContext = mock(Context.class); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(true); EventTracker eventTracker = new EventTracker(mockContext, null, false); eventTracker.utils = mockUtils; Activity activity = new Activity(){ @FoamDontTrack @SuppressWarnings("unused") public void myAnnotatedMethod(){} }; assertFalse(eventTracker.shouldTrack(activity)); }