Lifecycle Testing with Robotium: Killing and Restarting Activity

笑着哭i 提交于 2019-11-27 07:17:48

问题


How is it possible to restart an activity that was ended using Robotium's solo.goBack()?

The following does not restart the activity: (the test finishes ok)

    solo.goBack();
    try {
    //  recreate activity here
    runTestOnUiThread(new Runnable() {
        public void run() {
        getInstrumentation().callActivityOnCreate(getActivity(), 
                              null);
        getInstrumentation().callActivityOnStart(getActivity());
        getInstrumentation().callActivityOnResume(getActivity());
        }});
    }

How do you restart an Activity that was ended by Solo.goBack()?

SO-questions

  • Testing activity flow with robotium addresses changing between two activities in Robotium, not destoying and restarting.
  • Simulate Android killing and restart service deals with a service, not an activity (and is unanswered)
  • Activity doesn't restart in different tests with Robotium asks how to restart the activity manually, but is answered in a different way

Minimal example

To reproduce a minimal test like this, create a project and its test project:

android create project -t 1 -p testRestart -k com.testRestart -a testactivity
cd testRestart
mkdir tests
cd tests
android create test-project -m .. -p .

Copy the Robotium jar to the tests/libs folder. Paste this code inside the file testactivityTest.java:

package com.testRestart;

import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;

public class testactivityTest extends ActivityInstrumentationTestCase2<testactivity> {
    private Solo solo;

    protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    }
    public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    }

    public testactivityTest() {
        super("com.testRestart", testactivity.class);

    }

    public void testDestroyAndRestart() {
        solo.goBack();
    try {
        //  recreate activity here
        runTestOnUiThread(new Runnable() {
            public void run() {
            getInstrumentation().callActivityOnCreate(getActivity(), 
                                  null);
            getInstrumentation().callActivityOnStart(getActivity());
            getInstrumentation().callActivityOnResume(getActivity());
            }});
    } catch ( Throwable t ) {
        throw new RuntimeException(t);
    }
    }
}

Inside the tests folder, do a

ant debug install
adb shell am instrument -w -e class com.testRestart.testactivityTest com.testRestart.tests/android.test.InstrumentationTestRunner

The question again: how can you restart an activity that was ended by Solo.goBack()?


回答1:


As @IHeartAndroid said in his answer to this robotium question (I had not seen it before, there was a link by @Flavio Capaccio in a comment to a "related question"):

launchActivity("com.testRestart", testactivity.class, null);

works. This is a function in InstrumentationTestCase.

(If you want to upvote this answer, upvote his answer as well)



来源:https://stackoverflow.com/questions/30096491/lifecycle-testing-with-robotium-killing-and-restarting-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!