Search in sources :

Example 86 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 87 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 88 with ServiceCall

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

the class DistributeBeforeDownloadTest method postponeDialog.

@Test
public void postponeDialog() 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");
    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);
        }
    });
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    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 dialog. */
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setNegativeButton(eq(R.string.appcenter_distribute_update_dialog_postpone), clickListener.capture());
    verify(mDialog).show();
    /* Postpone it. */
    long now = 20122112L;
    mockStatic(System.class);
    when(System.currentTimeMillis()).thenReturn(now);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            when(SharedPreferencesManager.getLong(invocation.getArguments()[0].toString(), 0)).thenReturn((Long) invocation.getArguments()[1]);
            return null;
        }
    }).when(SharedPreferencesManager.class);
    SharedPreferencesManager.putLong(eq(PREFERENCE_KEY_POSTPONE_TIME), anyLong());
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
    when(mDialog.isShowing()).thenReturn(false);
    /* Verify. */
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_RELEASE_DETAILS);
    verifyStatic();
    SharedPreferencesManager.putLong(eq(PREFERENCE_KEY_POSTPONE_TIME), eq(now));
    /* Verify we didn't track distribution group stats since we already had redirection parameters. */
    verifyStatic(never());
    SharedPreferencesManager.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, distributionGroupId);
    verify(mDistributeInfoTracker, never()).updateDistributionGroupId(distributionGroupId);
    verify(mChannel, never()).enqueue(any(DistributionStartSessionLog.class), eq(Distribute.getInstance().getGroupName()), eq(DEFAULTS));
    /* 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(mHttpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Restart should check release and should not show dialog again until 1 day has elapsed. */
    now += DistributeConstants.POSTPONE_TIME_THRESHOLD - 1;
    when(System.currentTimeMillis()).thenReturn(now);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialog).show();
    /* Now its time to show again. */
    now += 1;
    when(System.currentTimeMillis()).thenReturn(now);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialog, times(2)).show();
    /* Postpone again. */
    clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setNegativeButton(eq(R.string.appcenter_distribute_update_dialog_postpone), clickListener.capture());
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
    /* Check postpone again. */
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialog, times(2)).show();
    /* If mandatory release, we ignore postpone and still show dialog. */
    when(releaseDetails.isMandatoryUpdate()).thenReturn(true);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialog, times(3)).show();
    /* Set back in time to make SDK clean state and force update. */
    verifyStatic(never());
    SharedPreferencesManager.remove(PREFERENCE_KEY_POSTPONE_TIME);
    when(releaseDetails.isMandatoryUpdate()).thenReturn(false);
    now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialog, times(4)).show();
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_POSTPONE_TIME);
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) 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 89 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 90 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)

Aggregations

ServiceCall (com.microsoft.appcenter.http.ServiceCall)116 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)116 Matchers.anyString (org.mockito.Matchers.anyString)113 InvocationOnMock (org.mockito.invocation.InvocationOnMock)95 Test (org.junit.Test)91 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)91 Activity (android.app.Activity)59 HttpResponse (com.microsoft.appcenter.http.HttpResponse)57 HashMap (java.util.HashMap)45 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)33 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)26 Log (com.microsoft.appcenter.ingestion.models.Log)25 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)25 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 DialogInterface (android.content.DialogInterface)21 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)21 HttpClient (com.microsoft.appcenter.http.HttpClient)21 Answer (org.mockito.stubbing.Answer)20 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)20