下面列出了android.widget.Button#performClick ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void shouldNegativeListener_whenClickingPositiveButtonFromSupportFragment() {
AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment)
.setTitle(TITLE)
.setRationale(RATIONALE)
.setPositiveButton(POSITIVE)
.setNegativeButton(NEGATIVE)
.setThemeResId(R.style.Theme_AppCompat)
.build()
.showDialog(positiveListener, negativeListener);
Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
positive.performClick();
verify(negativeListener, times(1))
.onClick(any(DialogInterface.class), anyInt());
}
@Test
public void testCustomAuthMethodPickerLayout() {
List<String> providers = Arrays.asList(EmailAuthProvider.PROVIDER_ID);
AuthMethodPickerLayout customLayout = new AuthMethodPickerLayout
.Builder(R.layout.fui_provider_button_email)
.setEmailButtonId(R.id.email_button)
.build();
AuthMethodPickerActivity authMethodPickerActivity = createActivityWithCustomLayout(providers, customLayout);
Button emailButton = authMethodPickerActivity.findViewById(R.id.email_button);
emailButton.performClick();
//Expected result -> Directing users to EmailActivity
ShadowActivity.IntentForResult nextIntent =
Shadows.shadowOf(authMethodPickerActivity).getNextStartedActivityForResult();
assertEquals(
EmailActivity.class.getName(),
nextIntent.intent.getComponent().getClassName());
}
@Test
public void startWithoutParametersCancel()
{
ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
activityController.start();
activityController.visible();
activityController.resume();
Activity activity = (Activity)activityController.get();
final Button cancelButton = (Button) activity.findViewById(R.id.cancelButton);
assertEquals(false, activity.isFinishing());
cancelButton.performClick();
assertEquals(true, activity.isFinishing());
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (mDialog != null) {
Button button = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.performClick();
}
return true;
} else {
return false;
}
}
@Test
public void instantiateItem_inActiveLoginButtonShouldToast() throws Exception {
TestLoginListener listener = new TestLoginListener();
loginAdapter.setLoginListener(listener);
View view = (View) loginAdapter.instantiateItem(new FrameLayout(application), PAGE_4);
Button loginButton = (Button) view.findViewById(R.id.login_button);
loginButton.performClick();
String expected = application.getString(R.string.agree_to_terms_please);
assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
}
@Test
public void testPhoneLoginFlow() {
List<String> providers = Arrays.asList(PhoneAuthProvider.PROVIDER_ID);
AuthMethodPickerActivity authMethodPickerActivity = createActivity(providers);
Button phoneButton = authMethodPickerActivity.findViewById(R.id.phone_button);
phoneButton.performClick();
ShadowActivity.IntentForResult nextIntent =
Shadows.shadowOf(authMethodPickerActivity).getNextStartedActivityForResult();
assertEquals(
PhoneActivity.class.getName(),
nextIntent.intent.getComponent().getClassName());
}
@Test
public void whenClickingSelectSoundExpectOpenFilePickerBeCalled() throws Exception {
Button selectSound = (Button) activity.findViewById(R.id.id_btn_select_step_sound);
selectSound.performClick();
verify(filePickerProxy)
.openFilePicker(any(TaskCreateFragment.class), any(AssetType.class));
}
@Test
public void testNeutralButton() {
// setup
fixture.show();
Button button = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
FilterAdapter filterAdapter = MainContextHelper.INSTANCE.getFilterAdapter();
MainActivity mainActivity = MainContextHelper.INSTANCE.getMainActivity();
// execute
button.performClick();
// validate
RobolectricUtil.INSTANCE.clearLooper();
assertFalse(alertDialog.isShowing());
verify(filterAdapter).reload();
verify(mainActivity, never()).update();
}
/**
* 处理返回键
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Button btn = (Button) findViewById(R.id.dialog_button_cancel);
btn.performClick();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Test
public void testButtonClickShouldShowToast() throws Exception {
RobolectricActivity activity = Robolectric.buildActivity(RobolectricActivity.class).create().get();
Button view = (Button) activity.findViewById(R.id.showToast);
assertNotNull(view);
view.performClick();
assertThat(ShadowToast.getTextOfLatestToast(), equalTo("Lala") );
}
@Test
public void buttonClickShouldStartNewActivity() throws Exception
{
Button button = (Button) activity.findViewById( R.id.startNextActivity );
button.performClick();
Intent intent = Shadows.shadowOf(activity).peekNextStartedActivity();
assertEquals(RobolectricSecondActivity.class.getCanonicalName(), intent.getComponent().getClassName());
}
@Test
public void inverse() {
CheckBox checkBox = (CheckBox) mTestActivity.findViewById(R.id.checkBox);
Button button = (Button) mTestActivity.findViewById(R.id.button4);
Assert.assertFalse(checkBox.isChecked());
button.performClick();
Assert.assertTrue(checkBox.isChecked());
button.performClick();
Assert.assertFalse(checkBox.isChecked());
}
@Test
public void whenClickingSelectPictureExpectOpenFilePickerBeCalled() throws Exception {
Button selectPicture = (Button) activity.findViewById(R.id.id_btn_select_step_picture);
selectPicture.performClick();
verify(filePickerProxy)
.openFilePicker(any(StepCreateFragment.class), any(AssetType.class));
}
@Test
@TargetApi(11)
public void whenBuild_thenSnackbarSetup() {
int messageTextColor = 0xFF111111;
int actionTextColor = 0xFF999999;
int backgroundCOlor = 0xFF777777;
String message = "message";
String action = "action";
RuntimeEnvironment.application.setTheme(R.style.TestSnackbarBuilder_AppTheme);
CoordinatorLayout parent = new CoordinatorLayout(RuntimeEnvironment.application);
Snackbar snackbar = new SnackbarBuilder(parent)
.messageTextColor(messageTextColor)
.actionTextColor(actionTextColor)
.message(message)
.actionText(action)
.duration(Snackbar.LENGTH_INDEFINITE)
.backgroundColor(backgroundCOlor)
.actionAllCaps(false)
.build();
assertThat(snackbar.getDuration()).isEqualTo(Snackbar.LENGTH_INDEFINITE);
Assertions.assertThat((TextView) snackbar.getView().findViewById(R.id.snackbar_text))
.hasCurrentTextColor(messageTextColor)
.hasText(message);
Assertions.assertThat((ColorDrawable) snackbar.getView().getBackground())
.hasColor(backgroundCOlor);
Button button = (Button) snackbar.getView().findViewById(R.id.snackbar_action);
Assertions.assertThat(button).hasCurrentTextColor(actionTextColor);
button.performClick();
assertThat(button.getTransformationMethod()).isNull();
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (mDialog != null) {
Button button = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.performClick();
}
return true;
} else {
return false;
}
}
@Test
public void startWithoutParametersCannotCreateGiftCard()
{
ActivityController activityController = Robolectric.buildActivity(GiftCardViewActivity.class).create();
activityController.start();
activityController.visible();
activityController.resume();
Activity activity = (Activity)activityController.get();
DBHelper db = new DBHelper(activity);
assertEquals(0, db.getGiftCardCount());
final EditText storeField = (EditText) activity.findViewById(R.id.storeName);
final EditText cardIdField = (EditText) activity.findViewById(R.id.cardId);
final TextView receiptField = (TextView) activity.findViewById(R.id.receiptLocation);
final Button saveButton = (Button) activity.findViewById(R.id.saveButton);
saveButton.performClick();
assertEquals(0, db.getGiftCardCount());
storeField.setText("store");
saveButton.performClick();
assertEquals(0, db.getGiftCardCount());
cardIdField.setText("cardId");
saveButton.performClick();
assertEquals(0, db.getGiftCardCount());
receiptField.setText("receipt");
saveButton.performClick();
assertEquals(0, db.getGiftCardCount());
}
@Test
public void instantiateItem_loginButtonShouldSendMixpanelEvent() throws Exception {
TestLoginListener listener = new TestLoginListener();
loginAdapter.setLoginListener(listener);
View view = (View) loginAdapter.instantiateItem(new FrameLayout(application), PAGE_4);
Button loginButton = (Button) view.findViewById(R.id.login_button);
CheckBox checkbox = (CheckBox) view.findViewById(R.id.agree);
checkbox.setEnabled(true);
loginButton.performClick();
assertThat(listener.login).isFalse();
}
@Test
public void startWithGiftCardWithReceiptUpdateReceiptCancel() throws IOException
{
ActivityController activityController = createActivityWithGiftCard();
Activity activity = (Activity)activityController.get();
DBHelper db = new DBHelper(activity);
db.insertGiftCard("store", "cardId", "value", "receipt");
activityController.start();
activityController.visible();
activityController.resume();
checkAllFields(activity, "store", "cardId", "value", "receipt", false);
// Add something that will 'handle' the media capture intent
registerMediaStoreIntentHandler();
// Complete image capture successfully
Uri imageLocation = captureImageWithResult(activity, R.id.updateButton, true);
checkAllFields(activity, "store", "cardId", "value", "receipt", true);
// Cancel the gift card creation
final Button cancelButton = (Button) activity.findViewById(R.id.cancelButton);
assertEquals(false, activity.isFinishing());
cancelButton.performClick();
assertEquals(true, activity.isFinishing());
activityController.destroy();
// Ensure the image has been deleted
File imageFile = new File(imageLocation.getPath());
assertEquals(false, imageFile.exists());
}
@Test
public void testSignUpButton_validatesFields() {
EmailActivity emailActivity = createActivity(EmailAuthProvider.PROVIDER_ID);
// Trigger RegisterEmailFragment (bypass check email)
emailActivity.onNewUser(
new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build());
Button button = emailActivity.findViewById(R.id.button_create);
button.performClick();
TextInputLayout nameLayout = emailActivity.findViewById(R.id.name_layout);
TextInputLayout passwordLayout = emailActivity.findViewById(R.id.password_layout);
assertEquals(
emailActivity.getString(R.string.fui_missing_first_and_last_name),
nameLayout.getError().toString());
assertEquals(
String.format(
emailActivity.getResources().getQuantityString(
R.plurals.fui_error_weak_password,
R.integer.fui_min_password_length),
emailActivity.getResources()
.getInteger(R.integer.fui_min_password_length)
),
passwordLayout.getError().toString());
}
/**
* Initiate and complete an image capture, returning the
* location of the resulting file if the capture was
* a success.
*
* @param success
* true if the image capture is a success, and a
* file is to be created at the requested location,
* false otherwise.
* @param buttonId
* id of the button to press to initiate the capture
* @return The URI pointing to the image file location,
* regardless if the operation was successful or not.
*/
private Uri captureImageWithResult(final Activity activity, final int buttonId, final boolean success) throws IOException
{
// Start image capture
final Button captureButton = (Button) activity.findViewById(buttonId);
captureButton.performClick();
ShadowActivity.IntentForResult intentForResult = shadowOf(activity).peekNextStartedActivityForResult();
assertNotNull(intentForResult);
Intent intent = intentForResult.intent;
assertNotNull(intent);
String action = intent.getAction();
assertNotNull(action);
assertEquals(MediaStore.ACTION_IMAGE_CAPTURE, action);
Bundle bundle = intent.getExtras();
assertNotNull(bundle);
assertEquals(false, bundle.isEmpty());
Uri argument = bundle.getParcelable(MediaStore.EXTRA_OUTPUT);
assertNotNull(argument);
assertTrue(argument.toString().length() > 0);
// Respond to image capture, success
shadowOf(activity).receiveResult(
intent,
success ? Activity.RESULT_OK : Activity.RESULT_CANCELED,
null);
if(success)
{
File imageFile = new File(argument.getPath());
assertEquals(false, imageFile.exists());
boolean result = imageFile.createNewFile();
assertTrue(result);
}
return argument;
}