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.getComponent().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?