Skip to content

Instantly share code, notes, and snippets.

@Egorand
Created July 2, 2016 09:44
Show Gist options
  • Save Egorand/c83ee614516d46c88e46fe6ee45540ee to your computer and use it in GitHub Desktop.
Save Egorand/c83ee614516d46c88e46fe6ee45540ee to your computer and use it in GitHub Desktop.
android-espresso-sorted-list-is-sorted-alphabetically
private static Matcher<View> isSortedAlphabetically() {
return new TypeSafeMatcher<View>() {
private final List<String> teamNames = new ArrayList<>();
@Override
protected boolean matchesSafely(View item) {
RecyclerView recyclerView = (RecyclerView) item;
TeamsAdapter teamsAdapter = (TeamsAdapter) recyclerView.getAdapter();
teamNames.clear();
teamNames.addAll(extractTeamNames(teamsAdapter.getTeams()));
return Ordering.natural().isOrdered(teamNames);
}
private List<String> extractTeamNames(List<Team> teams) {
List<String> teamNames = new ArrayList<>();
for (Team team : teams) {
teamNames.add(team.name);
}
return teamNames;
}
@Override
public void describeTo(Description description) {
description.appendText("has items sorted alphabetically: " + teamNames);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment