Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #94 from vacxe/recycler-view-child-count-impliment…
Browse files Browse the repository at this point in the history
…ation

Add child count assertation for recycler view
  • Loading branch information
Unlimity committed Apr 17, 2018
2 parents 9ac9986 + 39bae9d commit 914d9bc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
41 changes: 37 additions & 4 deletions kakao/src/main/kotlin/com/agoda/kakao/Assertions.kt
Expand Up @@ -20,10 +20,6 @@ import android.support.test.espresso.web.webdriver.DriverAtoms
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.Description
import android.view.View
import android.widget.RatingBar
import java.lang.AssertionError

/**
Expand Down Expand Up @@ -489,6 +485,43 @@ interface ViewPagerAssertions : BaseAssertions {
}
}

/**
* Provides assertions for view with adapters
*
* @see RecyclerAdapterAssertions
*/
interface AdapterAssertions {
val view: ViewInteraction
}

/**
* Provides assertions for recyclerView adapter
*/
interface RecyclerAdapterAssertions : AdapterAssertions {
/**
* Check size of recycler view
*
* @param size expected child count size in recycler view
*/
fun hasSize(size: Int) {
view.check(ViewAssertions.matches(RecyclerViewAdapterSizeMatcher(size)))
}
}

/**
* Provides assertions for listView adapter
*/
interface ListViewAdapterAssertions : AdapterAssertions {
/**
* Check size of recycler view
*
* @param size expected child count size in recycler view
*/
fun hasSize(size: Int) {
view.check(ViewAssertions.matches(ListViewViewAdapterSizeMatcher(size)))
}
}

/**
* Provides assertions for NavigationView
*/
Expand Down
33 changes: 29 additions & 4 deletions kakao/src/main/kotlin/com/agoda/kakao/Matchers.kt
Expand Up @@ -19,10 +19,7 @@ import android.support.v4.view.ViewPager
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RatingBar
import android.widget.TextView
import android.widget.*
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
Expand Down Expand Up @@ -192,6 +189,34 @@ class RatingBarMatcher(private val value: Float) : BoundedMatcher<View, RatingBa
}
}

/**
* Matches RecyclerView with count of childs
*
* @param size of childs count in RecyclerView
*/
class RecyclerViewAdapterSizeMatcher(private val size: Int) : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun matchesSafely(view: RecyclerView?) = view?.adapter?.let {
it.itemCount == size } ?: false

override fun describeTo(description: Description) {
description.appendText("recycle view size is: $size")
}
}

/**
* Matches ListView with count of childs
*
* @param size of childs count in ListView
*/
class ListViewViewAdapterSizeMatcher(private val size: Int) : BoundedMatcher<View, ListView>(ListView::class.java) {
override fun matchesSafely(view: ListView?) = view?.adapter?.let {
it.count == size } ?: false

override fun describeTo(description: Description) {
description.appendText("recycle view size is: $size")
}
}

/**
* Matches given drawable with current one
*
Expand Down
4 changes: 2 additions & 2 deletions kakao/src/main/kotlin/com/agoda/kakao/Views.kt
Expand Up @@ -340,7 +340,7 @@ class KTextInputLayout : KBaseView<KTextInputLayout>, TextInputLayoutAssertions
* @see KAdapterItem
* @see KAdapterItemTypeBuilder
*/
class KListView : ScrollViewActions, BaseAssertions {
class KListView : ScrollViewActions, BaseAssertions, ListViewAdapterAssertions {
val matcher: Matcher<View>
val itemTypes: Map<KClass<out KAdapterItem<*>>, KAdapterItemType<KAdapterItem<*>>>

Expand Down Expand Up @@ -509,7 +509,7 @@ class KListView : ScrollViewActions, BaseAssertions {
* @see KRecyclerItem
* @see KRecyclerItemTypeBuilder
*/
class KRecyclerView : RecyclerActions, BaseAssertions {
class KRecyclerView : RecyclerActions, BaseAssertions, RecyclerAdapterAssertions {
val matcher: Matcher<View>
val itemTypes: Map<KClass<out KRecyclerItem<*>>, KRecyclerItemType<KRecyclerItem<*>>>

Expand Down
3 changes: 2 additions & 1 deletion sample/src/androidTest/kotlin/com/agoda/sample/ListTest.kt
Expand Up @@ -20,6 +20,7 @@ class ListTest {
screen {
list {
isVisible()
hasSize(10)

firstChild<TestListScreen.Item> {
isVisible()
Expand Down Expand Up @@ -65,4 +66,4 @@ class ListTest {
}
}
}
}
}
Expand Up @@ -20,6 +20,7 @@ class RecyclerTest {
screen {
recycler {
isVisible()
hasSize(10)

firstChild<TestRecyclerScreen.Item> {
isVisible()
Expand Down Expand Up @@ -51,4 +52,4 @@ class RecyclerTest {
}
}
}
}
}

0 comments on commit 914d9bc

Please sign in to comment.