Search in sources :

Example 11 with StringDescription

use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.

the class ViewInteraction method doPerform.

private void doPerform(final ViewAction viewAction) {
    checkNotNull(viewAction);
    final Matcher<? extends View> constraints = checkNotNull(viewAction.getConstraints());
    runSynchronouslyOnUiThread(new Runnable() {

        @Override
        public void run() {
            uiController.loopMainThreadUntilIdle();
            View targetView = viewFinder.getView();
            Log.i(TAG, String.format("Performing '%s' action on view %s", viewAction.getDescription(), viewMatcher));
            if (!constraints.matches(targetView)) {
                // TODO(user): update this to describeMismatch once hamcrest is updated to new
                StringDescription stringDescription = new StringDescription(new StringBuilder("Action will not be performed because the target view " + "does not match one or more of the following constraints:\n"));
                constraints.describeTo(stringDescription);
                stringDescription.appendText("\nTarget view: ").appendValue(HumanReadables.describe(targetView));
                if (viewAction instanceof ScrollToAction && isDescendantOfA(isAssignableFrom((AdapterView.class))).matches(targetView)) {
                    stringDescription.appendText("\nFurther Info: ScrollToAction on a view inside an AdapterView will not work. " + "Use Espresso.onData to load the view.");
                }
                throw new PerformException.Builder().withActionDescription(viewAction.getDescription()).withViewDescription(viewMatcher.toString()).withCause(new RuntimeException(stringDescription.toString())).build();
            } else {
                viewAction.perform(uiController, targetView);
            }
        }
    });
}
Also used : ScrollToAction(com.google.android.apps.common.testing.ui.espresso.action.ScrollToAction) StringDescription(org.hamcrest.StringDescription) AdapterView(android.widget.AdapterView) View(android.view.View) AdapterView(android.widget.AdapterView)

Example 12 with StringDescription

use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.

the class ViewAssertions method matches.

/**
   * Returns a generic {@link ViewAssertion} that asserts that a view exists in the view hierarchy
   * and is matched by the given view matcher.
   */
public static ViewAssertion matches(final Matcher<? super View> viewMatcher) {
    checkNotNull(viewMatcher);
    return new ViewAssertion() {

        @Override
        public void check(Optional<View> view, Optional<NoMatchingViewException> noViewException) {
            StringDescription description = new StringDescription();
            description.appendText("'");
            viewMatcher.describeTo(description);
            if (noViewException.isPresent()) {
                description.appendText(String.format("' check could not be performed because view '%s' was not found.\n", viewMatcher));
                Log.e(TAG, description.toString());
                throw noViewException.get();
            } else {
                // TODO(user): ideally, we should append the matcher used to find the view
                // This can be done in DefaultFailureHandler (just like we currently to with
                // PerformException)
                description.appendText("' doesn't match the selected view.");
                assertThat(description.toString(), view.get(), viewMatcher);
            }
        }
    };
}
Also used : ViewAssertion(com.google.android.apps.common.testing.ui.espresso.ViewAssertion) Optional(com.google.common.base.Optional) StringDescription(org.hamcrest.StringDescription)

Example 13 with StringDescription

use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.

the class ViewMatchers method assertThat.

/**
   * A replacement for MatcherAssert.assertThat that renders View objects nicely.
   *
   * @param message the message to display.
   * @param actual the actual value.
   * @param matcher a matcher that accepts or rejects actual.
   */
public static <T> void assertThat(String message, T actual, Matcher<T> matcher) {
    if (!matcher.matches(actual)) {
        Description description = new StringDescription();
        description.appendText(message).appendText("\nExpected: ").appendDescriptionOf(matcher).appendText("\n     Got: ");
        if (actual instanceof View) {
            description.appendValue(HumanReadables.describe((View) actual));
        } else {
            description.appendValue(actual);
        }
        description.appendText("\n");
        throw new AssertionFailedError(description.toString());
    }
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription) AssertionFailedError(junit.framework.AssertionFailedError) TextView(android.widget.TextView) View(android.view.View)

Example 14 with StringDescription

use of org.hamcrest.StringDescription in project cucumber-jvm by cucumber.

the class AbstractMatcherTest method assertDescription.

public static void assertDescription(String expected, Matcher<?> matcher) {
    Description description = new StringDescription();
    description.appendDescriptionOf(matcher);
    Assert.assertEquals("Expected description", expected, description.toString().trim());
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription)

Example 15 with StringDescription

use of org.hamcrest.StringDescription in project cucumber-jvm by cucumber.

the class AbstractMatcherTest method mismatchDescription.

public static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
    Description description = new StringDescription();
    matcher.describeMismatch(arg, description);
    return description.toString().trim();
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription)

Aggregations

StringDescription (org.hamcrest.StringDescription)32 Description (org.hamcrest.Description)22 Test (org.junit.Test)15 JsonPathMatchers.isJsonFile (com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonFile)3 JsonPathMatchers.isJsonString (com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonString)3 ResourceHelpers.resourceAsFile (com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile)3 File (java.io.File)3 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 Adapter (android.widget.Adapter)1 TextView (android.widget.TextView)1 ViewAssertion (com.google.android.apps.common.testing.ui.espresso.ViewAssertion)1 ScrollToAction (com.google.android.apps.common.testing.ui.espresso.action.ScrollToAction)1 Optional (com.google.common.base.Optional)1 AssertionFailedError (junit.framework.AssertionFailedError)1 AnnotatedElementDeclaration (org.freud.analysed.javasource.AnnotatedElementDeclaration)1 Annotation (org.freud.analysed.javasource.Annotation)1 FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)1