Reputation: 2609
I have read the following SO posts but my questions is completely different.
We use custom Android layouts to construct our notifications. This is what our Android notifications look like when they are rendered.
I want to write an espresso test for it. Since they are notifications layouts, there are rendered in a separate process, and they are not associated with an activity, and a content view.
So I have thought of inflating the layouts in an espresso test and see if the two text views don't overlap with the image view. If the check passes, then test will have passed. My espresso test is given below
@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())));
}
}
When I run this test, the test fails on the following line
View view = LayoutInflater.from(context).inflate(R.layout.notification_layout, null, false);
with the following error. The stack trace is given below
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)
I don't know what I am doing wrong. How I can verify that there is no overlap between the text and the image in the notification? I can confirm that the layout file exists, and it is inflated in the application, but it does not inflate in my unit tests.
My layout file notification_layout.xml
is given below
<?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>
Upvotes: 3
Views: 1596
Reputation: 1
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())));
try following code
Upvotes: 1