Search in sources :

Example 61 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class BrowserUtilsTest method onlySystemBrowserAndIsDefault.

@Test
public void onlySystemBrowserAndIsDefault() throws Exception {
    /* Mock no browser. */
    Activity activity = mock(Activity.class);
    doThrow(new ActivityNotFoundException()).when(activity).startActivity(argThat(BrowserUtilsTest.CHROME_MATCHER));
    PackageManager packageManager = mock(PackageManager.class);
    when(activity.getPackageManager()).thenReturn(packageManager);
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "browser";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(resolveInfo);
    }
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "browser";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(resolveInfo));
    }
    /* Open Chrome then abort. */
    BrowserUtils.openBrowser(TEST_URL, activity);
    InOrder order = inOrder(activity);
    order.verify(activity).startActivity(argThat(CHROME_MATCHER));
    order.verify(activity).startActivity(argThat(new ArgumentMatcher<Intent>() {

        @Override
        public boolean matches(Object o) {
            Intent intent = (Intent) o;
            return Intent.ACTION_VIEW.equals(intent.getAction()) && Uri.parse(TEST_URL).equals(intent.getData()) && intent.getComponent().getClassName().equals("browser");
        }
    }));
    order.verifyNoMoreInteractions();
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ActivityInfo(android.content.pm.ActivityInfo) InOrder(org.mockito.InOrder) PackageManager(android.content.pm.PackageManager) ActivityNotFoundException(android.content.ActivityNotFoundException) ArgumentMatcher(org.mockito.ArgumentMatcher) Activity(android.app.Activity) Intent(android.content.Intent) Test(org.junit.Test)

Example 62 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method happyPathUntilHangingCallWithToken.

@Test
public void happyPathUntilHangingCallWithToken() 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_REQUEST_ID)).thenReturn(requestId.toString());
    when(mPackageManager.getPackageInfo(DistributeUtils.TESTER_APP_PACKAGE_NAME, 0)).thenThrow(new PackageManager.NameNotFoundException());
    /* Mock install id from AppCenter. */
    UUID installId = UUID.randomUUID();
    when(mAppCenterFuture.get()).thenReturn(installId);
    when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
    /* Start and resume: open browser. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    verifyStatic();
    String url = DistributeConstants.DEFAULT_INSTALL_URL;
    url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
    url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
    url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
    url += "&" + PARAMETER_REDIRECT_SCHEME + "=" + "appcenter";
    url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
    url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
    url += "&" + PARAMETER_ENABLE_UPDATE_SETUP_FAILURE_REDIRECT_KEY + "=" + "true";
    url += "&" + PARAMETER_INSTALL_ID + "=" + installId.toString();
    BrowserUtils.openBrowser(url, mActivity);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
    /* If browser already opened, activity changed must not recall it. */
    Distribute.getInstance().onActivityPaused(mActivity);
    Distribute.getInstance().onActivityResumed(mActivity);
    verifyStatic();
    BrowserUtils.openBrowser(url, mActivity);
    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);
    verify(mDistributeInfoTracker).updateDistributionGroupId("g");
    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(mDistributeInfoTracker).updateDistributionGroupId("g");
    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. */
    verifyStatic();
    BrowserUtils.openBrowser(url, mActivity);
    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(mDistributeInfoTracker).updateDistributionGroupId("g");
    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) PackageManager(android.content.pm.PackageManager) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) 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 63 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method happyPathUntilHangingCallWithoutToken.

@Test
public void happyPathUntilHangingCallWithoutToken() 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_REQUEST_ID)).thenReturn(requestId.toString());
    when(mPackageManager.getPackageInfo(DistributeUtils.TESTER_APP_PACKAGE_NAME, 0)).thenThrow(new PackageManager.NameNotFoundException());
    /* Mock install id from AppCenter. */
    UUID installId = UUID.randomUUID();
    when(mAppCenterFuture.get()).thenReturn(installId);
    when(AppCenter.getInstallId()).thenReturn(mAppCenterFuture);
    /* Start and resume: open browser. */
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    verifyStatic();
    String url = DistributeConstants.DEFAULT_INSTALL_URL;
    url += String.format(UPDATE_SETUP_PATH_FORMAT, "a");
    url += "?" + PARAMETER_RELEASE_HASH + "=" + TEST_HASH;
    url += "&" + PARAMETER_REDIRECT_ID + "=" + mContext.getPackageName();
    url += "&" + PARAMETER_REDIRECT_SCHEME + "=" + "appcenter";
    url += "&" + PARAMETER_REQUEST_ID + "=" + requestId.toString();
    url += "&" + PARAMETER_PLATFORM + "=" + PARAMETER_PLATFORM_VALUE;
    url += "&" + PARAMETER_ENABLE_UPDATE_SETUP_FAILURE_REDIRECT_KEY + "=" + "true";
    url += "&" + PARAMETER_INSTALL_ID + "=" + installId.toString();
    BrowserUtils.openBrowser(url, mActivity);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
    /* If browser already opened, activity changed must not recall it. */
    Distribute.getInstance().onActivityPaused(mActivity);
    Distribute.getInstance().onActivityResumed(mActivity);
    verifyStatic();
    BrowserUtils.openBrowser(url, mActivity);
    verifyStatic();
    PreferencesStorage.putString(PREFERENCE_KEY_REQUEST_ID, requestId.toString());
    /* Store token. */
    Distribute.getInstance().storeRedirectionParameters(requestId.toString(), "g", null);
    /* Verify behavior. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_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(mDistributeInfoTracker).updateDistributionGroupId("g");
    HashMap<String, String> headers = new HashMap<>();
    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.remove(PREFERENCE_KEY_UPDATE_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(mDistributeInfoTracker).updateDistributionGroupId("g");
    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. */
    verifyStatic();
    BrowserUtils.openBrowser(url, mActivity);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_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(mDistributeInfoTracker).updateDistributionGroupId("g");
    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");
    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) PackageManager(android.content.pm.PackageManager) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) 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 64 with ArgumentMatcher

use of org.mockito.ArgumentMatcher 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 65 with ArgumentMatcher

use of org.mockito.ArgumentMatcher 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)

Aggregations

ArgumentMatcher (org.mockito.ArgumentMatcher)142 Test (org.junit.Test)116 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 Context (android.content.Context)26 Matchers.anyString (org.mockito.Matchers.anyString)26 HashMap (java.util.HashMap)25 Appender (ch.qos.logback.core.Appender)23 Logger (org.slf4j.Logger)23 ArrayList (java.util.ArrayList)19 UUID (java.util.UUID)19 Intent (android.content.Intent)18 File (java.io.File)15 ResolveInfo (android.content.pm.ResolveInfo)14 PackageManager (android.content.pm.PackageManager)13 Channel (com.microsoft.azure.mobile.channel.Channel)13 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 IOException (java.io.IOException)12 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)11 Activity (android.app.Activity)10