void mount(Component component, int width, int height) { if (mLithoView.getComponentTree() == null) { mLithoView.setComponentTree( ComponentTree.create(mLithoView.getComponentContext(), component) .incrementalMount(false) .build()); } else { mLithoView.setComponent(component); } mComponentWidth = width; mComponentHeight = height; }
@Before public void setup() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return TestDrawableComponent.create(c).widthPx(100).heightPx(100).build(); } }; mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent(component); }
@Override public void bind(@NonNull LithoView view) { Component<T> component = getComponent(view.getComponentContext()); if (view.getComponentTree() == null) { view.setComponentTree(ComponentTree.create(view.getComponentContext(), component) .asyncStateUpdates(false) // does this need to be off? .layoutThreadLooper(Looper.getMainLooper()) .incrementalMount( false) // this needs to be off for recyclerview to work. Should we manually // incrementally mount views in a scroll listener? .layoutDiffing(false) // should we use this? .build()); } view.setComponent(component); }
private LithoView getLithoView(Component component) { LithoView lithoView = new LithoView(mContext); lithoView.setComponent(component); lithoView.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); lithoView.layout( 0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); return lithoView; }
private LithoView getLithoView(Component component) { LithoView lithoView = new LithoView(mContext); lithoView.setComponent(component); lithoView.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); return lithoView; }
@Test public void testClearUnusedStateContainers() { mComponentTree.updateStateSync(mTestComponent.getGlobalKey(), new TestStateUpdate(), "test"); assertThat(getStateContainersMap().keySet().size()).isEqualTo(1); assertThat(getStateContainersMap().keySet().contains(mTestComponent.getGlobalKey())).isTrue(); final Component child1 = new TestComponent(); child1.setKey("key"); mLithoView.setComponent(child1); mLithoView.onAttachedToWindow(); ComponentTestHelper.measureAndLayout(mLithoView); mComponentTree.updateStateSync(child1.getGlobalKey(), new TestStateUpdate(), "test"); assertThat(getStateContainersMap().keySet().size()).isEqualTo(1); assertThat(getStateContainersMap().keySet().contains("key")).isTrue(); }
@Test public void testBasicViewToString() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return TestDrawableComponent.create(c).widthPx(100).heightPx(100).build(); } }; final LithoView lithoView = new LithoView(RuntimeEnvironment.application); lithoView.setComponent(component); lithoView.measure( makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); final String string = LithoViewTestHelper.viewToString(lithoView); assertThat(string) .containsPattern( "litho.InlineLayout\\{\\w+ V.E..... .. 0,0-100,100\\}\n" + " litho.TestDrawableComponent\\{\\w+ V.E..... .. 0,0-100,100\\}"); }
@Test public void testMeasureComputesLayoutStateWhenSpecsAreNotExact() { mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent( TestDrawableComponent.create(mLithoView.getComponentContext()).heightPx(100).build()); mLithoView.measure(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, AT_MOST)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(100); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); assertThat(mLithoView.getComponentTree().getMainThreadLayoutState()).isNotNull(); }
@Test public void testMeasureDoesNotComputeLayoutStateWhenSpecsAreExact() { mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent(TestDrawableComponent.create(mLithoView.getComponentContext()).build()); mLithoView.measure(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(100); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); assertThat(mLithoView.getComponentTree().getMainThreadLayoutState()).isNull(); mLithoView.layout(0, 0, 50, 50); final LayoutState layoutState = mLithoView.getComponentTree().getMainThreadLayoutState(); assertThat(layoutState).isNotNull(); assertThat(layoutState.isCompatibleSize(50, 50)).isTrue(); }
@Test public void testNullLithoViewDimensions() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return null; } }; LithoView nullLithoView = new LithoView(application); nullLithoView.setComponent(component); nullLithoView.measure( makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); nullLithoView.layout( 0, 0, nullLithoView.getMeasuredWidth(), nullLithoView.getMeasuredHeight()); LithoViewAssert.assertThat(nullLithoView) .hasMeasuredWidthOf(0) .hasMeasuredHeightOf(0); }
/** This verifies that the width is 0 with normal layout params. */ @Test public void measureWithLayoutParams() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return TestDrawableComponent.create(c).widthPercent(100).heightPx(100).build(); } }; mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent(component); mLithoView.setLayoutParams(new ViewGroup.LayoutParams(0, 200)); mLithoView.measure(makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(200, EXACTLY)); mLithoView.layout(0, 0, mLithoView.getMeasuredWidth(), mLithoView.getMeasuredHeight()); // View got measured. assertThat(mLithoView.getMeasuredWidth()).isEqualTo(0); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(200); // Attaching will not mount anything as we have no width. ShadowView shadow = shadowOf(mLithoView); shadow.callOnAttachedToWindow(); assertThat(getInternalMountItems(mLithoView)).isNull(); }
lithoView.setComponent(component); lithoView.measure( makeMeasureSpec(0, UNSPECIFIED),
@Test public void testNoCrashOnSameComponentKey() { final Component child1 = new TestComponent(); child1.setKey("key"); final Component child2 = new TestComponent(); child2.setKey("key"); final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return Column.create(c).child(child1).child(child2).build(); } }; final LithoView lithoView = new LithoView(mContext); lithoView.setComponent(component); lithoView.onAttachedToWindow(); ComponentTestHelper.measureAndLayout(lithoView); }
lithoView.setComponent(component); lithoView.measure(makeMeasureSpec(1000, EXACTLY), makeMeasureSpec(0, UNSPECIFIED));
/** This verifies that the width is correct with at most layout params. */ @Test public void measureWithAtMostLayoutParams() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return TestDrawableComponent.create(c).widthPercent(50).heightPercent(10).build(); } }; mLithoView = new LithoView(RuntimeEnvironment.application); mLithoView.setComponent(component); mLithoView.setLayoutParams( new RecyclerViewLayoutManagerOverrideParams( SizeSpec.makeSizeSpec(100, SizeSpec.AT_MOST), SizeSpec.makeSizeSpec(200, SizeSpec.AT_MOST))); mLithoView.measure(makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); mLithoView.layout(0, 0, mLithoView.getMeasuredWidth(), mLithoView.getMeasuredHeight()); // View got measured. assertThat(mLithoView.getMeasuredWidth()).isEqualTo(50); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(20); // Attaching will automatically mount since we already have a layout fitting our size. ShadowView shadow = shadowOf(mLithoView); shadow.callOnAttachedToWindow(); assertThat(getInternalMountItems(mLithoView)).hasSize(2); }
@Test public void testNoCrashOnSameComponentKeyNestedContainers() { final Component child1 = new TestComponent(); child1.setKey("key"); final Component child2 = new TestComponent(); child2.setKey("key"); final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return Column.create(c) .child(Column.create(c).child(child1)) .child(Column.create(c).child(child2)) .build(); } }; final LithoView lithoView = new LithoView(mContext); lithoView.setComponent(component); lithoView.onAttachedToWindow(); ComponentTestHelper.measureAndLayout(lithoView); }
lithoView.setComponent(Column.create(mContext).build()); assertThat(content.getDispatchedEventHandlers()).contains(invisibleEventHandler); assertThat(content.getDispatchedEventHandlers()).doesNotContain(visibleEventHandler); lithoView.setComponent(Column.create(mContext).child(wrappedContent).build()); assertThat(content.getDispatchedEventHandlers()).contains(visibleEventHandler); assertThat(content.getDispatchedEventHandlers()).doesNotContain(invisibleEventHandler);