Espresso for Android Hints: Properties

Testing properties is done with the espresso-contrib library.
Add to your gradle configuration. This is the dependencies I use:

dependencies {
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1',
'org.hamcrest:hamcrest-core:1.1',
'org.hamcrest:hamcrest-library:1.1'
androidTestCompile ('com.android.support.test:runner:0.2'){
exclude group: 'com.android.support', module: 'support-annotations' }
androidTestCompile ('com.android.support.test:rules:0.2'){
exclude group: 'com.android.support', module: 'support-annotations' }

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.1'){
exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'appcompat-v7' exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.1'){
exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'appcompat-v7' exclude group: 'com.android.support', module: 'support-annotations'
}

androidTestCompile ('com.android.support.test.espresso:espresso-intents:2.1'){
exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'appcompat-v7' exclude group: 'com.android.support', module: 'support-annotations'
}

}

Basics.

In your test you can use the "onData" with:

allOf
(is(instanceOf(Preference.class)), withKey("prefkey"))

As such, to check if a preference is displayed:

onData
(allOf(is(instanceOf(Preference.class)), withKey("prefkey"))).check(matches(isDisplayed()));

Is preference enabled.

To find if a preference is enabled, use the "isEnabled()" check:

onData
(allOf(is(instanceOf(Preference.class)), withKey("prefkey))).check(matches(isEnabled()));

Boolean Preference Switch.

Check the state of a boolean preference with "isChecked()" on the Switch child view:

onData
(allOf(is(instanceOf(Preference.class)), withKey("prefkey"))).onChildView(withClassName(is(Switch.class.getName()))).check(matches(isChecked()));

Click a preference.

Preform "onClick()"

onData
(allOf(is(instanceOf(Preference.class)), withKey("prefkey"))).onChildView(withClassName(is(Switch.class.getName()))).perform(click());

List preferences.

For example to show the list and select the item with the defined text:

1. Create a matcher:
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);
}
};
}

2. Test
private static String NUMBER_FORMAT_LOCAL_NZ = "English (New Zealand)";
private static String PREF_NUMBERFORMATLOCALE = "number_locale";

@Test
()
public void testListPreference() {
onData(allOf(is(instanceOf(Preference.class)), withKey(PREF_NUMBERFORMATLOCALE))).perform(click());
// find new zealand..  onData(withDialogListContent(NUMBER_FORMAT_LOCAL_NZ)).inAdapterView(withChild(withId(android.R.id.text1))).check(matches(isDisplayed()));
onData(withDialogListContent(NUMBER_FORMAT_LOCAL_NZ)).inAdapterView(withChild(withId(android.R.id.text1))).perform(click());

// we are licensed. onData(allOf(is(instanceOf(Preference.class)), withKey(SettingsConstants.PREF_NUMBERFORMATLOCALE), withSummaryText(NUMBER_FORMAT_LOCAL_NZ))).check(matches(isDisplayed()));
}

Testing a preference value after changing with espresso:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
assertThat(preferences.getBoolean(SettingsConstants.PREF_QUIET_TIME_ENABLED, false), is(true));

Leave a Reply

Your email address will not be published. Required fields are marked *