@VisibleForTesting @Override public void setLithoView(@NonNull LithoView view) { super.setLithoView(view); }
@Test public void testListenerNotInvokedWhenMeasureDoesntComputeALayout() { mComponentTree.setNewLayoutStateReadyListener(mListener); mComponentTree.setLithoView(new LithoView(mContext)); mComponentTree.attach(); mComponentTree.setSizeSpec(mWidthSpec, mHeightSpec); verify(mListener).onNewLayoutStateReady(mComponentTree); reset(mListener); mComponentTree.measure(mWidthSpec, mHeightSpec, new int[] {0, 0}, false); verify(mListener, never()).onNewLayoutStateReady(mComponentTree); }
@Test public void testListenerInvokedForSetRootAsyncWhenAttached() { mComponentTree.setNewLayoutStateReadyListener(mListener); mComponentTree.setSizeSpecAsync(mWidthSpec, mHeightSpec); mComponentTree.setLithoView(new LithoView(mContext)); mComponentTree.attach(); verify(mListener, never()).onNewLayoutStateReady(any(ComponentTree.class)); // Now the background thread run the queued task. mLayoutThreadShadowLooper.runToEndOfTasks(); verify(mListener).onNewLayoutStateReady(mComponentTree); }
@Test public void testListenerInvokedForMeasure() { mComponentTree.setNewLayoutStateReadyListener(mListener); mComponentTree.setLithoView(new LithoView(mContext)); mComponentTree.attach(); mComponentTree.setSizeSpec(mWidthSpec, mHeightSpec); verify(mListener).onNewLayoutStateReady(mComponentTree); reset(mListener); mComponentTree.measure(mWidthSpec2, mHeightSpec2, new int[] {0, 0}, false); verify(mListener).onNewLayoutStateReady(mComponentTree); }
@Test public void testListenerNotInvokedWhenNewMeasureSpecsAreCompatible() { mComponentTree.setLithoView(new LithoView(mContext)); mComponentTree.attach(); mComponentTree.setSizeSpec(mWidthSpec, mHeightSpec); mComponentTree.setNewLayoutStateReadyListener(mListener); mComponentTree.setSizeSpec(mWidthSpec, mHeightSpec); verify(mListener, never()).onNewLayoutStateReady(mComponentTree); } }
+ mComponentTree.getReleasedComponent()); mComponentTree.setLithoView(this);
@Test public void testSetSizeSpecWithOutputWhenAttachedToViewWithNewSpec() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); Size size = new Size(); componentTree.measure(mWidthSpec2, mHeightSpec2, new int[2], false); componentTree.attach(); componentTree.setSizeSpec(mWidthSpec, mHeightSpec, size); assertEquals(SizeSpec.getSize(mWidthSpec), size.width, 0.0); assertEquals(SizeSpec.getSize(mHeightSpec), size.height, 0.0); assertThat(componentTree.hasCompatibleLayout(mWidthSpec, mHeightSpec)).isTrue(); assertThat(componentTree.getRoot()).isEqualTo(mComponent); }
@Test public void testSetSizeSpecWithOutputWhenAttachedToViewWithSameSpec() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); Size size = new Size(); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach(); componentTree.setSizeSpec(mWidthSpec, mHeightSpec, size); assertEquals(SizeSpec.getSize(mWidthSpec), size.width, 0.0); assertEquals(SizeSpec.getSize(mHeightSpec), size.height, 0.0); assertThat(componentTree.hasCompatibleLayout(mWidthSpec, mHeightSpec)).isTrue(); assertThat(componentTree.getRoot()).isEqualTo(mComponent); }
.build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach();
.build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach();
.build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach();
@Test public void testSetRootAsyncFollowedByNonCompatibleMeasureComputesSyncLayout() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach(); Component newComponent = TestDrawableComponent.create(mContext).color(1234).build(); componentTree.setRootAsync(newComponent); componentTree.measure(mWidthSpec2, mHeightSpec2, new int[2], false); assertThat(componentTree.getRoot()).isEqualTo(newComponent); assertThat(componentTree.hasCompatibleLayout(mWidthSpec2, mHeightSpec2)).isTrue(); assertThat(componentTree.getMainThreadLayoutState().isForComponentId(newComponent.getId())) .isTrue(); // Clear tasks mLayoutThreadShadowLooper.runToEndOfTasks(); }
@Test public void testMeasureWithIncompatibleSetRootAsync() throws InterruptedException { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext));
@Test public void testSetRootAsyncFollowedByMeasureDoesntComputeSyncLayout() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); componentTree.attach(); Component newComponent = TestDrawableComponent.create(mContext).color(1234).build(); componentTree.setRootAsync(newComponent); assertThat(componentTree.getRoot()).isEqualTo(newComponent); componentTree.measure(mWidthSpec, mHeightSpec, new int[2], false); assertThat(componentTree.getMainThreadLayoutState().isForComponentId(mComponent.getId())) .isTrue(); mLayoutThreadShadowLooper.runToEndOfTasks(); assertThat(componentTree.getMainThreadLayoutState().isForComponentId(newComponent.getId())) .isTrue(); }
@Test public void testMeasureWithIncompatibleSetRootAsyncBeforeStart() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext));
@Test public void testMeasureWithIncompatibleSetRootAsyncThatFinishes() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); int widthSpec1 = SizeSpec.makeSizeSpec(1000, SizeSpec.EXACTLY); int heightSpec1 = SizeSpec.makeSizeSpec(1000, SizeSpec.AT_MOST); int widthSpec2 = SizeSpec.makeSizeSpec(1000, SizeSpec.EXACTLY); int heightSpec2 = SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED); componentTree.measure(widthSpec2, heightSpec2, new int[2], false); componentTree.attach(); componentTree.measure(widthSpec1, heightSpec1, new int[2], false); Component newComponent = TestDrawableComponent.create(mContext).color(1234).build(); componentTree.setRootAsync(newComponent); runOnBackgroundThreadSync( new Runnable() { @Override public void run() { // "Commit" layout (it will fail since it doesn't have compatible size specs) mLayoutThreadShadowLooper.runToEndOfTasks(); } }); componentTree.measure(widthSpec2, heightSpec2, new int[2], false); assertThat(componentTree.getRoot()).isEqualTo(newComponent); assertThat(componentTree.hasCompatibleLayout(widthSpec2, heightSpec2)).isTrue(); assertThat(componentTree.getMainThreadLayoutState().isForComponentId(newComponent.getId())) .isTrue() .withFailMessage( "The main thread should calculate a new layout synchronously because the async layout will not be used once it completes"); }
@Test public void testSetRootWithTreePropsThenMeasure() { ComponentTree componentTree = create(mContext, mComponent).build(); componentTree.setLithoView(new LithoView(mContext)); componentTree.attach(); final TreeProps treeProps = new TreeProps(); treeProps.put(Object.class, "hello world"); componentTree.setRootAndSizeSpecAsync( TestDrawableComponent.create(mContext).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), treeProps); assertThat(componentTree.getBackgroundLayoutState()).isNull(); componentTree.measure(makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), new int[2], true); final ComponentContext c = getInternalState(componentTree.getMainThreadLayoutState(), "mContext"); assertThat(c.getTreeProps()).isNotNull(); assertThat(c.getTreeProps().get(Object.class)).isEqualTo(treeProps.get(Object.class)); }