【发布时间】:2015-07-19 21:38:51
【问题描述】:
我必须测试一个有视频的屏幕启动画面,视频持续时间为 11 秒,视频结束后开始其他活动。
我有以下测试类:
public class ScreenSplashTest {
private ShadowActivity screenSplash;
private ShadowVideoView videoView;
@Before
public void setUp(){
ScreenSplash screenSplashActivity = Robolectric.buildActivity(ScreenSplash.class).create().get();
screenSplash = Shadows.shadowOf(screenSplashActivity);
VideoView videoViewWidget = (VideoView)screenSplash.findViewById(R.id.videoViewSplash);
videoView = Shadows.shadowOf(videoViewWidget);
}
@Test
public void activityStarts_VideoStartsToPlay() throws Exception{
assertTrue(videoView.isPlaying());
}
@Test
public void whenVideoFinish_StartsChooseTeamActivity() throws Exception{
videoView.stopPlayback();
Intent nextActivity = screenSplash.getNextStartedActivity();
assertEquals(nextActivity.getComponent().getClassName(), ChooseTeamActivity.class.getName());
}
}
这是我的 ScreenSplash 活动:
public class ScreenSplash extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_splash);
getSupportActionBar().hide();
this.getWindow().getDecorView().setBackgroundColor(0xffffff);
VideoView videoView = (VideoView) findViewById(R.id.videoViewSplash);
Uri videoFile = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
videoView.setVideoURI(videoFile);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Intent i = new Intent(getApplicationContext(), ChooseTeamActivity.class);
startActivity(i);
}
});
videoView.start();
}
}
问题是如何为这段代码编写测试?
【问题讨论】:
标签: java android unit-testing robolectric