use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method testDistributeInactive.
private void testDistributeInactive() throws Exception {
/* Check browser not opened. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verifyStatic(never());
BrowserUtils.openBrowser(anyString(), any(Activity.class));
/*
* Even if we had a token on a previous install that was not from store
* (if package name and signature matches an APK on Google Play you can upgrade to
* Google Play version without losing data.
*/
when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
restartProcessAndSdk();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verify(httpClient, never()).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler 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. */
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));
/* 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.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method happyPathUntilHangingCall.
@Test
public void happyPathUntilHangingCall() throws Exception {
/* Setup mock. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
UUID requestId = UUID.randomUUID();
when(UUIDUtils.randomUUID()).thenReturn(requestId);
when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn(requestId.toString());
/* Start and resume: open browser. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mActivity);
verifyStatic();
String url = DistributeConstants.DEFAULT_INSTALL_URL;
url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
/* If browser already opened, activity changed must not recall it. */
Distribute.getInstance().onActivityPaused(mActivity);
Distribute.getInstance().onActivityResumed(mActivity);
verifyStatic();
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
/* Store token. */
Distribute.getInstance().storeUpdateToken("some token", requestId.toString());
/* Verify behavior. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* If call already made, activity changed must not recall it. */
Distribute.getInstance().onActivityPaused(mActivity);
Distribute.getInstance().onActivityResumed(mActivity);
/* Verify behavior. */
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
/* Call is still in progress. If we restart app, nothing happens we still wait. */
restartResumeLauncher(mActivity);
Distribute.getInstance().onActivityPaused(mActivity);
Distribute.getInstance().onActivityStopped(mActivity);
Distribute.getInstance().onActivityDestroyed(mActivity);
Distribute.getInstance().onActivityCreated(mActivity, mock(Bundle.class));
Distribute.getInstance().onActivityResumed(mActivity);
/* Verify behavior not changed. */
verifyStatic();
BrowserUtils.openBrowser(url, mActivity);
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {
@Override
public boolean matches(Object argument) {
return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
}
}), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method storeTokenBeforeStart.
@Test
public void storeTokenBeforeStart() throws Exception {
/* Setup mock. */
when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn("r");
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
/* Store token before start, start in background, no storage access. */
Distribute.getInstance().storeUpdateToken("some token", "r");
Distribute.getInstance().onStarted(mContext, "", mock(Channel.class));
verifyStatic(never());
PreferencesStorage.putString(anyString(), anyString());
verifyStatic(never());
PreferencesStorage.remove(anyString());
/* Unlock the processing by going into foreground. */
Distribute.getInstance().onActivityResumed(mock(Activity.class));
verifyStatic();
PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
HashMap<String, String> headers = new HashMap<>();
headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
use of com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method disableBeforePostponeDialog.
@Test
public void disableBeforePostponeDialog() 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);
}
});
ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
when(releaseDetails.getId()).thenReturn(4);
when(releaseDetails.getVersion()).thenReturn(7);
when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
/* Trigger call. */
Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify dialog. */
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setNegativeButton(eq(R.string.mobile_center_distribute_update_dialog_postpone), clickListener.capture());
verify(mDialog).show();
/* Disable. */
Distribute.setEnabled(false);
verifyStatic();
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
/* Postpone it. */
clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
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);
verifyStatic(never());
PreferencesStorage.putLong(eq(PREFERENCE_KEY_POSTPONE_TIME), anyLong());
}
Aggregations