测试单击按钮时是否启动正确的活动
我正在为有几个按钮的活动编写测试, 每一个都启动一个新的活动,
我如何知道按钮是否启动了正确的活动?
这是我到目前为止所拥有的:
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
private Intent mMainIntent;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mMainIntent = new Intent(Intent.ACTION_MAIN);
}
@MediumTest
public void testButtonActivityA () {
MainActivity activity = startActivity(mMainIntent, null, null);
Button buttonActivityA = (Button) activity.findViewById(com.project.R.id.button_activity_a);
buttonVoice.performClick();
Intent i = getStartedActivityIntent();
assertNotNull(i);
assertTrue(isFinishCalled());
}
}
PS:“isFinishedCalled()”失败了,如果我引发一个新的全屏活动,这怎么可能? 谢谢,
I'm writing a test for an activity that have a few buttons,
each one of than starts a new Activity,
How can I know if the button is starting the correct activity?
This is what I have so far:
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
private Intent mMainIntent;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mMainIntent = new Intent(Intent.ACTION_MAIN);
}
@MediumTest
public void testButtonActivityA () {
MainActivity activity = startActivity(mMainIntent, null, null);
Button buttonActivityA = (Button) activity.findViewById(com.project.R.id.button_activity_a);
buttonVoice.performClick();
Intent i = getStartedActivityIntent();
assertNotNull(i);
assertTrue(isFinishCalled());
}
}
PS: the 'isFinishedCalled()' is failing, how can this be if I raise a new fullscreen Activity?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它失败是因为未调用 finish() 。
您必须自己完成一项活动,否则当您打开一个新活动时,它会出现在“堆栈”的顶部,并且原始活动已调用 onPause 但仍然“活动”。 finish() 是一个隐式调用,您可以将其编码到应用程序中以销毁活动。
请阅读Android Activity Lifecycle
然后学习调用 < a href="http://developer.android.com/reference/android/app/Activity.html#finish%28%29" rel="nofollow">何时应该调用 finish()
It's failing because finish() isn't called.
you have to finish an activity yourself, otherwise when you open a new one it comes up over the top on the 'stack' and the original activity has onPause called but is still 'alive'. finish() is an implicit call that you can code into your app to destroy an activity.
Please go read about the Android Activity Lifecycle
Then learn to call when you should call finish()