Skip to content

Commit

Permalink
Add dirsync for rendercore-testing utils
Browse files Browse the repository at this point in the history
Summary: We can adapt these for testing in Litho

Reviewed By: adityasharat

Differential Revision: D21426340

fbshipit-source-id: 5775c7dc61eca4f7a1658455a24f97547e02eae9
  • Loading branch information
astreet authored and facebook-github-bot committed May 6, 2020
1 parent 78cfc91 commit 8b4a868
Show file tree
Hide file tree
Showing 11 changed files with 877 additions and 0 deletions.
24 changes: 24 additions & 0 deletions litho-rendercore-testing/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load(
"//tools/build_defs/oss:litho_defs.bzl",
"LITHO_RENDERCORE_TARGET",
"LITHO_ANDROIDSUPPORT_TARGET",
"LITHO_ASSERTJ_TARGET",
"LITHO_INFERANNOTATIONS_TARGET",
"LITHO_JUNIT_TARGET",
"litho_android_library",
)

litho_android_library(
name = "litho-rendercore-testing",
srcs = glob(["src/main/java/**/*.java"]),
visibility = [
"PUBLIC",
],
deps = [
LITHO_RENDERCORE_TARGET,
LITHO_ANDROIDSUPPORT_TARGET,
LITHO_ASSERTJ_TARGET,
LITHO_INFERANNOTATIONS_TARGET,
LITHO_JUNIT_TARGET,
],
)
68 changes: 68 additions & 0 deletions litho-rendercore-testing/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.
*/

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion

useLibrary 'org.apache.http.legacy'

defaultConfig {
minSdkVersion rootProject.minSdkVersion
}

buildTypes {
def internalBuildField = 'IS_INTERNAL_BUILD'

debug {
buildConfigField 'boolean', internalBuildField, 'true'
}

release {
buildConfigField 'boolean', internalBuildField, 'false'
}
}

// TODO(#62): Re-enable abort on error.
lintOptions {
abortOnError false
}
}

dependencies {
api project(':litho-rendercore')

// Annotations
compileOnly deps.jsr305
implementation deps.inferAnnotations

compileOnly deps.proguardAnnotations

// Android Support Library
compileOnly deps.supportAnnotations
implementation deps.supportCore
}

apply from: rootProject.file('gradle/release.gradle')

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
archiveClassifier.set('sources')
}
artifacts.add('archives', sourcesJar)
18 changes: 18 additions & 0 deletions litho-rendercore-testing/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

POM_NAME=LithoRenderCoreTesting
POM_DESCRIPTION=Testing library for rendercore
POM_ARTIFACT_ID=litho-rendercore-testing
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.testing;

import android.util.Pair;
import android.view.View;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.RenderState;
import com.facebook.rendercore.RenderTree;
import com.facebook.rendercore.RootHost;

