use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method moreRecentWithIncompatibleMinApiLevel.
@Test
public void moreRecentWithIncompatibleMinApiLevel() throws Exception {
mockSessionContext();
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP);
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);
}
});
Map<String, String> headers = new HashMap<>();
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
String distributionGroupId = UUID.randomUUID().toString();
when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* 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 incompatible version we complete workflow. */
verifyStatic(never());
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH);
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));
}
use of com.microsoft.appcenter.http.HttpResponse 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();
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method moreRecentVersionCode.
@Test
public void moreRecentVersionCode() 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(7);
when(releaseDetails.getShortVersion()).thenReturn("7.0");
String distributionGroupId = UUID.randomUUID().toString();
when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
when(InstallerUtils.isUnknownSourcesEnabled(any(Context.class))).thenReturn(true);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify dialog. */
verify(mDialogBuilder).setTitle(R.string.appcenter_distribute_update_dialog_title);
verify(mDialogBuilder).setMessage("unit-test-app7.07");
verify(mDialogBuilder).create();
verify(mDialog).show();
/* Verify we 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 we refresh dialog. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* No more http call. */
verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* But dialog refreshed. */
InOrder order = inOrder(mDialog);
order.verify(mDialog).hide();
order.verify(mDialog).show();
order.verifyNoMoreInteractions();
verify(mDialog, times(2)).show();
verify(mDialogBuilder, times(2)).create();
/* Disable does not hide the dialog. */
Distribute.setEnabled(false);
/* We already called hide once, make sure its not called a second time. */
verify(mDialog).hide();
/* Also no toast if we don't click on actionable button. */
verify(mToast, never()).show();
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-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.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method releaseNotes.
@Test
public void releaseNotes() throws Exception {
/* 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");
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);
}
});
/* No release notes. */
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog. */
verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
verify(mDialog).show();
reset(mDialog);
/* Release notes but somehow no URL. */
when(releaseDetails.getReleaseNotes()).thenReturn("Fix a bug");
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
verify(mDialog).show();
reset(mDialog);
/* Release notes URL this time. */
final Uri uri = mock(Uri.class);
Intent intent = mock(Intent.class);
whenNew(Intent.class).withArguments(Intent.ACTION_VIEW, uri).thenReturn(intent);
when(releaseDetails.getReleaseNotesUrl()).thenReturn(uri);
/* Empty release notes and URL. */
when(releaseDetails.getReleaseNotes()).thenReturn("");
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialogBuilder, never()).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), any(DialogInterface.OnClickListener.class));
verify(mDialog).show();
reset(mDialog);
/* Release notes and URL. */
when(releaseDetails.getReleaseNotes()).thenReturn("Fix a bug");
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mActivity);
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), clickListener.capture());
verify(mDialog).show();
reset(mDialog);
/* Click and check navigation. */
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEUTRAL);
verify(mActivity).startActivity(intent);
/* We thus leave app. */
Distribute.getInstance().onActivityPaused(mActivity);
when(mDialog.isShowing()).thenReturn(false);
/* Going back should restore dialog. */
Distribute.getInstance().onActivityResumed(mActivity);
clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder, times(2)).setNeutralButton(eq(R.string.appcenter_distribute_update_dialog_view_release_notes), clickListener.capture());
verify(mDialog).show();
/* Do the same test and simulate failed navigation. */
mockStatic(AppCenterLog.class);
ActivityNotFoundException exception = new ActivityNotFoundException();
doThrow(exception).when(mActivity).startActivity(intent);
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEUTRAL);
verify(mActivity, times(2)).startActivity(intent);
verifyStatic();
AppCenterLog.error(anyString(), anyString(), eq(exception));
}
Aggregations