J'essaie d'écrire un test Android Espresso pour vérifier si une vidéo est jouée. Je vérifie pour voir si le joueur associé de mon SimpleExoPlayerView
joue après que je clique sur le bouton de lecture. Le problème est qu'il y a aussi un PlaybackControlView
dans la hiérarchie qui a le même identifiant que mon SimpleExoPlayerView
(que je n'ai jamais défini donc je ne sais pas comment il a le même id). Comment puis-je spécifier que je veux tester le SimpleExoPlayerView
?AmbiguousViewMatcherException SimpleExoPlayerView Android
Voici mon test:
@RunWith(AndroidJUnit4.class)
public class VideoPlaybackTest {
@Rule
public ActivityTestRule<MainActivity> mMainActivityTestRule =
new ActivityTestRule<MainActivity>(MainActivity.class);
@Before
public void registerIdlingResources() {
Espresso.registerIdlingResources(mMainActivityTestRule.getActivity().getIdlingResource());
}
@Test
public void videoIsPlayed() {
Intents.init();
onView(withId(R.id.recipes_recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
onView(withId(R.id.steps_recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
onView(withId(R.id.exo_play))
.perform(click());
onView(withId(R.id.simple_video_view))
.check(new VideoPlaybackAssertion(true));
Intents.release();
}
@After
public void unregisterIdlingResources() {
Espresso.unregisterIdlingResources(mMainActivityTestRule.getActivity().getIdlingResource());
}
}
class VideoPlaybackAssertion implements ViewAssertion {
private final Matcher<Boolean> matcher;
//Constructor
public VideoPlaybackAssertion(Matcher<Boolean> matcher) {
this.matcher = matcher;
}
//Sets the Assertion's matcher to the expected playbck state.
public VideoPlaybackAssertion(Boolean expectedState) {
this.matcher = is(expectedState);
}
//Method to check if the video is playing.
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
SimpleExoPlayerView exoPlayerView = (SimpleExoPlayerView) view;
SimpleExoPlayer exoPlayer = exoPlayerView.getPlayer();
int state = exoPlayer.getPlaybackState();
Boolean isPlaying;
if ((state == STATE_BUFFERING) || (state == STATE_READY)) {
isPlaying = true;
} else {
isPlaying = false;
}
assertThat(isPlaying, matcher);
}
}
android.support.test.espresso.AmbiguousViewMatcherException: 'avec id: com.example.android.bakingapp: id/simple_video_view' correspond à plusieurs vues en la hiérarchie.
Les deux vues qui ont le même id sont ma SimpleExoPlayerView
et PlaybackControlView
que je ne comprends pas vraiment.