AmbiguousViewMatcherException SimpleExoPlayerView Android

时间:2017-09-02 18:20:33

标签: android exoplayer android-espresso

我正在尝试编写Android Espresso测试以检查是否播放了视频。我点击播放按钮后检查我的SimpleExoPlayerView的关联播放器是否正在播放。问题是层次结构中还有PlaybackControlView与我的SimpleExoPlayerView具有相同的ID(我从未设置过,因此我不知道它具有相同的ID)。如何指定我要测试SimpleExoPlayerView

这是我的测试:

@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:&#39; id:   com.example.android.bakingapp:ID / simple_video_view&#39;匹配多个   层次结构中的视图。

具有相同ID的两个视图是我的SimpleExoPlayerViewPlaybackControlView,我不太了解。

1 个答案:

答案 0 :(得分:2)

试试这个匹配器:

onView(allOf(withId(R.id.simple_video_view),
  withClassName(is(SimpleExoPlayerView.class.getName())))