use of com.microsoft.appcenter.distribute.download.ReleaseDownloader in project mobile-center-sdk-android by Microsoft.
the class DistributeCustomizationTest method handleUserUpdateActionNotProceededWithoutListener.
@Test
public void handleUserUpdateActionNotProceededWithoutListener() throws Exception {
/* Mock. */
mockForCustomizationTest(false);
mockStatic(DistributeUtils.class);
Distribute.unsetInstance();
Distribute distribute = spy(Distribute.getInstance());
doNothing().when(distribute).completeWorkflow();
/* Counters to verify multiple times for specific methods. */
int appCenterLogErrorCounter = 0;
int getStoredDownloadStateCounter = 0;
/* Start Distribute service. */
start(distribute);
distribute.onActivityResumed(mActivity);
/* Verify the method is called by onActivityCreated. */
verifyStatic(times(++getStoredDownloadStateCounter));
DistributeUtils.getStoredDownloadState();
/* Disable the service. */
distribute.setInstanceEnabled(false);
/* Call handleUpdateAction. */
distribute.handleUpdateAction(UpdateAction.POSTPONE);
verifyStatic(times(++appCenterLogErrorCounter));
AppCenterLog.error(anyString(), anyString());
/* mReleaseListener is null. */
Mockito.when(ReleaseDownloaderFactory.create(any(Context.class), any(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(null);
distribute.handleUpdateAction(UpdateAction.POSTPONE);
/* Verify the user action has NOT been processed. */
verifyStatic(times(++appCenterLogErrorCounter));
AppCenterLog.error(anyString(), anyString());
/* Enable the service. */
ReleaseDownloader cleanupReleaseDownloader = mock(ReleaseDownloader.class);
Mockito.when(ReleaseDownloaderFactory.create(any(Context.class), isNull(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(cleanupReleaseDownloader);
distribute.setInstanceEnabled(true);
/* Verify the method is called by resumeDistributeWorkflow. */
verifyStatic(times(++getStoredDownloadStateCounter));
DistributeUtils.getStoredDownloadState();
/* Call handleUpdateAction. */
distribute.handleUpdateAction(UpdateAction.POSTPONE);
/* Verify the user action has NOT been processed. */
verifyStatic(times(++getStoredDownloadStateCounter));
DistributeUtils.getStoredDownloadState();
verifyStatic(times(++appCenterLogErrorCounter));
AppCenterLog.error(anyString(), anyString());
/* Mock the download state to DOWNLOAD_STATE_AVAILABLE. */
when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_AVAILABLE);
/* Call handleUpdateAction. */
distribute.handleUpdateAction(UpdateAction.POSTPONE);
/* Verify the user action has NOT been processed. */
verifyStatic(times(++getStoredDownloadStateCounter));
DistributeUtils.getStoredDownloadState();
verifyStatic(times(++appCenterLogErrorCounter));
AppCenterLog.error(anyString(), anyString());
/* Verify again to make sure the user action has NOT been processed yet. */
verify(distribute, never()).completeWorkflow();
}
use of com.microsoft.appcenter.distribute.download.ReleaseDownloader in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method updateASecondTimeClearsPreviousReleaseCache.
@Test
public void updateASecondTimeClearsPreviousReleaseCache() throws Exception {
/* Mock first update completed. */
mockStatic(DistributeUtils.class);
when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
when(mReleaseDetails.getVersion()).thenReturn(6);
when(mReleaseDetails.getId()).thenReturn(1);
when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(mReleaseDetails);
ReleaseDownloader cachedReleaseDownloader = mock(ReleaseDownloader.class);
when(ReleaseDownloaderFactory.create(any(Context.class), same(mReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(cachedReleaseDownloader);
when(cachedReleaseDownloader.getReleaseDetails()).thenReturn(mReleaseDetails);
/* Mock next release. */
final ReleaseDetails nextReleaseDetails = mock(ReleaseDetails.class);
when(nextReleaseDetails.getId()).thenReturn(2);
when(nextReleaseDetails.getVersion()).thenReturn(7);
when(ReleaseDetails.parse(anyString())).thenReturn(nextReleaseDetails);
ReleaseDownloader nextReleaseDownloader = mock(ReleaseDownloader.class);
when(ReleaseDownloaderFactory.create(any(Context.class), same(nextReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(nextReleaseDownloader);
when(nextReleaseDownloader.getReleaseDetails()).thenReturn(nextReleaseDetails);
/* Simulate cache update. */
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(nextReleaseDetails);
return null;
}
}).when(SharedPreferencesManager.class);
SharedPreferencesManager.putString(eq(PREFERENCE_KEY_RELEASE_DETAILS), anyString());
/* Mock we receive a second update. */
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
return mock(ServiceCall.class);
}
});
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mHttpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify prompt is shown. */
verify(mDialog).show();
/* Verify previous download canceled. */
verify(cachedReleaseDownloader).cancel();
}
use of com.microsoft.appcenter.distribute.download.ReleaseDownloader in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method disableThenEnableBeforeUpdatingSecondTime.
@Test
public void disableThenEnableBeforeUpdatingSecondTime() throws Exception {
/* Mock first update completed but we disable in this test so mock we don't have release details. */
mockStatic(DistributeUtils.class);
when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
/* Mock next release. */
final ReleaseDetails nextReleaseDetails = mock(ReleaseDetails.class);
when(nextReleaseDetails.getId()).thenReturn(2);
when(nextReleaseDetails.getVersion()).thenReturn(7);
when(ReleaseDetails.parse(anyString())).thenReturn(nextReleaseDetails);
ReleaseDownloader nextReleaseDownloader = mock(ReleaseDownloader.class);
when(ReleaseDownloaderFactory.create(any(Context.class), same(nextReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(nextReleaseDownloader);
when(nextReleaseDownloader.getReleaseDetails()).thenReturn(nextReleaseDetails);
/* Simulate cache update. */
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) {
when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(nextReleaseDetails);
return null;
}
}).when(SharedPreferencesManager.class);
SharedPreferencesManager.putString(eq(PREFERENCE_KEY_RELEASE_DETAILS), anyString());
/* Mock we receive a second update. */
when(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
return mock(ServiceCall.class);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* Start SDK. */
Distribute.setUpdateTrack(UpdateTrack.PRIVATE);
start();
/* Disable SDK. */
ReleaseDownloader cleanupReleaseDownloader = mock(ReleaseDownloader.class);
when(ReleaseDownloaderFactory.create(any(Context.class), isNull(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(cleanupReleaseDownloader);
Distribute.setEnabled(false).get();
Distribute.setEnabled(true).get();
/* Verify previous download canceled. */
verify(cleanupReleaseDownloader).cancel();
/* Resume workflow. */
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify prompt is shown. */
verify(mDialog).show();
}
Aggregations