public List<Component> getSubComponents() { return extractSubComponents(getMainThreadLayoutState().getDiffTree()); }
@Test public void testDiffTreeEnabled() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return create(c) .child(TestDrawableComponent.create(c)) .child(create(c).child(TestDrawableComponent.create(c))) .build(); } }; LayoutState layoutState = calculateLayoutStateWithDiffing( mContext, component, makeSizeSpec(350, SizeSpec.EXACTLY), makeSizeSpec(200, SizeSpec.EXACTLY), null); // Check diff tree is not null and consistent. DiffNode node = layoutState.getDiffTree(); assertThat(node).isNotNull(); assertThat(4).isEqualTo(countNodes(node)); }
@Test public void testDiffTreeDisabled() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return create(c) .child(TestDrawableComponent.create(c)) .child(create(c).child(TestDrawableComponent.create(c))) .build(); } }; LayoutState layoutState = calculateLayoutState( mContext, component, makeSizeSpec(350, SizeSpec.EXACTLY), makeSizeSpec(200, SizeSpec.EXACTLY)); // Check diff tree is null. assertThat(layoutState.getDiffTree()).isNull(); }
@Test public void testCachedMeasures() { final Component component1 = new TestLayoutSpec(false); final Component component2 = new TestLayoutSpec(false); LayoutState prevLayoutState = calculateLayoutStateWithDiffing( mContext, component1, SizeSpec.makeSizeSpec(350, SizeSpec.EXACTLY), SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY), null); // Check diff tree is consistent. DiffNode node = prevLayoutState.getDiffTree(); InternalNode layoutTreeRoot = LayoutState.createTree( component2, mContext); LayoutState.applyDiffNodeToUnchangedNodes(layoutTreeRoot, node); checkAllComponentsHaveMeasureCache(layoutTreeRoot); }
@Test public void testPartiallyCachedMeasures() { final Component component1 = new TestLayoutSpec(false); final Component component2 = new TestLayoutSpec(true); LayoutState prevLayoutState = calculateLayoutStateWithDiffing( mContext, component1, SizeSpec.makeSizeSpec(350, SizeSpec.EXACTLY), SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY), null); // Check diff tree is consistent. DiffNode node = prevLayoutState.getDiffTree(); InternalNode layoutTreeRoot = LayoutState.createTree( component2, mContext); LayoutState.applyDiffNodeToUnchangedNodes(layoutTreeRoot, node); InternalNode child_1 = (InternalNode) layoutTreeRoot.getChildAt(0); assertCachedMeasurementsDefined(child_1); InternalNode child_2 = (InternalNode) layoutTreeRoot.getChildAt(1); assertCachedMeasurementsNotDefined(child_2); InternalNode child_3 = (InternalNode) child_2.getChildAt(0); assertCachedMeasurementsDefined(child_3); InternalNode child_4 = (InternalNode) layoutTreeRoot.getChildAt(2); assertCachedMeasurementsNotDefined(child_4); }