DefaultChannel implements Channel { @Override public void setEnabled(boolean enabled) { if (mEnabled == enabled) { return; } if (enabled) { mEnabled = true; mDiscardLogs = false; mCurrentState++; for (Ingestion ingestion : mIngestions) { ingestion.reopen(); } for (GroupState groupState : mGroupStates.values()) { checkPendingLogs(groupState); } } else { suspend(true, new CancellationException()); } for (Listener listener : mListeners) { listener.onGloballyEnabled(enabled); } } DefaultChannel(@NonNull Context context, String appSecret, @NonNull LogSerializer logSerializer, @NonNull HttpClient httpClient, @NonNull Handler appCenterHandler); @VisibleForTesting  DefaultChannel(@NonNull Context context, String appSecret, @NonNull Persistence persistence, @NonNull Ingestion ingestion, @NonNull Handler appCenterHandler); @WorkerThread @Override boolean setMaxStorageSize(long maxStorageSizeInBytes); @WorkerThread @Override void setAppSecret(@NonNull String appSecret); @Override void addGroup(final String groupName, int maxLogsPerBatch, long batchTimeInterval, int maxParallelBatches, Ingestion ingestion, GroupListener groupListener); @Override void removeGroup(String groupName); @Override void pauseGroup(String groupName, String targetToken); @Override void resumeGroup(String groupName, String targetToken); @Override boolean isEnabled(); @Override void setEnabled(boolean enabled); @Override void setLogUrl(String logUrl); @Override void clear(String groupName); @Override void invalidateDeviceCache(); @Override void enqueue(@NonNull Log log, @NonNull final String groupName, int flags); @Override void addListener(Listener listener); @Override void removeListener(Listener listener); @Override void shutdown();  }
@Test public void setEnabled() throws IOException { Ingestion ingestion = mock(Ingestion.class); doThrow(new IOException()).when(ingestion).close(); Persistence persistence = mock(Persistence.class); when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1)); DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler); Channel.Listener listener = spy(new AbstractChannelListener()); channel.addListener(listener); channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null); channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS); verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL)); channel.setEnabled(false); verify(mAppCenterHandler).removeCallbacks(any(Runnable.class)); verify(ingestion).close(); verify(persistence).deleteLogs(TEST_GROUP); verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class)); verify(listener).onGloballyEnabled(false); ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class); when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true); channel.setEnabled(true); channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS); delayedRunnable.getValue().run(); verify(ingestion).reopen(); verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class)); verify(listener).onGloballyEnabled(true); }