Skip to content

Commit

Permalink
Add 1 more test
Browse files Browse the repository at this point in the history
  • Loading branch information
guperrot committed Oct 9, 2018
1 parent af00acf commit 93aa0f5
Showing 1 changed file with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ public Object answer(InvocationOnMock invocation) {
}).when(mChannel).enqueue(any(Log.class), anyString());

/* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
when(AppCenter.isConfigured()).thenReturn(true);
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
Expand Down Expand Up @@ -499,6 +498,53 @@ public Object answer(InvocationOnMock invocation) {
verify(authenticationProvider2).checkTokenExpiry();
}

@Test
public void registerCallbackWhenDisabledWorks() {

/* Simulate disabling and background thread. */
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(backgroundRunnable.capture(), disabledRunnable.capture());
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, "test", true);

/* Disable. */
Analytics.setEnabled(false);
backgroundRunnable.getValue().run();

/* Add authentication provider while disabled. */
AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);

/* Unlock command. */
disabledRunnable.getValue().run();

/* Verify update while disabled. */
assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider).acquireTokenAsync();

/* Enable. */
Analytics.setEnabled(true);
disabledRunnable.getValue().run();

/* Call prepare log. */
ProtocolExtension protocol = new ProtocolExtension();
Extensions ext = new Extensions();
ext.setProtocol(protocol);
CommonSchemaLog log = new CommonSchemaEventLog();
log.setExt(ext);
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");

/* Verify log. */
assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());

/* And that we check expiry. */
verify(authenticationProvider).checkTokenExpiry();
}

@Test
public void pauseResume() {

Expand Down

0 comments on commit 93aa0f5

Please sign in to comment.