繁体   English   中英

如何在 Espresso 中测试 Android 通知

[英]How do I test Android notifications in Espresso

我已阅读以下 SO 帖子,但我的问题完全不同。

  1. Android 中的测试通知

我们使用自定义 Android 布局来构建我们的通知。 这就是我们的 Android 通知在呈现时的样子。

自定义 Android 通知

我想为它写一个浓缩咖啡测试。 由于它们是通知布局,因此在单独的进程中呈现,并且它们与活动和内容视图无关。

所以我想在浓缩咖啡测试中增加布局,看看两个文本视图是否与图像视图重叠。 如果检查通过,则测试将通过。 我的浓缩咖啡测试如下

@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>
TextView body = findViewById(R.id.frame_title);
        ImageView imageView = 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())));

尝试以下代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM