AnalysisFragmentImpl extends AnalysisScreenContract.View { @Override CompletableFuture<Void> waitForViewLayout() { final View view = mFragment.getView(); if (view == null) { return CompletableFuture.completedFuture(null); } final CompletableFuture<Void> future = new CompletableFuture<>(); LOG.debug("Observing the view layout"); view.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { future.complete(null); mHintsAnimator.setContainerViewHeight(view.getHeight()); view.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); view.requestLayout(); return future; } AnalysisFragmentImpl(final FragmentImplCallback fragment,
            @NonNull final Document document,
            final String documentAnalysisErrorMessage); @Override void hideError(); @Override void onNoExtractionsFound(); @Override void onDocumentAnalyzed(); @Override void showError(@NonNull final String message, final int duration); @Override void showError(@NonNull final String message, @NonNull final String buttonTitle,
            @NonNull final View.OnClickListener onClickListener); @Override void startScanAnimation(); @Override void stopScanAnimation(); @Override void setListener(@NonNull final AnalysisFragmentListener listener); void onCreate(final Bundle savedInstanceState); View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState); void onStart(); void onDestroy();  }
@Test public void should_waitForViewLayout_whenRequested() throws Exception { final AtomicReference<AnalysisFragmentImpl> analysisFragmentImplRef = new AtomicReference<>(); final AtomicReference<CompletableFuture<Void>> futureRef; try (final ActivityScenario<AnalysisFragmentHostActivity> scenario = launchHostActivity( analysisFragmentImplRef)) { futureRef = new AtomicReference<>(); scenario.onActivity( new ActivityScenario.ActivityAction<AnalysisFragmentHostActivity>() { @Override public void perform(final AnalysisFragmentHostActivity activity) { final AnalysisFragmentImpl analysisFragment = analysisFragmentImplRef.get(); final CompletableFuture<Void> future = analysisFragment.waitForViewLayout(); futureRef.set(future); } }); final CompletableFuture<Void> future = futureRef.get(); assertThat(future).isNotNull(); assertThat(future.isDone()).isTrue(); } }