测试最近启动的活动是否确实已经开始
我已经研究了一些类似的问题,但还没有成功地测试这一点。
我尝试了这个,但是对我来说,origactivity 结果是 null,所以我使用 .baseIntent.getComponent() 来识别已启动的活动(我想这应该给我相同的结果?)
我的 JUnit 测试代码:
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());
}
});
}
}
不幸的是,这会导致assertEquals(recentTask.baseIntent.getComponent().getClassName(),
断言失败。
为什么会这样,有没有更好的方法来测试这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
断言失败,因为
ActivityManager.getRecentTasks(...)
返回导致新 要创建的任务,在您的情况下是MainMenuActivity
,而不是GameActivity
。您可以使用 Instrumentation.ActivityMonitor ,如下所示:The assert fails because
ActivityManager.getRecentTasks(...)
returns activities that caused new task to be created, and in your case that isMainMenuActivity
, notGameActivity
. You can use Instrumentation.ActivityMonitor though, like this: