Search in sources :

Example 36 with HttpClientNetworkStateHandler

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));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) Uri(android.net.Uri) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ActivityNotFoundException(android.content.ActivityNotFoundException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with HttpClientNetworkStateHandler

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);
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) PackageInfo(android.content.pm.PackageInfo) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with HttpClientNetworkStateHandler

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;
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler)

Example 39 with HttpClientNetworkStateHandler

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();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) Channel(com.microsoft.appcenter.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler)

Example 40 with HttpClientNetworkStateHandler

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;
    }
}
Also used : NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) IOException(java.io.IOException) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)42 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)41 Matchers.anyString (org.mockito.Matchers.anyString)40 Test (org.junit.Test)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)33 HashMap (java.util.HashMap)27 ServiceCall (com.microsoft.appcenter.http.ServiceCall)26 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 Activity (android.app.Activity)23 UUID (java.util.UUID)10 ArgumentMatcher (org.mockito.ArgumentMatcher)9 DialogInterface (android.content.DialogInterface)8 Context (android.content.Context)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Semaphore (java.util.concurrent.Semaphore)4 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)3 NetworkStateHelper (com.microsoft.appcenter.utils.NetworkStateHelper)3 JSONException (org.json.JSONException)3