Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable OnCodeAnalysisFinishedListenerTest #701

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import android.graphics.drawable.Drawable;
import android.view.View;
import com.facebook.rendercore.RenderUnit;
import com.facebook.rendercore.transitions.TransitionRenderUnit;
import java.util.Map;

/** This {@link RenderUnit} encapsulates a Litho output to be mounted using Render Core. */
public class LithoRenderUnit extends RenderUnit<Object> {
public class LithoRenderUnit extends RenderUnit<Object> implements TransitionRenderUnit {

final LayoutOutput output;

Expand Down Expand Up @@ -81,6 +82,11 @@ private static RenderType getRenderType(LayoutOutput output) {
: RenderType.VIEW;
}

@Override
public boolean getMatchHostBounds() {
return (output.getFlags() & LayoutOutput.LAYOUT_FLAG_MATCH_HOST_BOUNDS) != 0;
}

public static class LithoMountBinder implements Binder<LithoRenderUnit, Object> {

public static final LithoMountBinder INSTANCE = new LithoMountBinder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@
import android.view.View;
import com.facebook.litho.AnimatableItem;
import com.facebook.litho.BoundsHelper;
import com.facebook.litho.ComponentHost;
import com.facebook.litho.LithoView;
import com.facebook.rendercore.Host;
import com.facebook.rendercore.MountItem;
import com.facebook.rendercore.RootHost;
import com.facebook.rendercore.transitions.TransitionRenderUnit;
import com.facebook.rendercore.utils.BoundsUtils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;

/** A convenience class for common View properties applicable to all subclasses of View. */
public final class AnimatedProperties {

/**
* The absolute X-position of a mount content, relative to the {@link
* com.facebook.litho.LithoView} that is rendering this component tree.
* The absolute X-position of a mount content, relative to the root {@link Host} that is rendering
* this component tree.
*/
public static final AnimatedProperty X = new XAnimatedProperty();

/**
* The absolute Y-position of a mount content, relative to the {@link
* com.facebook.litho.LithoView} that is rendering this component tree.
* The absolute Y-position of a mount content, relative to the root {@link Host} that is rendering
* this component tree.
*/
public static final AnimatedProperty Y = new YAnimatedProperty();

Expand Down Expand Up @@ -105,13 +109,13 @@ public String getName() {

@Override
public float get(Object mountContent) {
if (mountContent instanceof LithoView) {
return ((LithoView) mountContent).getX();
if (mountContent instanceof Host && mountContent instanceof RootHost) {
return ((Host) mountContent).getX();
} else if (mountContent instanceof View) {
return getPositionRelativeToLithoView((View) mountContent, true);
return getPositionRelativeToRootHost((View) mountContent, true);
} else if (mountContent instanceof Drawable) {
final Drawable drawable = (Drawable) mountContent;
float parentX = getPositionRelativeToLithoView(getHostView(drawable), true);
float parentX = getPositionRelativeToRootHost(getHostView(drawable), true);
return parentX + drawable.getBounds().left;
} else {
throw new UnsupportedOperationException(
Expand All @@ -126,15 +130,15 @@ public float get(AnimatableItem animatableItem) {

@Override
public void set(Object mountContent, float value) {
if (mountContent instanceof LithoView) {
if (mountContent instanceof Host && mountContent instanceof RootHost) {
((View) mountContent).setX(value);
} else if (mountContent instanceof View) {
final View view = (View) mountContent;
float parentX = getPositionRelativeToLithoView((View) view.getParent(), true);
float parentX = getPositionRelativeToRootHost((View) view.getParent(), true);
view.setX(value - parentX);
} else if (mountContent instanceof Drawable) {
final Drawable drawable = (Drawable) mountContent;
float parentX = getPositionRelativeToLithoView(getHostView(drawable), true);
float parentX = getPositionRelativeToRootHost(getHostView(drawable), true);
BoundsHelper.applyXYToDrawableForAnimation(
drawable, (int) (value - parentX), drawable.getBounds().top);
} else {
Expand Down Expand Up @@ -162,13 +166,13 @@ public String getName() {

@Override
public float get(Object mountContent) {
if (mountContent instanceof LithoView) {
return ((LithoView) mountContent).getY();
if (mountContent instanceof Host && mountContent instanceof RootHost) {
return ((Host) mountContent).getY();
} else if (mountContent instanceof View) {
return getPositionRelativeToLithoView((View) mountContent, false);
return getPositionRelativeToRootHost((View) mountContent, false);
} else if (mountContent instanceof Drawable) {
final Drawable drawable = (Drawable) mountContent;
float parentY = getPositionRelativeToLithoView(getHostView(drawable), false);
float parentY = getPositionRelativeToRootHost(getHostView(drawable), false);
return parentY + drawable.getBounds().top;
} else {
throw new UnsupportedOperationException(
Expand All @@ -183,15 +187,15 @@ public float get(AnimatableItem animatableItem) {

@Override
public void set(Object mountContent, float value) {
if (mountContent instanceof LithoView) {
if (mountContent instanceof Host && mountContent instanceof RootHost) {
((View) mountContent).setY(value);
} else if (mountContent instanceof View) {
final View view = (View) mountContent;
float parentY = getPositionRelativeToLithoView((View) view.getParent(), false);
float parentY = getPositionRelativeToRootHost((View) view.getParent(), false);
view.setY(value - parentY);
} else if (mountContent instanceof Drawable) {
final Drawable drawable = (Drawable) mountContent;
float parentY = getPositionRelativeToLithoView(getHostView(drawable), false);
float parentY = getPositionRelativeToRootHost(getHostView(drawable), false);
BoundsHelper.applyXYToDrawableForAnimation(
drawable, drawable.getBounds().left, (int) (value - parentY));
} else {
Expand Down Expand Up @@ -236,8 +240,8 @@ public float get(AnimatableItem animatableItem) {

@Override
public void set(Object mountContent, float value) {
if (mountContent instanceof ComponentHost) {
final ComponentHost view = (ComponentHost) mountContent;
if (mountContent instanceof Host) {
final Host view = (Host) mountContent;
if (view instanceof LithoView) {
((LithoView) view).setAnimatedWidth((int) value);
} else {
Expand All @@ -246,7 +250,7 @@ public void set(Object mountContent, float value) {
left, view.getTop(), (int) (left + value), view.getBottom(), null, view, false);
}

final List<Drawable> animatingDrawables = view.getLinkedDrawablesForAnimation();
final List<Drawable> animatingDrawables = getLinkedDrawables(view);
if (animatingDrawables != null) {
final int width = (int) value;
final int height = view.getHeight();
Expand Down Expand Up @@ -303,8 +307,8 @@ public float get(AnimatableItem animatableItem) {

@Override
public void set(Object mountContent, float value) {
if (mountContent instanceof ComponentHost) {
final ComponentHost view = (ComponentHost) mountContent;
if (mountContent instanceof Host) {
final Host view = (Host) mountContent;
if (view instanceof LithoView) {
((LithoView) view).setAnimatedHeight((int) value);
} else {
Expand All @@ -313,7 +317,7 @@ public void set(Object mountContent, float value) {
view.getLeft(), top, view.getRight(), (int) (top + value), null, view, false);
}

final List<Drawable> animatingDrawables = view.getLinkedDrawablesForAnimation();
final List<Drawable> animatingDrawables = getLinkedDrawables(view);
if (animatingDrawables != null) {
final int width = view.getWidth();
final int height = (int) value;
Expand Down Expand Up @@ -501,17 +505,17 @@ public void reset(Object mountContent) {
}

/**
* @return the x or y position of the given view relative to the LithoView that this ComponentTree
* is being rendered in to.
* @return the x or y position of the given view relative to the root {@link Host} that this
* ComponentTree is being rendered in to.
*/
private static float getPositionRelativeToLithoView(View mountContent, boolean getX) {
private static float getPositionRelativeToRootHost(View mountContent, boolean getX) {
float pos = 0;
@Nullable View currentView = mountContent;
while (true) {
if (currentView == null || !(currentView.getParent() instanceof View)) {
return pos;
}
if (currentView instanceof LithoView) {
if (currentView instanceof Host && currentView instanceof RootHost) {
return pos;
}
pos += getX ? currentView.getX() : currentView.getY();
Expand All @@ -533,4 +537,22 @@ private static View getHostView(Drawable drawable) {
}
}
}

private static @Nullable List<Drawable> getLinkedDrawables(Host host) {
List<Drawable> drawables = null;

for (int i = 0, size = host.getMountItemCount(); i < size; i++) {
final MountItem mountItem = host.getMountItemAt(i);
if (mountItem.getContent() instanceof Drawable
&& mountItem.getRenderTreeNode().getRenderUnit() instanceof TransitionRenderUnit
&& ((TransitionRenderUnit) mountItem.getRenderTreeNode().getRenderUnit())
.getMatchHostBounds()) {
if (drawables == null) {
drawables = new ArrayList<>();
}
drawables.add((Drawable) mountItem.getContent());
}
}
return drawables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public OnCodeAnalysisFinishedListenerTest() {
super("testdata/actions");
}

@Ignore("T73932936")
@Test
public void daemonFinished_settingsTrue_resolved() throws IOException {
final PsiFile specPsiFile = testHelper.configure("LayoutSpec.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,7 @@ public void animationProperties_animatingPropertyX_elementShouldAnimateInTheXAxi
.TestComponent() { // This could be a lambda but it fails ci.
@Override
public Component getComponent(ComponentContext componentContext, boolean state) {
return Row.create(componentContext)
.heightDip(200)
.widthDip(200)
.justifyContent(state ? YogaJustify.FLEX_START : YogaJustify.FLEX_END)
.alignItems(state ? YogaAlign.FLEX_START : YogaAlign.FLEX_END)
.child(
Row.create(componentContext)
.heightDip(40)
.widthDip(40)
.backgroundColor(Color.parseColor("#ee1111"))
.transitionKey(TRANSITION_KEY)
.viewTag(TRANSITION_KEY)
.build())
.build();
return getAnimatingXPropertyComponent();
}
})
.build();
Expand Down Expand Up @@ -1192,6 +1179,86 @@ public Component getComponent(ComponentContext c, boolean state) {
mTransitionTestRule.step(1000);
}

@Test
public void
animationProperties_animatingPropertyOnRootComponent_elementShouldAnimateInTheXAxis() {
final TestAnimationsComponent component =
TestAnimationsComponent.create(mLithoViewRule.getContext())
.stateCaller(mStateCaller)
.transition(
Transition.create(TRANSITION_KEY)
.animator(Transition.timing(144))
.animate(AnimatedProperties.X)
.animate(AnimatedProperties.Y)
.animate(AnimatedProperties.WIDTH)
.animate(AnimatedProperties.HEIGHT))
.testComponent(
new TestAnimationsComponentSpec
.TestComponent() { // This could be a lambda but it fails ci.
@Override
public Component getComponent(ComponentContext componentContext, boolean state) {
return Row.create(componentContext)
.heightDip(state ? 200 : 100)
.widthDip(state ? 200 : 100)
.backgroundColor(Color.RED)
.positionPx(YogaEdge.LEFT, !state ? 0 : 100)
.positionPx(YogaEdge.TOP, !state ? 0 : 100)
.transitionKey(TRANSITION_KEY)
.build();
}
})
.build();
mLithoViewRule.setRoot(component);
mActivityController.get().setContentView(mLithoViewRule.getLithoView());
mActivityController.resume().visible();

View lithoView = mLithoViewRule.getLithoView();

// 160 is equal to height and width of 200 - 40 for the size of the row.
assertThat(lithoView.getX())
.describedAs("view X axis should be at start position")
.isEqualTo(0);
assertThat(lithoView.getY())
.describedAs("view Y axis should be at start position")
.isEqualTo(0);
assertThat(lithoView.getWidth())
.describedAs("view Width should be at start position")
.isEqualTo(320);
assertThat(lithoView.getHeight())
.describedAs("view Height should be at start position")
.isEqualTo(422);

mStateCaller.update();

// X after state update should be at 0 because is going to be animated.
assertThat(lithoView.getX()).describedAs("view X axis after toggle").isEqualTo(0);
// Y after state update should be at 0 because is going to be animated.
assertThat(lithoView.getY()).describedAs("view Y axis after toggle").isEqualTo(0);
// Width after state update should be at 320 because is going to be animated.
assertThat(lithoView.getWidth()).describedAs("view Width after toggle").isEqualTo(320);
// Height after state update should be at 422 because is going to be animated.
assertThat(lithoView.getHeight()).describedAs("view Height after toggle").isEqualTo(422);
mTransitionTestRule.step(5);

// Check java doc for how we calculate this value.
assertThat(lithoView.getX()).describedAs("view X axis after 5 frames").isEqualTo(41.31759f);
assertThat(lithoView.getY()).describedAs("view Y axis after 5 frames").isEqualTo(28.238356f);
assertThat(lithoView.getWidth()).describedAs("view Width axis after 5 frames").isEqualTo(128);
assertThat(lithoView.getHeight()).describedAs("view Height axis after 5 frames").isEqualTo(128);

// Enough frames to finish all animations
mTransitionTestRule.step(500);

assertThat(lithoView.getX()).describedAs("view X axis after animation finishes").isEqualTo(100);
assertThat(lithoView.getY()).describedAs("view Y axis after animation finishes").isEqualTo(100);
assertThat(lithoView.getWidth())
.describedAs("view Width after animation finishes")
.isEqualTo(200);
assertThat(lithoView.getHeight())
.describedAs("view Height after animation finishes")
.isEqualTo(200);
}

private Component getAnimatingXPropertyComponent() {
return TestAnimationsComponent.create(mLithoViewRule.getContext())
.stateCaller(mStateCaller)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.rendercore.transitions;

public interface TransitionRenderUnit {
boolean getMatchHostBounds();
}