@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The hosting component view always matches the component size. This will // ensure that there will never be a size-mismatch between the view and the // component-based content, which would trigger a layout pass in the // UI thread. mLithoView.measure( MeasureSpec.makeMeasureSpec(mComponentWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mComponentHeight, MeasureSpec.EXACTLY)); // The mounted view always gets exact dimensions from the framework. setMeasuredDimension( MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); }
public static LithoView createAndMeasureLithoView( ComponentContext c, Component component, int widthSpec, int heightSpec) { final ComponentTree componentTree = ComponentTree.create(c, component).build(); final LithoView lithoView = new LithoView(c); lithoView.setComponentTree(componentTree); lithoView.onAttachedToWindow(); lithoView.measure(widthSpec, heightSpec); // This is really lithoView#layout without the mount componentTree.measure(widthSpec, heightSpec, LAYOUT_SIZE_OUT, false); return lithoView; }
@Test public void testNoCorrectionWhenBugIsNotMatched2() { mLithoView = setupLithoViewForDoubleMeasureTest(411, 2.625f, 1080); mLithoView.measure(makeMeasureSpec(1078, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(1078); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); }
@Test public void testNoCorrectionWhenBugIsNotMatched() { mLithoView = setupLithoViewForDoubleMeasureTest(411, 2f, 1080); mLithoView.measure(makeMeasureSpec(1079, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(1079); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); }
@Test public void testUnsetSize() { LithoView view = getMountedView(); view.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); assertThat(view.getMeasuredWidth()).isEqualTo(ProgressSpec.DEFAULT_SIZE); assertThat(view.getMeasuredHeight()).isEqualTo(ProgressSpec.DEFAULT_SIZE); }
@Test public void testCorrectsDoubleMeasureBug() { mLithoView = setupLithoViewForDoubleMeasureTest(411, 2.625f, 1080); mLithoView.measure(makeMeasureSpec(1079, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(1080); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); }
@Test public void testCorrectsDoubleMeasureBugWithAtMost() { mLithoView = setupLithoViewForDoubleMeasureTest(411, 2.625f, 1080); mLithoView.measure(makeMeasureSpec(1079, AT_MOST), makeMeasureSpec(100, EXACTLY)); assertThat(mLithoView.getMeasuredWidth()).isEqualTo(1080); assertThat(mLithoView.getMeasuredHeight()).isEqualTo(100); }
@Test public void testSuppressMeasureComponentTree() { final ComponentTree mockComponentTree = mock(ComponentTree.class); final int width = 240; final int height = 400; mLithoView.setComponentTree(mockComponentTree); mLithoView.suppressMeasureComponentTree(true); mLithoView.measure( makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY)); verify(mockComponentTree, never()) .measure(anyInt(), anyInt(), any(int[].class), anyBoolean()); LithoViewAssert.assertThat(mLithoView) .hasMeasuredWidthOf(width) .hasMeasuredHeightOf(height); }
private LithoView getLithoView(ComponentTree componentTree) { LithoView lithoView = new LithoView(mContext); lithoView.setComponentTree(componentTree); lithoView.measure( View.MeasureSpec.makeMeasureSpec(HOST_WIDTH, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(HOST_HEIGHT, View.MeasureSpec.EXACTLY)); 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; }
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(ComponentTree componentTree) { LithoView lithoView = new LithoView(mContext); lithoView.setComponentTree(componentTree); lithoView.measure( View.MeasureSpec.makeMeasureSpec(HOST_WIDTH, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(HOST_HEIGHT, View.MeasureSpec.EXACTLY)); lithoView.layout( 0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); return lithoView; }
@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); }
@Test public void testBasicComponentTreeDumping() { final Component component = new InlineLayoutSpec() { @Override protected Component onCreateLayout(ComponentContext c) { return TestDrawableComponent.create(c).widthPx(100).heightPx(100).build(); } }; ComponentContext componentContext = new ComponentContext(RuntimeEnvironment.application); final ComponentTree componentTree = ComponentTree.create(componentContext, component).build(); componentContext = ComponentContext.withComponentTree(componentContext, componentTree); final LithoView lithoView = new LithoView(RuntimeEnvironment.application); lithoView.setComponentTree(componentTree); lithoView.measure(makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); final String string = ComponentTreeDumpingHelper.dumpContextTree(componentContext); assertThat(string).containsPattern("InlineLayout\\{V}\n" + " TestDrawableComponent\\{V}"); } }
/** 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.measure( makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED));
@Test public void measureBeforeBeingAttached() { mLithoView.measure( makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); mLithoView.layout( 0, 0, mLithoView.getMeasuredWidth(), mLithoView.getMeasuredHeight()); // View got measured. assertThat(mLithoView.getMeasuredWidth()).isGreaterThan(0); assertThat(mLithoView.getMeasuredHeight()).isGreaterThan(0); // Attaching will automatically mount since we already have a layout fitting our size. ShadowView shadow = shadowOf(mLithoView); shadow.callOnAttachedToWindow(); assertThat(getInternalMountItems(mLithoView)).hasSize(2); }