Last active
February 3, 2017 19:46
-
-
Save mwinters-stuff/cdab4556cfe2f7ea563d to your computer and use it in GitHub Desktop.
Espresso for Android - Finding Listviews by Tag for onData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Object> withSettingItemContent(String expectedText) { | |
checkNotNull(expectedText); | |
return withSettingItemContent(equalTo(expectedText)); | |
} | |
@SuppressWarnings("rawtypes") | |
public static Matcher<Object> withSettingItemContent(final Matcher<String> itemTextMatcher) { | |
checkNotNull(itemTextMatcher); | |
return new BoundedMatcher<Object, SettingItem>(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<Object> withDialogListContent(String expectedText) { | |
checkNotNull(expectedText); | |
return withDialogListContent(equalTo(expectedText)); | |
} | |
@SuppressWarnings("rawtypes") | |
public static Matcher<Object> withDialogListContent(final Matcher<String> itemTextMatcher) { | |
checkNotNull(itemTextMatcher); | |
return new BoundedMatcher<Object, String>(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<Object> withStringMatching(String expectedText) { | |
checkNotNull(expectedText); | |
return withStringMatching(equalTo(expectedText)); | |
} | |
@SuppressWarnings("rawtypes") | |
public static Matcher<Object> withStringMatching(final Matcher<String> itemTextMatcher) { | |
checkNotNull(itemTextMatcher); | |
return new BoundedMatcher<Object, String>(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); | |
} | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nz.org.winters.android.nzmobileaccountwidget.tests.setup; | |
// NOTE THIS IS INCOMPLETE, ONLY AND EXAMPLE | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.graphics.Color; | |
import android.preference.Preference; | |
import android.preference.PreferenceManager; | |
import android.support.test.InstrumentationRegistry; | |
import android.support.test.espresso.DataInteraction; | |
import android.support.test.espresso.action.ViewActions; | |
import android.support.test.espresso.matcher.BoundedMatcher; | |
import android.support.test.espresso.matcher.ViewMatchers; | |
import android.support.test.rule.ActivityTestRule; | |
import android.support.test.runner.AndroidJUnit4; | |
import android.util.Log; | |
import android.widget.CheckedTextView; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import java.text.MessageFormat; | |
import static android.support.test.espresso.Espresso.onData; | |
import static android.support.test.espresso.Espresso.onView; | |
import static android.support.test.espresso.Espresso.pressBack; | |
import static android.support.test.espresso.action.ViewActions.clearText; | |
import static android.support.test.espresso.action.ViewActions.click; | |
import static android.support.test.espresso.action.ViewActions.typeText; | |
import static android.support.test.espresso.assertion.ViewAssertions.matches; | |
import static android.support.test.espresso.matcher.PreferenceMatchers.withKey; | |
import static android.support.test.espresso.matcher.PreferenceMatchers.withSummary; | |
import static android.support.test.espresso.matcher.PreferenceMatchers.withSummaryText; | |
import static android.support.test.espresso.matcher.ViewMatchers.assertThat; | |
import static android.support.test.espresso.matcher.ViewMatchers.hasSibling; | |
import static android.support.test.espresso.matcher.ViewMatchers.isChecked; | |
import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | |
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled; | |
import static android.support.test.espresso.matcher.ViewMatchers.withChild; | |
import static android.support.test.espresso.matcher.ViewMatchers.withClassName; | |
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; | |
import static android.support.test.espresso.matcher.ViewMatchers.withId; | |
import static android.support.test.espresso.matcher.ViewMatchers.withParent; | |
import static android.support.test.espresso.matcher.ViewMatchers.withText; | |
import static com.google.android.apps.common.testing.deps.guava.base.Preconditions.checkNotNull; | |
import static nz.org.winters.android.widgetscommon.tests.CustomMatchers.onSettingRow; | |
import static nz.org.winters.android.widgetscommon.tests.CustomMatchers.withSettingItemContent; | |
import static org.hamcrest.Matchers.allOf; | |
import static org.hamcrest.Matchers.equalTo; | |
import static org.hamcrest.Matchers.instanceOf; | |
import static org.hamcrest.Matchers.is; | |
import static org.hamcrest.Matchers.not; | |
import static org.hamcrest.Matchers.notNullValue; | |
/** | |
* Created by mathew on 5/05/15. | |
*/ | |
@SuppressWarnings({"unchecked", "FieldCanBeLocal"}) | |
@RunWith(AndroidJUnit4.class) | |
public class AccountSettingsTest { | |
private static final String TITLE_ACCOUNTS = "Accounts"; | |
private static final String ACCOUNT_1_NAME = "Bob Smith"; | |
private static final String ACCOUNT_1_USERNAME = "BOBSUSERNAME"; | |
private static final String ACCOUNT_1_PASSWORD = "would you like to know"; | |
private static final String COLOUR_ORANGE_CONTENT_DESCRIPTION = "Colour 7"; | |
private static final int COLOUR_ORANGE = 0xffff8800; | |
private static final String COLOUR_CONTENT_DESCRIPTION = "Colour "; | |
//SettingGroupListActivity_.intent(this).accountId(mAccountId).appWidgetId(mAppWidgetId).startForResult(REQUEST_CODE_SETTINGS_FINISHED); | |
@SuppressWarnings("unchecked") | |
@Before | |
public void onBefore(){ | |
assertThat(InstrumentationRegistry.getContext(), is(notNullValue())); | |
assertThat(InstrumentationRegistry.getTargetContext(), is(notNullValue())); | |
Intent intent = new Intent(); | |
intent.putExtra(SettingGroupListActivity_.ACCOUNT_ID_EXTRA, accountId1); | |
testCase.launchActivity(intent); | |
context = testCase.getActivity(); | |
preferences = PreferenceManager.getDefaultSharedPreferences(context); | |
} | |
@Rule | |
public ActivityTestRule<SettingGroupListActivity_> testCase = new ActivityTestRule<>(SettingGroupListActivity_.class, false, false); | |
private Context context; | |
SharedPreferences preferences; | |
@SuppressWarnings("unchecked") | |
@Test() | |
public void testAccountSettings() { | |
onView(allOf(withParent(withId(R.id.toolbar)), withText(R.string.settings))).check(matches(isDisplayed())); | |
onSettingRow(SettingsConstants.PREF_GENERAL).check(matches(isCompletelyDisplayed())); | |
onSettingRow(SettingsConstants.PREF_ACCOUNTS).check(matches(isCompletelyDisplayed())); | |
onSettingRow(SettingsConstants.PREF_LICENSE).check(matches(isCompletelyDisplayed())); | |
onPreferenceRow(SettingsConstants.PREF_USE_WIFI_ONLY).check(matches(isDisplayed())); | |
onPreferenceRow(SettingsConstants.PREF_USE_WIFI_ONLY).onChildView(withClassName(is(Switch.class.getName()))).check(matches(not(isChecked()))); | |
onPreferenceRow(SettingsConstants.PREF_NUMBERFORMATLOCALE).check(matches(isDisplayed())); | |
onPreferenceRow(SettingsConstants.PREF_UPDATE_WHEN).check(matches(isDisplayed())); | |
onPreferenceRow(SettingsConstants.PREF_QUIET_TIME).check(matches(isDisplayed())); | |
onPreferenceRow(SettingsConstants.PREF_ERROR_REPORTING).check(matches(isDisplayed())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment