Skip to content

Commit

Permalink
Add assertNoError method side of assertError methods (#385)
Browse files Browse the repository at this point in the history
* Add NoError asserts

* Add failure cases

* Update readme

* Remove text param

* Rename methods

* Update readme

* Rollback old errors, redirwect to new ones
  • Loading branch information
alorma authored Mar 10, 2021
1 parent c00f0a6 commit 59c899f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 18 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,11 @@ assertHint(R.id.edittext, "Hint");

#### Check TextInputLayout and EditText's errors
```java
assertError(R.id.edittext, R.string.error);
assertError(R.id.edittext, "Error message");
assertErrorDisplayed(R.id.edittext, R.string.error);
assertErrorDisplayed(R.id.edittext, "Error message");

assertNoErrorDisplayed(R.id.edittext, R.string.error);
assertNoErrorDisplayed(R.id.edittext, "Error message");
```

#### Check TextInputLayout's assistive helper text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
package com.schibsted.spain.barista.assertion

import android.content.Context
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.google.android.material.textfield.TextInputLayout
import androidx.test.espresso.matcher.ViewMatchers
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.matcher.ViewMatchers
import com.google.android.material.textfield.TextInputLayout
import com.schibsted.spain.barista.internal.assertAny
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

object BaristaErrorAssertions {

@Deprecated(
message = "Use assertErrorDisplayed(id, text)",
replaceWith = ReplaceWith(
"assertErrorDisplayed(viewId, text)",
"com.schibsted.spain.barista.assertion.BaristaErrorAssertions.assertErrorDisplayed"
)
)
@JvmStatic
fun assertError(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = ApplicationProvider.getApplicationContext<Context>().resources.getString(text)
assertError(viewId, resourceString)
assertErrorDisplayed(viewId, text)
}

@Deprecated(
message = "Use assertErrorDisplayed(id, text)",
replaceWith = ReplaceWith(
"assertErrorDisplayed(viewId, text)",
"com.schibsted.spain.barista.assertion.BaristaErrorAssertions.assertErrorDisplayed"
)
)
@JvmStatic
fun assertError(@IdRes viewId: Int, text: String) {
assertErrorDisplayed(viewId, text)
}

@JvmStatic
fun assertErrorDisplayed(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = ApplicationProvider.getApplicationContext<Context>().resources.getString(text)
assertErrorDisplayed(viewId, resourceString)
}

@JvmStatic
fun assertErrorDisplayed(@IdRes viewId: Int, text: String) {
ViewMatchers.withId(viewId).assertAny(matchError(text))
}

@JvmStatic
fun assertNoErrorDisplayed(@IdRes viewId: Int) {
ViewMatchers.withId(viewId).assertAny(matchNoError())
}

private fun matchError(expectedError: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
Expand All @@ -43,4 +72,22 @@ object BaristaErrorAssertions {
}
}
}

private fun matchNoError(): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("without error")
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
is TextView -> item.error.isNullOrEmpty()
is TextInputLayout -> item.error.isNullOrEmpty()
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.internal.failurehandler.BaristaException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.schibsted.spain.barista.assertion.BaristaErrorAssertions.assertError;
import static com.schibsted.spain.barista.assertion.BaristaErrorAssertions.assertErrorDisplayed;
import static com.schibsted.spain.barista.assertion.BaristaErrorAssertions.assertNoErrorDisplayed;
import static com.schibsted.spain.barista.assertion.BaristaHintAssertions.assertHint;
import static com.schibsted.spain.barista.interaction.BaristaClickInteractions.clickOn;

@RunWith(AndroidJUnit4.class)
public class HintAndErrorTest {
Expand All @@ -31,15 +34,47 @@ public void assertHintByResource() {

@Test
public void assertErrorByString() {
assertError(R.id.hintanderror_inputlayout, "TextInputLayout error");
assertError(R.id.hintanderror_inputedittext, "TextInputEditText error");
assertError(R.id.hintanderror_edittext, "EditText error");
clickOn(R.id.showErrors);
assertErrorDisplayed(R.id.hintanderror_inputlayout, "TextInputLayout error");
assertErrorDisplayed(R.id.hintanderror_inputlayout, "TextInputLayout error");
assertErrorDisplayed(R.id.hintanderror_inputedittext, "TextInputEditText error");
assertErrorDisplayed(R.id.hintanderror_edittext, "EditText error");
}

@Test
public void assertErrorByResource() {
assertError(R.id.hintanderror_inputlayout, R.string.hintanderror_inputlayout_error);
assertError(R.id.hintanderror_inputedittext, R.string.hintanderror_inputedittext_error);
assertError(R.id.hintanderror_edittext, R.string.hintanderror_edittext_error);
clickOn(R.id.showErrors);
assertErrorDisplayed(R.id.hintanderror_inputlayout, R.string.hintanderror_inputlayout_error);
assertErrorDisplayed(R.id.hintanderror_inputedittext, R.string.hintanderror_inputedittext_error);
assertErrorDisplayed(R.id.hintanderror_edittext, R.string.hintanderror_edittext_error);
}

@Test(expected = BaristaException.class)
public void assertErrorByStringFails() {
assertErrorDisplayed(R.id.hintanderror_inputlayout, "TextInputLayout error");
assertErrorDisplayed(R.id.hintanderror_inputedittext, "TextInputEditText error");
assertErrorDisplayed(R.id.hintanderror_edittext, "EditText error");
}

@Test(expected = BaristaException.class)
public void assertErrorByResourceFails() {
assertErrorDisplayed(R.id.hintanderror_inputlayout, R.string.hintanderror_inputlayout_error);
assertErrorDisplayed(R.id.hintanderror_inputedittext, R.string.hintanderror_inputedittext_error);
assertErrorDisplayed(R.id.hintanderror_edittext, R.string.hintanderror_edittext_error);
}

@Test
public void assertNoErrorByResource() {
assertNoErrorDisplayed(R.id.hintanderror_inputlayout);
assertNoErrorDisplayed(R.id.hintanderror_inputedittext);
assertNoErrorDisplayed(R.id.hintanderror_edittext);
}

@Test(expected = BaristaException.class)
public void assertNoErrorByResourceFails() {
clickOn(R.id.showErrors);
assertNoErrorDisplayed(R.id.hintanderror_inputlayout);
assertNoErrorDisplayed(R.id.hintanderror_inputedittext);
assertNoErrorDisplayed(R.id.hintanderror_edittext);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.schibsted.spain.barista.sample;

import android.os.Bundle;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.EditText;

public class HintAndErrorActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hintanderrortext);

showError();
findViewById(R.id.showErrors).setOnClickListener(v -> showError());
}

private void showError() {
Expand Down
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_hintanderrortext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
android:padding="16dp"
>

<com.google.android.material.button.MaterialButton
android:id="@+id/showErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show errrors"
/>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/hintanderror_inputlayout"
android:layout_width="match_parent"
Expand Down

0 comments on commit 59c899f

Please sign in to comment.