我有以下HorizontalScrollView,可通过编程方式向其中添加项目:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="vm"
type="com.sample.android.tmdb.ui.detail.MovieDetailViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:visibleGone="@{vm.isTrailersVisible}">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/trailers"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
<HorizontalScrollView
android:id="@+id/trailer_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:items="@{vm.trailers}" />
</HorizontalScrollView>
</LinearLayout>
</layout>
在这里我向其中添加项目:
@JvmStatic
@BindingAdapter("items")
fun addItems(linearLayout: LinearLayout, trailers: List<Video>) {
linearLayout.removeAllViews()
val context = linearLayout.context
for (trailer in trailers) {
val thumbContainer = context.layoutInflater.inflate(R.layout.video, linearLayout, false)
val thumbView = thumbContainer.findViewById<ImageView>(R.id.video_thumb)
thumbView.apply {
setOnClickListener {
val playVideoIntent = Intent(Intent.ACTION_VIEW, Uri.parse(Video.getUrl(trailer)))
context.startActivity(playVideoIntent)
}
}
linearLayout.addView(thumbContainer)
}
}
现在,我想为其添加一个Espresso
测试。我想滚动HorizontalScrollView
并单击第三项。到目前为止,我编写了以下测试:
@Test
fun shouldBeAbleToDisplayTrailer() {
onView(withId(R.id.list)).perform(RecyclerViewActions
.actionOnItemAtPosition<MovieViewHolder>(8, click()))
onView(withId(R.id.trailer_scroll_view)).perform(nestedScrollTo()).check(matches(isDisplayed()))
// intended(Matcher<Intent> matcher) asserts the given matcher matches one and only one
// intent sent by the application.
//intended(allOf(hasAction(Intent.ACTION_VIEW)))
}
但是我不知道如何滚动HorizontalScrollView
。你能帮忙吗?
答案 0 :(得分:0)
The answer is just two clicks away:
Check scrollTo()
View Action implementation:
public static ViewAction scrollTo() {
return actionWithAssertions(new ScrollToAction());
}
Check ScrollToAction()
implementation:
/** Enables scrolling to the given view. View must be a descendant of a ScrollView or ListView. */
The view should fulfil below constraints to apply ScrollToAction()
to it. I agree that ScrollToAction()
description can be improved a bit:
withEffectiveVisibility(Visibility.VISIBLE),
isDescendantOfA(
anyOf(
isAssignableFrom(ScrollView.class),
isAssignableFrom(HorizontalScrollView.class),
isAssignableFrom(ListView.class))));
And the answer is - use ViewActions.scrollTo()
to scroll HorizontalScrollView
.