Search in sources :

Example 21 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall 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 22 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall 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 23 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall 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 24 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall 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 25 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.

the class IngestionHttpTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate(String appSecret) throws Exception {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    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;
        }
    });
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), mock(LogSerializer.class));
    ingestionHttp.setLogUrl("http://mock");
    assertEquals(call, ingestionHttp.sendAsync(appSecret, UUIDUtils.randomUUID(), mock(LogContainer.class), mock(ServiceCallback.class)));
    return callTemplate.get();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)26 ServiceCall (com.microsoft.appcenter.http.ServiceCall)26 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)26 Matchers.anyString (org.mockito.Matchers.anyString)26 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 Test (org.junit.Test)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Activity (android.app.Activity)19 HashMap (java.util.HashMap)13 DialogInterface (android.content.DialogInterface)8 Context (android.content.Context)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UUID (java.util.UUID)4 Semaphore (java.util.concurrent.Semaphore)4 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)3 PackageInfo (android.content.pm.PackageInfo)2 Build (android.os.Build)2 Log (com.microsoft.appcenter.ingestion.models.Log)2 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2