Search in sources :

Example 11 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method disableBeforeDownload.

@Test
@PrepareForTest(AsyncTaskUtils.class)
public void disableBeforeDownload() 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);
        }
    });
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(7);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    mockStatic(AsyncTaskUtils.class);
    when(InstallerUtils.isUnknownSourcesEnabled(any(Context.class))).thenReturn(true);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    /* Verify dialog. */
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
    verify(mDialog).show();
    /* Disable. */
    Distribute.setEnabled(false);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Click on download. */
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    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(httpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Verify no download scheduled. */
    verifyStatic(never());
    AsyncTaskUtils.execute(anyString(), any(DownloadTask.class), Mockito.<Void>anyVararg());
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method sameVersionCodeSameHash.

@Test
public void sameVersionCodeSameHash() throws Exception {
    /* 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);
        }
    });
    HashMap<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.getReleaseHash()).thenReturn(TEST_HASH);
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify on failure we complete workflow. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verify(mDialogBuilder, never()).create();
    verify(mDialog, never()).show();
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method dialogActivityStateChanges.

@Test
public void dialogActivityStateChanges() 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);
    final Semaphore beforeSemaphore = new Semaphore(0);
    final Semaphore afterSemaphore = new Semaphore(0);
    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(final InvocationOnMock invocation) throws Throwable {
            new Thread() {

                @Override
                public void run() {
                    beforeSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded("mock");
                    afterSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    HashMap<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(7);
    when(releaseDetails.getReleaseNotes()).thenReturn("mock");
    when(ReleaseDetails.parse(anyString())).thenReturn(releaseDetails);
    /* Trigger call. */
    start();
    Activity activity = mock(Activity.class);
    Distribute.getInstance().onActivityResumed(activity);
    Distribute.getInstance().onActivityPaused(activity);
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Release call in background. */
    beforeSemaphore.release();
    afterSemaphore.acquireUninterruptibly();
    /* Verify dialog not shown. */
    verify(mDialogBuilder, never()).create();
    verify(mDialog, never()).show();
    /* Go foreground. */
    Distribute.getInstance().onActivityResumed(activity);
    /* Verify dialog now shown. */
    verify(mDialogBuilder).create();
    verify(mDialog).show();
    /* Pause/resume should not alter dialog. */
    Distribute.getInstance().onActivityPaused(activity);
    Distribute.getInstance().onActivityResumed(activity);
    /* Only once check, and no hiding. */
    verify(mDialogBuilder).create();
    verify(mDialog).show();
    verify(mDialog, never()).hide();
    /* Cover activity. Dialog must be replaced. */
    Distribute.getInstance().onActivityPaused(activity);
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mDialogBuilder, times(2)).create();
    verify(mDialog, times(2)).show();
    verify(mDialog).hide();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) Activity(android.app.Activity) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method disableBeforePostponeDialog.

@Test
public void disableBeforePostponeDialog() 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);
        }
    });
    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. */
    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();
    /* Disable. */
    Distribute.setEnabled(false);
    verifyStatic();
    PreferencesStorage.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(httpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic(never());
    PreferencesStorage.putLong(eq(PREFERENCE_KEY_POSTPONE_TIME), anyLong());
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with HttpClientNetworkStateHandler

use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler 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(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);
        }
    });
    HashMap<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");
    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. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).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();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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