PreferenceMatcher が用意されていたので利用しました。
- import static com.google.common.base.Preconditions.checkNotNull;
- ...
- public class EspressoTest extends ActivityInstrumentationTestCase2<MainPreferenceActivity> {
- public EspressoTest() {
- super(MainPreferenceActivity.class);
- }
- @Override
- public void setUp() throws Exception {
- super.setUp();
- // Espresso will not launch our activity for us, we must launch it via
- // getActivity().
- getActivity();
- }
- // Preference のキーを指定して、対応するビューをクリックする
- public void testPreference() {
- onData(withPreferenceKey("pref-key")).perform(click());
- }
- public static Matcher<Object> withPreferenceKey(final Matcher<Preference> preferenceMatcher) {
- checkNotNull(preferenceMatcher);
- return new BoundedMatcher<Object, Preference>(Preference.class) {
- @Override
- public void describeTo(Description description) {
- description.appendText("with preference key: ");
- preferenceMatcher.describeTo(description);
- }
- @Override
- protected boolean matchesSafely(Preference pref) {
- return preferenceMatcher.matches(pref);
- }
- };
- }
- public static Matcher<Object> withPreferenceKey(String expectedText) {
- checkNotNull(expectedText);
- return withPreferenceKey(PreferenceMatchers.withKey(expectedText));
- }
- }
応用で、Preference をクリック → なんかする → summary が適切な値になっていることをチェック
- public void testPreference() {
- // Preference をクリック
- onData(withPreferenceKey("pref-key")).perform(click());
- // クリック先でごにょごにょ
- // summary が適切な値になっていることをチェック
- // summary の Id が android.R.id.summary であることを利用
- onData(withPreferenceKey("pref-key"))
- .onChildView(withId(android.R.id.summary))
- .check(matches(withText("correct summary value")));
- }
0 件のコメント:
コメントを投稿