Question: Would anyone know how to test for the appearance of a Toast message in android espresso? In robotium its easy & I used but started working in espresso but dont getting the exact command.

Answer: 
The accepted answer is a good one but didn't work for me. So I searched a bit and found [this blog article](http://baroqueworksdev.blogspot.de/2015/03/how-to-check-toast-window-on-android.html).
This gave me an idea of how to do it and I updated the solution above.

First I implemented the ToastMatcher:

```java
import android.os.IBinder;
import android.support.test.espresso.Root;
import android.view.WindowManager;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class ToastMatcher extends TypeSafeMatcher<Root> {

  @Override
  public void describeTo(Description description) {
    description.appendText("is toast");
  }

  @Override
  public boolean matchesSafely(Root root) {
    int type = root.getWindowLayoutParams().get().type;
    if (type == WindowManager.LayoutParams.TYPE_TOAST) {
        IBinder windowToken = root.getDecorView().getWindowToken();
        IBinder appToken = root.getDecorView().getApplicationWindowToken();
        if (windowToken == appToken) {
            // windowToken == appToken means this window isn't contained by any other windows.
            // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
            return true;
        }
    }
    return false;
  }

}

```

Then I implemented my check methods like this:

```java
public void isToastMessageDisplayed(int textId) {
    onView(withText(textId)).inRoot(MobileViewMatchers.isToast()).check(matches(isDisplayed()));
}

```

MobileViewMatchers is a container for accessing the matchers. There I defined the static method `isToast()`.

```
public static Matcher<Root> isToast() {
    return new ToastMatcher();
}

```

This works like a charm for me.
I would like to suggest an alternative method, especially if you need to check that particular toast is **NOT** displayed

The problem here that

```
onView(viewMatcher)
    .inRoot(RootMatchers.isPlatformPopup())
    .check(matches(not(isDisplayed())))

```

or 

```
onView(viewMatcher)
    .inRoot(RootMatchers.isPlatformPopup())
    .check(doesNotExist())

```

or any other custom `inRoot` checks
are throwing `NoMatchingRootException` even before the code passes to `check` method

You may just catch the exception and complete the test but that's not a good option since throwing and catching `NoMatchingRootException` consumes a lot of time in a comparison with the default test case. Seems that Espresso is waiting for the Root for a while

For this case is suggest just to give up with espresso here and use `UiAutomator` for this assertion. The `Espresso` and `UiAutomator` frameworks could easily work together in one environment.

```
val device: UiDevice
   get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun assertPopupIsNotDisplayed() {
    device.waitForIdle()
    assertFalse(device.hasObject(By.text(yourText))))
}

fun assertPopupIsDisplayed() {
    device.waitForIdle()
    assertTrue(device.hasObject(By.text(yourText))))
}

```