Espresso, how to check an item from RecyclerView consists of certain layout











up vote
1
down vote

favorite












Hi my grid recycler view's 0th item should be always camera and
other items should be always video contents.



To check 0th item has layout of com.xx.yy.R.layout.item_camera
I tried like this:




onView(withRecyclerView(recyclerViewResId).atPosition(0)).check(matches(withId(com.xx.yy.R.layout.item_camera)))




But it doesn't work, someone knows what I miss?



This is helper class:



fun withRecyclerView(recyclerViewId: Int) = RecyclerViewTestHelper.RecyclerViewMatcher(recyclerViewId)

object RecyclerViewTestHelper {

class RecyclerViewMatcher(private val recyclerViewId: Int) {

fun atPosition(position: Int): Matcher<View> {
return atPositionOnView(position, -1)
}

fun atPositionOnView(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}

fun withResourceId(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}
}

class CustomAssertions {
companion object {
fun hasItemCount(count: Int): ViewAssertion {
return RecyclerViewItemCountAssertion(count)
}
}

private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}

if (view !is RecyclerView) {
throw IllegalStateException("The asserted view is not RecyclerView")
}

if (view.adapter == null) {
throw IllegalStateException("No adapter is assigned to RecyclerView")
}
ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))
}
}
}

class HintTextColorMatcher private constructor(private val color: Int) : BoundedMatcher<View, TextView>(TextView::class.java) {
override fun matchesSafely(item: TextView): Boolean {
return item.currentHintTextColor == color
}

override fun describeTo(description: Description) {
description.appendText("with hint text color:")
.appendValue(color)
}

companion object {
fun withHintTextColor(color: Int): HintTextColorMatcher {
return HintTextColorMatcher(color)
}
}
}
}


Error log:



android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.BaseActions.checkRecyclerViewItemRes(BaseActions.kt:46)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActions.clickFirstMediaItem(PickerActivityActions.kt:42)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:76)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:20)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActionsKt.picker(PickerActivityActions.kt:9)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest.launchPickerForAllTest(GalleryInstruTest.kt:72)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
Caused by: junit.framework.AssertionFailedError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)









share|improve this question
























  • Could you post the error?
    – Aaron
    Nov 12 at 9:17










  • @Aaron I added error log
    – John
    Nov 13 at 1:45















up vote
1
down vote

favorite












Hi my grid recycler view's 0th item should be always camera and
other items should be always video contents.



To check 0th item has layout of com.xx.yy.R.layout.item_camera
I tried like this:




onView(withRecyclerView(recyclerViewResId).atPosition(0)).check(matches(withId(com.xx.yy.R.layout.item_camera)))




But it doesn't work, someone knows what I miss?



This is helper class:



fun withRecyclerView(recyclerViewId: Int) = RecyclerViewTestHelper.RecyclerViewMatcher(recyclerViewId)

object RecyclerViewTestHelper {

class RecyclerViewMatcher(private val recyclerViewId: Int) {

fun atPosition(position: Int): Matcher<View> {
return atPositionOnView(position, -1)
}

fun atPositionOnView(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}

fun withResourceId(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}
}

class CustomAssertions {
companion object {
fun hasItemCount(count: Int): ViewAssertion {
return RecyclerViewItemCountAssertion(count)
}
}

private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}

if (view !is RecyclerView) {
throw IllegalStateException("The asserted view is not RecyclerView")
}

if (view.adapter == null) {
throw IllegalStateException("No adapter is assigned to RecyclerView")
}
ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))
}
}
}

class HintTextColorMatcher private constructor(private val color: Int) : BoundedMatcher<View, TextView>(TextView::class.java) {
override fun matchesSafely(item: TextView): Boolean {
return item.currentHintTextColor == color
}

override fun describeTo(description: Description) {
description.appendText("with hint text color:")
.appendValue(color)
}

companion object {
fun withHintTextColor(color: Int): HintTextColorMatcher {
return HintTextColorMatcher(color)
}
}
}
}


Error log:



android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.BaseActions.checkRecyclerViewItemRes(BaseActions.kt:46)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActions.clickFirstMediaItem(PickerActivityActions.kt:42)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:76)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:20)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActionsKt.picker(PickerActivityActions.kt:9)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest.launchPickerForAllTest(GalleryInstruTest.kt:72)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
Caused by: junit.framework.AssertionFailedError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)









share|improve this question
























  • Could you post the error?
    – Aaron
    Nov 12 at 9:17










  • @Aaron I added error log
    – John
    Nov 13 at 1:45













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Hi my grid recycler view's 0th item should be always camera and
other items should be always video contents.



To check 0th item has layout of com.xx.yy.R.layout.item_camera
I tried like this:




onView(withRecyclerView(recyclerViewResId).atPosition(0)).check(matches(withId(com.xx.yy.R.layout.item_camera)))




But it doesn't work, someone knows what I miss?



This is helper class:



fun withRecyclerView(recyclerViewId: Int) = RecyclerViewTestHelper.RecyclerViewMatcher(recyclerViewId)

object RecyclerViewTestHelper {

class RecyclerViewMatcher(private val recyclerViewId: Int) {

fun atPosition(position: Int): Matcher<View> {
return atPositionOnView(position, -1)
}

fun atPositionOnView(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}

fun withResourceId(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}
}

class CustomAssertions {
companion object {
fun hasItemCount(count: Int): ViewAssertion {
return RecyclerViewItemCountAssertion(count)
}
}

private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}

if (view !is RecyclerView) {
throw IllegalStateException("The asserted view is not RecyclerView")
}

if (view.adapter == null) {
throw IllegalStateException("No adapter is assigned to RecyclerView")
}
ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))
}
}
}

class HintTextColorMatcher private constructor(private val color: Int) : BoundedMatcher<View, TextView>(TextView::class.java) {
override fun matchesSafely(item: TextView): Boolean {
return item.currentHintTextColor == color
}

override fun describeTo(description: Description) {
description.appendText("with hint text color:")
.appendValue(color)
}

companion object {
fun withHintTextColor(color: Int): HintTextColorMatcher {
return HintTextColorMatcher(color)
}
}
}
}


Error log:



android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.BaseActions.checkRecyclerViewItemRes(BaseActions.kt:46)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActions.clickFirstMediaItem(PickerActivityActions.kt:42)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:76)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:20)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActionsKt.picker(PickerActivityActions.kt:9)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest.launchPickerForAllTest(GalleryInstruTest.kt:72)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
Caused by: junit.framework.AssertionFailedError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)









share|improve this question















Hi my grid recycler view's 0th item should be always camera and
other items should be always video contents.



To check 0th item has layout of com.xx.yy.R.layout.item_camera
I tried like this:




onView(withRecyclerView(recyclerViewResId).atPosition(0)).check(matches(withId(com.xx.yy.R.layout.item_camera)))




But it doesn't work, someone knows what I miss?



This is helper class:



fun withRecyclerView(recyclerViewId: Int) = RecyclerViewTestHelper.RecyclerViewMatcher(recyclerViewId)

object RecyclerViewTestHelper {

class RecyclerViewMatcher(private val recyclerViewId: Int) {

fun atPosition(position: Int): Matcher<View> {
return atPositionOnView(position, -1)
}

fun atPositionOnView(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}

fun withResourceId(position: Int, targetViewId: Int): Matcher<View> {

return object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null

override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
try {
idDescription = this.resources!!.getResourceName(recyclerViewId)
} catch (var4: Resources.NotFoundException) {
idDescription = String.format("%s (resource name not found)",
*arrayOf<Any>(Integer.valueOf(recyclerViewId)))
}

}

description.appendText("with id: $idDescription")
}

override fun matchesSafely(view: View): Boolean {

this.resources = view.getResources()

if (childView == null) {
val recyclerView = view.getRootView().findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView!!.getId() === recyclerViewId) {
childView = recyclerView!!.findViewHolderForAdapterPosition(position).itemView
} else {
return false
}
}

if (targetViewId == -1) {
return view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
return view === targetView
}

}
}
}
}

