@Test public void testListenerInvokedForSetRoot() { mComponentTree.setNewLayoutStateReadyListener(mListener); mComponentTree.setRootAndSizeSpec(mComponent, mWidthSpec, mHeightSpec); verify(mListener).onNewLayoutStateReady(mComponentTree); }
@Test public void testThreadSafeConcurrentPropListComponentAccess() { TestDrawableComponent testComponent = Mockito.spy(TestDrawableComponent.create(mContext).build()); List<Component> componentList = new ArrayList<>(); for (int i = 0; i < 10; i++) { componentList.add(testComponent); } final Component root = TestWrappedComponentProp.create(mContext).componentList(componentList).build(); final ComponentTree componentTree = ComponentTree.create(mContext, root).build(); componentTree.setRootAndSizeSpec( TestWrappedComponentProp.create(mContext).componentList(componentList).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), null); componentTree.setRootAndSizeSpecAsync( TestWrappedComponentProp.create(mContext).componentList(componentList).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY)); mLayoutThreadShadowLooper.runToEndOfTasks(); verify(testComponent, times(19)).makeShallowCopy(); } }
@Test public void testMainThreadLayoutSet() { mComponentTree.setRootAndSizeSpec(mComponent, mWidthSpec, mHeightSpec); assertThat(mComponentTree.hasCompatibleLayout(mWidthSpec, mHeightSpec)).isTrue(); assertThat(mComponentTree.hasCompatibleLayout(mWidthSpec2, mHeightSpec2)).isFalse(); }
@Test public void testThreadSafeConcurrentPropComponentAccess() { TestDrawableComponent testComponent = Mockito.spy(TestDrawableComponent.create(mContext).build()); final TestWrappedComponentPropSpec.ComponentWrapper wrapper = new TestWrappedComponentPropSpec.ComponentWrapper(testComponent); final Component root = TestWrappedComponentProp.create(mContext).wrapper(wrapper).build(); final ComponentTree componentTree = ComponentTree.create(mContext, root).build(); componentTree.setRootAndSizeSpec( TestWrappedComponentProp.create(mContext).wrapper(wrapper).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), null); componentTree.setRootAndSizeSpecAsync( TestWrappedComponentProp.create(mContext).wrapper(wrapper).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY)); mLayoutThreadShadowLooper.runToEndOfTasks(); verify(testComponent).makeShallowCopy(); }
@Test public void testSplitMainThreadLayouts() { SplitLayoutResolver.createForTag(splitTag, mMainConfig, mBgConfig, mEnabledComponent); SplitLayoutResolver resolver = SplitLayoutResolver.getForTag(splitTag); when(ThreadUtils.isMainThread()).thenReturn(true); Whitebox.setInternalState(resolver, "mainService", mainService); Whitebox.setInternalState(resolver, "bgService", bgService); ComponentTree tree = ComponentTree.create(mContext, mComponent).splitLayoutTag(splitTag).build(); tree.setRootAndSizeSpec( mComponent, makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), new Size()); verify(mainService).submit(any(Runnable.class), eq(0)); verify(mainService).submit(any(Runnable.class), eq(1)); verify(mainService, never()).submit(any(Runnable.class), eq(2)); verify(bgService, never()).submit(any(Runnable.class), any()); }
public void computeLayoutSync( ComponentContext context, int widthSpec, int heightSpec, Size size) { final ComponentTree componentTree; final Component component; final TreeProps treeProps; synchronized (this) { if (mRenderInfo.rendersView()) { // Nothing to do for views. return; } mLastRequestedWidthSpec = widthSpec; mLastRequestedHeightSpec = heightSpec; ensureComponentTree(context); componentTree = mComponentTree; component = mRenderInfo.getComponent(); treeProps = mRenderInfo instanceof TreePropsWrappedRenderInfo ? ((TreePropsWrappedRenderInfo) mRenderInfo).getTreeProps() : null; } componentTree.setRootAndSizeSpec(component, widthSpec, heightSpec, size, treeProps); synchronized (this) { if (componentTree == mComponentTree && component == mRenderInfo.getComponent()) { mIsTreeValid = true; if (size != null) { mLastMeasuredHeight = size.height; } } } }
childComponentTree.setRootAndSizeSpec( childComponent, widthSpec, SizeSpec.makeSizeSpec(0, UNSPECIFIED), size);
@Test public void testRootWrapperComponent() { final Component component = TestLayoutComponent.create(mContext).build(); final Component wrapperComponent = TestLayoutComponent.create(mContext).build(); ErrorBoundariesConfiguration.rootWrapperComponentFactory = new RootWrapperComponentFactory() { @Override public Component createWrapper(ComponentContext c, Component root) { return wrapperComponent; } }; ComponentTree componentTree = ComponentTree.create(mContext, component).build(); componentTree.setRootAndSizeSpec(mComponent, mWidthSpec, mHeightSpec); assertThat(componentTree.getRoot()).isEqualTo(wrapperComponent); }
@Test public void testRemountAfterSettingNewRootTwice() { final TestComponent component1 = create(mContext).color(Color.RED).returnSelfInMakeShallowCopy().build(); final TestComponent component2 = create(mContext).returnSelfInMakeShallowCopy().color(Color.BLUE).build(); final LithoView lithoView = new LithoView(mContext); final ComponentTree componentTree = ComponentTree.create(mContext, Column.create(mContext).child(component1).build()).build(); mountComponent( lithoView, componentTree, makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(component1.isMounted()).isTrue(); componentTree.setRootAndSizeSpec( Column.create(mContext).child(component2).build(), makeMeasureSpec(50, EXACTLY), makeMeasureSpec(50, EXACTLY)); componentTree.setSizeSpec(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY)); assertThat(component2.isMounted()).isTrue(); }
@Test public void testSetCompatibleSizeSpecWithDifferentRoot() { ComponentTree componentTree = create(mContext, mComponent) .build(); Size size = new Size(); componentTree.setSizeSpec( makeSizeSpec(100, AT_MOST), makeSizeSpec(100, AT_MOST), size); assertEquals(100, size.width, 0.0); assertEquals(100, size.height, 0.0); LayoutState firstLayoutState = componentTree.getBackgroundLayoutState(); assertThat(firstLayoutState).isNotNull(); componentTree.setRootAndSizeSpec( TestDrawableComponent.create(mContext).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), size); assertNotEquals(firstLayoutState, componentTree.getBackgroundLayoutState()); }
.build(); componentTree.setRootAndSizeSpec( newComponent, makeMeasureSpec(400, EXACTLY), makeMeasureSpec(400, EXACTLY));
.build(); componentTree.setRootAndSizeSpec( newComponent, makeMeasureSpec(400, EXACTLY), makeMeasureSpec(400, EXACTLY));
.build(); componentTree.setRootAndSizeSpec( newComponent, makeMeasureSpec(400, EXACTLY), makeMeasureSpec(400, EXACTLY));
@Test public void testSetRootAndSizeSpecWithTreeProps() { ComponentTree componentTree = create(mContext, mComponent).build(); final Size size = new Size(); final TreeProps treeProps = new TreeProps(); treeProps.put(Object.class, "hello world"); componentTree.setRootAndSizeSpec( TestDrawableComponent.create(mContext).build(), makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY), size, treeProps); final ComponentContext c = getInternalState(componentTree.getBackgroundLayoutState(), "mContext"); assertThat(c.getTreeProps()).isSameAs(treeProps); }