Skip to content

Commit

Permalink
Housekeeping, testing some RecyclerView espresso improvment
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquimLey committed Apr 25, 2018
1 parent cba2d46 commit 882d68f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 61 deletions.
2 changes: 1 addition & 1 deletion transport-eta-android/mobile-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ dependencies {
// Android unit tests
kaptAndroidTest deps.dagger.compiler
androidTestImplementation deps.javax.inject
androidTestImplementation deps.mockito.kotlin
androidTestImplementation deps.mockito.android
// androidTestImplementation deps.dexmaker.linkedin_inline
// Android Testing Support runner and rules and AAC
androidTestImplementation deps.atsl.rules
androidTestImplementation deps.atsl.runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ 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 org.hamcrest.CoreMatchers.not
import org.hamcrest.CoreMatchers.nullValue
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import org.hamcrest.CoreMatchers.*
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import java.util.regex.Matcher


@MediumTest
@RunWith(AndroidJUnit4::class)
class FavoritesFragmentTest {

@Rule @JvmField val activityRule = ActivityTestRule(TestFragmentActivity::class.java, false, true)
@Rule @JvmField
val activityRule = ActivityTestRule(TestFragmentActivity::class.java, false, true)
@Rule @JvmField val instantTaskExecutorRule = InstantTaskExecutorRule()

private val requestsAvailable = MutableLiveData<Boolean>()
private val results = MutableLiveData<Resource<List<FavoriteView>>>()
private val viewModel = mock(FavoritesViewModel::class.java)

Expand All @@ -49,6 +52,8 @@ class FavoritesFragmentTest {
// Init mock ViewModel
`when`(TestFavoriteFragmentModule.favoritesViewModelsFactory.create(FavoritesViewModel::class.java)).thenReturn(viewModel)
`when`(viewModel.getFavourites()).thenReturn(results)
`when`(viewModel.getAcceptingRequests()).thenReturn(requestsAvailable)

// Instantiate fragment and add to the TestFragmentActivity
favoritesFragment = FavoritesFragment.newInstance()
activityRule.activity.addFragment(favoritesFragment)
Expand Down Expand Up @@ -128,10 +133,9 @@ class FavoritesFragmentTest {
onView(withText(R.string.create_favorite_title)).check(matches(isDisplayed()))
onView(withId(R.id.favorite_code_edit_text)).check(matches(isDisplayed()))
onView(withId(R.id.favorite_title_edit_text)).check(matches(isDisplayed()))

// TODO Fix hints
// onView(withText(R.string.create_favorite_code_hint)).check(matches(isDisplayed()))
// onView(withText(R.string.create_favorite_title_hint)).check(matches(isDisplayed()))
// onView(withHint(R.string.create_favorite_code_hint)).check(matches(isDisplayed()))
// onView(withHint(R.string.create_favorite_title_hint)).check(matches(isDisplayed()))
// Dialog Action buttons
onView(withText(R.string.action_create)).check(matches(isDisplayed()))
onView(withText(R.string.action_discard)).check(matches(isDisplayed()))
Expand Down Expand Up @@ -176,7 +180,6 @@ class FavoritesFragmentTest {
onView(withId(R.id.favorite_code_edit_text)).check(matches(hasErrorText(nullValue(String::class.java))))
}


@Test
fun whenThereIsDataItIsCorrectlyDisplayedOnTheList() {
// When
Expand All @@ -192,6 +195,38 @@ class FavoritesFragmentTest {
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_view).atPosition(favoriteView.index)).check(matches(hasDescendant(withText(resultsList[favoriteView.index].originalText))))
}
}

@Test
fun whenRequestButtonIsClickedViewModelRequestIsCalled() {
// Given
val resultsList = TestFactoryFavoriteView.generateFavoriteViewList()
results.postValue(Resource.success(resultsList))
requestsAvailable.postValue(true)
// When
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.scrollToPosition<FavoritesAdapter.FavoriteViewHolder>(0))
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions
.actionOnHolderItem<FavoritesAdapter.FavoriteViewHolder>(, click()))
RecyclerViewActions.actionOnItemAtPosition<FavoritesAdapter.FavoriteViewHolder>(0, )
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])
}


