[英]Robolectric Test for ProgressDialog in android
I have 2 methods我有两种方法
public void showProgress() {
progressDialog = ProgressDialog.show(LoanDemandDetails.this, "", resourceUtil.getLabelById(R.string.uploading_loan_demand), true, false);
}
and和
public void onHideProgressDialog() {
progressDialog.hide();
}
I want to test these functions using Robolectric.我想使用 Robolectric 测试这些功能。
I've written a test case for showProgress
and it passed which looks like the following.我为showProgress
编写了一个测试用例,它通过了,如下所示。
@Test
public void showProgressDialog() {
activity.showProgress();
ProgressDialog dialog = (ProgressDialog) ShadowProgressDialog.getLatestDialog();
assertEquals("Uploading Loan Demand...", shadowOf(dialog).getMessage());
}
Similarly, when I write a test case for hideProgressDialog
, it fails.同样,当我为hideProgressDialog
编写测试用例时,它失败了。
@Test
public void hideProgressDialog(){
activity.showProgress();
activity.onHideProgressDialog();
ProgressDialog dialog1 = (ProgressDialog) ShadowProgressDialog.getLatestDialog();
assertFalse(dialog1.isShowing());
}
Any idea?任何的想法? Thanks in advance!提前致谢!
It looks like, the code that checks the dialog is showing or not is being executed too fast before the dialog is actually being hidden.看起来,在实际隐藏对话框之前,检查对话框是否显示的代码执行得太快了。 I would like to suggest adding an intentional Thread.sleep
between the executions as follows.我想建议在执行之间添加一个有意的Thread.sleep
如下。
@Test
public void hideProgressDialog() throws Exception {
activity.showProgress();
activity.onHideProgressDialog();
// Introduce some intentional delay here
Thread.sleep(2000); // 2 seconds delay added
// Now check if the tests are passing.
ProgressDialog dialog1 = (ProgressDialog) ShadowProgressDialog.getLatestDialog();
assertFalse(dialog1.isShowing());
}
If you are using RxJava, you can use TestScheduler
to do this in a graceful way instead of having a Thread.sleep
in your test code.如果您使用 RxJava,您可以使用TestScheduler
以优雅的方式执行此操作,而不是在您的测试代码中使用Thread.sleep
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.