Search in sources :

Example 6 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method happyPathUsingTesterAppUpdateSetup.

@Test
public void happyPathUsingTesterAppUpdateSetup() throws Exception {
    /* Setup mock. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    UUID requestId = UUID.randomUUID();
    when(UUIDUtils.randomUUID()).thenReturn(requestId);
    when(PreferencesStorage.getString(PREFERENCE_KEY_TESTER_APP_UPDATE_SETUP_FAILED_MESSAGE_KEY)).thenReturn(null);
    when(PreferencesStorage.getString(PREFERENCE_KEY_REQUEST_ID)).thenReturn(requestId.toString());
    String url = "ms-actesterapp://update-setup";
    url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
    url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
    url += "&" + PARAMETER_REDIRECT_SCHEME + "=" + "appcenter";
    url += "&" + PARAMETER_REQUEST_ID + "=" + requestId;
    url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
    whenNew(Intent.class).withArguments(Intent.ACTION_VIEW, Uri.parse(url)).thenReturn(mock(Intent.class));
    when(mPackageManager.getPackageInfo(DistributeUtils.TESTER_APP_PACKAGE_NAME, 0)).thenReturn(mock(PackageInfo.class));
    /* Start and resume: open tester app. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    verify(mActivity).startActivity(intent);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
    /* Store token. */
    Distribute.getInstance().storeRedirectionParameters(requestId.toString(), "g", "some token");
    /* Verify behavior. */
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
        }
    }), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* If call already made, activity changed must not recall it. */
    Distribute.getInstance().onActivityPaused(mActivity);
    Distribute.getInstance().onActivityResumed(mActivity);
    /* Verify behavior. */
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
        }
    }), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Call is still in progress. If we restart app, nothing happens we still wait. */
    restartResumeLauncher(mActivity);
    /* Verify behavior not changed. */
    verify(mActivity).startActivity(intent);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token");
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "g");
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_REQUEST_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verify(httpClient).callAsync(argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
        }
    }), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* If process is restarted, a new call will be made. Need to mock storage for that. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("g");
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mActivity);
    verify(httpClient, times(2)).callAsync(argThat(new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().startsWith(DistributeConstants.DEFAULT_API_URL);
        }
    }), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) PackageInfo(android.content.pm.PackageInfo) ArgumentMatcher(org.mockito.ArgumentMatcher) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method useMobileCenterFailOverForDecryptAndReleaseDetailsPrivate.

@Test
public void useMobileCenterFailOverForDecryptAndReleaseDetailsPrivate() throws Exception {
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN, null)).thenReturn("some token MC");
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, null)).thenReturn("some group MC");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token MC");
    /* Primary storage will be missing data. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify the new strings were put into PreferencesStorage */
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_UPDATE_TOKEN, "some token MC");
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, "some group MC");
    verify(mDistributeInfoTracker).updateDistributionGroupId("some group MC");
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method willNotReportReleaseInstallForPrivateGroupWhenReleaseHashesDontMatch.

@Test
public void willNotReportReleaseInstallForPrivateGroupWhenReleaseHashesDontMatch() throws Exception {
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN, null)).thenReturn("some token MC");
    when(mMobileCenterPreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID, null)).thenReturn("fake-distribution-id");
    when(PreferencesStorage.getString(PREFERENCE_KEY_DOWNLOADED_RELEASE_HASH)).thenReturn("fake-release-hash");
    /* Mock httpClient. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token MC");
    /* Primary storage will be missing data. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    ArgumentMatcher<String> urlArg = new ArgumentMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            return argument.toString().matches("^https://.*?/sdk/apps/a/releases/latest\\?release_hash=" + TEST_HASH + "$");
        }
    };
    verify(httpClient).callAsync(argThat(urlArg), eq("GET"), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with HttpClientNetworkStateHandler

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

the class DistributeBeforeApiSuccessTest method testDistributeInactive.

private void testDistributeInactive() throws Exception {
    /* Check browser not opened. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verifyStatic(never());
    BrowserUtils.openBrowser(anyString(), any(Activity.class));
    /*
         * Even if we had a token on a previous install that was not from store
         * (if package name and signature matches an APK on Google Play you can upgrade to
         * Google Play version without losing data.
         */
    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);
    restartProcessAndSdk();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient, never()).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler)

Example 10 with HttpClientNetworkStateHandler

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

the class DistributeBeforeDownloadTest method olderVersionCode.

@Test
public void olderVersionCode() throws Exception {
    /* Mock we already have public group, no token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID)).thenReturn("some group");
    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<>();
    ReleaseDetails releaseDetails = mock(ReleaseDetails.class);
    when(releaseDetails.getId()).thenReturn(4);
    when(releaseDetails.getVersion()).thenReturn(5);
    when(releaseDetails.getMinApiLevel()).thenReturn(Build.VERSION_CODES.M);
    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(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));
    /* 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) 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) 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