Search in sources :

Example 16 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class DistributeDownloadTest method notifyThenRestartAppTwice.

@Test
@PrepareForTest(Uri.class)
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void notifyThenRestartAppTwice() throws Exception {
    /* Simulate async task. */
    waitDownloadTask();
    /* Process fake download completion, should not interfere and will be ignored. */
    {
        Intent completionIntent = mock(Intent.class);
        when(completionIntent.getAction()).thenReturn(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        when(completionIntent.getLongExtra(eq(EXTRA_DOWNLOAD_ID), anyLong())).thenReturn(404L);
        new DownloadManagerReceiver().onReceive(mContext, completionIntent);
        waitCheckDownloadTask();
        verify(mDownloadManager, never()).query(any(DownloadManager.Query.class));
    }
    /* Process download completion with the real download identifier. */
    completeDownload();
    Cursor cursor = mockSuccessCursor();
    Intent installIntent = mockInstallIntent();
    /* In background. */
    Distribute.getInstance().onActivityPaused(mActivity);
    /* Mock notification. */
    when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
    Notification.Builder notificationBuilder = mockNotificationBuilderChain();
    when(notificationBuilder.build()).thenReturn(mock(Notification.class));
    /* Simulate task. */
    waitCheckDownloadTask();
    /* Verify notification. */
    verify(mContext, never()).startActivity(installIntent);
    verifyStatic();
    PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
    verify(notificationBuilder).build();
    verify(notificationBuilder, never()).getNotification();
    verify(mNotificationManager).notify(eq(DistributeUtils.getNotificationId()), any(Notification.class));
    verifyNoMoreInteractions(mNotificationManager);
    verify(cursor).close();
    /* Launch app should pop install U.I. and cancel notification. */
    when(mActivity.getPackageManager()).thenReturn(mPackageManager);
    Intent launcherIntent = mock(Intent.class);
    when(mPackageManager.getLaunchIntentForPackage(anyString())).thenReturn(launcherIntent);
    ComponentName launcher = mock(ComponentName.class);
    when(launcherIntent.resolveActivity(mPackageManager)).thenReturn(launcher);
    when(launcher.getClassName()).thenReturn(mActivity.getClass().getName());
    restartActivity();
    /* Wait again. */
    waitCheckDownloadTask();
    /* Verify U.I shown after restart and workflow completed. */
    verify(mContext).startActivity(installIntent);
    verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
    verifyStatic();
    /* Verify workflow completed. */
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Verify however downloaded file was kept. */
    verifyStatic(never());
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verify(mDownloadManager, never()).remove(DOWNLOAD_ID);
    /* Verify second download (restart app again) cleans first one. */
    when(mDownloadManager.enqueue(mDownloadRequest)).thenReturn(DOWNLOAD_ID + 1);
    restartActivity();
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.appcenter_distribute_update_dialog_download), clickListener.capture());
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    waitDownloadTask();
    /* Verify new download id in storage. */
    verifyStatic();
    PreferencesStorage.putLong(PREFERENCE_KEY_DOWNLOAD_ID, DOWNLOAD_ID + 1);
    /* Verify previous download removed. */
    verify(mDownloadManager).remove(DOWNLOAD_ID);
    /* Notification already canceled so no more call, i.e. only once. */
    verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
}
Also used : DialogInterface(android.content.DialogInterface) Build(android.os.Build) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) Notification(android.app.Notification) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) TargetApi(android.annotation.TargetApi)

Example 17 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class DistributeDownloadTest method notifyThenRestartThenInstallerFails.

