Search in sources :

Example 41 with Description

use of org.hamcrest.Description in project dobby-android by InceptAi.

the class WifiExpertUITests method childAtPosition.

private static Matcher<View> childAtPosition(final Matcher<View> parentMatcher, final int position) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) ViewGroup(android.view.ViewGroup) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView)

Example 42 with Description

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

the class ViewMatchers method hasImeAction.

/**
   * Returns a matcher that matches views that support input methods (e.g. EditText) and have the
   * specified IME action set in its {@link EditorInfo}.
   *
   * @param imeActionMatcher a matcher for the IME action
   */
public static Matcher<View> hasImeAction(final Matcher<Integer> imeActionMatcher) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("has ime action: ");
            imeActionMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            EditorInfo editorInfo = new EditorInfo();
            InputConnection inputConnection = view.onCreateInputConnection(editorInfo);
            if (inputConnection == null) {
                return false;
            }
            int actionId = editorInfo.actionId != 0 ? editorInfo.actionId : editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION;
            return imeActionMatcher.matches(actionId);
        }
    };
}
Also used : InputConnection(android.view.inputmethod.InputConnection) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) EditorInfo(android.view.inputmethod.EditorInfo) TextView(android.widget.TextView) View(android.view.View)

Example 43 with Description

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

the class ViewMatchers method isDisplayingAtLeast.

/**
   * Returns a matcher which accepts a view so long as a given percentage of that view's area is
   * not obscured by any other view and is thus visible to the user.
   *
   * @param areaPercentage an integer ranging from (0, 100] indicating how much percent of the
   *   surface area of the view must be shown to the user to be accepted.
   */
public static Matcher<View> isDisplayingAtLeast(final int areaPercentage) {
    checkState(areaPercentage <= 100, "Cannot have over 100 percent: %s", areaPercentage);
    checkState(areaPercentage > 0, "Must have a positive, non-zero value: %s", areaPercentage);
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText(String.format("at least %s percent of the view's area is displayed to the user.", areaPercentage));
        }

        @Override
        public boolean matchesSafely(View view) {
            Rect visibleParts = new Rect();
            boolean visibleAtAll = view.getGlobalVisibleRect(visibleParts);
            if (!visibleAtAll) {
                return false;
            }
            double maxArea = view.getHeight() * view.getWidth();
            double visibleArea = visibleParts.height() * visibleParts.width();
            int displayedPercentage = (int) ((visibleArea / maxArea) * 100);
            return displayedPercentage >= areaPercentage && withEffectiveVisibility(Visibility.VISIBLE).matches(view);
        }
    };
}
Also used : Rect(android.graphics.Rect) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) TextView(android.widget.TextView) View(android.view.View)

Example 44 with Description

use of org.hamcrest.Description 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 45 with Description

use of org.hamcrest.Description in project protoman by spotify.

the class MatcherHelperBase method matchesSafely.

@Override
protected boolean matchesSafely(final T item, final Description mismatchDescription) {
    boolean ret = true;
    mismatchDescription.appendText(className).appendText(" {\n");
    for (final Matcher<T> matcher : matchers) {
        if (!matcher.matches(item)) {
            mismatchDescription.appendText("  ");
            Description innerDescription = new StringDescription();
            matcher.describeMismatch(item, innerDescription);
            indentDescription(mismatchDescription, innerDescription);
            ret = false;
        }
    }
    mismatchDescription.appendText("}");
    return ret;
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription)

Aggregations

Description (org.hamcrest.Description)122 Test (org.junit.Test)38 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)35 StringDescription (org.hamcrest.StringDescription)29 BaseMatcher (org.hamcrest.BaseMatcher)27 View (android.view.View)22 ViewParent (android.view.ViewParent)11 TextView (android.widget.TextView)11 ViewGroup (android.view.ViewGroup)8 Expectations (org.jmock.Expectations)8 URL (java.net.URL)7 Matcher (org.hamcrest.Matcher)7 Invocation (org.jmock.api.Invocation)7 BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 ImageView (android.widget.ImageView)6 File (java.io.File)6 IOException (java.io.IOException)6 URI (java.net.URI)6 List (java.util.List)6 JsonNode (org.codehaus.jackson.JsonNode)6