gpt4 book ai didi

android - AmbiguousViewMatcherException SimpleExoPlayerView Android

转载 作者:行者123 更新时间:2023-11-29 19:09:09 26 4
gpt4 key购买 nike

我正在尝试编写一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com