I assume that this is a bug, but I might be doing something wrong.
My layout is very simple and only contains a floating action button:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutWithoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/stopButton"
android:src="@drawable/ic_stop_white_24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_anchor="@id/layoutWithoutContent"
app:backgroundTint="@color/colorPrimary"
app:layout_anchorGravity="bottom|right|end"/>
My activity only inherits the onCreate method containing:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CoordinatorLayout coordinatorLayout = findViewById(R.id.contentCoordinatorLayout);
Snackbar.make(coordinatorLayout, R.string.snackbar_message, Snackbar.LENGTH_LONG).show();
}
I wrote an Espresso test which only validates that the Snackbar is shown within halve a second
@Rule
public ActivityTestRule<MainActivity> mainActivity = new ActivityTestRule<>(MainActivity.class, true, true);
@Test
public void testSnackbarIsShown() throws Exception { onView(withText(R.string.snackbar_message)).check(matches(isDisplayed()));
}
When I debug this test, onView(withText(R.string.snackbar_message)).check(matches(isDisplayed()));
is executed after the snackbar disappeared.
So far I have found 3 ways to resolve this issue, the issue is resolved when:
I change the FloatingActionButton's anchorGravity to top:
app:layout_anchorGravity="top|right|end"
I remove the FloatingActionButton completely
Change the first argument of the Snackbar to use
findViewById(android.R.id.content)
. But this does not push the FloatingActionButton up when the snackbar appears.
As I said, I assume this is a bug, but please let me know if I'm doing something wrong here.
sleep()
method is a mistake. You should use someViewAction
which loops test tread. Other thing is that espresso don't work well with animations. Turn of animations on device developer options and then try to launch test. – FoldawayActivityMonitor
? (developer.android.com/reference/android/app/…) Like, wait for activity to appear and only after check your snackbar. – Profligate