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

Commit

Permalink
Add new features in BaristaListAssertions (AdevintaSpain#280)
Browse files Browse the repository at this point in the history
* Support textId in BaristaListAssertions

* Add custom assertion in BaristaListAssertions

* Update BaristaListAssertions' documentation

* Use the custom matcher function

* Add tests for new BaristaListAssertions's assertions
  • Loading branch information
zhenleiji authored and alorma committed Mar 1, 2019
1 parent 08ce731 commit 534b975
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ scrollListToPosition(R.id.list, 4);
assertListItemCount(R.id.listId, 5)
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);
assertDisplayedAtPosition(R.id.listId, 0, R.id.text_field, R.string.hello_world);
assertCustomAssertionAtPosition(R.id.recycler, 0, customViewAssertion);

clickSpinnerItem(R.id.spinner, 1);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.schibsted.spain.barista.assertion

import android.support.annotation.IdRes
import android.support.annotation.StringRes
import android.support.test.espresso.Espresso
import android.support.test.espresso.NoMatchingViewException
import android.support.test.espresso.ViewAssertion
import android.support.test.espresso.assertion.ViewAssertions
import android.support.test.espresso.matcher.ViewMatchers
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ListView
import com.schibsted.spain.barista.interaction.BaristaListInteractions
import com.schibsted.spain.barista.interaction.BaristaListInteractions.findListViewMatcher
import com.schibsted.spain.barista.interaction.BaristaListInteractions.findRecyclerMatcher
import com.schibsted.spain.barista.interaction.BaristaListInteractions.scrollListToPosition
Expand Down Expand Up @@ -50,10 +53,49 @@ object BaristaListAssertions {
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, text: String) {
scrollListToPosition(listId, position)

assertCustomAssertionAtPosition(
listId = listId,
position = position,
targetViewId = targetViewId,
viewAssertion = ViewAssertions.matches(
CoreMatchers.anyOf(
ViewMatchers.withChild(ViewMatchers.withText(text)),
ViewMatchers.withText(text)
)
)
)
}

@JvmStatic
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @StringRes textId: Int) {
assertDisplayedAtPosition(listId = listId, position = position, targetViewId = NO_VIEW_ID, textId = textId)
}

@JvmStatic
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, @StringRes textId: Int) {
BaristaListInteractions.scrollListToPosition(listId, position)

assertCustomAssertionAtPosition(
listId = listId,
position = position,
targetViewId = targetViewId,
viewAssertion = ViewAssertions.matches(
CoreMatchers.anyOf(
ViewMatchers.withChild(ViewMatchers.withText(textId)),
ViewMatchers.withText(textId)
)
)
)
}

@JvmStatic
fun assertCustomAssertionAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, viewAssertion: ViewAssertion) {
BaristaListInteractions.scrollListToPosition(listId, position)

Espresso.onView(atPositionOnList(listId = listId,
position = position,
targetViewId = targetViewId))
.check(ViewAssertions.matches(CoreMatchers.anyOf(ViewMatchers.withChild(ViewMatchers.withText(text)), ViewMatchers.withText(text))))
.check(viewAssertion)
}

private fun atPositionOnList(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int): Matcher<View> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.schibsted.spain.barista.sample

import android.support.test.InstrumentationRegistry
import android.support.test.espresso.assertion.ViewAssertions
import android.support.test.espresso.matcher.ViewMatchers
import android.support.test.rule.ActivityTestRule
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.internal.failurehandler.BaristaException
import junit.framework.AssertionFailedError
import org.hamcrest.CoreMatchers
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -53,19 +57,87 @@ class ListViewAssertionTest {
@Test
fun shouldFindItemInListViewWithoutIdInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(R.id.listview, 86, "Tamarind")
assertDisplayedAtPosition(listId = R.id.listview, position = 86, text = "Tamarind")
}

@Test
fun shouldFindItemInListViewWithIdInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(R.id.listview, 19, R.id.textview, "Dragonfruit")
assertDisplayedAtPosition(listId = R.id.listview, position = 19, targetViewId = R.id.textview, text = "Dragonfruit")
}

@Test(expected = AssertionFailedError::class)
fun shouldFailWhenUnableToFindItemInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(R.id.listview, 86, "Missing")
assertDisplayedAtPosition(listId = R.id.listview, position = 86, text = "Missing")
}

@Test
fun shouldFindItemByIdInListViewWithoutIdInSimpleList() {
openSimpleListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 2, textId = R.string.avocado)
}

@Test
fun shouldFindByIdItemInListViewWithIdInSimpleList() {
openSimpleListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 4, targetViewId = android.R.id.text1, textId = R.string.bilberry)
}

@Test(expected = AssertionFailedError::class)
fun shouldFailWhenUnableToFindItemByIdInSimpleList() {
openSimpleListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 2, textId = R.string.not_there)
}

@Test
fun shouldFindItemByIdInListViewWithoutIdInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 86, textId = R.string.tamarind)
}

@Test
fun shouldFindItemByIdInListViewWithIdInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 19, targetViewId = R.id.textview, textId = R.string.dragonfruit)
}

@Test(expected = AssertionFailedError::class)
fun shouldFailWhenUnableToFindItemByIdInComplexList() {
openComplexListActivity()
assertDisplayedAtPosition(listId = R.id.listview, position = 86, textId = R.string.missing)
}

@Test
fun shouldFindItemInListViewWithCustomAssertionInSimpleList() {
openSimpleListActivity()
assertCustomAssertionAtPosition(
listId = R.id.listview,
position = 4,
targetViewId = android.R.id.text1,
viewAssertion = ViewAssertions.matches(
CoreMatchers.anyOf(
ViewMatchers.withChild(ViewMatchers.withText("Bilberry")),
ViewMatchers.withText("Bilberry")
)
)
)
}

@Test
fun shouldFindItemInListViewWithCustomAssertionInComplexList() {
openComplexListActivity()
assertCustomAssertionAtPosition(
listId = R.id.listview,
position = 19,
targetViewId = R.id.textview,
viewAssertion = ViewAssertions.matches(
CoreMatchers.anyOf(
ViewMatchers.withChild(ViewMatchers.withText("Dragonfruit")),
ViewMatchers.withText("Dragonfruit")
)
)
)
}

private fun openActivity(intentBuilder: ListsActivity.IntentBuilder) {
Expand Down
9 changes: 9 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@
<string name="hintanderror_inputedittext_error">TextInputEditText error</string>
<string name="hintanderror_edittext_hint">EditText hint</string>
<string name="hintanderror_edittext_error">EditText error</string>

<string name="avocado">Avocado</string>
<string name="bilberry">Bilberry</string>
<string name="tamarind">Tamarind</string>
<string name="dragonfruit">Dragonfruit</string>

<string name="not_there">NotThere</string>
<string name="missing">Missing</string>

</resources>

0 comments on commit 534b975

Please sign in to comment.