Search in sources :

Example 31 with StringDescription

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

the class AdapterDataLoaderAction method perform.

@SuppressWarnings("unchecked")
@Override
public void perform(UiController uiController, View view) {
    AdapterView<? extends Adapter> adapterView = (AdapterView<? extends Adapter>) view;
    List<AdapterViewProtocol.AdaptedData> matchedDataItems = Lists.newArrayList();
    for (AdapterViewProtocol.AdaptedData data : adapterViewProtocol.getDataInAdapterView(adapterView)) {
        if (dataToLoadMatcher.matches(data.data)) {
            matchedDataItems.add(data);
        }
    }
    if (matchedDataItems.size() == 0) {
        StringDescription dataMatcherDescription = new StringDescription();
        dataToLoadMatcher.describeTo(dataMatcherDescription);
        if (matchedDataItems.isEmpty()) {
            dataMatcherDescription.appendText(" contained values: ");
            dataMatcherDescription.appendValue(adapterViewProtocol.getDataInAdapterView(adapterView));
            throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException("No data found matching: " + dataMatcherDescription)).build();
        }
    }
    synchronized (dataLock) {
        checkState(!performed, "perform called 2x!");
        performed = true;
        if (atPosition.isPresent()) {
            int matchedDataItemsSize = matchedDataItems.size() - 1;
            if (atPosition.get() > matchedDataItemsSize) {
                throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException(String.format("There are only %d elements that matched but requested %d element.", matchedDataItemsSize, atPosition.get()))).build();
            } else {
                adaptedData = matchedDataItems.get(atPosition.get());
            }
        } else {
            if (matchedDataItems.size() != 1) {
                StringDescription dataMatcherDescription = new StringDescription();
                dataToLoadMatcher.describeTo(dataMatcherDescription);
                throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException("Multiple data elements " + "matched: " + dataMatcherDescription + ". Elements: " + matchedDataItems)).build();
            } else {
                adaptedData = matchedDataItems.get(0);
            }
        }
    }
    int requestCount = 0;
    while (!adapterViewProtocol.isDataRenderedWithinAdapterView(adapterView, adaptedData)) {
        if (requestCount > 1) {
            if ((requestCount % 50) == 0) {
                // sometimes an adapter view will receive an event that will block its attempts to scroll.
                adapterView.invalidate();
                adapterViewProtocol.makeDataRenderedWithinAdapterView(adapterView, adaptedData);
            }
        } else {
            adapterViewProtocol.makeDataRenderedWithinAdapterView(adapterView, adaptedData);
        }
        uiController.loopMainThreadForAtLeast(100);
        requestCount++;
    }
}
Also used : StringDescription(org.hamcrest.StringDescription) AdapterView(android.widget.AdapterView) Adapter(android.widget.Adapter)

Example 32 with StringDescription

use of org.hamcrest.StringDescription in project intellij-plugins by JetBrains.

the class MatcherAssert method assertThat.

public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
    if (!matcher.matches(actual)) {
        Description description = new StringDescription();
        description.appendText(reason).appendText("\nExpected: ").appendDescriptionOf(matcher).appendText("\n     but: ");
        matcher.describeMismatch(actual, description);
        throw new AssertionError(description.toString());
    }
}
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