Search in sources :

Example 6 with HttpResponse

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

the class DistributeBeforeDownloadTest method olderVersionCode.

@Test
public void olderVersionCode() 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(5);
    when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
    String distributionGroupId = UUID.randomUUID().toString();
    when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N_MR1);
    /* 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 failure we complete workflow. */
    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));
    /* Verify release hash was not even considered. */
    // noinspection ResultOfMethodCallIgnored
    verify(releaseDetails, never()).getReleaseHash();
}
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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with HttpResponse

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

the class DistributeBeforeDownloadTest method sameVersionCodeSameHash.

@Test
public void sameVersionCodeSameHash() 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);
        }
    });
    Map<String, String> headers = new HashMap<>();
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(6);
    when(releaseDetails.getReleaseHash()).thenReturn(TEST_HASH);
    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 failure we complete workflow. */
    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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with HttpResponse

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

the class DistributeBeforeDownloadTest method disableBeforePostponeDialog.

@Test
public void disableBeforePostponeDialog() throws Exception {
    /* Setup mock. */
    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();
    /* 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));
    /* Disable. */
    Distribute.setEnabled(false);
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Postpone it. */
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
    when(mDialog.isShowing()).thenReturn(false);
    /* Since we were disabled, no action but toast to explain what happened. */
    verify(mToast).show();
    /* 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));
    verifyStatic();
    SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(PREFERENCE_KEY_POSTPONE_TIME), anyLong());
}
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) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with HttpResponse

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

the class DistributeBeforeDownloadTest method sameVersionDifferentHashWithHardcodedAppName.

@Test
public void sameVersionDifferentHashWithHardcodedAppName() 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);
        }
    });
    Map<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(6);
    when(releaseDetails.getShortVersion()).thenReturn("1.2.3");
    when(releaseDetails.getReleaseHash()).thenReturn("9f52199c986d9210842824df695900e1656180946212bd5e8978501a5b732e60");
    String distributionGroupId = UUID.randomUUID().toString();
    when(releaseDetails.getDistributionGroupId()).thenReturn(distributionGroupId);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Mock app name to be not localizable. */
    mockStatic(AppNameHelper.class);
    when(AppNameHelper.getAppName(mContext)).thenReturn("hardcoded-app-name");
    /* Trigger call. */
    Distribute.setUpdateTrack(UpdateTrack.PRIVATE);
    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("hardcoded-app-name1.2.36");
    verify(mDialogBuilder).create();
    verify(mDialog).show();
    /* Verify we didn't track the distribution group statistics since it was already done at redirection. */
    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));
}
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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with HttpResponse

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

the class DistributeBeforeDownloadTest method mandatoryUpdateDialogAndCacheTests.

@Test
public void mandatoryUpdateDialogAndCacheTests() 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(true);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    assertNotNull(serviceCallbackRef.get());
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    serviceCallbackRef.set(null);
    /* Verify release notes persisted. */
    verifyStatic();
    SharedPreferencesManager.putString(PREFERENCE_KEY_RELEASE_DETAILS, "mock");
    verifyStatic();
    SharedPreferencesManager.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
    /* Verify dialog. */
    verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    /* Verify dialog restored offline even if process restarts. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    verify(mHttpClient, times(2)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    assertNotNull(serviceCallbackRef.get());
    /* Simulate network back and get same release again, should do nothing particular. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(true);
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    /* Check we didn't change state, e.g. happened only once. */
    verifyStatic();
    SharedPreferencesManager.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
    /* Restart and this time we will detect a more recent optional release. */
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify call is made and that we restored again mandatory update dialog in the mean time. */
    verify(mHttpClient, times(3)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    verify(mDialogBuilder, times(3)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    verify(mDialogBuilder, never()).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    /* Then detect new release in background. */
    releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(5);
    when(releaseDetails.getVersion()).thenReturn(8);
    when(releaseDetails.isMandatoryUpdate()).thenReturn(false);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    /* Check state updated again when we detect it. */
    verifyStatic(times(2));
    SharedPreferencesManager.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_AVAILABLE);
    /* Restart SDK, even offline, should show optional dialog. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(false);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialogBuilder, times(4)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), any(DialogInterface.OnClickListener.class));
    verify(mDialogBuilder).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    /* And still check again for further update. */
    verify(mHttpClient, times(4)).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Unblock call with network up. */
    when(mNetworkStateHelper.isNetworkConnected()).thenReturn(true);
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    /* If we restart SDK online, its an optional update so dialog will not be restored until new call made. */
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog behavior happened only once. */
    verify(mDialogBuilder).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    /* Dialog shown only after new call made in that scenario. */
    serviceCallbackRef.get().onCallSucceeded(new HttpResponse(200, "mock"));
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(5)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
    verify(mDialogBuilder, times(2)).setNegativeButton(anyInt(), any(DialogInterface.OnClickListener.class));
    /* If we finally click on download, no call cancel since already successful. */
    when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    verify(serviceCall, never()).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

HttpResponse (com.microsoft.appcenter.http.HttpResponse)36 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)33 Matchers.anyString (org.mockito.Matchers.anyString)31 Test (org.junit.Test)30 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 ServiceCall (com.microsoft.appcenter.http.ServiceCall)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Activity (android.app.Activity)18 Context (android.content.Context)12 HttpException (com.microsoft.appcenter.http.HttpException)12 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)10 Log (com.microsoft.appcenter.ingestion.models.Log)10 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)10 Persistence (com.microsoft.appcenter.persistence.Persistence)10 HashMap (java.util.HashMap)9 UUID (java.util.UUID)9 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)7 Answer (org.mockito.stubbing.Answer)7 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)6 DialogInterface (android.content.DialogInterface)5