use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method releaseNotes.
@Test
public void releaseNotes() 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);
}
});
/* 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.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeDownloadTest method shouldNotRemoveReleaseHashStorageIfHashesDontMatch.
@Test
public void shouldNotRemoveReleaseHashStorageIfHashesDontMatch() 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-old-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(never());
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH);
verifyStatic(never());
PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_ID);
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeCustomizationTest method mockForCustomizationTest.
private ReleaseDetails mockForCustomizationTest(boolean mandatory) throws Exception {
/* Mock http call. */
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);
}
});
/* Mock data model. */
mockStatic(ReleaseDetails.class);
ReleaseDetails details = mock(ReleaseDetails.class);
when(details.getId()).thenReturn(1);
when(details.getVersion()).thenReturn(10);
when(details.getShortVersion()).thenReturn("2.3.4");
when(details.isMandatoryUpdate()).thenReturn(mandatory);
when(ReleaseDetails.parse(anyString())).thenReturn(details);
/* Mock update token. */
when(StorageHelper.PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
return details;
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class DistributeHttpTest method getCallTemplate.
private HttpClient.CallTemplate getCallTemplate(String appSecret, String apiToken) throws Exception {
/* Configure mock HTTP to get an instance of IngestionCallTemplate. */
Distribute.getInstance().onStarting(mAppCenterHandler);
Distribute.getInstance().onStarted(mContext, appSecret, mock(Channel.class));
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
mockStatic(NetworkStateHelper.class);
when(NetworkStateHelper.getSharedInstance(any(Context.class))).thenReturn(mock(NetworkStateHelper.class));
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))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
Distribute.getInstance().getLatestReleaseDetails("mockGroup", apiToken);
return callTemplate.get();
}
use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project mobile-center-sdk-android by Microsoft.
the class RealUserMeasurements method applyEnabledState.
/**
* React to enable state change.
*
* @param enabled current state.
*/
@Override
protected synchronized void applyEnabledState(boolean enabled) {
if (enabled) {
/* Snapshot run key for the run to avoid race conditions, we ignore updates. */
final String rumKey = mRumKey;
/* Check rum key. */
if (rumKey == null) {
AppCenterLog.error(LOG_TAG, "Rum key must be configured before start.");
return;
}
/* Configure HTTP client with no retries but handling network state. */
NetworkStateHelper networkStateHelper = NetworkStateHelper.getSharedInstance(mContext);
mHttpClient = new HttpClientNetworkStateHandler(new DefaultHttpClient(), networkStateHelper);
/* Get configuration. */
getConfiguration(0, rumKey, mHttpClient);
} else /* On disabling, cancel everything. */
if (mHttpClient != null) {
try {
mHttpClient.close();
} catch (IOException e) {
AppCenterLog.error(LOG_TAG, "Failed to close http client.", e);
}
mHttpClient = null;
mConfiguration = null;
mTestUrls = null;
}
}
Aggregations