В моей деятельности есть логика, в которой я могу отключить предпочтения, если будут выполнены определенные условия. Вот как это будет выглядеть, когда настройки отключены.
Я хочу быть в состоянии проверить это в моем эспрессо-тесте, но яне в состоянии понять, как это сделать. Вот мой упрощенный тест, который я выполняю в Android Studio
@RunWith(AndroidJUnit4.class)
@LargeTest
public class NotificationPreferencesErrorTest
{
@Rule
public ActivityTestRule<SettingsActivity> activityRule = new ActivityTestRule<>(SettingsActivity.class,
true, false);
@Test
public void testViewModelDisabled()
{
// Some code logic to set up conditions that will disable the preferences.
activityRule.launchActivity(null);
Espresso.onData(Matchers.instanceOf(Preference.class)).onChildView(Matchers.allOf(
resourceNameMatcher("title"), resourceNameMatcher("summary"))).check(
ViewAssertions.matches(ViewMatchers.isEnabled()));
}
private Matcher<View> resourceNameMatcher(String resourceName)
{
return new TypeSafeMatcher<View>()
{
@Override
public void describeTo(Description description)
{
description.appendText("with res-name : " + resourceName);
}
@Override
protected boolean matchesSafely(View item)
{
Resources resources = item.getResources();
String matchableResName = resources.getResourceName(item.getId());
return !TextUtils.isEmpty(matchableResName) && matchableResName.equals(resourceName);
}
};
}
}
Эта строка не проходит
Espresso.onData(Matchers.instanceOf(Preference.class)).onChildView(Matchers.allOf(
resourceNameMatcher("title"), resourceNameMatcher("summary"))).check(
ViewAssertions.matches(ViewMatchers.isEnabled()));
Трассировка стека ошибок
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x10302f8
fl=KEEP_SCREEN_ON LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1794, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@96c56bb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16908682, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@53017d8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1731, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@99dce16, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0, child-count=1}
|
+--->ActionBarOverlayLayout{id=2131363048, res-name=decor_content_parent, visibility=VISIBLE, width=1080, height=1731, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@7ad056d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.appcompat.widget.ActionBarOverlayLayout$LayoutParams@a8e09f0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=147.0, child-count=2}
|
+----->ConstraintLayout{id=-1, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@3f889ee, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------>FrameLayout{id=2131363082, res-name=detail_container, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@c61d51c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@f5c64ab, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-------->FrameLayout{id=16908351, res-name=list_container, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@8452708, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--------->RecyclerView{id=2131365080, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1584, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@f114a9e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=9}
|
Я не знаю, что я делаю неправильно, и я был бы признателен за любую помощь.