private void notifyCbs(int width, int height) { // One or more callbacks may trigger the removal of one or more additional callbacks, so we // need a copy of the list to avoid a concurrent modification exception. One place this // happens is when a full request completes from the in memory cache while its thumbnail is // still being loaded asynchronously. See #2237. for (SizeReadyCallback cb : new ArrayList<>(cbs)) { cb.onSizeReady(width, height); } }
private void notifyCbs(int width, int height) { // One or more callbacks may trigger the removal of one or more additional callbacks, so we // need a copy of the list to avoid a concurrent modification exception. One place this // happens is when a full request completes from the in memory cache while its thumbnail is // still being loaded asynchronously. See #2237. for (SizeReadyCallback cb : new ArrayList<>(cbs)) { cb.onSizeReady(width, height); } }
@Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SizeReadyCallback cb = (SizeReadyCallback) invocationOnMock.getArguments()[0]; cb.onSizeReady(width, height); return null; } }
@Test public void getSize_withWidthAndHeightEqualToPadding_doesNotCallSizeReady() { view.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); view.requestLayout(); view.setPadding(50, 50, 50, 50); target.getSize(cb); verify(cb, never()).onSizeReady(anyInt(), anyInt()); }
@Test public void testCallsSizeReadyCallbackOnGetSize() { SizeReadyCallback cb = mock(SizeReadyCallback.class); future.getSize(cb); verify(cb).onSizeReady(eq(width), eq(height)); }
@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 testDecreasesDimensionsByViewPadding() { activity.visible(); view.setLayoutParams(new FrameLayout.LayoutParams(100, 100)); view.setPadding(25, 25, 25, 25); view.requestLayout(); target.getSize(cb); verify(cb).onSizeReady(50, 50); }
@Test public void testSizeCallbackIsCalledSynchronouslyIfLayoutParamsConcreteSizeSet() { int dimens = 444; LayoutParams layoutParams = new LayoutParams(dimens, dimens); view.setLayoutParams(layoutParams); shadowView.setIsLaidOut(true); target.getSize(cb); verify(cb).onSizeReady(eq(dimens), eq(dimens)); }
@Test public void testDecreasesDimensionsByViewPadding() { view.setLayoutParams(new LayoutParams(100, 100)); view.setPadding(25, 25, 25, 25); shadowView.setIsLaidOut(true); target.getSize(cb); verify(cb).onSizeReady(50, 50); }
@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 getSize_withLayoutParams_notLaidOut_doesCallSizeReady() { view.setLayoutParams(new FrameLayout.LayoutParams(10, 10)); view.setRight(100); view.setBottom(100); target.getSize(cb); verify(cb, times(1)).onSizeReady(anyInt(), anyInt()); }
@Test public void testSizeCallbackIsCalledPreDrawIfNoDimensAndNoLayoutParamsButLayoutParamsSetLater() { target.getSize(cb); int width = 689; int height = 354; LayoutParams layoutParams = new LayoutParams(width, height); view.setLayoutParams(layoutParams); shadowView.setIsLaidOut(true); shadowObserver.fireOnPreDrawListeners(); verify(cb).onSizeReady(eq(width), eq(height)); }
@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 getSize_withWidthAndHeightEqualToPadding_doesNotCallSizeReady() { shadowView .setWidth(100) .setHeight(100) .setIsLaidOut(true); view.setPadding(50, 50, 50, 50); target.getSize(cb); verify(cb, never()).onSizeReady(anyInt(), anyInt()); }
@Test public void getSize_withWrapContentHeightAndValidWidth_returnsWidthAndDisplayDimen() { int width = 100; LayoutParams params = new LayoutParams(width, LayoutParams.WRAP_CONTENT); view.setLayoutParams(params); shadowView.setIsLaidOut(true); setDisplayDimens(200, 100); target.getSize(cb); verify(cb).onSizeReady(width, 200); }
@Test public void getSize_withLayoutParams_notLaidOut_doesCallSizeReady() { shadowView .setLayoutParams(new LayoutParams(10, 10)) .setWidth(100) .setHeight(100) .setIsLaidOut(false); target.getSize(cb); verify(cb, times(1)).onSizeReady(anyInt(), anyInt()); }
@Test public void getSize_withWrapContentWidthAndValidHeight_usesDisplayDimenAndValidHeight() { int height = 100; LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height); view.setLayoutParams(params); setDisplayDimens(100, 200); activity.visible(); view.setRight(0); target.getSize(cb); verify(cb).onSizeReady(200, 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)); }
@Test public void getSize_withValidWidthAndHeight_notLaidOut_notLayoutRequested_callsSizeReady() { view.setRight(100); view.setBottom(100); target.getSize(cb); verify(cb).onSizeReady(100, 100); }
@Test public void getSize_withLayoutParams_emptyParams_notLaidOutOrLayoutRequested_callsSizeReady() { shadowView .setLayoutParams(new LayoutParams(0, 0)) .setWidth(100) .setHeight(100) .setIsLaidOut(false); target.getSize(cb); verify(cb).onSizeReady(100, 100); }