我在SO上研究了一些类似的问题,但是还没有成功地对此进行测试.
Android – testing if another activity has started
我尝试过这个,但是对我来说,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() { @R_301_6818@<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(),断言失败.
为什么会这样,有没有更好的方法来测试呢?
@R_403_6120@:
断言失败是因为ActivityManager.getRecentTasks(…)返回导致创建新task的活动,在您的情况下为MainMenuActivity,而不是GameActivity.您可以使用Instrumentation.ActivityMonitor,如下所示:
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); }}
总结 以上是内存溢出为你收集整理的android-测试最近启动的活动是否实际上已经开始全部内容,希望文章能够帮你解决android-测试最近启动的活动是否实际上已经开始所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)