@Test
@PrepareForTest(Uri.class)
@SuppressWarnings("deprecation")
public void notifyThenRestartThenInstallerFails() throws Exception {
    /* Simulate async task. */
    waitDownloadTask();
    /* Kill app, this has nothing to do with failure, but we need to test that too. */
    Distribute.unsetInstance();
    /* Process download completion. */
    completeDownload();
    /* Mock old device URI. */
    Cursor cursor = mockSuccessCursor();
    when(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME)).thenReturn(2);
    Intent installIntent = mock(Intent.class);
    whenNew(Intent.class).withArguments(Intent.ACTION_INSTALL_PACKAGE).thenReturn(installIntent);
    when(installIntent.resolveActivity(any(PackageManager.class))).thenReturn(null).thenReturn(mock(ComponentName.class));
    /* Mock notification. */
    when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
    Notification.Builder notificationBuilder = mockNotificationBuilderChain();
    when(notificationBuilder.getNotification()).thenReturn(mock(Notification.class));
    /* Simulate task. */
    waitCheckDownloadTask();
    /* Verify notification. */
    verify(mContext, never()).startActivity(installIntent);
    verifyStatic();
    PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
    verify(mNotificationManager).notify(eq(DistributeUtils.getNotificationId()), any(Notification.class));
    verifyNoMoreInteractions(mNotificationManager);
    verify(cursor).getString(2);
    verify(cursor).close();
    /* Restart app should pop install U.I. and cancel notification and pop a new dialog then a new download. */
    doThrow(new ActivityNotFoundException()).when(mContext).startActivity(installIntent);
    start();
    Distribute.getInstance().onActivityResumed(mActivity);
    /* Wait download manager query. */
    waitCheckDownloadTask();
    /* Verify workflow completed even on failure to show install U.I. */
    verify(mContext).startActivity(installIntent);
    verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic(never());
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
}
Also used : ActivityNotFoundException(android.content.ActivityNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Cursor(android.database.Cursor) Notification(android.app.Notification) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class DistributeWarnUnknownSourcesTest method clickSettingsOnAndroidO.

@Test
@PrepareForTest(Build.class)
@SuppressLint("InlinedApi")
public void clickSettingsOnAndroidO() throws Exception {
    /* Click settings. */
    Intent intentSecuritySettings = mock(Intent.class);
    whenNew(Intent.class).withArguments(Settings.ACTION_SECURITY_SETTINGS).thenReturn(intentSecuritySettings);
    Intent intentManageUnknownAppSources = mock(Intent.class);
    whenNew(Intent.class).withArguments(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).thenReturn(intentManageUnknownAppSources);
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_unknown_sources_dialog_settings), clickListener.capture());
    /* Verify behaviour on old version. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", BuildConfig.MIN_SDK_VERSION);
    clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
    verify(mFirstActivity).startActivity(intentSecuritySettings);
    verify(mFirstActivity, never()).startActivity(intentManageUnknownAppSources);
    reset(mFirstActivity);
    /* Verify behaviour on Android O. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.O);
    clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
    verify(mFirstActivity, never()).startActivity(intentSecuritySettings);
    verify(mFirstActivity).startActivity(intentManageUnknownAppSources);
}
Also used : DialogInterface(android.content.DialogInterface) Build(android.os.Build) Intent(android.content.Intent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) SuppressLint(android.annotation.SuppressLint)

Example 19 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class DistributeWarnUnknownSourcesTest method clickSettingsThenEnableThenBack.

@Test
@PrepareForTest(AsyncTaskUtils.class)
public void clickSettingsThenEnableThenBack() throws Exception {
    /* Click settings. */
    Intent intent = mock(Intent.class);
    whenNew(Intent.class).withArguments(Settings.ACTION_SECURITY_SETTINGS).thenReturn(intent);
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_unknown_sources_dialog_settings), clickListener.capture());
    clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
    when(mUnknownSourcesDialog.isShowing()).thenReturn(false);
    /* Verify navigation. */
    verify(mFirstActivity).startActivity(intent);
    /* Simulate we go to settings, change value then go back. */
    mockStatic(AsyncTaskUtils.class);
    Distribute.getInstance().onActivityPaused(mFirstActivity);
    when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
    Distribute.getInstance().onActivityResumed(mFirstActivity);
    /* No more dialog, start download. */
    verify(mDialog).show();
    verify(mDialog, never()).hide();
    verify(mUnknownSourcesDialog).show();
    verify(mUnknownSourcesDialog, never()).hide();
    verifyStatic();
    AsyncTaskUtils.execute(anyString(), argThat(new ArgumentMatcher<AsyncTask<Object, ?, ?>>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof DownloadTask;
        }
    }), anyVararg());
}
Also used : DialogInterface(android.content.DialogInterface) ArgumentMatcher(org.mockito.ArgumentMatcher) Intent(android.content.Intent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class HashUtilsTest method algorithmNotFound.

@Test(expected = RuntimeException.class)
@PrepareForTest(HashUtils.class)
public void algorithmNotFound() throws NoSuchAlgorithmException {
    mockStatic(MessageDigest.class);
    NoSuchAlgorithmException cause = new NoSuchAlgorithmException();
    doThrow(cause).when(MessageDigest.class);
    MessageDigest.getInstance(anyString());
    try {
        HashUtils.sha256("");
    } catch (RuntimeException e) {
        assertEquals(cause, e.getCause());
        throw e;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)188 Test (org.junit.Test)186 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)30 StringWriter (java.io.StringWriter)28 PrintWriter (java.io.PrintWriter)27 File (java.io.File)21 ArrayList (java.util.ArrayList)15 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 Config (com.twitter.heron.spi.common.Config)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 Point (org.pentaho.di.core.gui.Point)10 Date (java.util.Date)9 HashMap (java.util.HashMap)9 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9