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++;
}
}
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());
}
}
Aggregations