/** * Only use if absolutely needed! This removes the cached layout so this component will be * remeasured even if it has alread been measured with the same size specs. */ public void releaseCachedLayout() { final InternalNode cachedLayout = getCachedLayout(); if (cachedLayout != null) { LayoutState.releaseNodeTree(cachedLayout, true /* isNestedTree */); clearCachedLayout(); } }
@Nullable static InternalNode consumeCachedLayout( Component component, InternalNode holder, int widthSpec, int heightSpec) { final InternalNode cachedLayout = component.getCachedLayout(); if (cachedLayout != null) { final boolean hasValidDirection = InternalNode.hasValidLayoutDirectionInNestedTree(holder, cachedLayout); final boolean hasCompatibleSizeSpec = hasCompatibleSizeSpec( cachedLayout.getLastWidthSpec(), cachedLayout.getLastHeightSpec(), widthSpec, heightSpec, cachedLayout.getLastMeasuredWidth(), cachedLayout.getLastMeasuredHeight()); // Transfer the cached layout to the node without releasing it if it's compatible. if (hasValidDirection && hasCompatibleSizeSpec) { component.clearCachedLayout(); return cachedLayout; } else { component.releaseCachedLayout(); return null; } } else { return null; } }
verify(componentSpy, times(1)).clearCachedLayout();
verify(componentSpy, times(1)).clearCachedLayout();
verify(sizeDependentComponentSpy1, times(1)).clearCachedLayout(); verify(sizeDependentComponentSpy2, never()).clearCachedLayout();
verify(sizeDependentComponentSpy, times(1)).clearCachedLayout();
@Test public void testComponentCreateAndRetrieveCachedLayout() { final ComponentContext c = new ComponentContext(application); final int unspecifiedSizeSpec = makeSizeSpec(0, UNSPECIFIED); final int exactSizeSpec = makeSizeSpec(50, EXACTLY); final Component textComponent = Text.create(c) .textSizePx(16) .text("test") .build(); final Size textSize = new Size(); textComponent.measure( c, exactSizeSpec, unspecifiedSizeSpec, textSize); assertThat(textComponent.getCachedLayout()).isNotNull(); InternalNode cachedLayout = textComponent.getCachedLayout(); assertThat(cachedLayout).isNotNull(); assertThat(cachedLayout.getLastWidthSpec()).isEqualTo(exactSizeSpec); assertThat(cachedLayout.getLastHeightSpec()).isEqualTo(unspecifiedSizeSpec); textComponent.clearCachedLayout(); assertThat(textComponent.getCachedLayout()).isNull(); }
verify(sizeDependentComponentSpy, times(1)).clearCachedLayout();