Search in sources :

Example 1 with ReleaseDownloader

use of com.microsoft.appcenter.distribute.download.ReleaseDownloader in project mobile-center-sdk-android by Microsoft.

the class DistributeCustomizationTest method handleUserUpdateActionNotProceededWithoutListener.

@Test
public void handleUserUpdateActionNotProceededWithoutListener() throws Exception {
    /* Mock. */
    mockForCustomizationTest(false);
    mockStatic(DistributeUtils.class);
    Distribute.unsetInstance();
    Distribute distribute = spy(Distribute.getInstance());
    doNothing().when(distribute).completeWorkflow();
    /* Counters to verify multiple times for specific methods. */
    int appCenterLogErrorCounter = 0;
    int getStoredDownloadStateCounter = 0;
    /* Start Distribute service. */
    start(distribute);
    distribute.onActivityResumed(mActivity);
    /* Verify the method is called by onActivityCreated. */
    verifyStatic(times(++getStoredDownloadStateCounter));
    DistributeUtils.getStoredDownloadState();
    /* Disable the service. */
    distribute.setInstanceEnabled(false);
    /* Call handleUpdateAction. */
    distribute.handleUpdateAction(UpdateAction.POSTPONE);
    verifyStatic(times(++appCenterLogErrorCounter));
    AppCenterLog.error(anyString(), anyString());
    /* mReleaseListener is null. */
    Mockito.when(ReleaseDownloaderFactory.create(any(Context.class), any(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(null);
    distribute.handleUpdateAction(UpdateAction.POSTPONE);
    /* Verify the user action has NOT been processed. */
    verifyStatic(times(++appCenterLogErrorCounter));
    AppCenterLog.error(anyString(), anyString());
    /* Enable the service. */
    ReleaseDownloader cleanupReleaseDownloader = mock(ReleaseDownloader.class);
    Mockito.when(ReleaseDownloaderFactory.create(any(Context.class), isNull(ReleaseDetails.class), any(ReleaseDownloadListener.class))).thenReturn(cleanupReleaseDownloader);
    distribute.setInstanceEnabled(true);
    /* Verify the method is called by resumeDistributeWorkflow. */
    verifyStatic(times(++getStoredDownloadStateCounter));
    DistributeUtils.getStoredDownloadState();
    /* Call handleUpdateAction. */
    distribute.handleUpdateAction(UpdateAction.POSTPONE);
    /* Verify the user action has NOT been processed. */
    verifyStatic(times(++getStoredDownloadStateCounter));
    DistributeUtils.getStoredDownloadState();
    verifyStatic(times(++appCenterLogErrorCounter));
    AppCenterLog.error(anyString(), anyString());
    /* Mock the download state to DOWNLOAD_STATE_AVAILABLE. */
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_AVAILABLE);
    /* Call handleUpdateAction. */
    distribute.handleUpdateAction(UpdateAction.POSTPONE);
    /* Verify the user action has NOT been processed. */
    verifyStatic(times(++getStoredDownloadStateCounter));
    DistributeUtils.getStoredDownloadState();
    verifyStatic(times(++appCenterLogErrorCounter));
    AppCenterLog.error(anyString(), anyString());
    /* Verify again to make sure the user action has NOT been processed yet. */
    verify(distribute, never()).completeWorkflow();
}
Also used : Context(android.content.Context) ReleaseDownloader(com.microsoft.appcenter.distribute.download.ReleaseDownloader) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ReleaseDownloader

use of com.microsoft.appcenter.distribute.download.ReleaseDownloader in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeDownloadTest method updateASecondTimeClearsPreviousReleaseCache.

@Test
public void updateASecondTimeClearsPreviousReleaseCache() throws Exception {
    /* Mock first update completed. */
    mockStatic(DistributeUtils.class);
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
    when(mReleaseDetails.getVersion()).thenReturn(6);
    when(mReleaseDetails.getId()).thenReturn(1);
    when(DistributeUtils.loadCachedReleaseDetails()).thenReturn(mReleaseDetails);
    ReleaseDownloader cachedReleaseDownloader = mock(ReleaseDownloader.class);
    when(ReleaseDownloaderFactory.create(any(Context.class), same(mReleaseDetails), any(ReleaseDownloadListener.class))).thenReturn(cachedReleaseDownloader);
    when(cachedReleaseDownloader.getReleaseDetails()).thenReturn(mReleaseDetails);
    /* 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(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);
        }
    });
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(mHttpClient).callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify prompt is shown. */
    verify(mDialog).show();
    /* Verify previous download canceled. */
    verify(cachedReleaseDownloader).cancel();
}
Also used : SessionContext(com.microsoft.appcenter.utils.context.SessionContext) Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) 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 3 with ReleaseDownloader

use of com.microsoft.appcenter.distribute.download.ReleaseDownloader 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)

Aggregations

Context (android.content.Context)3 ReleaseDownloader (com.microsoft.appcenter.distribute.download.ReleaseDownloader)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Activity (android.app.Activity)2 HttpResponse (com.microsoft.appcenter.http.HttpResponse)2 ServiceCall (com.microsoft.appcenter.http.ServiceCall)2 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)2 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)2 HashMap (java.util.HashMap)1