Search in sources :

Example 56 with ServiceCall

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

the class DistributeBeforeDownloadTest method moreRecentWithIncompatibleMinApiLevel.

@Test
public void moreRecentWithIncompatibleMinApiLevel() throws Exception {
    mockSessionContext();
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP);
    when(mHttpClient.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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
            return mock(ServiceCall.class);
        }
    });
    Map<String, String> headers = new HashMap<>();
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
    String distributionGroupId = UUID.randomUUID().toString();
    when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify on incompatible version we complete workflow. */
    verifyStatic(never());
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH);
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verify(mDialogBuilder, never()).create();
    verify(mDialog, never()).show();
    /* Verify we still track the distribution group statistics. */
    verifyStatic();
    SharedPreferencesManager.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, distributionGroupId);
    verify(mDistributeInfoTracker).updateDistributionGroupId(distributionGroupId);
    verify(mChannel).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Build(android.os.Build) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 57 with ServiceCall

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

the class DistributeBeforeDownloadTest method disableThenEnableBeforeUpdatingSecondTime.

@Test
public void disableThenEnableBeforeUpdatingSecondTime() throws Exception {
    /* Mock first update completed but we disable in this test so mock we don't have release details. */
    mockStatic(DistributeUtils.class);
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
    /* Mock next release. */
    final ReleaseDetails nextReleaseDetails = mock(ReleaseDetails.class);
    when(nextReleaseDetails.getId()).thenReturn(2);
    when(nextReleaseDetails.getVersion()).thenReturn(7);
    when(ReleaseDetails.parse(anyString())).thenReturn(nextReleaseDetails);
    ReleaseDownloader nextReleaseDownloader = mock(ReleaseDownloader.class);
    when(ReleaseDownloaderFactory.create(any(Context.class), same(nextReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(nextReleaseDownloader);
    when(nextReleaseDownloader.getReleaseDetails()).thenReturn(nextReleaseDetails);
    /* Simulate cache update. */
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(nextReleaseDetails);
            return null;
        }
    }).when(SharedPreferencesManager.class);
    SharedPreferencesManager.putString(eq(PREFERENCE_KEY_RELEASE_DETAILS), anyString());
    /* Mock we receive a second update. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    when(mHttpClient.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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* Start SDK. */
    Distribute.setUpdateTrack(UpdateTrack.PRIVATE);
    start();
    /* Disable SDK. */
    ReleaseDownloader cleanupReleaseDownloader = mock(ReleaseDownloader.class);
    when(ReleaseDownloaderFactory.create(any(Context.class), isNull(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(cleanupReleaseDownloader);
    Distribute.setEnabled(false).get();
    Distribute.setEnabled(true).get();
    /* Verify previous download canceled. */
    verify(cleanupReleaseDownloader).cancel();
    /* Resume workflow. */
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify prompt is shown. */
    verify(mDialog).show();
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ReleaseDownloader(com.microsoft.appcenter.distribute.download.ReleaseDownloader) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 58 with ServiceCall

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

the class DistributeBeforeDownloadTest method moreRecentVersionCode.

@Test
public void moreRecentVersionCode() throws Exception {
    mockSessionContext();
    when(mHttpClient.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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock"));
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(releaseDetails.getShortVersion()).thenReturn("7.0");
    String distributionGroupId = UUID.randomUUID().toString();
    when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    when(InstallerUtils.isUnknownSourcesEnabled(any(Context.class))).thenReturn(true);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify dialog. */
    verify(mDialogBuilder).setTitle(R.string.appcenter_distribute_update_dialog_title);
    verify(mDialogBuilder).setMessage("unit-test-app7.07");
    verify(mDialogBuilder).create();
    verify(mDialog).show();
    /* Verify we track the distribution group statistics. */
    verifyStatic();
    SharedPreferencesManager.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, distributionGroupId);
    verify(mDistributeInfoTracker).updateDistributionGroupId(distributionGroupId);
    verify(mChannel).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
    /* After that if we resume app we refresh dialog. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* No more http call. */
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* But dialog refreshed. */
    InOrder order = inOrder(mDialog);
    order.verify(mDialog).hide();
    order.verify(mDialog).show();
    order.verifyNoMoreInteractions();
    verify(mDialog, times(2)).show();
    verify(mDialogBuilder, times(2)).create();
    /* Disable does not hide the dialog. */
    Distribute.setEnabled(false);
    /* We already called hide once, make sure its not called a second time. */
    verify(mDialog).hide();
    /* Also no toast if we don't click on actionable button. */
    verify(mToast, never()).show();
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InOrder(org.mockito.InOrder) HashMap(java.util.HashMap) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 59 with ServiceCall

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

the class DistributeBeforeDownloadTest method cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart.

@Test
public void cancelGetReleaseCallIfDownloadingCachedDialogAfterRestart() throws Exception {
    /* Mock some storage calls. */
    mockSomeStorage();
    /* Mock we already have redirection parameters. */
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    final AtomicReference<ServiceCallback> serviceCallbackRef = new AtomicReference<>();
    final ServiceCall serviceCall = mock(ServiceCall.class);
    when(mHttpClient.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) {
            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(false);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog. */
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    /* Restart offline. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog restored and call scheduled. */
    verify(mHttpClient, times(2)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
    /* We are offline and call is scheduled, clicking download must cancel pending call. */
    when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    verify(serviceCall).cancel();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) HttpResponse(com.microsoft.appcenter.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 60 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(SharedPreferencesManager.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    when(mHttpClient.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) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "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) HttpResponse(com.microsoft.appcenter.http.HttpResponse) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ServiceCall (com.microsoft.appcenter.http.ServiceCall)69 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)69 Matchers.anyString (org.mockito.Matchers.anyString)68 InvocationOnMock (org.mockito.invocation.InvocationOnMock)61 Test (org.junit.Test)53 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)53 Activity (android.app.Activity)43 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)39 HashMap (java.util.HashMap)30 HttpResponse (com.microsoft.appcenter.http.HttpResponse)19 DialogInterface (android.content.DialogInterface)16 Context (android.content.Context)15 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 Log (com.microsoft.appcenter.ingestion.models.Log)11 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)11 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)11 UUID (java.util.UUID)11 ArrayList (java.util.ArrayList)9 Answer (org.mockito.stubbing.Answer)9