/**
* Utility methods to render a RenderCore {@link Node} into a {@link RootHost}.
*
* <pre>
* Example:
*
* Node rootNode = ...;
* RootHostView rootHostView = new RootHostView(RuntimeEnvironment.application);
*
* RendercoreTestDriver.forHost(rootHostView)
* .withNode(root)
* .layoutWithBounds(500, 500)
* .render();
* </pre>
*/
public class RendercoreTestDriver {

public static NodeBuilderPart forHost(RootHost rootHost) {
return new Builder(rootHost);
}

public interface NodeBuilderPart {

/** Supply the root Node of the tree. */
LayoutBuilderPart withNode(Node rootNode);
}

public interface LayoutBuilderPart {

/** Measure the RootHost with these specs. */
RenderBuilderPart layoutWithSpecs(int widthSpec, int heightSpec);

/** Equivalent to {@link #layoutWithSpecs} using EXACT specs. */
RenderBuilderPart layoutWithBounds(int widthPx, int heightPx);
}

public interface RenderBuilderPart {

/** Render into the RootHost using the supplied configuration. */
void render();
}

private static class Builder implements NodeBuilderPart, LayoutBuilderPart, RenderBuilderPart {

private final RootHost mRootHost;
private Node mRootNode;
private int mWidthSpec;
private int mHeightSpec;

private Builder(RootHost rootHost) {
mRootHost = rootHost;
}

@Override
public LayoutBuilderPart withNode(Node rootNode) {
mRootNode = rootNode;
return this;
}

@Override
public RenderBuilderPart layoutWithSpecs(int widthSpec, int heightSpec) {
mWidthSpec = widthSpec;
mHeightSpec = heightSpec;
return this;
}

@Override
public RenderBuilderPart layoutWithBounds(int widthPx, int heightPx) {
mWidthSpec = View.MeasureSpec.makeMeasureSpec(widthPx, View.MeasureSpec.EXACTLY);
mHeightSpec = View.MeasureSpec.makeMeasureSpec(heightPx, View.MeasureSpec.EXACTLY);
return this;
}

@Override
public void render() {
View rootHostAsView = (View) mRootHost;

RenderState renderState =
new RenderState(
rootHostAsView.getContext(),
new RenderState.Delegate() {
@Override
public void commit(
int layoutVersion,
RenderTree current,
RenderTree next,
Object currentState,
Object nextState) {}

@Override
public void commitToUI(RenderTree tree, Object o) {}
});

mRootHost.setRenderState(renderState);

renderState.setTree(
new RenderState.LazyTree() {
@Override
public Pair resolve() {
return new Pair(mRootNode, null);
}
});

rootHostAsView.measure(mWidthSpec, mHeightSpec);
rootHostAsView.layout(
0, 0, rootHostAsView.getMeasuredWidth(), rootHostAsView.getMeasuredHeight());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.testing;

import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.RenderUnit;
import java.util.ArrayList;
import java.util.List;

public class SimpleLayoutResult implements Node.LayoutResult {

private final Node mNode;
private final int mX;
private final int mY;
private final int mWidth;
private final int mHeight;
private final List<Node.LayoutResult> mChildren;
private final Object mLayoutData;

public SimpleLayoutResult(Node node, Object layoutData, int x, int y, int width, int height) {
mNode = node;
mX = x;
mY = y;
mWidth = width;
mHeight = height;
mChildren = new ArrayList<>();
mLayoutData = layoutData;
}

public SimpleLayoutResult(Node node, int x, int y, int width, int height) {
this(node, null, x, y, width, height);
}

@Override
public RenderUnit getRenderUnit() {
return mNode.getRenderUnit();
}

@Nullable
@Override
public Object getLayoutData() {
return mLayoutData;
}

@Override
public int getChildrenCount() {
return mChildren.size();
}

@Override
public Node.LayoutResult getChildAt(int index) {
return mChildren.get(index);
}

@Override
public int getXForChildAtIndex(int index) {
return ((SimpleLayoutResult) mChildren.get(index)).mX;
}

@Override
public int getYForChildAtIndex(int index) {
return ((SimpleLayoutResult) mChildren.get(index)).mY;
}

@Override
public int getWidth() {
return mWidth;
}

@Override
public int getHeight() {
return mHeight;
}

@Override
public int getPaddingTop() {
return 0;
}

@Override
public int getPaddingRight() {
return 0;
}

@Override
public int getPaddingBottom() {
return 0;
}

@Override
public int getPaddingLeft() {
return 0;
}

@Override
public int getWidthSpec() {
return View.MeasureSpec.makeMeasureSpec(mWidth, View.MeasureSpec.EXACTLY);
}

@Override
public int getHeightSpec() {
return View.MeasureSpec.makeMeasureSpec(mHeight, View.MeasureSpec.EXACTLY);
}

public List<Node.LayoutResult> getChildren() {
return mChildren;
}
}
Loading

0 comments on commit 8b4a868

Please sign in to comment.