diff --git a/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/ConnectivityManagerTest.java b/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/ConnectivityManagerTest.java index 7ca3ff72..0ca39dca 100644 --- a/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/ConnectivityManagerTest.java +++ b/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/ConnectivityManagerTest.java @@ -2,8 +2,12 @@ import static com.launchdarkly.sdk.android.TestUtil.requireNoMoreValues; import static com.launchdarkly.sdk.android.TestUtil.requireValue; +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -28,6 +32,7 @@ import org.easymock.Mock; import org.easymock.MockType; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -491,16 +496,19 @@ public void refreshDataSourceForNewContext() throws Exception { eventProcessor.setInBackground(false); // we expect this call replayAll(); + long connectionTimeBeforeSwitch = connectivityManager.getConnectionInformation().getLastSuccessfulConnection(); LDContext context2 = LDContext.create("context2"); contextDataManager.switchToContext(context2); AwaitableCallback done = new AwaitableCallback<>(); connectivityManager.switchToContext(context2, done); done.await(); + long connectionTimeAfterSwitch = connectivityManager.getConnectionInformation().getLastSuccessfulConnection(); verifyAll(); // verifies eventProcessor calls verifyDataSourceWasStopped(); verifyForegroundDataSourceWasCreatedAndStarted(context2); verifyNoMoreDataSourcesWereCreated(); + assertNotEquals(connectionTimeBeforeSwitch, connectionTimeAfterSwitch); } @Test @@ -557,6 +565,43 @@ public void refreshDataSourceWhileInBackgroundWithBackgroundPollingDisabled() { verifyNoMoreDataSourcesWereCreated(); } + @Test + public void notifyListenersWhenStatusChanges() throws Exception { + createTestManager(false, false, makeSuccessfulDataSourceFactory()); + + awaitStartUp(); + + LDStatusListener mockListener = mock(LDStatusListener.class); + // expected initial connection + mockListener.onConnectionModeChanged(anyObject(ConnectionInformation.class)); + // expected second connection after identify + mockListener.onConnectionModeChanged(anyObject(ConnectionInformation.class)); + expectLastCall(); + replayAll(); + + AwaitableCallback identifyListenersCalled = new AwaitableCallback<>(); + connectivityManager.registerStatusListener(mockListener); + connectivityManager.registerStatusListener(new LDStatusListener() { + @Override + public void onConnectionModeChanged(ConnectionInformation connectionInformation) { + // since the callback system is on another thread, need to use awaitable callback + identifyListenersCalled.onSuccess(null); + } + + @Override + public void onInternalFailure(LDFailure ldFailure) { + Assert.fail(); // unexpected + } + }); + + LDContext context2 = LDContext.create("context2"); + contextDataManager.switchToContext(context2); + connectivityManager.switchToContext(context2, new AwaitableCallback<>()); + identifyListenersCalled.await(); + + verifyAll(); + } + private ComponentConfigurer makeSuccessfulDataSourceFactory() { return clientContext -> makeSuccessfulDataSource(clientContext); }