use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-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.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeBeforeDownloadTest method sameVersionCodeSameHash.
@Test
public void sameVersionCodeSameHash() 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);
}
});
Map<String, String> headers = new HashMap<>();
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(6);
when(releaseDetails.getReleaseHash()).thenReturn(TEST_HASH);
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 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));
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-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));
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class AppCenterIngestionTest method sendAsync.
@Test
public void sendAsync() throws Exception {
/* Build some payload. */
LogContainer container = new LogContainer();
Log log = mock(Log.class);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
/* Configure mock HTTP. */
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
/* Test calling code. */
AppCenterIngestion ingestion = new AppCenterIngestion(mHttpClient, serializer);
ingestion.setLogUrl("http://mock");
String appSecret = UUID.randomUUID().toString();
UUID installId = UUID.randomUUID();
ServiceCallback serviceCallback = mock(ServiceCallback.class);
assertEquals(call, ingestion.sendAsync(appSecret, installId, container, serviceCallback));
/* Verify call to http client. */
HashMap<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put(Constants.APP_SECRET, appSecret);
expectedHeaders.put(AppCenterIngestion.INSTALL_ID, installId.toString());
verify(mHttpClient).callAsync(eq("http://mock" + AppCenterIngestion.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.get());
assertEquals("mockPayload", callTemplate.get().buildRequestBody());
/* Verify close. */
ingestion.close();
verify(mHttpClient).close();
/* Verify reopen. */
ingestion.reopen();
verify(mHttpClient).reopen();
}
use of com.microsoft.appcenter.http.ServiceCall in project AppCenter-SDK-Android by Microsoft.
the class DistributeHttpTest method getCallTemplate.
private HttpClient.CallTemplate getCallTemplate(String appSecret, String apiToken) {
/* Configure mock HTTP to get an instance of IngestionCallTemplate. */
Distribute.getInstance().onStarting(mAppCenterHandler);
Distribute.getInstance().onStarted(mContext, mock(Channel.class), appSecret, null, true);
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
Distribute.getInstance().getLatestReleaseDetails("mockGroup", apiToken);
return callTemplate.get();
}
Aggregations