Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tests so that they expect button to the disabled #1098 #1099

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ class LoginActivityTest {
* with:
* username: EMPTY
* password: EMPTY
* expected: Empty username and password error
* expected: Login button disabled
*/
@Test
fun testLoginButtonClickedWhenUsernameAndPasswordAreEmpty() {
enterCredentials("", "")

// Checks that the error message in both the editTexts appears after button click
onView(withId(R.id.tiUsername)).check(matches(hasTextInputLayoutErrorText(EMPTY_USERNAME_ERROR)))
onView(withId(R.id.tiPassword)).check(matches(hasTextInputLayoutErrorText(EMPTY_PASSWORD_ERROR)))
// Checks that the login button is disabled
onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))

}

Expand All @@ -133,31 +132,28 @@ class LoginActivityTest {
* with:
* username: EMPTY
* password: PRESENT
* expected : Empty username error
* expected : Login button disabled
*/
@Test
fun testLoginButtonClickedWhenUsernameIsEmptyAndPasswordIsFilled() {
enterCredentials("", CORRECT_TEST_PASSWORD)

// Check for error message on username and not on password
onView(withId(R.id.tiUsername)).check(matches(hasTextInputLayoutErrorText(EMPTY_USERNAME_ERROR)))
onView(withId(R.id.tiPassword)).check(matches(not(hasTextInputLayoutErrorText(EMPTY_PASSWORD_ERROR))))
onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))
}

/**
* This test checks that error messages are shown after the Login button is clicked
* with:
* username: PRESENT, correct
* password: EMPTY
* expected: empty password error
* expected: login button disabled
*/
@Test
fun testLoginButtonClickedWhenUsernameIsFilledAndPasswordIsEmpty() {
enterCredentials(CORRECT_TEST_USERNAME, "")

// Check for no error message on username with an error message on password
onView(withId(R.id.tiUsername)).check(matches(not(hasTextInputLayoutErrorText(EMPTY_USERNAME_ERROR))))
onView(withId(R.id.tiPassword)).check(matches(hasTextInputLayoutErrorText(EMPTY_PASSWORD_ERROR)))
onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))
}

/**
Expand Down Expand Up @@ -230,10 +226,10 @@ class LoginActivityTest {
*/
private fun enterCredentials(username: String, password: String) {
// Type in a username
findEditTextInTextInputLayout(R.id.tiUsername).perform(typeText(username), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiUsername).perform(replaceText(username), closeSoftKeyboard())

// Type in a password
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText(password), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiPassword).perform(replaceText(password), closeSoftKeyboard())

// Perform a click on the Login Button
onView(withId(R.id.btnLogin)).perform(click())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.platform.app.InstrumentationRegistry
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -75,7 +72,7 @@ class SignUpActivityTest {
*/
fun findEditTextInTextInputLayout(@IdRes textInputLayoutId: Int): ViewInteraction {

return Espresso.onView(Matchers.allOf(ViewMatchers.isDescendantOfA(ViewMatchers.withId(textInputLayoutId)),ViewMatchers.isAssignableFrom(EditText::class.java)))
return onView(Matchers.allOf(isDescendantOfA(withId(textInputLayoutId)), isAssignableFrom(EditText::class.java)))
}


Expand Down Expand Up @@ -127,8 +124,8 @@ class SignUpActivityTest {
}

/**
* This test method is defined to test that when all the fields are empty and the user
* clicks the signUp button then the errors in all the fields are displayed or not
* Input : All fields are empty
* Expected : Button is disabled
*/
@Test
fun testSignUpClickedWhenAllFieldsAreEmpty() {
Expand All @@ -142,12 +139,7 @@ class SignUpActivityTest {

onView(withId(R.id.btnSignUp)).perform(scrollTo(), click())

onView(withId(R.id.tiName)).check(matches(hasTextInputLayoutErrorText(EMPTY_NAME_ERROR)))
onView(withId(R.id.tiUsername)).check(matches(hasTextInputLayoutErrorText(EMPTY_USERNAME_ERROR)))
onView(withId(R.id.tiEmail)).check(matches(hasTextInputLayoutErrorText(EMPTY_EMAIL_ERROR)))
onView(withId(R.id.tiPassword)).check(matches(hasTextInputLayoutErrorText(EMPTY_PASSWORD_ERROR)))
onView(withId(R.id.tiConfirmPassword)).check(matches(hasTextInputLayoutErrorText(EMPTY_CONFIRM_PASSWORD_ERROR)))
onView(withId(R.id.tvNoteSignUp)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
onView(withId(R.id.btnSignUp)).check(matches(CoreMatchers.not(isEnabled())))

}

Expand All @@ -163,6 +155,7 @@ class SignUpActivityTest {
findEditTextInTextInputLayout(R.id.tiEmail).perform(typeText("[email protected]"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText("qwertz123!"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiConfirmPassword).perform(typeText("qwertz123!45"), closeSoftKeyboard())
onView(withId(R.id.cbMentor)).perform(click())
onView(withId(R.id.cbTC)).perform(click())

onView(withId(R.id.btnSignUp)).perform(scrollTo(), click())
Expand All @@ -183,7 +176,7 @@ class SignUpActivityTest {
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText("qwertz123!"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiConfirmPassword).perform(typeText("qwertz123!"), closeSoftKeyboard())
onView(withId(R.id.cbTC)).perform(click())

onView(withId(R.id.cbMentor)).perform(click())
onView(withId(R.id.btnSignUp)).perform(scrollTo(), click())

onView(withId(R.id.tiPassword)).check(matches(hasTextInputLayoutErrorText(TOO_WEAK_PASSWORD_ERROR)))
Expand All @@ -201,6 +194,7 @@ class SignUpActivityTest {
findEditTextInTextInputLayout(R.id.tiEmail).perform(typeText("[email protected]"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText("Qwertz123"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiConfirmPassword).perform(typeText("Qwertz123"), closeSoftKeyboard())
onView(withId(R.id.cbMentor)).perform(click())
onView(withId(R.id.cbTC)).perform(click())

onView(withId(R.id.btnSignUp)).perform(scrollTo(), click())
Expand All @@ -225,10 +219,10 @@ class SignUpActivityTest {
@Test
fun whenUsernameIsAlreadyRegistered() {
findEditTextInTextInputLayout(R.id.tiName).perform(typeText("name"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiUsername).perform(typeText("Divyansh"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiEmail).perform(typeText("justdvnsh2208@gmail.com"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText("Divyansh@2001"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiConfirmPassword).perform(typeText("Divyansh@2001"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiUsername).perform(typeText("isabel"), closeSoftKeyboard())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isabelcosta sorry I couldn't really think of any other username so I used your, if we do end up creating a user solely for testing we can update it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username is fine ;)

findEditTextInTextInputLayout(R.id.tiEmail).perform(typeText("random_emailid@gmail.com"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiPassword).perform(typeText("Password@2001"), closeSoftKeyboard())
findEditTextInTextInputLayout(R.id.tiConfirmPassword).perform(typeText("Password@2001"), closeSoftKeyboard())
onView(withId(R.id.cbBoth)).perform(click())
onView(withId(R.id.cbTC)).perform(click())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import org.hamcrest.CoreMatchers
import org.hamcrest.Description
Expand All @@ -23,6 +24,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.systers.mentorship.R
import org.systers.mentorship.view.activities.SettingsActivity
import kotlin.concurrent.thread

@RunWith(AndroidJUnit4ClassRunner::class)
class ChangePasswordFragmentTest {
Expand All @@ -49,19 +51,13 @@ class ChangePasswordFragmentTest {
/*
* This test checks that errors are being displayed correctly when the password is too weak
* */
@Test
/*@Test
fun check_if_errors_are_shown_when_password_is_too_weak() {
// launch the fragment
ChangePasswordFragment.newInstance().show(activityTestRule.activity.supportFragmentManager, null)

enter_credentials(R.id.tilNewPassword, "ab")

// clicks the OK button in the Dialog Fragment
onView(withText(R.string.ok)).inRoot(isDialog())
.check(matches(isDisplayed())).perform(click())

// Checks if the errors are showing with the correct text
onView(withId(R.id.tilNewPassword)).check(matches(hasTextInputLayoutErrorText(PASSWORD_TOO_WEAK)))
onView(withId(R.id.tietCurrentPassword)).perform(replaceText("SomePassword"))
enter_credentials(R.id.tilNewPassword, "blah")
.check(matches(hasTextInputLayoutErrorText(PASSWORDS_DO_NOT_MATCH)))
}

/*
Expand All @@ -73,25 +69,25 @@ class ChangePasswordFragmentTest {
ChangePasswordFragment.newInstance().show(activityTestRule.activity.supportFragmentManager, null)

enter_credentials(R.id.tilNewPassword,"AnitaB.org@2020")
enter_credentials(R.id.tilConfirmPassword, "AnitaB.org")

// clicks the OK button in the Dialog Fragment
onView(withText(R.string.ok)).inRoot(isDialog())
.check(matches(isDisplayed())).perform(click())
enter_credentials(R.id.tietConfirmPassword, "AnitaB.org")

// Checks if the errors are showing with the correct text
onView(withId(R.id.tilConfirmPassword)).check(matches(hasTextInputLayoutErrorText(PASSWORDS_DO_NOT_MATCH)))
// onView(withId(R.id.tietConfirmPassword)).perform(replaceText("555"))
}

// --- Helper Methods --- //

/*
* helper method to enter password
* */
private fun enter_credentials(id: Int, password: String) = findEditTextInTextInputLayout(id).run {
private fun enter_credentials(id: Int, password: String): ViewInteraction = findEditTextInTextInputLayout(id).run {
check(matches(isDisplayed()))
perform(click())
perform(typeText(password))
perform(replaceText(password))
}
private fun enter_credentials1(id: Int, password: String){
onView(withId(id))
.perform(replaceText(password))

}

// helper method to find Edit Text
Expand All @@ -104,7 +100,28 @@ class ChangePasswordFragmentTest {
* Helper method
* This simply implements the null check, checks the type and then casts.
*/
fun hasTextInputLayoutErrorText(expectedErrorText : String) : Matcher<View> {

companion object {
val matcher = object : TypeSafeMatcher<View>(){
override fun describeTo(description: Description?) {
TODO("Not yet implemented")
}

override fun matchesSafely(item: View?): Boolean {
if ((item !is TextInputLayout)) {
return false
}

val error = item.error ?: return false

val errorMsg: String = error.toString()
return "somethext" == errorMsg
}

}
}

private fun hasTextInputLayoutErrorText(expectedErrorText : String) : Matcher<View> {

return object : TypeSafeMatcher<View>() {
/**
Expand All @@ -129,15 +146,12 @@ class ChangePasswordFragmentTest {
return false
}

val error = item.error
if (error == null) {
return false
}
val error = item.error ?: return false

val errorMsg: String = error.toString()
return expectedErrorText.equals(errorMsg)
return expectedErrorText == errorMsg
}
}
}

*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,18 @@ class SignUpActivity : BaseActivity() {
tiPassword.error = null
}

if (password != confirmedPassword) {
tiConfirmPassword.error = getString(R.string.error_not_matching_passwords)
isValid = false
} else if (confirmedPassword.isBlank()) {
tiConfirmPassword.error = getString(R.string.error_empty_password_confirmation)
isValid = false
} else {
tiConfirmPassword.error = null
when {
password != confirmedPassword -> {
tiConfirmPassword.error = getString(R.string.error_not_matching_passwords)
isValid = false
}
confirmedPassword.isBlank() -> {
tiConfirmPassword.error = getString(R.string.error_empty_password_confirmation)
isValid = false
}
else -> {
tiConfirmPassword.error = null
}
}

if (!needsMentoring && !isAvailableToMentor && !isAvailableForBoth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ChangePasswordFragment : DialogFragment() {
private lateinit var confirmPassword: String

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
changePasswordViewModel.successfulUpdate.observe(this, Observer { successful ->
changePasswordViewModel.successfulUpdate.observe(this, { successful ->

if (successful != null) {
when {
Expand Down Expand Up @@ -84,18 +84,18 @@ class ChangePasswordFragment : DialogFragment() {

// Runtime check New Password & ConfirmPassword
passwordDialog?.tilNewPassword?.editText?.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {

}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
validatePassword();
}

override fun afterTextChanged(s: Editable?) {

}

})
passwordDialog?.tilConfirmPassword?.editText?.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/fragment_change_password.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
app:passwordToggleEnabled="true">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tietCurrentPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
Expand All @@ -41,6 +42,7 @@
app:passwordToggleEnabled="true">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tietNewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
Expand All @@ -62,6 +64,7 @@
app:passwordToggleEnabled="true">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tietConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
Expand Down