Skip to content

Commit

Permalink
Improve assertion tests and errors (#242)
Browse files Browse the repository at this point in the history
* Fix and improve tests for visibility assertions

Some of them were a bit confussing because of mixing things.
I separated them, give consistent names between them, and added assertions for error types and messages.

Until know we didn't care about what errors we return from methods. That's about to change, my boy!

* Fixed AssertAny error handling

It was too simplistic, producing wrong or inconsistent error messages and types.

Now, we ALWAYS produce a BaristaException with an appropiate message

This change fixes the tests broken in the previous commit, because they test more things.

* Use AssertJ catchThrowable

* Add SpyFailureHandlerRule to visibility assertion tests

* Improve error messages in tests

* Update exception types

* Update exception types

* Improved names of catched exception

* Fix checktyle issues
  • Loading branch information
Sloy authored Jul 4, 2018
1 parent e679cf1 commit 8d5d8f7
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.schibsted.spain.barista.internal

import android.support.test.espresso.AmbiguousViewMatcherException
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.NoMatchingViewException
import android.support.test.espresso.assertion.ViewAssertions
import android.view.View
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
Expand All @@ -27,12 +29,17 @@ fun assertAnyView(viewMatcher: Matcher<View>, condition: Matcher<View>) {
val spyFailureHandler = SpyFailureHandler()
try {
tryToAssert(viewMatcher, condition, spyFailureHandler)
} catch (firstError: RuntimeException) {
} catch (multipleViewsError: AmbiguousViewMatcherException) {
try {
tryToAssertFirstView(viewMatcher, condition, spyFailureHandler)
} catch (secondError: RuntimeException) {
spyFailureHandler.resendFirstError("View ${viewMatcher.description()} wasn't displayed on the screen")
} catch (noneMatchedError: Throwable) {
spyFailureHandler.resendFirstError(
"None of the views matching (${viewMatcher.description()}) did match the condition (${condition.description()})")
}
} catch (singleViewNotFoundError: NoMatchingViewException) {
spyFailureHandler.resendFirstError("No view matching (${viewMatcher.description()}) was found")
} catch (singleViewMatchError: Throwable) {
spyFailureHandler.resendFirstError("View (${viewMatcher.description()}) didn't match condition (${condition.description()})")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.schibsted.spain.barista.internal.failurehandler.BaristaException;
import com.schibsted.spain.barista.internal.util.BaristaResourceTypeException;

import junit.framework.AssertionFailedError;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -37,117 +34,7 @@ public class AssertionsTest {

@Rule
public ActivityTestRule<SomeViewsWithDifferentVisibilitiesActivity> activityRule =
new ActivityTestRule<>(SomeViewsWithDifferentVisibilitiesActivity.class);

@Test
public void checkVisibleViews() {
assertDisplayed(R.id.visible_view);

assertDisplayed(R.string.hello_world);
assertDisplayed("Hello world!");
}

@Test
public void checkVisibleViews_breaksWhenNeeded() {
try {
assertDisplayed(R.id.invisible_view);
fail();
} catch (Throwable expected) {
}
try {
assertDisplayed(R.string.unknown);
fail();
} catch (Throwable expected) {
}
try {
assertDisplayed("Unknown");
fail();
} catch (Throwable expected) {
}
}

@Test
public void checkVisible_withRepeatedViews() throws Exception {
assertNotDisplayed(R.id.repeated_view_1_gone);

assertDisplayed("Repeated");
assertDisplayed(R.string.repeated);
}

@Test
public void checkExpectedText() throws Exception {
assertDisplayed(R.id.visible_view, "Hello world!");
}

@Test(expected = AssertionFailedError.class)
public void checkExpectedText_failsWhenTextIsNotTheExpected() throws Exception {
assertDisplayed(R.id.visible_view, "This is not the text you are looking for");
}

@Test
public void checkNotExpectedText() throws Exception {
assertNotDisplayed(R.id.visible_view, "This text must not be displayed on the view");
}

@Test(expected = AssertionFailedError.class)
public void checkNotExpectedText_failsWhenTextIsDisplayedOnTheView() throws Exception {
assertNotDisplayed(R.id.visible_view, "Hello world!");
}

@Test
public void checkInvisibleViews() {
assertNotDisplayed(R.id.invisible_view);
assertNotDisplayed(R.id.gone_view);

assertNotDisplayed(R.string.im_invisible);
assertNotDisplayed("I'm invisible!");
}

@Test
public void checkInvisibleViews_breaksWhenNeeded() {
try {
assertNotDisplayed(R.id.visible_view);
fail();
} catch (Throwable expected) {
}
try {
assertNotDisplayed(R.string.hello_world);
fail();
} catch (Throwable expected) {
}
try {
assertNotDisplayed("Hello world!");
fail();
} catch (Throwable expected) {
}
}

@Test
public void checkUnexistingView() {
assertNotExist(R.id.view_in_another_layout);

assertNotExist(R.string.unknown);
assertNotExist("Unknown");
}

@Test
public void checkUnexistingView_breaksWhenNeeded() {
try {
assertNotExist(R.id.visible_view);
fail();
} catch (Throwable expected) {
}
try {
assertNotExist(R.string.hello_world);
fail();
} catch (Throwable expected) {
}
try {
assertNotExist("Hello world!");
fail();
} catch (Throwable expected) {
}
}
new ActivityTestRule<>(SomeViewsWithDifferentVisibilitiesActivity.class);

@Test
public void checkEnabledView() {
Expand Down Expand Up @@ -318,7 +205,7 @@ public void checkDrawable_withId() throws Exception {
assertHasDrawable(R.id.image_view, R.drawable.ic_barista);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkDrawable_withId_failure() throws Exception {
assertHasDrawable(R.id.image_view, R.drawable.ic_action_menu);
}
Expand All @@ -328,7 +215,7 @@ public void checkDrawable_withAnyDrawable() throws Exception {
assertHasAnyDrawable(R.id.image_view);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkDrawable_withAnyDrawable_failure() throws Exception {
assertHasAnyDrawable(R.id.image_view_without_image);
}
Expand All @@ -338,7 +225,7 @@ public void checkDrawable_withoutDrawable() throws Exception {
assertHasNoDrawable(R.id.image_view_without_image);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkDrawable_withoutDrawable_failure() throws Exception {
assertHasNoDrawable(R.id.image_view);
}
Expand All @@ -348,7 +235,7 @@ public void checkBackground_withId() throws Exception {
assertHasBackground(R.id.view_with_backbround, R.drawable.ic_barista);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkBackground_withId_failure() throws Exception {
assertHasBackground(R.id.view_without_backbround, R.drawable.ic_action_menu);
}
Expand All @@ -358,7 +245,7 @@ public void checkBackground_withAnyDrawable() throws Exception {
assertHasAnyBackground(R.id.view_with_backbround);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkBackground_withAnyDrawable_failure() throws Exception {
assertHasAnyBackground(R.id.view_without_backbround);
}
Expand All @@ -368,7 +255,7 @@ public void checkBackground_withoutDrawable() throws Exception {
assertHasNoBackground(R.id.view_without_backbround);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkBackground_withoutDrawable_failure() throws Exception {
assertHasNoBackground(R.id.view_with_backbround);
}
Expand All @@ -392,7 +279,7 @@ public void checkTextViewContainsText_withViewId() {
assertContains(R.id.enabled_button, "Enabled");
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkTextViewContainsText_withViewId_failsWhenNeeded() {
assertContains(R.id.enabled_button, "Disabled");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.internal.failurehandler.BaristaException;
import com.schibsted.spain.barista.sample.util.FailureHandlerValidatorRule;
import junit.framework.AssertionFailedError;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -42,22 +42,22 @@ public void checkColorList_whenChecked() {
assertTextColorIs(R.id.textSelectorChecked, R.color.checked);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkSimpleColor_fails() {
assertTextColorIs(R.id.textRed, R.color.blue);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkColorList_whenDefault_fails() {
assertTextColorIs(R.id.textSelectorDefault, R.color.checked);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkColorList_whenDisabled_fails() {
assertTextColorIs(R.id.textSelectorDisabled, R.color.checked);
}

@Test(expected = AssertionFailedError.class)
@Test(expected = BaristaException.class)
public void checkColorList_whenChecked_fails() {
assertTextColorIs(R.id.textSelectorChecked, R.color.disabled);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void fails_whenRecyclerChildNotExist() {
.withRecyclers(R.id.recycler)
);

Throwable thrown = catchThrowable(() -> clickListItemChild(20, R.id.unknown));
Throwable thrown = catchThrowable(() -> clickListItemChild(20, R.id.not_exists));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
Expand All @@ -122,7 +122,7 @@ public void fails_whenListViewChildNotExist() {
.withComplexLists(R.id.listview)
);

Throwable thrown = catchThrowable(() -> clickListItemChild(20, R.id.unknown));
Throwable thrown = catchThrowable(() -> clickListItemChild(20, R.id.not_exists));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
Expand Down
Loading

0 comments on commit 8d5d8f7

Please sign in to comment.