use of org.powermock.core.classloader.annotations.PrepareForTest in project checkstyle by checkstyle.
the class MainFrameModelTest method testOpenFileWithUnknownParseMode.
@Test
@PrepareForTest(ParseMode.class)
public void testOpenFileWithUnknownParseMode() throws CheckstyleException {
final ParseMode unknownParseMode = PowerMockito.mock(ParseMode.class);
Whitebox.setInternalState(unknownParseMode, "ordinal", 3);
PowerMockito.when(unknownParseMode.toString()).thenReturn("Unknown parse mode");
PowerMockito.mockStatic(ParseMode.class);
PowerMockito.when(ParseMode.values()).thenReturn(new ParseMode[] { ParseMode.PLAIN_JAVA, ParseMode.JAVA_WITH_COMMENTS, ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS, unknownParseMode });
try {
model.setParseMode(unknownParseMode);
model.openFile(testData);
fail("Expected IllegalArgumentException is not thrown.");
} catch (IllegalArgumentException ex) {
assertEquals("Unknown mode: Unknown parse mode", ex.getMessage());
}
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project checkstyle by checkstyle.
the class CommonUtilsTest method testLoadSuppressionsUriSyntaxException.
@Test
@PrepareForTest({ CommonUtils.class, CommonUtilsTest.class })
@SuppressWarnings("unchecked")
public void testLoadSuppressionsUriSyntaxException() throws Exception {
final URL configUrl = mock(URL.class);
when(configUrl.toURI()).thenThrow(URISyntaxException.class);
mockStatic(CommonUtils.class, Mockito.CALLS_REAL_METHODS);
final String fileName = "suppressions_none.xml";
when(CommonUtils.class.getResource(fileName)).thenReturn(configUrl);
try {
CommonUtils.getUriByFilename(fileName);
fail("Exception is expected");
} catch (CheckstyleException ex) {
assertTrue(ex.getCause() instanceof URISyntaxException);
assertEquals("Unable to find: " + fileName, ex.getMessage());
}
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project weex-example by KalicyZhou.
the class WXImageTest method testInitComponentHostView.
@Test
@PrepareForTest(WXImageView.class)
public void testInitComponentHostView() throws Exception {
ImageView imageView = mWXImage.initComponentHostView(Robolectric.setupActivity(TestActivity.class));
assertEquals(imageView.getClass(), WXImageView.class);
}
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.mobile_center_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());
}
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.mobile_center_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());
}
Aggregations