简体   繁体   English

Robotium:单击“主页”按钮,确认活动已完成并且基础活动已恢复

[英]Robotium: verify that activity gets finished and the underlying one restored when “home” button clicked

I' m using Android annotations and I have the following code in my Activity: 我正在使用Android批注,并且在“活动”中有以下代码:

@OptionsItem
void homeSelected() {
    finish();
}

obviously, this code should finish the current activity, when the user has clicked the home button in ActionBar. 显然,当用户单击ActionBar中的主页按钮时,此代码应完成当前活动。 But how can I test this code? 但是如何测试此代码? I would like to be able to verify somehow that after clicking on the home button the current activity finished, and the previous activity restored. 我希望能够以某种方式验证单击主页按钮后,当前活动已完成,并且先前的活动已还原。 Is this possible? 这可能吗? Or at least can I verify that my homeSelected method gets called after clicking the home button? 或者至少可以验证单击“主页”按钮后是否调用了homeSelected方法?

I'm using Robotium and I have the following test method: 我正在使用Robotium,并且具有以下测试方法:

@SmallTest
public void testGoBack() {
    solo.clickOnActionBarHomeButton();
    // ???? How do I verify the homeSelected method was actually called?
}

You could use waitForActivity(...) . 您可以使用waitForActivity(...)

waitForActivity(Class<? extends android.app.Activity> activityClass)

Waits for an Activity matching the specified class. 等待与指定类匹配的Activity。

Check out the documentation . 查看文档

@SmallTest
public void testGoBack() {
    solo.clickOnActionBarHomeButton();

    solo.waitForActivity(com.example.UpActivity.class);
}

The default routine uses a 20 seconds timeout. 默认例程使用20秒超时。

ps: alternatively add a debug log to the onDestroy() method of your activity: ps:或者将调试日志添加到您活动的onDestroy()方法中:

public class MyActivity extends Activity {

    ...
    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d ("TAG", "Finishing MyActivity");
    }
} 

and wait for this log message inside of your test method: 并在测试方法中等待以下日志消息:

waitForLogMessage("Finishing MyActivity");

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

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