use of com.microsoft.azure.mobile.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method moreRecentWithIncompatibleMinApiLevel.
@Test
public void moreRecentWithIncompatibleMinApiLevel() throws Exception {
/* Mock we already have token. */
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR2);
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");
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.KITKAT);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Verify on incompatible version we complete workflow. */
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(mDialogBuilder, never()).create();
verify(mDialog, never()).show();
/* 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.azure.mobile.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method mandatoryUpdateDialogAndCacheTests.
@Test
public void mandatoryUpdateDialogAndCacheTests() throws Exception {
/* Mock some storage calls. */
mockSomeStorage();
/* 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 AtomicReference<ServiceCallback> serviceCallbackRef = new AtomicReference<>();
final ServiceCall serviceCall = mock(ServiceCall.class);
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 {
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(true);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
assertNotNull(serviceCallbackRef.get());
serviceCallbackRef.get().onCallSucceeded("mock");
serviceCallbackRef.set(null);
/* Verify release notes persisted. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_RELEASE_DETAILS, "mock");
verifyStatic();
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
/* Verify dialog. */
verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
verify(mDialogBuilder).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
/* Verify dialog restored offline even if process restarts. */
when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
verify(httpClient, times(2)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
assertNotNull(serviceCallbackRef.get());
/* Simulate network back and get same release again, should do nothing particular. */
when(mNetworkStateHelper.isNetworkConnected()).thenReturn(true);
serviceCallbackRef.get().onCallSucceeded("mock");
/* Check we didn't change state, e.g. happened only once. */
verifyStatic();
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
/* Restart and this time we will detect a more recent optional release. */
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify call is made and that we restored again mandatory update dialog in the mean time. */
verify(httpClient, times(3)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
verify(mDialogBuilder, times(3)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
/* Then detect new release in background. */
releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(5);
when(releaseDetails.getVersion()).thenReturn(8);
when(releaseDetails.isMandatoryUpdate()).thenReturn(false);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
serviceCallbackRef.get().onCallSucceeded("mock");
/* Check state updated again when we detect it. */
verifyStatic(times(2));
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
/* Restart SDK, even offline, should show optional dialog. */
when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(mDialogBuilder, times(4)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
verify(mDialogBuilder).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
/* And still check again for further update. */
verify(httpClient, times(4)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Unblock call with network up. */
when(mNetworkStateHelper.isNetworkConnected()).thenReturn(true);
serviceCallbackRef.get().onCallSucceeded("mock");
/* If we restart SDK online, its an optional update so dialog will not be restored until new call made. */
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog behavior happened only once. */
verify(mDialogBuilder).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
/* Dialog shown only after new call made in that scenario. */
serviceCallbackRef.get().onCallSucceeded("mock");
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder, times(5)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), clickListener.capture());
verify(mDialogBuilder, times(2)).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
/* If we finally click on download, no call cancel since already successful. */
when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
verify(serviceCall, never()).cancel();
}
use of com.microsoft.azure.mobile.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method sameVersionCodeSameHash.
@Test
public void sameVersionCodeSameHash() 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");
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. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
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);
verify(mDialogBuilder, never()).create();
verify(mDialog, never()).show();
/* 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.azure.mobile.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 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");
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. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
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.azure.mobile.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);
long logAbsoluteTime = 123L;
when(log.getToffset()).thenReturn(logAbsoluteTime);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
/* Stable time. */
mockStatic(System.class);
long now = 456L;
when(System.currentTimeMillis()).thenReturn(now);
/* 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(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 toffset manipulation. */
verify(log).setToffset(now - logAbsoluteTime);
verify(log).setToffset(logAbsoluteTime);
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
}
Aggregations