Skip to content

Commit

Permalink
Create tests related to the Favorite creation
Browse files Browse the repository at this point in the history
- AlertDialog shown on Fab pressed
- AlertDialog is dismissed when Discard pressed
- AlertDialog shows error when user doesn't input the required BusStop code
- AlertDialog hides error when user starts typing BusStop code

No styling on the views, just basic UI implementation.

For #29
  • Loading branch information
JoaquimLey committed Apr 8, 2018
1 parent 7646963 commit 77907b1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.joaquimley.transporteta.home

import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.clearText
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.*
import android.support.test.espresso.assertion.ViewAssertions.doesNotExist
import android.support.test.espresso.assertion.ViewAssertions.matches
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 com.joaquimley.transporteta.R
import com.joaquimley.transporteta.home.favorite.FavoritesFragment
import org.hamcrest.CoreMatchers.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -20,6 +22,12 @@ import org.junit.runner.RunWith
class FavoritesFragmentTest {

@get:Rule val activityRule = ActivityTestRule(HomeActivity::class.java)
lateinit var favoritesFragment: FavoritesFragment

@Before
fun setup() {
favoritesFragment = activityRule.activity.supportFragmentManager.fragments[0] as FavoritesFragment
}

@Test
fun fabIsShown() {
Expand All @@ -28,7 +36,10 @@ class FavoritesFragmentTest {

@Test
fun whenFabIsClickedCreateFavoriteScreenIsShown() {
// When
onView(withId(R.id.fab)).perform(click())

// Check dialog is showing
onView(withText("Create favorite")).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()))
Expand All @@ -38,19 +49,36 @@ class FavoritesFragmentTest {

@Test
fun inCreateFavoriteDialogWhenUserClicksDiscardDialogIsDismissed() {
onView(withId(R.id.fab)).perform(click())
onView(withText("Discard")).perform(click())
onView(withText("Create favorite")).check(matches(not(isDisplayed())))
onView(withText("Create favorite")).check(doesNotExist())
}

@Test
fun inCreateFavoriteDialogWhenUserClicksCreateWithNoCodeErrorMessageIsShown() {
onView(withId(R.id.fab)).perform(click())

onView(withId(R.id.favorite_code_edit_text)).perform(clearText())
onView(withText("Create")).perform(click())

// Dialog is not dismissed
onView(withText("Create favorite")).check(matches(isDisplayed()))

// Error message is shown
onView(withText("Please input bus stop code")).check(matches(isDisplayed()))
}

@Test
fun inCreateFavoriteDialogWhenUserStarsTypingCodeErrorMessageShouldHide() {
// Show error message
onView(withId(R.id.fab)).perform(click())
onView(withId(R.id.favorite_code_edit_text)).perform(clearText())
onView(withText("Create")).perform(click())

// Start typing
onView(withId(R.id.favorite_code_edit_text)).perform(typeText("1337"))

// Error message is hidden
onView(withText("Please input bus stop code")).check(doesNotExist())
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -62,31 +64,48 @@ class FavoritesFragment : Fragment() {
}

private fun showAddFavoriteDialog() {
val currentContext = context
if (currentContext != null) {
val builder = AlertDialog.Builder(currentContext)
if (activity != null) {
val builder = AlertDialog.Builder(activity!!)
builder.setTitle(R.string.create_favorite_title)
builder.setView(R.layout.dialog_create_favorite)
builder.setPositiveButton(getString(R.string.action_create), null)
builder.setNegativeButton(getString(R.string.action_discard), null)
val dialog = builder.create()

dialog.show()
val busStopCodeInputLayout: TextInputLayout? = dialog.findViewById(R.id.favorite_code_text_input_layout)
val busStopCodeEditText: TextInputEditText? = dialog.findViewById(R.id.favorite_code_edit_text)
val busStopTitleEditText: TextInputEditText? = dialog.findViewById(R.id.favorite_title_edit_text)

val busStopCodeEditText: TextInputEditText? = dialog.findViewById(R.id.favorite_code_edit_text)
busStopCodeEditText?.addTextChangedListener(object: TextWatcher{
override fun afterTextChanged(s: Editable?) {
if(!TextUtils.isEmpty(s)) {
busStopCodeInputLayout?.error = null
}
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


})

dialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.action_create)) { dialog, which ->
if(TextUtils.isEmpty(busStopCodeEditText?.text)) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
if (TextUtils.isEmpty(busStopCodeEditText?.text)) {
busStopCodeInputLayout?.error = "Please input bus stop code"
}
}

dialog.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.action_discard)) { dialog, which ->
// track discard action
context?.let { ContextCompat.getColor(it, R.color.colorLightGrey) }?.let { dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(it) }
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
dialog.dismiss()
}

dialog.show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(currentContext, R.color.colorLightGrey))


}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down Expand Up @@ -33,7 +32,7 @@
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout">
app:layout_constraintTop_toBottomOf="@+id/favorite_code_text_input_layout">

<android.support.design.widget.TextInputEditText
android:id="@+id/favorite_title_edit_text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"/>
Expand Down

0 comments on commit 77907b1

Please sign in to comment.