我已经阅读了以下的SO帖子,但我的问题完全不同。
我们使用自定义的Android布局来构建通知。这是我们的Android通知在渲染时的样子。
我想为它编写一份Espresso测试。由于它们是通知布局,所以它们在单独的进程中呈现,并且它们与活动和内容视图无关。
因此,我考虑在Espresso测试中填充布局,然后查看两个文本视图是否与图像视图重叠。如果检查通过,则测试将通过。我的Espresso测试如下:
@RunWith(AndroidJUnit4.class)
public class NotificationsTest
{
// Just to launch an espresso test. It does nothing else.
@Rule
public ActivityTestRule<SettingsActivity>
activityTestRule = new ActivityTestRule<>(MainActivity.class,
false, false);
@Test
public void validateNotifications() throws Exception
{
Context context = InstrumentationRegistry.getInstrumentation().getContext();
View view = LayoutInflater.from(context).inflate(R.layout.notification_layout, null, false);
TextView body = view.findViewById(R.id.frame_title);
ImageView imageView = view.findViewById(R.id.image);
Assert.assertThat(body, Matchers.is(isNotNull()));
Assert.assertThat(imageView, Matchers.is(isNotNull()));
Espresso.onView(ViewMatchers.withId(R.id.header)).check(
PositionAssertions.isCompletelyLeftOf(Matchers.allOf(ViewMatchers.withId(R.id.image), ViewMatchers.isDisplayed())));
}
}
当我运行这个测试时,测试在以下行失败。
View view = LayoutInflater.from(context).inflate(R.layout.notification_layout, null, false);
以下是错误信息,下面提供堆栈跟踪。
E/TestRunner: android.content.res.Resources$NotFoundException: Resource ID #0x7f0d0305
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2155)
at android.content.res.Resources.getLayout(Resources.java:1155)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.myproject.NotificationsTest.validateNotifications(CarouselVerticalNotificationsTest.java:73)
我不知道我做错了什么。如何验证通知中文本和图像之间没有重叠?我可以确认布局文件存在,并且已经在应用程序中膨胀,但是在我的单元测试中没有膨胀。
我的布局文件
notification_layout.xml
如下所示。<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/notification"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/image"
android:layout_alignParentTop="true"
android:importantForAccessibility="no"
>
<ImageView
android:id="@+id/logo_image"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:src="@drawable/new_logo"
android:background="@color/logo_background"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This item's gone but there's more!!"
android:layout_alignBottom="@+id/logo_image"
android:layout_alignTop="@+id/logo_image"
android:layout_toRightOf="@+id/logo_image"
android:fontFamily="?android:attr/fontFamily"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingEnd="8dp"
android:paddingStart="8dp"
/>
<TextView
android:id="@+id/frame_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="@id/title"
android:text="Vous avez consulté Connecteurs et câbles vidéo, nous vous recommandons ces produits !"
android:layout_below="@+id/title"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:fontFamily="@string/font_family_roboto"
/>
</RelativeLayout>
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/vertical_image_height"
android:layout_height="@dimen/vertical_image_height"
android:src="@drawable/stock_image"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
/>
</RelativeLayout>