android - AmbiguousViewMatcherException SimpleExoPlayerView Android

标签 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: 'with id: com.example.android.bakingapp:id/simple_video_view' matches multiple views in the hierarchy.

具有相同 id 的两个 View 是我的 SimpleExoPlayerView 和一个我不太了解的 PlaybackControlView

最佳答案

试试这个匹配器:

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

关于android - AmbiguousViewMatcherException SimpleExoPlayerView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46016895/

相关文章:

android - 我为什么要使用绑定(bind)服务?

Android 查看寻呼机 : how to distinguish user and programmatic swipes?

java - 从线程启动 Android Activity

android - ExoPlayer:控制前进和后退按钮的跳过间隔

android - 使用 espresso 测试软键盘是否可见

java - 数据绑定(bind)插件使用旧版本的支持库

android - 如何使用 exoplayer 流式传输 rtsp url?

android - 用于音频的 MediaExtractor,得到意外的音频

android - 我无法让 ViewActions.closeSoftKeyboard() 在 Espresso 2.2.2 中工作

android - 测试背景色 espresso Android