public static Detacher dispatchAttach(View view) {
    if (view.getWindowToken() != null || sAttachments.containsKey(view)) {
      // Screenshot tests can often be run against a View that's
      // attached to a real activity, in which case we have nothing to
      // do
      Log.i("WindowAttachment", "Skipping window attach hack since it's really attached");
      return new NoopDetacher();
    }

    sAttachments.put(view, true);
    if (Build.VERSION.SDK_INT < 23) {
      // On older versions of Android, requesting focus on a view that would bring the
      // soft keyboard up prior to attachment would result in a NPE in the view's onAttachedToWindow
      // callback. This is due to the fact that it internally calls
      // InputMethodManager.peekInstance() to
      // grab the singleton instance, however it isn't created at that point, leading to the NPE.
      // So in order to avoid that, we just grab the InputMethodManager from the view's context
      // ahead of time to ensure the instance exists.
      // https://android.googlesource.com/platform/frameworks/base/+/a046faaf38ad818e6b5e981a39fd7394cf7cee03
      view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    }
    sAttachInfo = generateAttachInfo(view);
    setAttachInfo(view);

    return new RealDetacher(view);
  }
Dispatch onAttachedToWindow to all the views in the view hierarchy.

<p>Detach the view by calling {@code detach()} on the returned {@code Detacher}.

<p>Note that if the view is already attached (either via WindowAttachment or to a real window),
then both the attach and the corresponding detach will be no-ops.

<p>Note that this is hacky, after these calls the views will still say that
isAttachedToWindow() is false and getWindowToken() == null.
core/src/main/java/com/facebook/testing/screenshot/WindowAttachment.java