@Test public void testCallbackIsNotCalledTwiceIfPreDrawFiresTwice() { activity.visible(); target.getSize(cb); LayoutParams layoutParams = new FrameLayout.LayoutParams(1234, 4123); view.setLayoutParams(layoutParams); view.requestLayout(); view.getViewTreeObserver().dispatchOnPreDraw(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb, times(1)).onSizeReady(anyInt(), anyInt()); }
@Test public void testSizeCallbackIsCalledPreDrawIfNoDimensAndNoLayoutParamsButLayoutParamsSetLater() { target.getSize(cb); int width = 689; int height = 354; LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height); view.setLayoutParams(layoutParams); view.requestLayout(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb).onSizeReady(eq(width), eq(height)); }
@Test public void testDoesAddSecondListenerIfFirstListenerIsRemovedBeforeSecondRequest() { SizeReadyCallback cb1 = mock(SizeReadyCallback.class); target.getSize(cb1); view.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); SizeReadyCallback cb2 = mock(SizeReadyCallback.class); view.setLayoutParams( new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); target.getSize(cb2); view.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb2).onSizeReady(anyInt(), anyInt()); }
@Test public void testDoesNotAddMultipleListenersIfMultipleCallbacksAreAdded() { SizeReadyCallback cb1 = mock(SizeReadyCallback.class); SizeReadyCallback cb2 = mock(SizeReadyCallback.class); target.getSize(cb1); target.getSize(cb2); view.getViewTreeObserver().dispatchOnPreDraw(); // assertThat(shadowObserver.getPreDrawListeners()).hasSize(1); }
@Test public void testSizeCallbackIsNotCalledPreDrawIfNoDimensSetOnPreDraw() { target.getSize(cb); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb, never()).onSizeReady(anyInt(), anyInt()); activity.visible(); verify(cb).onSizeReady(anyInt(), anyInt()); }
@Test public void testDoesNotNotifyCallbackTwiceIfAddedTwice() { target.getSize(cb); target.getSize(cb); view.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb, times(1)).onSizeReady(anyInt(), anyInt()); }
@Test public void testCallbacksFromMultipleRequestsAreNotifiedOnPreDraw() { SizeReadyCallback firstCb = mock(SizeReadyCallback.class); SizeReadyCallback secondCb = mock(SizeReadyCallback.class); target.getSize(firstCb); target.getSize(secondCb); int width = 68; int height = 875; LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height); view.setLayoutParams(layoutParams); activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(firstCb, times(1)).onSizeReady(eq(width), eq(height)); verify(secondCb, times(1)).onSizeReady(eq(width), eq(height)); }
@Test public void testMatchParentWidthAndHeight() { LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); view.setLayoutParams(params); target.getSize(cb); verify(cb, never()).onSizeReady(anyInt(), anyInt()); activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb).onSizeReady(eq(parent.getWidth()), eq(parent.getHeight())); }
@Test public void testDoesNotThrowOnPreDrawIfViewTreeObserverIsDead() { target.getSize(cb); int width = 1; int height = 2; LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height); view.setLayoutParams(layoutParams); ViewTreeObserver vto = view.getViewTreeObserver(); view.requestLayout(); activity.visible(); assertFalse(vto.isAlive()); vto.dispatchOnPreDraw(); verify(cb).onSizeReady(eq(width), eq(height)); }
@Test public void getSize_withWrapContentWidthAndMatchParentHeight_usesDisplayDimenWidthAndHeight() { LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); view.setLayoutParams(params); setDisplayDimens(500, 600); target.getSize(cb); verify(cb, never()).onSizeReady(anyInt(), anyInt()); int height = 32; parent.getLayoutParams().height = height; activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb).onSizeReady(600, height); }
@Test public void getSize_withMatchParentWidthAndWrapContentHeight_usesWidthAndDisplayDimenHeight() { LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); view.setLayoutParams(params); setDisplayDimens(300, 400); target.getSize(cb); verify(cb, never()).onSizeReady(anyInt(), anyInt()); int width = 32; parent.getLayoutParams().width = 32; activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb).onSizeReady(width, 400); }
@Test public void testSizeCallbacksAreCalledInOrderPreDraw() { SizeReadyCallback[] cbs = new SizeReadyCallback[25]; for (int i = 0; i < cbs.length; i++) { cbs[i] = mock(SizeReadyCallback.class); target.getSize(cbs[i]); } int width = 100, height = 111; parent.getLayoutParams().width = width; parent.getLayoutParams().height = height; activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); InOrder order = inOrder((Object[]) cbs); for (SizeReadyCallback cb : cbs) { order.verify(cb).onSizeReady(eq(width), eq(height)); } }
@Test public void testSizeCallbackIsCalledPreDrawIfNoDimensAndNoLayoutParams() { target.getSize(cb); int width = 12; int height = 32; parent.getLayoutParams().width = width; parent.getLayoutParams().height = height; activity.visible(); view.getViewTreeObserver().dispatchOnPreDraw(); verify(cb).onSizeReady(eq(width), eq(height)); }
/** * Some views (e.g. SimpleVariableTextLayoutView) in FB4A rely on the predraw. Actually I don't * know why, ideally it shouldn't. * * <p>However if you find that text is not showing in your layout, try dispatching the pre draw * using this method. Note this method is only supported for views that are not attached to a * Window, and the behavior is slightly different than views attached to a window. (Views attached * to a window have a single ViewTreeObserver for all child views, whereas for unattached views, * each child has its own ViewTreeObserver.) */ private void dispatchPreDraw(View view) { while (view.getViewTreeObserver().dispatchOnPreDraw()) {} if (view instanceof ViewGroup) { ViewGroup vg = (ViewGroup) view; for (int i = 0; i < vg.getChildCount(); i++) { dispatchPreDraw(vg.getChildAt(i)); } } }