package nz.org.winters.android.widgetscommon.tests; import android.support.test.espresso.DataInteraction; import android.support.test.espresso.matcher.BoundedMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; import nz.org.winters.android.widgetscommon.settings.FABPreferenceFragment; import nz.org.winters.android.widgetscommon.settings.SettingGroupListFragment; import nz.org.winters.android.widgetscommon.settings.SettingItem; import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.matcher.ViewMatchers.withTagValue; import static com.google.android.apps.common.testing.deps.guava.base.Preconditions.checkNotNull; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; /** * Created by MWINTERS on 12/05/2015. */ public class CustomMatchers { public static String upCase(String in){ return in.substring(0,1).toUpperCase() + in.substring(1); } public static DataInteraction onSettingRow(String str) { DataInteraction interaction = onData(allOf( is(instanceOf(SettingItem.class)), withSettingItemContent(upCase(str)))); return interaction .inAdapterView(withTagValue(withStringMatching(SettingGroupListFragment.LIST_TAG))); } public static DataInteraction onPreferenceRow(String str) { DataInteraction interaction = onData(allOf( is(instanceOf(Preference.class)), withKey(str))); return interaction .inAdapterView(withTagValue(withStringMatching(FABPreferenceFragment.LIST_TAG))); } public static Matcher withSettingItemContent(String expectedText) { checkNotNull(expectedText); return withSettingItemContent(equalTo(expectedText)); } @SuppressWarnings("rawtypes") public static Matcher withSettingItemContent(final Matcher itemTextMatcher) { checkNotNull(itemTextMatcher); return new BoundedMatcher(SettingItem.class) { @Override public boolean matchesSafely(SettingItem settingItem) { return itemTextMatcher.matches(settingItem.title); } @Override public void describeTo(Description description) { description.appendText("with SettingItem title: "); itemTextMatcher.describeTo(description); } }; } public static Matcher withDialogListContent(String expectedText) { checkNotNull(expectedText); return withDialogListContent(equalTo(expectedText)); } @SuppressWarnings("rawtypes") public static Matcher withDialogListContent(final Matcher itemTextMatcher) { checkNotNull(itemTextMatcher); return new BoundedMatcher(String.class) { @Override public boolean matchesSafely(String value){ return itemTextMatcher.matches(value); } @Override public void describeTo(Description description) { description.appendText("with Dialog List Content: "); itemTextMatcher.describeTo(description); } }; } public static Matcher withStringMatching(String expectedText) { checkNotNull(expectedText); return withStringMatching(equalTo(expectedText)); } @SuppressWarnings("rawtypes") public static Matcher withStringMatching(final Matcher itemTextMatcher) { checkNotNull(itemTextMatcher); return new BoundedMatcher(String.class) { @Override public boolean matchesSafely(String string) { return itemTextMatcher.matches(string); } @Override public void describeTo(Description description) { description.appendText("with string: "); itemTextMatcher.describeTo(description); } }; } }