简体   繁体   English

如何为 AlertDialog 进行 Robolectric 测试

[英]How to make Robolectric Test for AlertDialog

I am new to robolectric and I am trying to make a test for a button that creates an AlertDialog.我是 robolectric 的新手,我正在尝试对创建 AlertDialog 的按钮进行测试。 When the button is clicked, an AlertDialog is made with a positive button that I want to click using Robolectric, and test if it launches an activity.当按钮被点击时,一个 AlertDialog 是一个正面的按钮,我想使用 Robolectric 点击它,并测试它是否启动了一个活动。 Here is the code for the button:这是按钮的代码:

newUserButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
            builder.setTitle(context.getResources().getString(R.string.start_title_message))
                    .setMessage(getResources().getString(R.string.start_dialog_message));
            builder.setPositiveButton(getString(R.string.start_confirm_message), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    startActivityForResult(new Intent(StartActivity.this, AvatarRoomActivity.class), 0);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            AlertDialog dialog = builder.create();
            ColorDrawable drawable = new ColorDrawable(Color.WHITE);
            drawable.setAlpha(200);
            dialog.getWindow().setBackgroundDrawable(drawable);
            dialog.show();
        }
    });

Does anyone know how I can test clicking the positive button, then launching AvatarRoomActivity?有谁知道我如何测试单击肯定按钮,然后启动 AvatarRoomActivity? Thanks in advance and hope to hear from someone soon.提前致谢,并希望尽快收到某人的来信。

I came across this problem today, and exposing a private function just for testing is not advised.我今天遇到了这个问题,不建议只为了测试而暴露私有函数。

Robolectric provides a ShadowAlertDialog , which can detect a shown Dialog or AlertDialog . Robolectric 提供了一个ShadowAlertDialog ,它可以检测显示的DialogAlertDialog

//get all shown dialogs    
ShadowAlertDialog.getShownDialogs()

//get single dialog  
(ShadowAlertDialog.getLatestDialog() as android.support.v7.app.AlertDialog)
    .getButton(AlertDialog.BUTTON_POSITIVE)
    .performClick()

//Continue the test

Let's forget the newUserButton for a while.让我们newUserButton忘记newUserButton It is not relevant to the problem.它与问题无关。

You will need to expose the AlertDialog object so that it is accessible in unit test code.您将需要公开AlertDialog对象,以便在单元测试代码中可以访问它。 So I assume you activity have such a method in StartActivity:所以我假设你的活动在 StartActivity 中有这样一个方法:

AlertDialog showDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
    builder.setTitle("This is title")
            .setMessage("Dialog Message");
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivityForResult(new Intent(this, AvatarRoomActivity.class), 0);
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();

    return dialog;

}

then the click event of newUserButton just invoke this method.那么newUserButton的点击事件就调用了这个方法。

Then we have the test case like this:然后我们有这样的测试用例:

@Test
public void testLaunchAvatarRoomWhenConfirm() {

    StartActivity startActivity = Robolectric.buildActivity(StartActivity.class).create().get();

    AlertDialog dialog = startActivity.showDialog();

    // Key part 1 : simulate button click in unit test
    Button confirm = dialog.getButton(Dialog.BUTTON_POSITIVE);
    confirm.performClick();

    // Key part 2 : Check that startActivityForResult is invoke
    ShadowActivity shadowActivity = shadowOf(startActivity);
    ShadowActivity.IntentForResult intentForResult = shadowActivity.getNextStartedActivityForResult();

    // assert that the proper request to start activity is sent
    ComponentName nextActivity = intentForResult.intent.getComponent();
    assertEquals(".AvatarRoomActivity", nextActivity.getShortClassName());

}

This test method verify that when the dialog's positive button is clicked, startActivityForResult is invoked with proper activity class name.此测试方法验证当单击对话框的肯定按钮时,使用正确的活动类名称调用startActivityForResult

So the remaining problem is how do we ensure the activity is really resolved and launched.所以剩下的问题是我们如何确保活动真正得到解决和启动。 Normally I would stop at this point for testing of alert dialog actions.通常我会在此时停下来测试警报对话框操作。 Whether the intent can be resolved and start activity property is out of the scope of this particular test case.是否可以解析意图和启动活动属性超出了此特定测试用例的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM