use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method releaseSuccessActivityIsNull.
@Test
public void releaseSuccessActivityIsNull() {
when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
/* Update is more recent. */
when(mReleaseDetails.getVersion()).thenReturn(7);
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) {
/* Do the call so that id had changed. */
Distribute.getInstance().onActivityPaused(mActivity);
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock", Collections.<String, String>emptyMap()));
return mock(ServiceCall.class);
}
}).thenAnswer(new Answer<ServiceCall>() {
/* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
@Override
public ServiceCall answer(InvocationOnMock invocation) {
return mock(ServiceCall.class);
}
});
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify on failure we don't complete workflow if ids don't match. */
verifyStatic(never());
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeApiSuccessTest method releaseFailureWithDifferentIds.
@Test
public void releaseFailureWithDifferentIds() {
/* Mock we already have token. */
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) {
/* Do the call so that ids do not match. */
Distribute.getInstance().getLatestReleaseDetails("mockGroup", "token");
((ServiceCallback) invocation.getArguments()[4]).onCallFailed(new HttpException(new HttpResponse(503)));
return mock(ServiceCall.class);
}
}).thenAnswer(new Answer<ServiceCall>() {
/* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
@Override
public ServiceCall answer(InvocationOnMock invocation) {
return mock(ServiceCall.class);
}
});
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify on failure we don't complete workflow if ids don't match. */
verifyStatic(never());
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeDownloadTest method cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart.
@Test
public void cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart() throws Exception {
/* Mock some storage calls. */
mockSomeStorage();
/* Mock we already have redirection parameters. */
when(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
final AtomicReference<ServiceCallback> serviceCallbackRef = new AtomicReference<>();
final ServiceCall serviceCall = mock(ServiceCall.class);
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) {
Object serviceCallback = invocation.getArguments()[4];
if (serviceCallback instanceof ServiceCallback) {
serviceCallbackRef.set((ServiceCallback) serviceCallback);
}
return serviceCall;
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.isMandatoryUpdate()).thenReturn(false);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog. */
serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
/* Restart offline. */
when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog restored and call scheduled. */
verify(mHttpClient, times(2)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
/* We are offline and call is scheduled, clicking download must cancel pending call. */
when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
verify(serviceCall).cancel();
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-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();
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeDownloadTest method olderVersionCode.
@Test
public void olderVersionCode() throws Exception {
mockSessionContext();
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<>();
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(5);
when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
String distributionGroupId = UUID.randomUUID().toString();
when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N_MR1);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify on failure we complete workflow. */
verifyStatic();
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDialogBuilder, never()).create();
verify(mDialog, never()).show();
/* Verify we still track the distribution group statistics. */
verifyStatic();
SharedPreferencesManager.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, distributionGroupId);
verify(mDistributeInfoTracker).updateDistributionGroupId(distributionGroupId);
verify(mChannel).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify release hash was not even considered. */
// noinspection ResultOfMethodCallIgnored
verify(releaseDetails, never()).getReleaseHash();
}
Aggregations