如何使用工具从TestCase中控制安卓活动的生命周期?
在official documentation上,声明了“生命周期控制:使用工具,您可以使用测试用例类提供的方法来启动、暂停和销毁测试中的活动。”当然,使用此测试用例时,Acitivity将在调用getActivity()时自动创建,并在每个测试用例之后停止。但是,如何在外部控制生命周期,以检查所有生命周期方法是否正确实现?
生命周期方法onActivityXXX只是帮助调用相应的方法,但不会导致活动暂停或停止。有没有人可以帮助我,告诉我我必须使用哪些方法?
有什么方法可以测试Android应用程序的生命周期实现吗?
发布于 2015-05-26 18:13:25
这不会让您完全控制生命周期,但它是here的示例
// Start the main activity of the application under test
mActivity = getActivity();
// Get a handle to the Activity object's main UI widget, a Spinner
mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);
// Set the Spinner to a known position
mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);
// Stop the activity - The onDestroy() method should save the state of the Spinner
mActivity.finish();
// Re-start the Activity - the onResume() method should restore the state of the Spinner
mActivity = getActivity();
// Get the Spinner's current position
int currentPosition = mActivity.getSpinnerPosition();
// Assert that the current position is the same as the starting position
assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);这使您可以对主要生命周期事件进行一些控制。因为我正在处理同样的问题,所以我正在研究robotium,它应该会有所帮助。
https://stackoverflow.com/questions/9606048
复制相似问题