@Test
fun whenRequestButtonIsClickedRequestingTextIsShown() {
// Given
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()))
}
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import android.support.test.espresso.matcher.RootMatchers.withDecorView
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.rule.ActivityTestRule
import org.hamcrest.CoreMatchers.not
import io.reactivex.internal.util.NotificationLite.getError



fun Int.click() = onView(withId(this)).perform(ViewActions.click())
fun Int.write(text: String) = onView(withId(this)).perform(typeText(text))
Expand All @@ -17,4 +20,19 @@ fun Int.textEquals(text: String) = onView(withId(this)).check(matches(withText(t
infix fun <T : Activity> ActivityTestRule<T>.containsToast(message: String) =
onView(withText(message))
.inRoot(withDecorView(not(activity.window.decorView)))
.check(matches(isDisplayed()))
.check(matches(isDisplayed()))


//private fun withError(expected: String): Matcher {
// return object : TypeSafeMatcher() {
// protected fun matchesSafely(item: View): Boolean {
// return if (item is EditText) {
// (item as EditText).getError().toString().equals(expected)
// } else false
// }
//
// fun describeTo(description: Description) {
// description.appendText("Not found error message [$expected]")
// }
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:paddingBottom="8dp"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:paddingBottom="8dp">

<ImageView
android:id="@+id/avatar_image_view"
Expand Down Expand Up @@ -84,12 +84,12 @@
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:elevation="9dp"
android:text="@string/action_send_sms"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card_view"
app:layout_constraintVertical_bias="0.0"
android:text="__Request ETA"/>
app:layout_constraintVertical_bias="0.0"/>

</android.support.constraint.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="nav_title_notifications">Notifications</string>

<!-- Actions -->
<string name="action_send_sms">Send SMS</string>
<string name="action_settings">Settings</string>
<string name="action_retry">Retry</string>
<string name="action_try_again">Try again</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class FavoritesViewModelImpl @Inject constructor(smsController: SmsController) :
private val favouritesLiveData = MutableLiveData<Resource<List<FavoriteView>>>()
private val acceptingRequestsLiveData = MutableLiveData<Boolean>()



init {
val currentValue = ArrayList<FavoriteView>()
currentValue.add(FavoriteView(1337, "This is mock data 1", isActionEnabled = true))
currentValue.add(FavoriteView(1338, "This is mock data 2", isActionEnabled = true))
currentValue.add(FavoriteView(1339, "This is mock data 3", isActionEnabled = true))
currentValue.add(FavoriteView(1330, "This is mock data 4", isActionEnabled = true))
currentValue.add(FavoriteView(1331, "This is mock data 5", isActionEnabled = true))
currentValue.add(FavoriteView(1332, "This is mock data 6", isActionEnabled = true))
currentValue.add(FavoriteView(1337, "This is mock data 1"))
currentValue.add(FavoriteView(1338, "This is mock data 2"))
currentValue.add(FavoriteView(1339, "This is mock data 3"))
currentValue.add(FavoriteView(1330, "This is mock data 4"))
currentValue.add(FavoriteView(1331, "This is mock data 5"))
currentValue.add(FavoriteView(1332, "This is mock data 6"))

favouritesLiveData.postValue(Resource.success(currentValue))
}
Expand All @@ -51,8 +53,8 @@ class FavoritesViewModelImpl @Inject constructor(smsController: SmsController) :
}

override fun cancelEtaRequest() {
smsController.invalidateRequest()
smsRequestDisposable?.dispose()
smsController.invalidateRequest()
acceptingRequestsLiveData.postValue(true)
}

Expand Down

0 comments on commit 882d68f

Please sign in to comment.