Android 如何测试带有文本的自定义视图是否是父视图下的子视图?
我不熟悉TDD和浓缩咖啡。我有以下用户故事来简化我的解释: 用户从列表中选择一个标记。选择后,显示上一活动中选定的标记 这很直截了当。我来自活动Android 如何测试带有文本的自定义视图是否是父视图下的子视图?,android,android-espresso,Android,Android Espresso,我不熟悉TDD和浓缩咖啡。我有以下用户故事来简化我的解释: 用户从列表中选择一个标记。选择后,显示上一活动中选定的标记 这很直截了当。我来自活动A,它启动活动B。它位于活动BI从中选择标签。如果我按submit,标签将显示在活动A上。问题是,我有一个自定义的TagViewGroup,它继承自LinearLayout,还有一个特定于该组的父子级,即TagView。这些都是非常简单的布局 我编写了以下测试: @Test public void shouldPickAndBindTag
A
,它启动活动B
。它位于活动B
I从中选择标签。如果我按submit,标签将显示在活动A
上。问题是,我有一个自定义的TagViewGroup
,它继承自LinearLayout
,还有一个特定于该组的父子级,即TagView
。这些都是非常简单的布局
我编写了以下测试:
@Test
public void shouldPickAndBindTagToNote()
{
onView(withId(R.id.edittext_description_minimized)).perform(click());
onView(withId(R.id.linearlayout_add_note_maximize)).check(matches(isDisplayed()));
onView(withId(R.id.relativelayout_quick_action_button)).check(matches(isDisplayed()));
onView(withId(R.id.imagebutton_tag)).perform(click());
onView(withId(R.id.coordinatorlayout_tag_list)).check(matches(isDisplayed()));
onView(withText("TODO")).perform(click());
onView(withText("Critical")).perform(click());
onView(withText("For Tomorrow")).perform(click());
onView(withId(R.id.fab_tag_list)).perform(click());
onView(withId(R.id.tagviewgroup_note_tags)).check(matches(isDisplayed()));
// This part checks if the selected tag TODO, Critical, Reminders are on the TagViewGroup
onView(withId(R.id.tagviewgroup_note_tags)).check(matches(NoteTagMatcher.hasTag(is("TODO"))));
onView(withId(R.id.tagviewgroup_note_tags)).check(matches(NoteTagMatcher.hasTag(is("Reminders"))));
onView(withId(R.id.tagviewgroup_note_tags)).check(matches(NoteTagMatcher.hasTag(is("For Tomorrow"))));
}
这是我的定制匹配器:
public class NoteTagMatcher
{
public static Matcher<View> hasTag(final Matcher<String> tagName)
{
return new TypeSafeMatcher<View>()
{
@Override
protected boolean matchesSafely(View item)
{
if(!(item instanceof TagViewGroup))
return false;
TagViewGroup tvg = (TagViewGroup) item;
int c = tvg.getChildCount();
for(int i = 0; i < c; ++i)
{
View v = tvg.getChildAt(i);
if(!(v instanceof TagView)) continue;
TagView tv = (TagView) v;
if(tagName.matches(tv.getText()))
return true;
return false;
}
return false;
}
@Override
public void describeTo(Description description)
{
}
};
}
}
我不理解这个错误。但我能用我刚刚意识到的这个简单的方法来检查它:
onView(withText("TODO")).check(matches(isDisplayed()));
onView(withText("Critical")).check(matches(isDisplayed()));
onView(withText("For Tomorrow")).check(matches(isDisplayed()));
这很简单,不需要太多的黑客代码,也很容易阅读和理解,但我需要一点挑战,所以我花了更多的时间实现了自己的matcher,并尝试检查我的自定义TagView
是否在TagViewGroup
下存在字符串TODO
如前所述,我想做的是测试带有特定文本的选定标记(TagView
)是否出现在我的自定义ViewGroup
(TagViewGroup
)下。鉴于我的定制匹配器显然不起作用,我如何才能做到这一点?我犯了什么错误
关于如何正确测试这一点,有什么想法吗
谢谢
onView(withText("TODO")).check(matches(isDisplayed()));
onView(withText("Critical")).check(matches(isDisplayed()));
onView(withText("For Tomorrow")).check(matches(isDisplayed()));