Skip to content

Commit

Permalink
Write UI tests related to request enabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquimLey committed Apr 25, 2018
1 parent 9cf1116 commit da5ee1a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.filters.MediumTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.support.v7.widget.RecyclerView
import com.joaquimley.transporteta.R
import com.joaquimley.transporteta.presentation.data.Resource
import com.joaquimley.transporteta.presentation.home.favorite.FavoritesViewModel
Expand All @@ -21,6 +22,8 @@ import com.joaquimley.transporteta.ui.home.favorite.FavoritesFragment
import com.joaquimley.transporteta.ui.test.util.RecyclerViewMatcher
import com.joaquimley.transporteta.ui.testing.TestFragmentActivity
import com.joaquimley.transporteta.ui.testing.factory.TestFactoryFavoriteView
import com.joaquimley.transporteta.ui.util.extensions.findViewById
import com.nhaarman.mockito_kotlin.verify
import org.hamcrest.CoreMatchers.*
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -183,7 +186,7 @@ class FavoritesFragmentTest {
results.postValue(Resource.success(resultsList))
// Then check all items
for (favoriteView in resultsList.withIndex()) {
// Scroll to item INDEX
// Scroll to item favoriteView.index
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition<FavoritesAdapter.FavoriteViewHolder>(favoriteView.index))
// Check item is displayed correctly
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(favoriteView.index)).check(matches(hasDescendant(withText(resultsList[favoriteView.index].code.toString()))))
Expand All @@ -192,43 +195,78 @@ class FavoritesFragmentTest {
}
}

/**
* TODO: Not correctly implemented
*/
@Test
fun whenRequestButtonIsClickedViewModelRequestIsCalled() {
// Given
val resultsList = TestFactoryFavoriteView.generateFavoriteViewList()
results.postValue(Resource.success(resultsList))
// requestsAvailable.postValue(false)

`when`(viewModel.onEtaRequested(resultsList[0])).then{requestsAvailable.postValue(false)}
// When
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.scrollToPosition<FavoritesAdapter.FavoriteViewHolder>(0))

onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions
.actionOnItemAtPosition<FavoritesAdapter.FavoriteViewHolder>(0, click()))

onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(0))
.check(matches(hasDescendant(withText(R.string.action_send_sms)))).perform(click())
// Check requestEta was called
// verify(viewModel, times(1)).onEtaRequested(resultsList[0])
verify(viewModel).onEtaRequested(resultsList[0])
}

@Test
fun whenAcceptingRequestStateIsFalseRequestingTextIsShown() {
// Given (make sure requesting is not being shown)
requestsAvailable.postValue(true)
// When
requestsAvailable.postValue(false)
// Then Snackbar with requesting text and cancel action are shown
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.info_requesting))).check(matches(isDisplayed()))
onView(allOf(withId(android.support.design.R.id.snackbar_action), withText(R.string.action_cancel))).check(matches(isDisplayed()))
}

@Test
fun whenRequestButtonIsClickedRequestingTextIsShown() {
// Given
fun whenAcceptingRequestStateIsTrueSnackbarWithCancelActionIsDismissed() {
// Given (make sure requesting is showing)
requestsAvailable.postValue(false)
// When
requestsAvailable.postValue(true)
// Then Snackbar with requesting text and cancel action are not shown
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.info_requesting))).check(doesNotExist())
onView(allOf(withId(android.support.design.R.id.snackbar_action), withText(R.string.action_cancel))).check(doesNotExist())
}

@Test
fun whenAcceptingRequestStateIsFalseRequestSmsButtonsAreDisabled() {
// Given (make sure requesting is not being shown)
val resultsList = TestFactoryFavoriteView.generateFavoriteViewList()
results.postValue(Resource.success(resultsList))
// When
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(0))
.check(matches(hasDescendant(withText(R.string.action_send_sms)))).perform(click())
// Then show requesting text
onView(withText(R.string.info_requesting)).check(matches(isDisplayed()))
requestsAvailable.postValue(false)
// Then ALL request ETA buttons are disabled
favoritesFragment.findViewById<RecyclerView>(R.id.recycler_view)?.let {
RecyclerViewMatcher.waitForAdapterChange(it)
for (favoriteView in resultsList.withIndex()) {
// Scroll to item at favoriteView.index
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition<FavoritesAdapter.FavoriteViewHolder>(favoriteView.index))
// Check item is displayed correctly
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(favoriteView.index))
.check(matches(hasDescendant(withId(R.id.eta_button)))).check(matches(not(isEnabled())))
}
}
}
}


// https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/
// https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/
// https://medium.com/@_rpiel/recyclerview-and-espresso-a-complicated-story-3f6f4179652e
@Test
fun whenAcceptingRequestStateIsTrueRequestSmsButtonsAreEnabled() {
// Given (make sure requesting is not being shown)
val resultsList = TestFactoryFavoriteView.generateFavoriteViewList()
results.postValue(Resource.success(resultsList))
// When
requestsAvailable.postValue(true)
// Then ALL request ETA buttons are enabled
for (favoriteView in resultsList.withIndex()) {
// Scroll to item favoriteView.index
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(favoriteView.index))
.check(matches(hasDescendant(withId(R.id.eta_button)))).check(matches(isEnabled()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.joaquimley.transporteta.ui.test.util

import com.joaquimley.transporteta.ui.home.favorite.FavoritesAdapter
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

/**
* Matches the {@link CustomAdapter.ViewHolder}s in the middle of the list.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FavoritesFragment : Fragment() {
viewModel.getAcceptingRequests().observe(this,
Observer {
adapter.setActionEnabledStatus(it ?: true)
requestingSnackbar.show()
requestingSnackbar.setVisible(it)
})
}

Expand Down Expand Up @@ -103,7 +103,6 @@ class FavoritesFragment : Fragment() {
message_view?.setVisible(false)
recycler_view?.setVisible(true)
adapter.submitList(favoriteViewList)
requestingSnackbar.dismiss()
}

private fun setupScreenEmptyState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.joaquimley.transporteta.ui.util.extensions

import android.support.design.widget.Snackbar
import android.support.v7.recyclerview.extensions.ListAdapter
import android.support.v7.widget.RecyclerView
import android.text.Editable
Expand All @@ -17,6 +18,9 @@ fun View.setVisible(isVisible: Boolean) {
visibility = if (isVisible) View.VISIBLE else View.GONE
}

fun Snackbar.setVisible(isVisible: Boolean?) {
if(isVisible != true) this.show() else this.dismiss()
}
fun ImageView.load(resourceId: Int) {
Glide.with(context)
.load(resourceId)
Expand Down

0 comments on commit da5ee1a

Please sign in to comment.