Skip to content

Commit

Permalink
Merge pull request #69 from AppDevNext/RecyclerViewItemCountAssertion
Browse files Browse the repository at this point in the history
RecyclerViewItemCountAssertion
  • Loading branch information
hannesa2 authored May 12, 2021
2 parents 92f173b + 9ba1810 commit 7c9a4a6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.moka.lib.assertions

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.ViewAssertion
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers

class RecyclerViewItemCountAssertion(private val expectedCount: Int, private val operator: MatchOperator) : ViewAssertion {

override fun check(view: View?, noViewFoundException: NoMatchingViewException?) {
noViewFoundException?.let {
throw it
}
val recyclerView = view as RecyclerView
val adapter = recyclerView.adapter
when (operator) {
MatchOperator.IS -> MatcherAssert.assertThat(adapter!!.itemCount, Matchers.`is`(expectedCount))
MatchOperator.LESS -> MatcherAssert.assertThat(adapter!!.itemCount, Matchers.lessThan(expectedCount))
MatchOperator.LESS_EQUAL -> MatcherAssert.assertThat(adapter!!.itemCount, Matchers.lessThanOrEqualTo(expectedCount))
MatchOperator.GREATER -> MatcherAssert.assertThat(adapter!!.itemCount, Matchers.greaterThan(expectedCount))
MatchOperator.GREATER_EQUAL -> MatcherAssert.assertThat(adapter!!.itemCount, Matchers.greaterThanOrEqualTo(expectedCount))
}

}
}
24 changes: 13 additions & 11 deletions sample/src/androidTest/java/com/sample/app/RecyclerViewTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.sample.app

import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.moka.EspressoMoka.onView
import com.moka.lib.assertions.MatchOperator
import com.moka.lib.assertions.WaitingAssertion
import com.moka.lib.assertions.RecyclerViewItemCountAssertion
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -14,33 +16,33 @@ class RecyclerViewTest {
@get:Rule
var activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun testRecyclerViewItemCount() {
WaitingAssertion.assertAdapterMinimumItemsCount(R.id.recyclerTest, 2, 500)
}

@Test
fun testRecyclerViewItemCountEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.IS, 500)
onView(ViewMatchers.withId(R.id.recyclerTest))
.check(RecyclerViewItemCountAssertion(3, MatchOperator.IS))
}

@Test
fun testRecyclerViewItemCountLessEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.LESS_EQUAL, 500)
onView(ViewMatchers.withId(R.id.recyclerTest))
.check(RecyclerViewItemCountAssertion(3, MatchOperator.LESS_EQUAL))
}

@Test
fun testRecyclerViewItemCountLess() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 4, MatchOperator.LESS, 500)
onView(ViewMatchers.withId(R.id.recyclerTest))
.check(RecyclerViewItemCountAssertion(4, MatchOperator.LESS))
}

@Test
fun testRecyclerViewItemCountGreater() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 2, MatchOperator.GREATER, 500)
onView(ViewMatchers.withId(R.id.recyclerTest))
.check(RecyclerViewItemCountAssertion(2, MatchOperator.GREATER))
}

@Test
fun testRecyclerViewItemCountGreaterEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.GREATER_EQUAL, 500)
onView(ViewMatchers.withId(R.id.recyclerTest))
.check(RecyclerViewItemCountAssertion(3, MatchOperator.GREATER_EQUAL))
}
}
46 changes: 46 additions & 0 deletions sample/src/androidTest/java/com/sample/app/RecyclerViewWaitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sample.app

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.moka.lib.assertions.MatchOperator
import com.moka.lib.assertions.WaitingAssertion
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class RecyclerViewWaitTest {

@get:Rule
var activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun testRecyclerViewWaitItemCount() {
WaitingAssertion.assertAdapterMinimumItemsCount(R.id.recyclerTest, 2, 500)
}

@Test
fun testRecyclerViewWaitItemCountEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.IS, 500)
}

@Test
fun testRecyclerViewWaitItemCountLessEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.LESS_EQUAL, 500)
}

@Test
fun testRecyclerViewWaitItemCountLess() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 4, MatchOperator.LESS, 500)
}

@Test
fun testRecyclerViewWaitItemCountGreater() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 2, MatchOperator.GREATER, 500)
}

@Test
fun testRecyclerViewWaitItemCountGreaterEqual() {
WaitingAssertion.assertRecyclerAdapterItemsCount(R.id.recyclerTest, 3, MatchOperator.GREATER_EQUAL, 500)
}
}

0 comments on commit 7c9a4a6

Please sign in to comment.