-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recyclerview 0 children, can't test viewholder click #3747
Comments
Doing the measure manually fixed the issue. It would still be nice if this was not required.
|
Getting the same issue on Robolectric 4.1, @JustinTullgren suggested solution works. |
I'm hoping this issue will be fixed by the fix for #4153 which I've been working on. Robolectric currently performs layout assuming the window is zero-size, which leads to a bunch of inconsistencies when sizing TextViews, RecyclerViews etc. |
Same issue on Roboelectric 4.2 |
Still seeing this issue on Robolectric 4.3.1 |
Just calling |
What is the Robolectric version you are using? |
This is how I handle it here testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation 'androidx.test.espresso:espresso-core:3.3.0' test code @Test
fun `should show all buttons and digit text content is empty when launch OplusDialpadFragment with open screen`() {
// GIVEN
val screenWidth = 1080
val screenHeight = 1920
// Manually load the soft keyboard interface into the activity
activityRule.scenario.onActivity {
it.supportFragmentManager
.beginTransaction()
.add(R.id.vp_screen_center, OplusDialpadFragment(), OplusDialpadFragment::class.simpleName)
.commitNow()
}
// WHEN
activityRule.scenario.onActivity {
it.supportFragmentManager
.findFragmentByTag(OplusDialpadFragment::class.simpleName)
?.view
?.findViewById<RecyclerView>(R.id.rv_dialpad_buttons)
?.run {
measure(
makeMeasureSpec(screenWidth, View.MeasureSpec.EXACTLY),
makeMeasureSpec(screenHeight, View.MeasureSpec.AT_MOST),
)
layout(0, 0, screenWidth, screenHeight)
}
}
Shadows.shadowOf(Looper.getMainLooper()).idle()
// THEN
for ((number, _) in (DialpadViewModel.DIALPAD_NUMBERS_LETTERS)) {
onView(withText(number.toString())).check(matches(isEnabled()))
}
} The key code is here measure(
makeMeasureSpec(screenWidth, View.MeasureSpec.EXACTLY),
makeMeasureSpec(screenHeight, View.MeasureSpec.AT_MOST),
)
layout(0, 0, screenWidth, screenHeight) Finally ensure that the test cases can cover the source code |
Perhaps, you can refer to this article |
Description
Calling
notifyDataSetChanged()
and callingActivityController.visible()
does not update the recyclerview childcount or returns null forrecycler.findViewHolderForAdapterPosition(0)
I tried follow steps in this Stackoverflow post which mentioned the visible method. The other results didn't work either.
The activity works and loads fine.
I am not sure its a bug or just a misunderstanding on my part. I tried looking at the github samples and articles on line but they all had similar answers (draw the recycler manually) or were very shallow.
Steps to Reproduce: Code below(modified to not show domain details)
Activity
Test
Robolectric & Android Version
Robolectric: 3.6.1
Android compileSdkVersion: 26,
android minSdkVersion : 21,
android targetSdkVersion : 26
The text was updated successfully, but these errors were encountered: