use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method disableBeforeDownload.
@Test
@PrepareForTest(AsyncTaskUtils.class)
public void disableBeforeDownload() throws Exception {
/* Mock we already have redirection parameters. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.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) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
mockStatic(AsyncTaskUtils.class);
when(InstallerUtils.isUnknownSourcesEnabled(any(Context.class))).thenReturn(true);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog. */
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
verify(mDialog).show();
/* Disable. */
Distribute.setEnabled(false);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* Click on download. */
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
when(mDialog.isShowing()).thenReturn(false);
/* Since we were disabled, no action but toast to explain what happened. */
verify(mToast).show();
/* Verify no more calls, e.g. happened only once. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialog).show();
verify(httpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* Verify no download scheduled. */
verifyStatic(never());
AsyncTaskUtils.execute(anyString(), any(DownloadTask.class), Mockito.<Void>anyVararg());
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method dialogActivityStateChanges.
@Test
public void dialogActivityStateChanges() throws Exception {
/* Mock we already have redirection parameters. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
final Semaphore beforeSemaphore = new Semaphore(0);
final Semaphore afterSemaphore = new Semaphore(0);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(final InvocationOnMock invocation) throws Throwable {
new Thread() {
@Override
public void run() {
beforeSemaphore.acquireUninterruptibly();
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
afterSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.getReleaseNotes()).thenReturn("mock");
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Activity activity = mock(Activity.class);
Distribute.getInstance().onActivityResumed(activity);
Distribute.getInstance().onActivityPaused(activity);
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Release call in background. */
beforeSemaphore.release();
afterSemaphore.acquireUninterruptibly();
/* Verify dialog not shown. */
verify(mDialogBuilder, never()).create();
verify(mDialog, never()).show();
/* Go foreground. */
Distribute.getInstance().onActivityResumed(activity);
/* Verify dialog now shown. */
verify(mDialogBuilder).create();
verify(mDialog).show();
/* Pause/resume should not alter dialog. */
Distribute.getInstance().onActivityPaused(activity);
Distribute.getInstance().onActivityResumed(activity);
/* Only once check, and no hiding. */
verify(mDialogBuilder).create();
verify(mDialog).show();
verify(mDialog, never()).hide();
/* Cover activity. Dialog must be replaced. */
Distribute.getInstance().onActivityPaused(activity);
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialogBuilder, times(2)).create();
verify(mDialog, times(2)).show();
verify(mDialog).hide();
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method shouldRemoveReleaseHashStorageIfReportedSuccessfully.
@Test
public void shouldRemoveReleaseHashStorageIfReportedSuccessfully() throws Exception {
/* Mock release hash storage. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH)).thenReturn("fake-hash");
mockStatic(DistributeUtils.class);
when(DistributeUtils.computeReleaseHash(any(PackageInfo.class))).thenReturn("fake-hash");
/* Mock install id from AppCenter. */
UUID installId = UUID.randomUUID();
when(mAppCenterFuture.get()).thenReturn(installId);
when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
/* Mock we already have token and no group. */
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.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) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(6);
when(releaseDetails.getReleaseHash()).thenReturn(TEST_HASH);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_ID);
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeWarnUnknownSourcesTest method setUpDialog.
@Before
public void setUpDialog() throws Exception {
/* Mock we already have redirection parameters. */
when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
when(httpClient.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) throws Throwable {
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
return mock(ServiceCall.class);
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.isMandatoryUpdate()).thenReturn(mMandatoryUpdate);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mFirstActivity);
/* Mock second dialog. */
when(mDialogBuilder.create()).thenReturn(mUnknownSourcesDialog);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(mUnknownSourcesDialog.isShowing()).thenReturn(true);
return null;
}
}).when(mUnknownSourcesDialog).show();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(mUnknownSourcesDialog.isShowing()).thenReturn(false);
return null;
}
}).when(mUnknownSourcesDialog).hide();
/* Click on first dialog. */
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
when(mDialog.isShowing()).thenReturn(false);
/* Second should show. */
verify(mUnknownSourcesDialog).show();
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class IngestionHttpTest 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. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
when(httpClient.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) throws Throwable {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
/* Test calling code. */
IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
ingestionHttp.setLogUrl("http://mock");
String appSecret = UUIDUtils.randomUUID().toString();
UUID installId = UUIDUtils.randomUUID();
ServiceCallback serviceCallback = mock(ServiceCallback.class);
assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
/* Verify call to http client. */
HashMap<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put(IngestionHttp.APP_SECRET, appSecret);
expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
verify(httpClient).callAsync(eq("http://mock" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.get());
assertEquals("mockPayload", callTemplate.get().buildRequestBody());
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
/* Verify reopen. */
ingestionHttp.reopen();
verify(httpClient).reopen();
}
Aggregations