测试等待 VideoView 完成以开始新的 activity robolectric 3
Test wait to VideoView finish to start new activity robolectric 3
我必须测试一个有视频的屏幕启动,视频持续时间为 11 秒,当视频结束时开始另一个 activity。
我有以下测试class:
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 activity:
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();
}
}
问题是如何为这段代码编写测试?
可以从ShadowVideoView的getOnCompletionListener获取MediaPlayer.OnCompletionListener,然后调用它的onCompletion方法。您可以模拟 MediaPlayer 对象。
可能的代码片段:
assertTrue(videoView.isPlaying());
MediaPlayer.OnCompletionListener completionListener = videoView.getOnCompletionListener();
completionListener.onCompletion(mock(MediaPlayer.class));
Intent intent = screenSplash.getNextStartedActivity();
assertNull(intent);
assertEquals(ChooseTeamActivity.class.getName(), intent.getComponent().getClassName());
这种方法将能够测试上面的代码。如果您正在考虑实际完整播放视频然后执行侦听器代码,那么这种方法将不符合单元测试方法。我们测试的是书面代码的功能,而不是底层框架。
在这种情况下,您的测试代码应该测试有效的 URI,检查它是否正在播放以及侦听器是否执行所需的操作。
通过 MediaPlayer 对象测试侦听器也将是底层 android 代码的测试,而不仅仅是您的代码。
我必须测试一个有视频的屏幕启动,视频持续时间为 11 秒,当视频结束时开始另一个 activity。
我有以下测试class:
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 activity:
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();
}
}
问题是如何为这段代码编写测试?
可以从ShadowVideoView的getOnCompletionListener获取MediaPlayer.OnCompletionListener,然后调用它的onCompletion方法。您可以模拟 MediaPlayer 对象。 可能的代码片段:
assertTrue(videoView.isPlaying());
MediaPlayer.OnCompletionListener completionListener = videoView.getOnCompletionListener();
completionListener.onCompletion(mock(MediaPlayer.class));
Intent intent = screenSplash.getNextStartedActivity();
assertNull(intent);
assertEquals(ChooseTeamActivity.class.getName(), intent.getComponent().getClassName());
这种方法将能够测试上面的代码。如果您正在考虑实际完整播放视频然后执行侦听器代码,那么这种方法将不符合单元测试方法。我们测试的是书面代码的功能,而不是底层框架。 在这种情况下,您的测试代码应该测试有效的 URI,检查它是否正在播放以及侦听器是否执行所需的操作。 通过 MediaPlayer 对象测试侦听器也将是底层 android 代码的测试,而不仅仅是您的代码。