title: Android Espresso matching root view
body: <p>I'm having trouble with matching toast message in one of my activities. In others it works without any problems but in this particular one it does not. I am not aware of any special layout changes that may cause this behavior. </p>
  
  <p>I'm getting following exception:</p>
  
  <pre><code>java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
  Root{application-window-token=android.view.ViewRootImpl$W@40dcb2, window-token=android.view.ViewRootImpl$W@40dcb2, has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1820002 pfl=0x8 fmt=-3 wanim=0x7f0a0088 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x10}, decor-view-string=DecorView{id=-1, visibility=GONE, width=1002, height=348, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
  . All Roots:
  Root{application-window-token=android.view.ViewRootImpl$W@19481ebd, window-token=android.view.ViewRootImpl$W@19481ebd, has-window-focus=true, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#2 ty=1 fl=#81810500 pfl=0x8 wanim=0x1030461 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x10}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
  Root{application-window-token=android.view.ViewRootImpl$W@40dcb2, window-token=android.view.ViewRootImpl$W@40dcb2, has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1820002 pfl=0x8 fmt=-3 wanim=0x7f0a0088 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x10}, decor-view-string=DecorView{id=-1, visibility=GONE, width=1002, height=348, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
  at android.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:99)
  at android.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:69)
  at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:23)
  at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:9)
  at android.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:68)
  at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:166)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5951)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  </code></pre>
  
  <p>I am checking the toast message as following (which does <strong>NOT</strong> work):</p>
  
  <pre><code>onView(withText(R.string.act_preferences_context_empty))
          .inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))))
          .check(matches(isDisplayed()));
  </code></pre>
  
  <p>However, when I check the toast like this, it <strong>works</strong>:</p>
  
  <pre><code>onView(withText(R.string.act_preferences_context_empty))
          .inRoot(not(isFocusable()))
          .check(matches(isDisplayed()));
  </code></pre>
  
  <p>Could anyone explain this behavior? Thanks.</p>