public static Builder create(ComponentContext context, Component.Builder<?> root) { return create(context, root.build()); }
/** * Create a new {@link LithoView} instance and initialize it * with the given {@link Component} root. * * @param context {@link ComponentContext}. * @param component The root component to draw. * @return {@link LithoView} able to render a {@link Component} hierarchy. */ public static LithoView create(ComponentContext context, Component component) { final LithoView lithoView = new LithoView(context); lithoView.setComponentTree(ComponentTree.create(context, component).build()); return lithoView; }
/** * Change the root component measuring it on a background thread before updating the UI. * If this {@link LithoView} doesn't have a ComponentTree initialized, the root will be * computed synchronously. */ public void setComponentAsync(Component component) { if (mComponentTree == null) { setComponentTree(ComponentTree.create(getComponentContext(), component).build()); } else { mComponentTree.setRootAsync(component); } }
/** * Change the root component synchronously. */ public void setComponent(Component component) { if (mComponentTree == null) { setComponentTree(ComponentTree.create(getComponentContext(), component).build()); } else { mComponentTree.setRoot(component); } }
void mount(Component component, int width, int height) { if (mLithoView.getComponentTree() == null) { mLithoView.setComponentTree( ComponentTree.create(mLithoView.getComponentContext(), component) .incrementalMount(false) .build()); } else { mLithoView.setComponent(component); } mComponentWidth = width; mComponentHeight = height; }
private void buildAndSetComponentTree(boolean useRT) { final ComponentContext context = new ComponentContext(this); final Component component = RTAnimationComponent.create(context).useRT(useRT).build(); mLithoView.setComponentTree(ComponentTree.create(context, component).build()); }
@Before public void setup() throws Exception { mLooper = mock(Looper.class); mComponentsLogger = mock(ComponentsLogger.class); mContext = new ComponentContext(RuntimeEnvironment.application, mLogTag, mComponentsLogger); mRoot = TestLayoutComponent.create(mContext) .build(); mComponentTreeBuilder = ComponentTree.create(mContext, mRoot); }
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 testSetSizeSpec() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent) .build(); componentTree.setSizeSpec(mWidthSpec, mHeightSpec); // Since this happens post creation, it's not in general safe to update the main thread layout // state synchronously, so the result should be in the background layout state postSizeSpecChecks(componentTree, "mBackgroundLayoutState"); }
@Test public void testSetSizeSpecWithOutput() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent) .build(); Size size = new Size(); componentTree.setSizeSpec(mWidthSpec, mHeightSpec, size); assertEquals(SizeSpec.getSize(mWidthSpec), size.width, 0.0); assertEquals(SizeSpec.getSize(mHeightSpec), size.height, 0.0); // Since this happens post creation, it's not in general safe to update the main thread layout // state synchronously, so the result should be in the background layout state postSizeSpecChecks(componentTree, "mBackgroundLayoutState"); }
@Test public void testOnlyMainEnabled() { SplitLayoutResolver.createForTag(splitTag, mMainConfig, null, mEnabledComponent); SplitLayoutResolver resolver = SplitLayoutResolver.getForTag(splitTag); when(ThreadUtils.isMainThread()).thenReturn(false); Whitebox.setInternalState(resolver, "mainService", mainService); final ComponentTree tree = ComponentTree.create(mContext, mComponent).splitLayoutTag(splitTag).build(); tree.setRootAndSizeSpecAsync( mComponent, makeSizeSpec(100, EXACTLY), makeSizeSpec(100, EXACTLY)); mLayoutThreadShadowLooper.runOneTask(); verify(bgService, never()).submit(any(Runnable.class), eq(0)); } }
private ComponentTree createComponentTree(boolean useSpy, boolean incMountEnabled, int width, int height) { ComponentTree componentTree = ComponentTree.create(mContext, mComponent) .incrementalMount(incMountEnabled) .layoutDiffing(false) .build(); componentTree.setSizeSpec( SizeSpec.makeSizeSpec(width, EXACTLY), SizeSpec.makeSizeSpec(height, EXACTLY)); return useSpy ? spy(componentTree) : componentTree; }
@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); }
@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); }
@Before public void setup() throws Exception { mLithoView = new LithoView(RuntimeEnvironment.application); mComponent = LifecycleMethodsComponent.create(10); final ComponentContext c = new ComponentContext(RuntimeEnvironment.application); mComponentTree = ComponentTree.create(c, mComponent).build(); mLithoView.setComponentTree(mComponentTree); }
@Test public void testSetRootAfterRelease() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); componentTree.release(); // Verify we don't crash componentTree.setRoot(TestDrawableComponent.create(mContext).build()); }
@Test public void testCachedValues() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent).build(); assertThat(componentTree.getCachedValue("key1")).isNull(); componentTree.putCachedValue("key1", "value1"); assertThat(componentTree.getCachedValue("key1")).isEqualTo("value1"); assertThat(componentTree.getCachedValue("key2")).isNull(); }
@Before public void setup() throws Exception { mContext = new ComponentContext(RuntimeEnvironment.application); mComponent = TestDrawableComponent.create(mContext).build(); mComponentTree = create(mContext, mComponent).build(); mLayoutThreadShadowLooper = Shadows.shadowOf( (Looper) Whitebox.invokeMethod(ComponentTree.class, "getDefaultLayoutThreadLooper")); mWidthSpec = makeSizeSpec(39, EXACTLY); mWidthSpec2 = makeSizeSpec(40, EXACTLY); mHeightSpec = makeSizeSpec(41, EXACTLY); mHeightSpec2 = makeSizeSpec(42, EXACTLY); mListener = mock(ComponentTree.NewLayoutStateReadyListener.class); }
@Test public void testCreate() { ComponentTree componentTree = ComponentTree.create(mContext, mComponent) .build(); creationCommonChecks(componentTree); // Both the main thread and the background layout state shouldn't be calculated yet. Assert.assertNull(Whitebox.getInternalState(componentTree, "mMainThreadLayoutState")); Assert.assertNull(Whitebox.getInternalState(componentTree, "mBackgroundLayoutState")); Assert.assertFalse(componentTreeHasSizeSpec(componentTree)); }
@Before public void setup() throws Exception { mContext = new ComponentContext(RuntimeEnvironment.application); mComponent = TestDrawableComponent.create(mContext).build(); mComponentTree = create(mContext, mComponent).build(); mLayoutThreadShadowLooper = Shadows.shadowOf( (Looper) Whitebox.invokeMethod(ComponentTree.class, "getDefaultLayoutThreadLooper")); mWidthSpec = makeSizeSpec(39, EXACTLY); mWidthSpec2 = makeSizeSpec(40, EXACTLY); mHeightSpec = makeSizeSpec(41, EXACTLY); mHeightSpec2 = makeSizeSpec(42, EXACTLY); }