Analytics extends AbstractAppCenterService { public static AppCenterFuture<Void> setEnabled(boolean enabled) { return getInstance().setInstanceEnabledAsync(enabled); } private  Analytics(); @SuppressWarnings({"WeakerAccess", "RedundantSuppression"}) static synchronized Analytics getInstance(); static AppCenterFuture<Boolean> isEnabled(); static AppCenterFuture<Void> setEnabled(boolean enabled); static boolean setTransmissionInterval(int seconds); static void pause(); static void resume(); static void trackEvent(String name); static void trackEvent(String name, Map<String, String> properties); static void trackEvent(String name, Map<String, String> properties, int flags); static void trackEvent(String name, EventProperties properties); static void trackEvent(String name, EventProperties properties, int flags); static AnalyticsTransmissionTarget getTransmissionTarget(String transmissionTargetToken); @Override boolean isAppSecretRequired(); @Override String getServiceName(); @Override Map<String, LogFactory> getLogFactories(); @Override synchronized void onActivityResumed(final Activity activity); @Override synchronized void onActivityPaused(Activity activity); @Override synchronized void onStarted(@NonNull Context context, @NonNull Channel channel, String appSecret, String transmissionTargetToken, boolean startedFromApp); @Override void onConfigurationUpdated(String appSecret, String transmissionTargetToken); static final String LOG_TAG; }
@Test public void setEnabled() throws InterruptedException { Channel channel = mock(Channel.class); final ArgumentCaptor<String> captorGroupName = ArgumentCaptor.forClass(String.class); Analytics analytics = Analytics.getInstance(); Analytics.setEnabled(true); assertFalse(Analytics.isEnabled().get()); Analytics.setEnabled(false); assertFalse(Analytics.isEnabled().get()); analytics.onStarting(mAppCenterHandler); analytics.onStarted(mock(Context.class), channel, "", null, true); verify(channel).removeGroup(eq(analytics.getGroupName())); verify(channel).addGroup(eq(ANALYTICS_CRITICAL_GROUP), anyInt(), anyLong(), anyInt(), isNull(Ingestion.class), any(Channel.GroupListener.class)); verify(channel).addGroup(eq(ANALYTICS_GROUP), anyInt(), anyLong(), anyInt(), isNull(Ingestion.class), any(Channel.GroupListener.class)); verify(channel).addListener(isA(SessionTracker.class)); verify(channel).addListener(isA(AnalyticsValidator.class)); verify(channel).addListener(isA(AnalyticsTransmissionTarget.getChannelListener().getClass())); assertTrue(Analytics.isEnabled().get()); Analytics.setEnabled(false).get(); assertFalse(Analytics.isEnabled().get()); verify(channel).removeListener(isA(SessionTracker.class)); verify(channel).removeListener(isA(AnalyticsValidator.class)); verify(channel).removeListener(isA(AnalyticsTransmissionTarget.getChannelListener().getClass())); verify(channel).removeGroup(eq(ANALYTICS_CRITICAL_GROUP)); verify(channel, times(2)).removeGroup(eq(ANALYTICS_GROUP)); verify(channel).clear(analytics.getGroupName()); verifyStatic(); SharedPreferencesManager.remove("sessions"); Analytics.trackEvent("test"); AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("t1"); target.trackEvent("test"); Analytics.trackPage("test"); analytics.onActivityResumed(new Activity()); analytics.onActivityPaused(new Activity()); verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()), anyInt()); final CountDownLatch latch = new CountDownLatch(1); Analytics.setEnabled(true).thenAccept(new AppCenterConsumer<Void>() { @Override public void accept(Void aVoid) { latch.countDown(); } }); assertTrue(latch.await(0, TimeUnit.MILLISECONDS)); assertTrue(Analytics.isEnabled().get()); Analytics.setEnabled(true); assertTrue(Analytics.isEnabled().get()); Analytics.trackEvent("test"); target.trackEvent("test"); target.getTransmissionTarget("t2").trackEvent("test"); Analytics.trackPage("test"); verify(channel, times(4)).enqueue(any(Log.class), eq(analytics.getGroupName()), eq(DEFAULTS)); Analytics.setEnabled(false); assertFalse(Analytics.isEnabled().get()); Analytics.trackEvent("test"); Analytics.trackPage("test"); analytics.onActivityResumed(new Activity()); analytics.onActivityPaused(new Activity()); verify(channel, times(4)).enqueue(any(Log.class), eq(analytics.getGroupName()), eq(DEFAULTS)); }