public static @Nullable Object generateAttachInfo(View view) {
    if (sAttachInfo != null) {
      return sAttachInfo;
    }

    try {
      Class cAttachInfo = Class.forName("android.view.View$AttachInfo");
      Class cViewRootImpl;

      if (Build.VERSION.SDK_INT >= 11) {
        cViewRootImpl = Class.forName("android.view.ViewRootImpl");
      } else {
        return null;
      }

      Class cIWindowSession = Class.forName("android.view.IWindowSession");
      Class cIWindow = Class.forName("android.view.IWindow");
      Class cICallbacks = Class.forName("android.view.View$AttachInfo$Callbacks");

      Context context = view.getContext();
      WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      Display display = Preconditions.checkNotNull(wm).getDefaultDisplay();

      Object window = createIWindow();

      final Object viewRootImpl;
      final Class[] viewRootCtorParams;
      final Object[] viewRootCtorValues;

      if (Build.VERSION.SDK_INT >= 26) {
        viewRootImpl =
            cViewRootImpl
                .getConstructor(Context.class, Display.class)
                .newInstance(context, display);

        viewRootCtorParams =
            new Class[] {
              cIWindowSession,
              cIWindow,
              Display.class,
              cViewRootImpl,
              Handler.class,
              cICallbacks,
              Context.class
            };

        viewRootCtorValues =
            new Object[] {
              stub(cIWindowSession),
              window,
              display,
              viewRootImpl,
              new Handler(),
              stub(cICallbacks),
              context
            };
      } else if (Build.VERSION.SDK_INT >= 17) {
        viewRootImpl =
            cViewRootImpl
                .getConstructor(Context.class, Display.class)
                .newInstance(context, display);

        viewRootCtorParams =
            new Class[] {
              cIWindowSession, cIWindow, Display.class, cViewRootImpl, Handler.class, cICallbacks
            };

        viewRootCtorValues =
            new Object[] {
              stub(cIWindowSession), window, display, viewRootImpl, new Handler(), stub(cICallbacks)
            };
      } else if (Build.VERSION.SDK_INT >= 16) {
        viewRootImpl = cViewRootImpl.getConstructor(Context.class).newInstance(context);

        viewRootCtorParams =
            new Class[] {cIWindowSession, cIWindow, cViewRootImpl, Handler.class, cICallbacks};

        viewRootCtorValues =
            new Object[] {
              stub(cIWindowSession), window, viewRootImpl, new Handler(), stub(cICallbacks)
            };
      } else {
        viewRootCtorParams = new Class[] {cIWindowSession, cIWindow, Handler.class, cICallbacks};

        viewRootCtorValues =
            new Object[] {stub(cIWindowSession), window, new Handler(), stub(cICallbacks)};
      }

      Object attachInfo = invokeConstructor(cAttachInfo, viewRootCtorParams, viewRootCtorValues);

      setField(attachInfo, "mHasWindowFocus", true);
      setField(attachInfo, "mWindowVisibility", View.VISIBLE);
      setField(attachInfo, "mInTouchMode", false);
      if (Build.VERSION.SDK_INT >= 11) {
        setField(attachInfo, "mHardwareAccelerated", false);
      }

      return attachInfo;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Simulates the view as being attached.
core/src/main/java/com/facebook/testing/screenshot/WindowAttachment.java