onView(withId(com.help.R.id.menu_item)).check(matches(CustomMatchers.withActionIconDrawableNumberOflayers(2)));
这里我使用自定义匹配器来检查菜单项上的图层数。
public static Matcher<View> withActionIconDrawableNumberOflayers(final int numberOflayers) {
return new BoundedMatcher<View, ActionMenuItemView>(ActionMenuItemView.class) {
@Override
public void describeTo(final Description description) {
description.appendText("has image layers " + numberOflayers);
}
@Override
public boolean matchesSafely(final ActionMenuItemView actionMenuItemView) {
LayerDrawable icon1 = (LayerDrawable) actionMenuItemView.getItemData().getIcon();
int i = icon1.getNumberOfLayers();
return i==numberOfLayers;
}
};
}
但是我无法超越这条线
LayerDrawable icon1 = (LayerDrawable) actionMenuItemView.getItemData().getIcon();
如何将MenuItem(com.help.R.id.menu_item)转换为ActionMenuItemView,以便我可以获得菜单项drawable的numberOfLayers?