use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class AbstractDistributeAfterDownloadTest method setUpDownload.
void setUpDownload(boolean mandatoryUpdate) throws Exception {
/* Allow unknown sources. */
when(InstallerUtils.isUnknownSourcesEnabled(any(Context.class))).thenReturn(true);
/* Mock download manager. */
when(mContext.getSystemService(Context.DOWNLOAD_SERVICE)).thenReturn(mDownloadManager);
whenNew(DownloadManager.Request.class).withAnyArguments().thenReturn(mDownloadRequest);
when(mDownloadManager.enqueue(mDownloadRequest)).thenReturn(DOWNLOAD_ID);
/* Mock notification manager. */
when(mContext.getSystemService(NOTIFICATION_SERVICE)).thenReturn(mNotificationManager);
/* Mock updates to storage. */
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getLong(invocation.getArguments()[0].toString(), INVALID_DOWNLOAD_IDENTIFIER)).thenReturn((Long) invocation.getArguments()[1]);
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.putLong(eq(PREFERENCE_KEY_DOWNLOAD_ID), anyLong());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getLong(invocation.getArguments()[0].toString(), INVALID_DOWNLOAD_IDENTIFIER)).thenReturn(INVALID_DOWNLOAD_IDENTIFIER);
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getInt(invocation.getArguments()[0].toString(), DOWNLOAD_STATE_COMPLETED)).thenReturn((Integer) invocation.getArguments()[1]);
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.putInt(eq(PREFERENCE_KEY_DOWNLOAD_STATE), anyInt());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getInt(invocation.getArguments()[0].toString(), DOWNLOAD_STATE_COMPLETED)).thenReturn(DOWNLOAD_STATE_COMPLETED);
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getString(invocation.getArguments()[0].toString())).thenReturn(invocation.getArguments()[1].toString());
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.putString(eq(PREFERENCE_KEY_RELEASE_DETAILS), anyString());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
when(PreferencesStorage.getString(invocation.getArguments()[0].toString())).thenReturn(null);
return null;
}
}).when(PreferencesStorage.class);
PreferencesStorage.remove(PREFERENCE_KEY_RELEASE_DETAILS);
/* Mock everything that triggers a download. */
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.getDownloadUrl()).thenReturn(mDownloadUrl);
when(releaseDetails.isMandatoryUpdate()).thenReturn(mandatoryUpdate);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
mockStatic(AsyncTaskUtils.class);
start();
Distribute.getInstance().onActivityResumed(mActivity);
/* Mock download asyncTask. */
mDownloadBeforeSemaphore = new Semaphore(0);
mDownloadAfterSemaphore = new Semaphore(0);
mDownloadTask = new AtomicReference<>();
when(AsyncTaskUtils.execute(anyString(), argThat(new ArgumentMatcher<DownloadTask>() {
@Override
public boolean matches(Object argument) {
return argument instanceof DownloadTask;
}
}), Mockito.<Void>anyVararg())).then(new Answer<DownloadTask>() {
@Override
public DownloadTask answer(InvocationOnMock invocation) throws Throwable {
final DownloadTask task = spy((DownloadTask) invocation.getArguments()[1]);
mDownloadTask.set(task);
new Thread() {
@Override
public void run() {
mDownloadBeforeSemaphore.acquireUninterruptibly();
task.doInBackground(null);
mDownloadAfterSemaphore.release();
}
}.start();
return task;
}
});
/* Mock remove download async task. */
when(AsyncTaskUtils.execute(anyString(), argThat(new ArgumentMatcher<RemoveDownloadTask>() {
@Override
public boolean matches(Object argument) {
return argument instanceof RemoveDownloadTask;
}
}), Mockito.<Void>anyVararg())).then(new Answer<RemoveDownloadTask>() {
@Override
public RemoveDownloadTask answer(InvocationOnMock invocation) throws Throwable {
final RemoveDownloadTask task = (RemoveDownloadTask) invocation.getArguments()[1];
task.doInBackground();
return task;
}
});
/* Mock download completion async task. */
mCheckDownloadBeforeSemaphore = new Semaphore(0);
mCheckDownloadAfterSemaphore = new Semaphore(0);
mCompletionTask = new AtomicReference<>();
when(AsyncTaskUtils.execute(anyString(), argThat(sCheckCompleteTask), Mockito.<Void>anyVararg())).then(new Answer<CheckDownloadTask>() {
@Override
public CheckDownloadTask answer(InvocationOnMock invocation) throws Throwable {
final CheckDownloadTask task = spy((CheckDownloadTask) invocation.getArguments()[1]);
mCompletionTask.set(task);
new Thread() {
@Override
public void run() {
mCheckDownloadBeforeSemaphore.acquireUninterruptibly();
task.onPostExecute(task.doInBackground());
mCheckDownloadAfterSemaphore.release();
}
}.start();
return task;
}
});
/* Click on 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);
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseSucceed.
@Test
public void disableBeforeCheckReleaseSucceed() throws Exception {
/* Mock we already have token. */
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");
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Disable before it succeeds. */
Distribute.setEnabled(false);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
beforeSemaphore.release();
afterSemaphore.acquireUninterruptibly();
/* Verify complete workflow call skipped. i.e. no more call to delete the state. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
verify(mDialog, never()).show();
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method checkReleaseFailsParsing.
@Test
public void checkReleaseFailsParsing() throws Exception {
/* Mock we already have token. */
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);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
when(ReleaseDetails.parse(anyString())).thenThrow(new JSONException("mock"));
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify on failure we complete workflow. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseFails.
@Test
public void disableBeforeCheckReleaseFails() throws Exception {
/* Mock we already have token. */
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]).onCallFailed(new HttpException(403));
afterSemaphore.release();
}
}.start();
return mock(ServiceCall.class);
}
});
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Disable before it fails. */
Distribute.setEnabled(false);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
beforeSemaphore.release();
afterSemaphore.acquireUninterruptibly();
/* Verify complete workflow call ignored. i.e. no more call to delete the state. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* After that if we resume app nothing happens. */
Distribute.getInstance().onActivityPaused(mock(Activity.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method disableWhileCheckingRelease.
@Test
public void disableWhileCheckingRelease() throws Exception {
/* Mock we already have token. */
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
ServiceCall firstCall = mock(ServiceCall.class);
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(firstCall).thenReturn(mock(ServiceCall.class));
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
/* The call is only triggered when app is resumed. */
start();
verify(httpClient, never()).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify cancel on disabling. */
verify(firstCall, never()).cancel();
Distribute.setEnabled(false);
verify(firstCall).cancel();
/* No more call on that one. */
Distribute.setEnabled(true);
Distribute.setEnabled(false);
verify(firstCall).cancel();
}
Aggregations