class CustomAssertions {
companion object {
fun hasItemCount(count: Int): ViewAssertion {
return RecyclerViewItemCountAssertion(count)
}
}

private class RecyclerViewItemCountAssertion(private val count: Int) : ViewAssertion {

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}

if (view !is RecyclerView) {
throw IllegalStateException("The asserted view is not RecyclerView")
}

if (view.adapter == null) {
throw IllegalStateException("No adapter is assigned to RecyclerView")
}
ViewMatchers.assertThat("RecyclerView item count", view.adapter.itemCount, CoreMatchers.equalTo(count))
}
}
}

class HintTextColorMatcher private constructor(private val color: Int) : BoundedMatcher<View, TextView>(TextView::class.java) {
override fun matchesSafely(item: TextView): Boolean {
return item.currentHintTextColor == color
}

override fun describeTo(description: Description) {
description.appendText("with hint text color:")
.appendValue(color)
}

companion object {
fun withHintTextColor(color: Int): HintTextColorMatcher {
return HintTextColorMatcher(color)
}
}
}
}


Error log:



android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.BaseActions.checkRecyclerViewItemRes(BaseActions.kt:46)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActions.clickFirstMediaItem(PickerActivityActions.kt:42)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:76)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest$launchPickerForAllTest$2.invoke(GalleryInstruTest.kt:20)
at gallery.android.line.naver.jp.line_android_gallery.RobotActions.PickerActivityActionsKt.picker(PickerActivityActions.kt:9)
at gallery.android.line.naver.jp.line_android_gallery.GalleryInstruTest.launchPickerForAllTest(GalleryInstruTest.kt:72)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)
Caused by: junit.framework.AssertionFailedError: 'with id: 2131427448' doesn't match the selected view.
Expected: with id: jp.naver.line.android.beta:layout/media_content_item
Got: "RelativeLayout{id=-1, visibility=VISIBLE, width=477, height=476, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=964.0, y=3.0, child-count=10}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:1053)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)






android kotlin espresso






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 1:45

























asked Nov 12 at 9:02









John

9210




9210












  • Could you post the error?
    – Aaron
    Nov 12 at 9:17










  • @Aaron I added error log
    – John
    Nov 13 at 1:45


















  • Could you post the error?
    – Aaron
    Nov 12 at 9:17










  • @Aaron I added error log
    – John
    Nov 13 at 1:45
















Could you post the error?
– Aaron
Nov 12 at 9:17




Could you post the error?
– Aaron
Nov 12 at 9:17












@Aaron I added error log
– John
Nov 13 at 1:45




@Aaron I added error log
– John
Nov 13 at 1:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I think here's the problem:



onView(withRecyclerView(recyclerViewResId).atPosition(0))
.check(matches(withId(com.xx.yy.R.layout.item_camera)))


You're trying to match an id with a layout, which of course is incorrect. You can only use a resource id on withId, not others.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258793%2fespresso-how-to-check-an-item-from-recyclerview-consists-of-certain-layout%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I think here's the problem:



    onView(withRecyclerView(recyclerViewResId).atPosition(0))
    .check(matches(withId(com.xx.yy.R.layout.item_camera)))


    You're trying to match an id with a layout, which of course is incorrect. You can only use a resource id on withId, not others.






    share|improve this answer

























      up vote
      0
      down vote













      I think here's the problem:



      onView(withRecyclerView(recyclerViewResId).atPosition(0))
      .check(matches(withId(com.xx.yy.R.layout.item_camera)))


      You're trying to match an id with a layout, which of course is incorrect. You can only use a resource id on withId, not others.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I think here's the problem:



        onView(withRecyclerView(recyclerViewResId).atPosition(0))
        .check(matches(withId(com.xx.yy.R.layout.item_camera)))


        You're trying to match an id with a layout, which of course is incorrect. You can only use a resource id on withId, not others.






        share|improve this answer












        I think here's the problem:



        onView(withRecyclerView(recyclerViewResId).atPosition(0))
        .check(matches(withId(com.xx.yy.R.layout.item_camera)))


        You're trying to match an id with a layout, which of course is incorrect. You can only use a resource id on withId, not others.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 2:08









        Aaron

        1,7051212




        1,7051212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258793%2fespresso-how-to-check-an-item-from-recyclerview-consists-of-certain-layout%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine