com.facebook.litho.Size Java Examples

The following examples show how to use com.facebook.litho.Size. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapContentWithRemoveRangeOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAt(0, 3);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(500);
}
 
Example #2
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveAsyncOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeItemAt(0);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(700);
}
 
Example #3
Source File: ComponentTreeHolderTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetListenerBeforeTreeCreation() {
  ComponentTreeHolder holder = createComponentTreeHolder(mComponentRenderInfo);
  ComponentTree.NewLayoutStateReadyListener listener =
      mock(ComponentTree.NewLayoutStateReadyListener.class);
  holder.setNewLayoutReadyListener(listener);

  assertThat(holder.getComponentTree()).isNull();

  holder.computeLayoutSync(
      mContext,
      SizeSpec.makeSizeSpec(100, EXACTLY),
      SizeSpec.makeSizeSpec(100, EXACTLY),
      new Size());

  assertThat(holder.getComponentTree().getNewLayoutStateReadyListener()).isEqualTo(listener);

  holder.setNewLayoutReadyListener(null);

  assertThat(holder.getComponentTree().getNewLayoutStateReadyListener()).isNull();
}
 
Example #4
Source File: ComponentWarmer.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronously post preparing the ComponentTree for the given ComponentRenderInfo to the
 * handler.
 */
public void prepare(
    String tag,
    ComponentRenderInfo componentRenderInfo,
    @Nullable Size size,
    LithoHandler handler) {
  if (!isReady()) {
    ComponentsReporter.emitMessage(
        ComponentsReporter.LogLevel.WARNING,
        COMPONENT_WARMER_LOG_TAG,
        "ComponentWarmer not ready: unable to prepare sync. This will be executed asynchronously when the ComponentWarmer is ready.");

    addToPending(tag, componentRenderInfo, handler);

    return;
  }

  executePrepare(tag, componentRenderInfo, size, false, handler);
}
 
Example #5
Source File: RecyclerBinder.java    From litho with Apache License 2.0 6 votes vote down vote up
private void maybeRequestRemeasureIfBoundsChanged() {
  if (mMeasuredSize.width == 0 || mMeasuredSize.height == 0) {
    // It was measured before, but no data was bound in previous measurement,
    // therefore we need to remeasure.
    requestRemeasure();
    return;
  }

  // Even after data change we may not require triggering remeasure event if bounds of
  // RecyclerView did not change.
  final Size initialSize = getInitialMeasuredSize(mLastWidthSpec, mLastHeightSpec, true);

  final Size wrapSize = new Size();
  fillListViewport(initialSize.width, initialSize.height, wrapSize);

  if (wrapSize.width != mMeasuredSize.width || wrapSize.height != mMeasuredSize.height) {
    requestRemeasure();
  }
}
 
Example #6
Source File: VerticalScrollSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop Component childComponent,
    @Prop(optional = true) boolean fillViewport,
    @State ComponentTree childComponentTree,
    Output<Integer> measuredWidth,
    Output<Integer> measuredHeight) {
  measureVerticalScroll(
      c, widthSpec, heightSpec, size, childComponentTree, childComponent, fillViewport);
  measuredWidth.set(size.width);
  measuredHeight.set(size.height);
}
 
Example #7
Source File: SizeSpecMountWrapperComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop Component component,
    @State AtomicReference<ComponentTree> componentTreeRef) {
  final ComponentTree componentTree = getOrCreateComponentTree(c, componentTreeRef);
  componentTree.setVersionedRootAndSizeSpec(
      component,
      widthSpec,
      heightSpec,
      size,
      getTreePropWithSize(c, widthSpec, heightSpec),
      c.getLayoutVersion());
  if (size.width < 0 || size.height < 0) {
    // if this happens it means that the componentTree was probably released in the UI Thread so
    // this measurement is not needed.
    size.width = size.height = 0;
  }
}
 
Example #8
Source File: MountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop LifecycleTracker lifecycleTracker,
    @Prop(optional = true) Size intrinsicSize) {

  int width = SizeSpec.getSize(widthSpec);
  int height = SizeSpec.getSize(heightSpec);

  if (intrinsicSize != null) {
    size.width = SizeSpec.resolveSize(widthSpec, intrinsicSize.width);
    size.height = SizeSpec.resolveSize(heightSpec, intrinsicSize.height);
  } else {
    size.width = width;
    size.height = height;
  }

  lifecycleTracker.addStep(LifecycleStep.ON_MEASURE, size);
}
 
Example #9
Source File: LithoStartupLoggerTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void firstlayout_noDataAttribution() {
  final List<RenderInfo> components = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    components.add(
        ComponentRenderInfo.create()
            .component(
                SimpleMountSpecTester.create(mComponentContext)
                    .widthPx(100)
                    .heightPx(100)
                    .build())
            .build());
  }
  mRecyclerBinder.insertRangeAt(0, components);
  mRecyclerBinder.measure(
      new Size(), makeSizeSpec(1000, EXACTLY), makeSizeSpec(1000, EXACTLY), null);

  assertThat(mTestLithoStartupLogger.tracePointCount()).isEqualTo(2);
  assertThat(mTestLithoStartupLogger.getTracedPointAt(0)).isEqualTo("litho_ui_firstlayout_start");
  assertThat(mTestLithoStartupLogger.getTracedPointAt(1)).isEqualTo("litho_ui_firstlayout_end");
}
 
Example #10
Source File: LithoStartupLoggerTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void firstlayout_withDataAttribution() {
  mTestLithoStartupLogger.setDataAttribution("myquery");

  final List<RenderInfo> components = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    components.add(
        ComponentRenderInfo.create()
            .component(
                SimpleMountSpecTester.create(mComponentContext)
                    .widthPx(100)
                    .heightPx(100)
                    .build())
            .build());
  }
  mRecyclerBinder.insertRangeAt(0, components);
  mRecyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);
  mRecyclerBinder.measure(
      new Size(), makeSizeSpec(1000, EXACTLY), makeSizeSpec(1000, EXACTLY), null);

  assertThat(mTestLithoStartupLogger.tracePointCount()).isEqualTo(2);
  assertThat(mTestLithoStartupLogger.getTracedPointAt(0))
      .isEqualTo("litho_ui_myquery_firstlayout_start");
  assertThat(mTestLithoStartupLogger.getTracedPointAt(1))
      .isEqualTo("litho_ui_myquery_firstlayout_end");
}
 
Example #11
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveRangeAsyncOnHorizontal() {
  final int widthSpec = makeSizeSpec(1000, AT_MOST);
  final int heightSpec = makeSizeSpec(1000, EXACTLY);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.HORIZONTAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAt(0, 3);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.width).isEqualTo(500);
}
 
Example #12
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapContentWithRemoveRangeOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAt(0, 3);
  recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(500);
}
 
Example #13
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveAsyncOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeItemAtAsync(0);
  recyclerBinder.notifyChangeSetCompleteAsync(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(700);
}
 
Example #14
Source File: RecyclerBinderWrapContentTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Ignore("t33888191") // TODO(t33888191): Support wrapContent with insertAsync
@Test
public void testWrapContentWithRemoveRangeAsyncOnVertical() {
  final int widthSpec = makeSizeSpec(1000, EXACTLY);
  final int heightSpec = makeSizeSpec(1000, AT_MOST);

  final RecyclerBinder recyclerBinder =
      prepareBinderWithMeasuredChildSize(
          widthSpec, heightSpec, 8, OrientationHelper.VERTICAL, 100, true);

  recyclerBinder.mount(mRecyclerView);

  recyclerBinder.removeRangeAtAsync(0, 3);
  recyclerBinder.notifyChangeSetCompleteAsync(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK);

  mLayoutThreadShadowLooper.runToEndOfTasks();

  // Verify remeasure is triggered through View#postOnAnimation(Runnable)
  verifyPostOnAnimationWasCalledAtLeastNTimesWith(
      mRecyclerView, 1, recyclerBinder.mRemeasureRunnable);

  Size size = new Size();
  recyclerBinder.measure(size, widthSpec, heightSpec, mock(EventHandler.class));
  assertThat(size.height).isEqualTo(500);
}
 
Example #15
Source File: ComponentTreeHolderTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetainStateHandlerAfterExitingRange() {
  ComponentTreeHolder holder = createComponentTreeHolder(mComponentRenderInfo);
  holder.computeLayoutSync(mContext, mWidthSpec, mHeightSpec, new Size());

  // component goes out of range
  holder.acquireStateAndReleaseTree(true);
  assertThat(holder.getComponentTree()).isNull();
  assertThat(holder.getStateHandler()).isNotNull();
}
 
Example #16
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testExactlyUnspecifiedEqual() {
  final Size size = new Size();
  measureWithEqualDimens(makeSizeSpec(20, EXACTLY), makeSizeSpec(10, UNSPECIFIED), size);
  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);

  measureWithEqualDimens(makeSizeSpec(20, UNSPECIFIED), makeSizeSpec(10, EXACTLY), size);
  assertThat(size.width).isEqualTo(10);
  assertThat(size.height).isEqualTo(10);
}
 
Example #17
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void textAtMostUnspecifiedEqual() {
  final Size size = new Size();
  measureWithEqualDimens(makeSizeSpec(20, AT_MOST), makeSizeSpec(10, UNSPECIFIED), size);
  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);

  measureWithEqualDimens(makeSizeSpec(10, UNSPECIFIED), makeSizeSpec(30, AT_MOST), size);
  assertThat(size.width).isEqualTo(30);
  assertThat(size.height).isEqualTo(30);
}
 
Example #18
Source File: ComponentTreeHolderTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testForceKeepStateHandlerAfterExitingRange() {
  final ComponentRenderInfo renderInfo =
      ComponentRenderInfo.create()
          .customAttribute(ComponentTreeHolder.ACQUIRE_STATE_HANDLER_ON_RELEASE, true)
          .component(mComponent)
          .build();
  ComponentTreeHolder holder = createComponentTreeHolder(renderInfo);
  holder.computeLayoutSync(mContext, mWidthSpec, mHeightSpec, new Size());

  // component goes out of range
  holder.acquireStateAndReleaseTree(false);
  assertThat(holder.getComponentTree()).isNull();
  assertThat(holder.getStateHandler()).isNotNull();
}
 
Example #19
Source File: ComponentTreeHolderTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void acquireStateAndReleaseTree_exitRangeWithoutAcquiringStates_hookHandlerIsDropped() {
  ComponentsConfiguration.isHooksImplEnabled = true;

  final ComponentTreeHolder holder = createComponentTreeHolder(mComponentRenderInfo);
  holder.computeLayoutSync(mContext, mWidthSpec, mHeightSpec, new Size());

  // component goes out of range
  holder.acquireStateAndReleaseTree(false);
  assertThat(holder.getComponentTree()).isNull();
  assertThat(holder.getHooksHandler()).isNull();

  ComponentsConfiguration.isHooksImplEnabled = false;
}
 
Example #20
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthUnspecifiedHeightUnspecifiedEqual() {
  final Size size = new Size();
  measureWithEqualDimens(makeSizeSpec(0, UNSPECIFIED), makeSizeSpec(0, UNSPECIFIED), size);

  assertThat(size.width).isEqualTo(0);
  assertThat(size.height).isEqualTo(0);
}
 
Example #21
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthExactlyHeightTooSmall() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(10, EXACTLY), makeSizeSpec(20, AT_MOST), 0.1f, size);

  assertThat(size.width).isEqualTo(10);
  assertThat(size.height).isEqualTo(20);
}
 
Example #22
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthUnspecifiedHeightUnspecified() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(0, UNSPECIFIED), makeSizeSpec(0, UNSPECIFIED), 10, size);

  assertThat(size.width).isEqualTo(0);
  assertThat(size.height).isEqualTo(0);
}
 
Example #23
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthAtMostHeightExactlyEqual() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(20, AT_MOST), makeSizeSpec(20, EXACTLY), 1, size);

  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);
}
 
Example #24
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithInstrinsicSize() {
  final Size size = new Size();
  measureWithAspectRatio(
      makeSizeSpec(0, UNSPECIFIED), makeSizeSpec(20, AT_MOST), 10, 10, 1f, size);

  assertThat(size.width).isEqualTo(10);
  assertThat(size.height).isEqualTo(10);
}
 
Example #25
Source File: ComponentTreeHolderTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testHasCompletedLatestLayoutForSyncRender() {
  ComponentTreeHolder holder = createComponentTreeHolder(mComponentRenderInfo);
  holder.computeLayoutSync(mContext, mWidthSpec, mHeightSpec, new Size());

  assertThat(holder.hasCompletedLatestLayout()).isTrue();
}
 
Example #26
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthAtMostHeightUnspecified() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(20, AT_MOST), makeSizeSpec(0, UNSPECIFIED), 1f, size);

  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);
}
 
Example #27
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthAtMostHeightAtMostWidthSmaller() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(10, AT_MOST), makeSizeSpec(20, AT_MOST), 0.5f, size);

  assertThat(size.width).isEqualTo(10);
  assertThat(size.height).isEqualTo(20);
}
 
Example #28
Source File: TestViewComponent.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  int width = SizeSpec.getSize(widthSpec);
  int height = SizeSpec.getSize(heightSpec);

  size.height = height;
  size.width = width;

  onMeasureCalled();
}
 
Example #29
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testWidthExactlyHeightUnspecified() {
  final Size size = new Size();
  measureWithAspectRatio(makeSizeSpec(10, EXACTLY), makeSizeSpec(0, UNSPECIFIED), 0.5f, size);

  assertThat(size.width).isEqualTo(10);
  assertThat(size.height).isEqualTo(20);
}
 
Example #30
Source File: MeasureUtilsTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testExactlyAtMostLargerEqual() {
  final Size size = new Size();
  measureWithEqualDimens(makeSizeSpec(20, EXACTLY), makeSizeSpec(30, AT_MOST), size);
  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);

  measureWithEqualDimens(makeSizeSpec(30, AT_MOST), makeSizeSpec(20, EXACTLY), size);
  assertThat(size.width).isEqualTo(20);
  assertThat(size.height).isEqualTo(20);
}