开发者

Testing if a recently launched activity actually has started

I have looked into a few similar questions on SO, but have not yeet managed to successfully test this.

Android - testing if another activity has started

I tried this one, but for me, origactivity turns out to be null, so I used .baseIntent.getComponent() instead to identify the started activity(I suppose this should give me the same result?)

My JUnit test code:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityManager am;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        am = (ActivityManager) mActivity.getSystemService(Service.ACTIVITY_SERVICE);
        Intent i = new Intent(mActivity, GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        final ActivityManager am2 = am;

        mActivity.runOnUiThread(new Runnable() {
            public void run() {                 
                List<ActivityManager.RecentTaskInfo> processes = am2.getRecentTasks(2, ActivityManager.RECENT_WITH_EXCLUDED);
                ActivityManager.RecentTaskInfo recentTask = processes.get(0);
                assertEquals(recentTask.baseIntent.get开发者_Go百科Component().getClassName(), GameActivity.class.getName());
            }
        });
    }
}

Unfortunately, this causes the assertEquals(recentTask.baseIntent.getComponent().getClassName(), assertion to fail.

Why is this, and is there a better way to test this?


The assert fails because ActivityManager.getRecentTasks(...) returns activities that caused new task to be created, and in your case that is MainMenuActivity, not GameActivity. You can use Instrumentation.ActivityMonitor though, like this:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityMonitor mGameActivityMonitor;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        Intent i  = new Intent(mActivity,
                GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add a monitor before we start the activity
        mGameActivityMonitor = 
                new ActivityMonitor(GameActivity.class.getName(), null, false);
        getInstrumentation().addMonitor(mGameActivityMonitor);

        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        // Use the monitor to ensure the activity was started
        Activity activity = mGameActivityMonitor.waitForActivityWithTimeout(5 * 1000);
        assertNotNull("Activity was not started", activity);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