Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Add assertListNotEmpty (AdevintaSpain#288)
Browse files Browse the repository at this point in the history
* Add assertListHasItems

* Rename assertListHasItems to assertListNotEmpty
  • Loading branch information
adrielcafe authored and alorma committed Mar 27, 2019
1 parent fc3364a commit 6029033
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ clickListItem(R.id.list, 4);
clickListItemChild(R.id.list, 3, R.id.row_button);
scrollListToPosition(R.id.list, 4);
assertListItemCount(R.id.listId, 5)
assertListNotEmpty(R.id.listId)
assertDisplayedAtPosition(R.id.recycler, 0, "text");
assertDisplayedAtPosition(R.id.listId, 0, R.id.text_field, "text");
assertDisplayedAtPosition(R.id.recycler, 0, R.string.hello_world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import com.schibsted.spain.barista.interaction.BaristaListInteractions.findListV
import com.schibsted.spain.barista.interaction.BaristaListInteractions.findRecyclerMatcher
import com.schibsted.spain.barista.interaction.BaristaListInteractions.scrollListToPosition
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
import com.schibsted.spain.barista.internal.matcher.ListViewNotEmptyAssertion
import com.schibsted.spain.barista.internal.matcher.ListViewItemCountAssertion
import com.schibsted.spain.barista.internal.matcher.RecyclerViewNotEmptyAssertion
import com.schibsted.spain.barista.internal.matcher.RecyclerViewItemCountAssertion
import org.hamcrest.CoreMatchers
import org.hamcrest.Description
Expand Down Expand Up @@ -44,6 +46,25 @@ object BaristaListAssertions {
}
}

@JvmStatic
fun assertListNotEmpty(@IdRes listId: Int) {
val spyFailureHandler = SpyFailureHandler()
val recyclerMatcher = findRecyclerMatcher(listId)
val listViewMatcher = findListViewMatcher(listId)

try {
Espresso.onView(recyclerMatcher).withFailureHandler(spyFailureHandler).check(RecyclerViewNotEmptyAssertion())
} catch (noRecyclerMatching: NoMatchingViewException) {
try {
Espresso.onView(listViewMatcher).withFailureHandler(spyFailureHandler).check(ListViewNotEmptyAssertion())
} catch (listViewError: Throwable) {
spyFailureHandler.resendLastError("Item count mismatch on ListView. Expected one or more items in the list.")
}
} catch (recyclerError: Throwable) {
spyFailureHandler.resendLastError("Item count mismatch on RecyclerView. Expected one or more items in the list.")
}
}

@JvmStatic
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, text: String) {
assertDisplayedAtPosition(listId = listId, position = position, targetViewId = NO_VIEW_ID, text = text)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.schibsted.spain.barista.internal.matcher

import android.support.test.espresso.NoMatchingViewException
import android.support.test.espresso.ViewAssertion
import android.view.View
import android.widget.ListView
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.not

class ListViewNotEmptyAssertion : ViewAssertion {

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

val listView = view as ListView
val adapter = listView.adapter
val listViewItemCount = adapter.count
assertThat<Int>("List item count mismatch. ", listViewItemCount, not<Int>(0))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.schibsted.spain.barista.internal.matcher

import android.support.test.espresso.NoMatchingViewException
import android.support.test.espresso.ViewAssertion
import android.support.v7.widget.RecyclerView
import android.view.View
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.not

class RecyclerViewNotEmptyAssertion : ViewAssertion {

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

val recyclerView = view as RecyclerView
val adapter = recyclerView.adapter
val recyclerViewItemCount = adapter.itemCount
assertThat<Int>("Recycler item count mismatch. ", recyclerViewItemCount, not<Int>(0))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.support.test.runner.AndroidJUnit4
import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertCustomAssertionAtPosition
import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition
import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertListItemCount
import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertListNotEmpty
import com.schibsted.spain.barista.internal.failurehandler.BaristaException
import junit.framework.AssertionFailedError
import org.hamcrest.CoreMatchers
Expand Down Expand Up @@ -36,6 +37,12 @@ class ListViewAssertionTest {
assertListItemCount(R.id.listview, expectedListLength + 1)
}

@Test
fun shouldHaveEntriesInListView() {
openSimpleListActivity()
assertListNotEmpty(R.id.listview)
}

@Test
fun shouldFindItemInListViewWithoutIdInSimpleList() {
openSimpleListActivity()
Expand Down

0 comments on commit 6029033

Please sign in to comment.