/** Assert that the LithoView under test has the provided measured width. */ public LithoViewAssert hasMeasuredWidthOf(int width) { Java6Assertions.assertThat(actual.getMeasuredWidth()) .overridingErrorMessage( "Expected LithoView to have a width of %d, but was %d.", width, actual.getMeasuredWidth()) .isEqualTo(width); return this; }
@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 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); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (mStickyHeader.getVisibility() == View.GONE) { return; } final int stickyHeaderLeft = getPaddingLeft(); final int stickyHeaderTop = getPaddingTop(); mStickyHeader.layout( stickyHeaderLeft, stickyHeaderTop, stickyHeaderLeft + mStickyHeader.getMeasuredWidth(), stickyHeaderTop + mStickyHeader.getMeasuredHeight()); }
@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); }
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; }
/** * Get the subcomponents of a component * * @param context A components context * @param component The component which to get the subcomponents of * @param widthSpec The width to measure the component with * @param heightSpec The height to measure the component with * @return The subcomponents of the given component */ public static List<SubComponent> getSubComponents( ComponentContext context, Component component, int widthSpec, int heightSpec) { final TestComponentTree componentTree = TestComponentTree.create(context, component) .incrementalMount(false) .build(); final LithoView lithoView = new LithoView(context); lithoView.setComponentTree(componentTree); lithoView.measure(widthSpec, heightSpec); lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight()); final List<Component> components = componentTree.getSubComponents(); final List<SubComponent> subComponents = new ArrayList<>(components.size()); for (Component lifecycle : components) { subComponents.add(SubComponent.of(lifecycle)); } return subComponents; }
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 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(); }
/** 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(); }
@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); }
@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 testDefault() { LithoView view = getMountedView(); assertThat(view.getMeasuredWidth()).isGreaterThan(0); assertThat(view.getMeasuredHeight()).isGreaterThan(0); }
makeMeasureSpec(0, UNSPECIFIED), makeMeasureSpec(0, UNSPECIFIED)); lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());
/** 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); }