InstrumentationTestCase2 中 AlertDialog 上的 peformClick() 不起作用
peformClick() on an AlertDialog in an InstrumentationTestCase2 doesn't work
我想点击 ActivityInstrumentationTestCase2
中的 AlertDialog
。但是 DialogInterface.OnClickListener()
在测试完成之后执行,而不是在测试期间执行(我知道,因为我调试了这个测试)。
我的测试检查删除按钮。如果按下删除按钮,将出现 AlertDialog
并再次询问用户。如果用户接受 AlertDialog
,数据库行将被删除。
public class ShiftTest2 extends ActivityInstrumentationTestCase2<Shift> {
@UiThreadTest
public void testDeleteButton() {
Shift shift = getActivity();
Button deleteButton = (Button) shift.findViewById(R.id.shift_delete);
deleteButton.performClick(); // That works fine
AlertDialog alertDialog = shift.getAlertDialog();
// The thread execute the performClick() after this test finished
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
final Bundle savedShift = mDb.getShiftById("1");
assertNull(savedShift); // This assert fail
}
}
我的删除按钮 OnClickListener
。
public void onDeleteButton(View view) {
if (mDeleteDialog != null) {
mAlert = mDeleteDialog.create();
mAlert.show();
}
}
这里是我 AlertDialog
的听众。
@Override
public void onCreate(Bundle savedInstanceState) {
...
mDeleteDialog = new AlertDialog.Builder(this);
mDeleteDialog.setMessage(getString(R.string.delete_shift_really));
mDeleteDialog.setPositiveButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mDb.deleteShift(mId);
resetAllPreferences();
Intent parentActivity = new Intent(context, WorkingTimeSettings.class);
startActivity(parentActivity);
}
});
mDeleteDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Nothing to do here
}
});
}
我已经在测试中尝试了 Thread.sleep(10000)
,但它没有任何改变。问题是,线程不会跳出测试执行肯定按钮侦听器。也许是因为 @UiThreadTest
注释?
我可以自己解决问题。这真的是 @UiThreadTest
注释。只是 运行 方法 shift.runOnUiThread()
.
中的 关键 部分
public void testDeleteButton() {
final Shift shift = getActivity();
final Button deleteButton = (Button) shift.findViewById(R.id.shift_delete);
final int deleteButtonViewMode = deleteButton.getVisibility();
assertEquals(View.VISIBLE, deleteButtonViewMode);
shift.runOnUiThread(new Runnable() {
@Override
public void run() {
deleteButton.performClick();
shift.getAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE).performClick();
}
});
getInstrumentation().waitForIdleSync();
final Bundle savedShift = mDb.getShiftById("1");
assertNull(savedShift);
}
提示:我也删除了所有其他测试的注释。否则它们就不会是绿色的。
我想点击 ActivityInstrumentationTestCase2
中的 AlertDialog
。但是 DialogInterface.OnClickListener()
在测试完成之后执行,而不是在测试期间执行(我知道,因为我调试了这个测试)。
我的测试检查删除按钮。如果按下删除按钮,将出现 AlertDialog
并再次询问用户。如果用户接受 AlertDialog
,数据库行将被删除。
public class ShiftTest2 extends ActivityInstrumentationTestCase2<Shift> {
@UiThreadTest
public void testDeleteButton() {
Shift shift = getActivity();
Button deleteButton = (Button) shift.findViewById(R.id.shift_delete);
deleteButton.performClick(); // That works fine
AlertDialog alertDialog = shift.getAlertDialog();
// The thread execute the performClick() after this test finished
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
final Bundle savedShift = mDb.getShiftById("1");
assertNull(savedShift); // This assert fail
}
}
我的删除按钮 OnClickListener
。
public void onDeleteButton(View view) {
if (mDeleteDialog != null) {
mAlert = mDeleteDialog.create();
mAlert.show();
}
}
这里是我 AlertDialog
的听众。
@Override
public void onCreate(Bundle savedInstanceState) {
...
mDeleteDialog = new AlertDialog.Builder(this);
mDeleteDialog.setMessage(getString(R.string.delete_shift_really));
mDeleteDialog.setPositiveButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mDb.deleteShift(mId);
resetAllPreferences();
Intent parentActivity = new Intent(context, WorkingTimeSettings.class);
startActivity(parentActivity);
}
});
mDeleteDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Nothing to do here
}
});
}
我已经在测试中尝试了 Thread.sleep(10000)
,但它没有任何改变。问题是,线程不会跳出测试执行肯定按钮侦听器。也许是因为 @UiThreadTest
注释?
我可以自己解决问题。这真的是 @UiThreadTest
注释。只是 运行 方法 shift.runOnUiThread()
.
public void testDeleteButton() {
final Shift shift = getActivity();
final Button deleteButton = (Button) shift.findViewById(R.id.shift_delete);
final int deleteButtonViewMode = deleteButton.getVisibility();
assertEquals(View.VISIBLE, deleteButtonViewMode);
shift.runOnUiThread(new Runnable() {
@Override
public void run() {
deleteButton.performClick();
shift.getAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE).performClick();
}
});
getInstrumentation().waitForIdleSync();
final Bundle savedShift = mDb.getShiftById("1");
assertNull(savedShift);
}
提示:我也删除了所有其他测试的注释。否则它们就不会是绿色的。