Here are the examples of the java api com.facebook.litho.LithoView taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
103 Examples
19
Source : SectionsRecyclerView.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Wrapper that encapsulates all the features {@link RecyclerSpec} provides such as sticky header
* and pull-to-refresh
*/
public clreplaced SectionsRecyclerView extends SwipeRefreshLayout implements HasLithoViewChildren {
private final LithoView mStickyHeader;
private final RecyclerView mRecyclerView;
/**
* Indicates whether {@link RecyclerView} has been detached. In such case we need to make sure to
* relayout its children eventually.
*/
private boolean mHasBeenDetachedFromWindow = false;
/**
* When we set an ItemAnimator during mount, we want to store the one that was already set on the
* RecyclerView so that we can reset it during unmount.
*/
private ItemAnimator mDetachedItemAnimator;
public SectionsRecyclerView(Context context, RecyclerView recyclerView) {
super(context);
mRecyclerView = recyclerView;
// We need to draw first visible item on top of other children to support sticky headers
mRecyclerView.setChildDrawingOrderCallback(new RecyclerView.ChildDrawingOrderCallback() {
@Override
public int onGetChildDrawingOrder(int childCount, int i) {
return childCount - 1 - i;
}
});
// ViewCache doesn't work well with RecyclerBinder which replacedumes that whenever item comes back
// to viewport it should be rebound which does not happen with ViewCache. Consider this case:
// LithoView goes out of screen and it is added to ViewCache, then its ComponentTree is replacedigned
// to another LV which means our LithoView's ComponentTree reference is nullified. It comes back
// to screen and it is not rebound therefore we will see 0 height LithoView which actually
// happened in multiple product surfaces. Disabling it fixes the issue.
mRecyclerView.sereplacedemViewCacheSize(0);
addView(mRecyclerView);
mStickyHeader = new LithoView(new ComponentContext(getContext()), null);
mStickyHeader.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addView(mStickyHeader);
}
public RecyclerView getRecyclerView() {
return mRecyclerView;
}
public void setStickyComponent(ComponentTree component) {
if (component.getLithoView() != null) {
component.getLithoView().startTemporaryDetach();
}
mStickyHeader.setComponentTree(component);
measureStickyHeader(getWidth());
}
public LithoView getStickyHeader() {
return mStickyHeader;
}
public void setStickyHeaderVerticalOffset(int verticalOffset) {
mStickyHeader.setTranslationY(verticalOffset);
}
public void showStickyHeader() {
mStickyHeader.setVisibility(View.VISIBLE);
mStickyHeader.notifyVisibleBoundsChanged();
}
public void hideStickyHeader() {
mStickyHeader.unmountAllItems();
mStickyHeader.setVisibility(View.GONE);
}
public boolean isStickyHeaderHidden() {
return mStickyHeader.getVisibility() == View.GONE;
}
public void sereplacedemAnimator(@Nullable ItemAnimator itemAnimator) {
mDetachedItemAnimator = mRecyclerView.gereplacedemAnimator();
mRecyclerView.sereplacedemAnimator(itemAnimator);
}
public void resereplacedemAnimator() {
mRecyclerView.sereplacedemAnimator(mDetachedItemAnimator);
mDetachedItemAnimator = null;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureStickyHeader(MeasureSpec.getSize(widthMeasureSpec));
}
private void measureStickyHeader(int parentWidth) {
measureChild(mStickyHeader, MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY), MeasureSpec.UNSPECIFIED);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mStickyHeader.getVisibility() == View.GONE) {
return;
}
final int stickyHeaderLeft = getPaddingLeft();
final int stickyHeaderTop = getPaddingTop();
mStickyHeader.layout(stickyHeaderLeft, stickyHeaderTop, stickyHeaderLeft + mStickyHeader.getMeasuredWidth(), stickyHeaderTop + mStickyHeader.getMeasuredHeight());
}
@Nullable
static SectionsRecyclerView getParentRecycler(RecyclerView recyclerView) {
if (recyclerView.getParent() instanceof SectionsRecyclerView) {
return (SectionsRecyclerView) recyclerView.getParent();
}
return null;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHasBeenDetachedFromWindow = true;
}
boolean hasBeenDetachedFromWindow() {
return mHasBeenDetachedFromWindow;
}
void setHasBeenDetachedFromWindow(boolean hasBeenDetachedFromWindow) {
mHasBeenDetachedFromWindow = hasBeenDetachedFromWindow;
}
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
super.requestDisallowInterceptTouchEvent(disallowIntercept);
// SwipeRefreshLayout can ignore this request if nested scrolling is disabled on the child,
// but it fails to delegate the request up to the parents.
// This fixes a bug that can cause parents to improperly intercept scroll events from
// nested recyclers.
if (getParent() != null && !isNestedScrollingEnabled()) {
getParent().requestDisallowInterceptTouchEvent(disallowIntercept);
}
}
@Override
public void setOnTouchListener(OnTouchListener listener) {
// When setting touch handler for RecyclerSpec we want RecyclerView to handle it.
mRecyclerView.setOnTouchListener(listener);
}
@Override
public void obtainLithoViewChildren(List<LithoView> lithoViews) {
for (int i = 0, size = mRecyclerView.getChildCount(); i < size; i++) {
final View child = mRecyclerView.getChildAt(i);
if (child instanceof LithoView) {
lithoViews.add((LithoView) child);
}
}
}
}
19
Source : LithoViewRule.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Sets a new {@link LithoView} which should be used to render.
*/
public LithoViewRule useLithoView(LithoView lithoView) {
mLithoView = lithoView;
if (mLithoView.getComponentContext() != mContext) {
throw new RuntimeException("You must use the same ComponentContext for the LithoView as what is on the LithoViewRule @Rule!");
}
if (mComponentTree != null) {
mLithoView.setComponentTree(mComponentTree);
}
return this;
}
19
Source : LithoViewAssert.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public static LithoViewreplacedert replacedertThat(LithoView actual) {
return new LithoViewreplacedert(actual);
}
19
Source : TextInputSpecTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
private static EditText getEditText(LithoView lithoView) {
return (EditText) lithoView.getChildAt(0);
}
19
Source : ComponentActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Sets or replaces the ComponentTree being rendered in this Activity.
*/
public void setComponentTree(ComponentTree componentTree) {
final LithoView lithoView = new LithoView(this);
lithoView.setComponentTree(componentTree);
setContentView(lithoView);
}
19
Source : DebugComponentDescriptor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public void setHighlighted(DebugComponent node, boolean selected, boolean isAlignmentMode) {
final LithoView lithoView = node.getLithoView();
if (lithoView == null) {
return;
}
if (!selected) {
HighlightedOverlay.removeHighlight(lithoView);
return;
}
final DebugLayoutNode layout = node.getLayoutNode();
final boolean hasNode = layout != null;
final Rect margin;
if (!node.isRoot()) {
margin = new Rect(hasNode ? (int) layout.getResultMargin(YogaEdge.START) : 0, hasNode ? (int) layout.getResultMargin(YogaEdge.TOP) : 0, hasNode ? (int) layout.getResultMargin(YogaEdge.END) : 0, hasNode ? (int) layout.getResultMargin(YogaEdge.BOTTOM) : 0);
} else {
// Margin not applied if you're at the root
margin = new Rect();
}
final Rect padding = new Rect(hasNode ? (int) layout.getResultPadding(YogaEdge.START) : 0, hasNode ? (int) layout.getResultPadding(YogaEdge.TOP) : 0, hasNode ? (int) layout.getResultPadding(YogaEdge.END) : 0, hasNode ? (int) layout.getResultPadding(YogaEdge.BOTTOM) : 0);
final Rect contentBounds = node.getBoundsInLithoView();
HighlightedOverlay.setHighlighted(lithoView, margin, padding, contentBounds, isAlignmentMode);
}
18
Source : ItemsRerenderingActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public clreplaced ItemsRerenderingActivity extends NavigatableDemoActivity {
private LithoView mLithoView;
private ComponentContext mComponentContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mComponentContext = new ComponentContext(this);
mLithoView = LithoView.create(this, ItemsRerenderingRootComponent.create(mComponentContext).dataModels(getData(15)).build());
setContentView(mLithoView);
fetchData();
}
private void fetchData() {
final HandlerThread thread = new HandlerThread("bg");
thread.start();
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mLithoView.setComponent(ItemsRerenderingRootComponent.create(mComponentContext).dataModels(getData(16)).build());
}
}, 4000);
}
private List<DataModel> getData(int size) {
final List<DataModel> dataModels = new ArrayList<>();
for (int i = 0; i < size; i++) {
dataModels.add(new DataModel("Item " + i, i));
}
return dataModels;
}
}
18
Source : StickyHeaderControllerImpl.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
private static void detachLithoViewIfNeeded(LithoView view) {
if (view == null) {
return;
}
// This is equivalent of calling view.isAttachedToWindow(),
// however, that method is available only from API19
final boolean isAttachedToWindow = view.getWindowToken() != null;
if (isAttachedToWindow) {
view.onStartTemporaryDetach();
}
}
18
Source : LithoViewRule.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* This test utility allows clients to test replacedertion on the view hierarchy rendered by a Litho
* components. The utility has methods to override the default {@link LithoView}, {@link
* ComponentTree}, width, and height specs.
*
* <pre><code>{@literal @RunWith(LithoTestRunner.clreplaced)}
* public clreplaced LithoSampleTest {
*
* public final @Rule LithoViewRule mLithoViewRule = new LithoViewRule();
*
* {@literal @Test}
* public void test() {
* final ComponentContext c = mLithoViewRule.getContext();
* final Component component = MyComponent.create(c).build();
*
* mLithoViewRule.setRoot(component)
* .attachToWindow()
* .setRoot(component)
* .measure()
* .layout();
*
* LithoView lithoView = mLithoViewRule.getLithoView();
*
* // Test your replacedertions on the litho view.
* }
* }
*
* }</code></pre>
*/
public clreplaced LithoViewRule implements TestRule {
public static final int DEFAULT_WIDTH_SPEC = makeMeasureSpec(1080, EXACTLY);
public static final int DEFAULT_HEIGHT_SPEC = makeMeasureSpec(0, UNSPECIFIED);
private ComponentContext mContext;
private ComponentTree mComponentTree;
private LithoView mLithoView;
private int mWidthSpec = DEFAULT_WIDTH_SPEC;
private int mHeightSpec = DEFAULT_HEIGHT_SPEC;
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
mContext = new ComponentContext(ApplicationProvider.getApplicationContext());
base.evaluate();
} finally {
ComponentsPools.clearMountContentPools();
mContext = null;
mComponentTree = null;
mLithoView = null;
mWidthSpec = DEFAULT_WIDTH_SPEC;
mHeightSpec = DEFAULT_HEIGHT_SPEC;
}
}
};
}
/**
* Gets the current {@link ComponentContext}.
*/
public ComponentContext getContext() {
return mContext;
}
/**
* Gets the current {@link LithoView}; creates a new instance if {@code null}.
*/
public LithoView getLithoView() {
if (mLithoView == null) {
mLithoView = new LithoView(mContext);
}
if (mLithoView.getComponentTree() == null) {
mLithoView.setComponentTree(getComponentTree());
}
return mLithoView;
}
/**
* Sets a new {@link LithoView} which should be used to render.
*/
public LithoViewRule useLithoView(LithoView lithoView) {
mLithoView = lithoView;
if (mLithoView.getComponentContext() != mContext) {
throw new RuntimeException("You must use the same ComponentContext for the LithoView as what is on the LithoViewRule @Rule!");
}
if (mComponentTree != null) {
mLithoView.setComponentTree(mComponentTree);
}
return this;
}
/**
* Gets the current {@link ComponentTree}; creates a new instance if {@code null}.
*/
public ComponentTree getComponentTree() {
if (mComponentTree == null) {
mComponentTree = ComponentTree.create(mContext).build();
}
return mComponentTree;
}
/**
* Sets a new {@link ComponentTree} which should be used to render.
*/
public LithoViewRule useComponentTree(ComponentTree componentTree) {
mComponentTree = componentTree;
getLithoView().setComponentTree(componentTree);
return this;
}
/**
* Sets the new root {@link Component} to render.
*/
public LithoViewRule setRoot(Component component) {
getComponentTree().setRoot(component);
return this;
}
/**
* Sets the new root {@link Component.Builder} to render.
*/
public LithoViewRule setRoot(Component.Builder builder) {
getComponentTree().setRoot(builder.build());
return this;
}
/**
* Sets the new root {@link Component} to render asynchronously.
*/
public LithoViewRule setRootAsync(Component component) {
getComponentTree().setRootAsync(component);
return this;
}
/**
* Sets the new root {@link Component.Builder} to render asynchronously.
*/
public LithoViewRule setRootAsync(Component.Builder builder) {
getComponentTree().setRootAsync(builder.build());
return this;
}
/**
* Sets the new root {@link Component} with new size spec to render.
*/
public LithoViewRule setRootAndSizeSpec(Component component, int widthSpec, int heightSpec) {
mWidthSpec = widthSpec;
mHeightSpec = heightSpec;
getComponentTree().setRootAndSizeSpec(component, mWidthSpec, mHeightSpec);
return this;
}
/**
* Sets a new width and height which should be used to render.
*/
public LithoViewRule setSizePx(int widthPx, int heightPx) {
mWidthSpec = makeMeasureSpec(widthPx, EXACTLY);
mHeightSpec = makeMeasureSpec(heightPx, EXACTLY);
return this;
}
/**
* Sets a new width spec and height spec which should be used to render.
*/
public LithoViewRule setSizeSpecs(int widthSpec, int heightSpec) {
mWidthSpec = widthSpec;
mHeightSpec = heightSpec;
return this;
}
/**
* Sets a new {@link TreeProp} for the next layout preplaced.
*/
public LithoViewRule setTreeProp(Clreplaced<?> klreplaced, Object instance) {
TreeProps props = mContext.getTreeProps();
if (props == null) {
props = new TreeProps();
}
props.put(klreplaced, instance);
mContext.setTreeProps(props);
return this;
}
/**
* Explicitly calls measure on the current root {@link LithoView}
*/
public LithoViewRule measure() {
getLithoView().measure(mWidthSpec, mHeightSpec);
return this;
}
/**
* Explicitly calls layout on the current root {@link LithoView}
*/
public LithoViewRule layout() {
final LithoView lithoView = getLithoView();
lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());
return this;
}
/**
* Explicitly attaches current root {@link LithoView}
*/
public LithoViewRule attachToWindow() {
getLithoView().onAttachedToWindowForTest();
return this;
}
/**
* Explicitly detaches current root {@link LithoView}
*/
public LithoViewRule detachFromWindow() {
getLithoView().onDetachedFromWindowForTest();
return this;
}
/**
* Explicitly releases current root {@link LithoView}
*/
public LithoViewRule release() {
getLithoView().release();
return this;
}
/**
* Gets the current width spec
*/
public int getWidthSpec() {
return mWidthSpec;
}
/**
* Gets the current height spec
*/
public int getHeightSpec() {
return mHeightSpec;
}
/**
* Finds the first {@link View} with the specified tag in the rendered hierarchy, returning null
* if is doesn't exist.
*/
@Nullable
public View findViewWithTagOrNull(Object tag) {
return findViewWithTagTransversal(mLithoView, tag);
}
/**
* Finds the first {@link View} with the specified tag in the rendered hierarchy, throwing if it
* doesn't exist.
*/
public View findViewWithTag(Object tag) {
final View view = findViewWithTagOrNull(tag);
if (view == null) {
throw new RuntimeException("Did not find view with tag '" + tag + "'");
}
return view;
}
@Nullable
private View findViewWithTagTransversal(View view, Object tag) {
if (view.getTag() != null && view.getTag().equals(tag)) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = ((ViewGroup) view);
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = findViewWithTagTransversal(viewGroup.getChildAt(i), tag);
if (child != null) {
return child;
}
}
}
return null;
}
@Nullable
protected LayoutState getCommittedLayoutState() {
return getComponentTree().getCommittedLayoutState();
}
@Nullable
protected InternalNode getCurrentRootNode() {
return getCommittedLayoutState() != null ? getCommittedLayoutState().getLayoutRoot() : null;
}
}
18
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Triggers a Litho visibility event
*
* <p>The event needs to be one of {@link VisibleEvent}, {@link InvisibleEvent}, {@link
* FocusedVisibleEvent}, {@link UnfocusedVisibleEvent}, or {@link FullImpressionVisibleEvent}.
*
* @param lithoView the view to invoke the event on its component tree
* @param visibilityClreplaced the type of the event to invoke
* @return true if a handler for this event type was found
*/
public static boolean triggerVisibilityEvent(LithoView lithoView, Clreplaced<?> visibilityClreplaced) {
return VisibilityEventsHelper.triggerVisibilityEvent(lithoView.getComponentTree(), visibilityClreplaced);
}
18
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Mount a component tree into a component view.
*
* @param lithoView The view to mount the component tree into
* @param componentTree The component tree to mount
* @return A LithoView with the component tree mounted in it.
*/
public static LithoView mountComponent(LithoView lithoView, ComponentTree componentTree) {
return mountComponent(lithoView, componentTree, makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY));
}
18
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Unbinds a component tree from a component view.
*
* @param lithoView The view to unbind.
*/
public static void unbindComponent(LithoView lithoView) {
try {
Whitebox.invokeMethod(lithoView, "onDetach");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
18
Source : LithoAssertions.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@CheckReturnValue
public static LithoViewreplacedert replacedertThat(LithoView lithoView) {
return LithoViewreplacedert.replacedertThat(lithoView);
}
18
Source : IncrementalMountUtilsTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
private static void setupLithoViewPreviousBounds(LithoView lithoView, int width, int height) {
when(lithoView.getPreviousMountBounds()).thenReturn(new Rect(0, 0, width, height));
}
18
Source : ComponentQueriesTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testTextOnComponent() {
final LithoView view = ComponentTestHelper.mountComponent(mContext, Text.create(mContext).text("hello").build());
replacedertThat(ComponentQueries.hasTextMatchingPredicate(view, Predicates.equalTo("hello"))).isTrue();
}
18
Source : ComponentQueriesTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testNoTextOnComponent() {
final LithoView view = ComponentTestHelper.mountComponent(mContext, Text.create(mContext).text("goodbye").build());
replacedertThat(ComponentQueries.hasTextMatchingPredicate(view, Predicates.equalTo("hello"))).isFalse();
}
18
Source : LithoViewMatchersTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Tests {@link LithoViewMatchers}
*/
@RunWith(AndroidJUnit4.clreplaced)
public clreplaced LithoViewMatchersTest {
private LithoView mView;
@UiThreadTest
@Before
public void before() throws Throwable {
ComponentsConfiguration.isEndToEndTestRun = true;
final ComponentContext mComponentContext = new ComponentContext(InstrumentationRegistry.getTargetContext());
final Component mTextComponent = MyComponent.create(mComponentContext).text("foobar").build();
mView = LithoView.create(InstrumentationRegistry.getTargetContext(), mTextComponent);
ViewHelpers.setupView(mView).setExactWidthPx(200).setExactHeightPx(100).layout();
}
@Test
public void testIsLithoView() throws Throwable {
replacedertThat(new TextView(InstrumentationRegistry.getTargetContext()), is(not(lithoView())));
replacedertThat(mView, is(lithoView()));
}
@Test
public void testHasTestKey() throws Throwable {
replacedertThat(mView, is(withTestKey("my_test_key")));
replacedertThat(mView, is(not(withTestKey("my_non_existant_test_key"))));
}
}
18
Source : ComponentHostMatchersTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Tests {@link ComponentHostMatchers}
*/
@RunWith(AndroidJUnit4.clreplaced)
public clreplaced ComponentHostMatchersTest {
private LithoView mView;
@UiThreadTest
@Before
public void before() throws Throwable {
final ComponentContext mComponentContext = new ComponentContext(InstrumentationRegistry.getTargetContext());
final Component mTextComponent = MyComponent.create(mComponentContext).text("foobar").customViewTag("zoidberg").build();
final ComponentTree tree = ComponentTree.create(mComponentContext, mTextComponent).build();
mView = new LithoView(mComponentContext);
mView.setComponentTree(tree);
ViewHelpers.setupView(mView).setExactWidthPx(200).setExactHeightPx(100).layout();
}
@Test
public void testContentDescriptionMatching() throws Throwable {
replacedertThat(mView, componentHostWithText("foobar"));
replacedertThat(mView, not(componentHostWithText("bar")));
replacedertThat(mView, componentHostWithText(containsString("oob")));
}
@Test
public void testIsComponentHost() throws Throwable {
replacedertThat(new TextView(InstrumentationRegistry.getTargetContext()), is(not(componentHost())));
replacedertThat(mView, is(componentHost()));
}
@Test
public void testIsComponentHostWithMatcher() throws Throwable {
replacedertThat(mView, is(componentHost(withText("foobar"))));
replacedertThat(mView, is(not(componentHost(withText("blah")))));
}
@Test
public void testContentDescription() throws Throwable {
replacedertThat(mView, is(componentHost(withContentDescription("foobar2"))));
}
@Test
public void testMountedComponent() throws Throwable {
replacedertThat(mView, is(componentHost(withLifecycle(isA(Text.clreplaced)))));
}
}
18
Source : LithoViewDescriptor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public int getChildCount(LithoView node) {
return DebugComponent.getRootInstance(node) == null ? 0 : 1;
}
17
Source : FeedItemComponentSpecTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testLithoViewSubComponentMatching() {
final ComponentContext c = mComponentsRule.getContext();
final LithoView lithoView = ComponentTestHelper.mountComponent(c, mComponent);
replacedertThat(lithoView).has(deepSubComponentWith(textEquals("Sindre Sorhus")));
}
17
Source : LifecycleDelegateActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public clreplaced LifecycleDelegateActivity extends NavigatableDemoActivity {
private static final AtomicInteger mId = new AtomicInteger(0);
private LithoView mLithoView;
private ConsoleView mConsoleView;
private final DelegateListener mDelegateListener = new DelegateListener() {
@Override
public void onDelegateMethodCalled(int type, Thread thread, long timestamp, int id) {
if (mConsoleView != null) {
mConsoleView.log(type, thread, timestamp, id);
}
}
public void setRootComponent(boolean isSync) {
if (mLithoView != null) {
final Random random = new Random();
final Component root = LifecycleDelegateComponent.create(new ComponentContext(LifecycleDelegateActivity.this)).id(mId.getAndIncrement()).key(// Force to reset component.
String.valueOf(random.nextInt())).delegateListener(mDelegateListener).build();
if (isSync) {
mLithoView.setComponent(root);
} else {
mLithoView.setComponentAsync(root);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.VERTICAL);
parent.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
final ComponentContext componentContext = new ComponentContext(this);
mLithoView = LithoView.create(this, LifecycleDelegateComponent.create(componentContext).id(mId.getAndIncrement()).delegateListener(mDelegateListener).build());
final LayoutParams params1 = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
params1.weight = 1;
mLithoView.setLayoutParams(params1);
parent.addView(mLithoView);
mConsoleView = new ConsoleView(this);
final LayoutParams params2 = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
params2.weight = 1;
mConsoleView.setLayoutParams(params2);
parent.addView(mConsoleView);
setContentView(parent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mLithoView != null) {
mLithoView.release();
}
}
}
17
Source : StateResettingActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public clreplaced StateResettingActivity extends NavigatableDemoActivity {
private LithoView mLithoView;
private List<DataModel> mData;
private ComponentContext mComponentContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
mComponentContext = new ComponentContext(this);
mLithoView = LithoView.create(this, StateResettingRootComponent.create(mComponentContext).dataModels(mData).showHeader(false).build());
setContentView(mLithoView);
fetchHeader();
}
private void fetchHeader() {
final HandlerThread thread = new HandlerThread("bg");
thread.start();
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mLithoView.setComponent(StateResettingRootComponent.create(mComponentContext).dataModels(mData).showHeader(true).build());
}
}, 4000);
}
private List<DataModel> getData() {
final List<DataModel> dataModels = new ArrayList<>();
for (int i = 0; i < 20; i++) {
dataModels.add(new DataModel("Item " + i, i));
}
return dataModels;
}
}
17
Source : ScrollingToBottomActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public clreplaced ScrollingToBottomActivity extends NavigatableDemoActivity {
private LithoView mLithoView;
private ComponentContext mComponentContext;
private List<DataModel> mFeedData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mComponentContext = new ComponentContext(this);
mFeedData = getData(15, 15);
mLithoView = LithoView.create(this, DelayedLoadingComponent.create(mComponentContext).headerDataModels(null).feedDataModels(mFeedData).build());
setContentView(mLithoView);
fetchData();
}
private void fetchData() {
final HandlerThread thread = new HandlerThread("bg");
thread.start();
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mLithoView.setComponent(DelayedLoadingComponent.create(mComponentContext).headerDataModels(getData(0, 15)).feedDataModels(mFeedData).build());
}
}, 4000);
}
private List<DataModel> getData(int start, int size) {
final List<DataModel> dataModels = new ArrayList<>();
for (int i = start; i < start + size; i++) {
dataModels.add(new DataModel("Item " + i, i));
}
return dataModels;
}
}
17
Source : PropUpdatingActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
public clreplaced PropUpdatingActivity extends NavigatableDemoActivity {
private LithoView mLithoView;
private ComponentContext mComponentContext;
private List<DataModel> mDataModels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mComponentContext = new ComponentContext(this);
mDataModels = getData(5);
mLithoView = LithoView.create(this, SelectedItemRootComponent.create(mComponentContext).dataModels(mDataModels).selectedItem(0).build());
setContentView(mLithoView);
fetchData();
}
private void fetchData() {
final HandlerThread thread = new HandlerThread("bg");
thread.start();
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mLithoView.setComponent(SelectedItemRootComponent.create(mComponentContext).dataModels(mDataModels).selectedItem(1).build());
Toast.makeText(mComponentContext.getAndroidContext(), "Updated selected item prop", Toast.LENGTH_SHORT);
}
}, 4000);
}
private List<DataModel> getData(int size) {
final List<DataModel> dataModels = new ArrayList<>();
for (int i = 0; i < size; i++) {
dataModels.add(new DataModel("Item " + i, i));
}
return dataModels;
}
}
17
Source : LithoScrollView.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Extension of {@link NestedScrollView} that allows to add more features needed for @{@link
* VerticalScrollSpec}.
*/
public clreplaced LithoScrollView extends NestedScrollView implements HasLithoViewChildren {
private final LithoView mLithoView;
@Nullable
private ScrollPosition mScrollPosition;
@Nullable
private ViewTreeObserver.OnPreDrawListener mOnPreDrawListener;
@Nullable
private OnInterceptTouchListener mOnInterceptTouchListener;
@Nullable
private ScrollStateDetector mScrollStateDetector;
private boolean mIsIncrementalMountEnabled;
public LithoScrollView(Context context) {
this(context, null);
}
public LithoScrollView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LithoScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mLithoView = new LithoView(context);
addView(mLithoView);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean result = false;
if (mOnInterceptTouchListener != null) {
result = mOnInterceptTouchListener.onInterceptTouch(this, ev);
}
if (!result && super.onInterceptTouchEvent(ev)) {
result = true;
}
return result;
}
@Override
public void fling(int velocityX) {
super.fling(velocityX);
if (mScrollStateDetector != null) {
mScrollStateDetector.fling();
}
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (mScrollStateDetector != null) {
mScrollStateDetector.onDraw();
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mIsIncrementalMountEnabled) {
mLithoView.notifyVisibleBoundsChanged();
}
if (mScrollPosition != null) {
mScrollPosition.y = getScrollY();
}
if (mScrollStateDetector != null) {
mScrollStateDetector.onScrollChanged();
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
boolean isConsumed = super.onTouchEvent(motionEvent);
if (mScrollStateDetector != null) {
mScrollStateDetector.onTouchEvent(motionEvent);
}
return isConsumed;
}
/**
* NestedScrollView does not automatically consume the fling event. However, RecyclerView consumes
* this event if it's either vertically or horizontally scrolling. {@link RecyclerView#fling}
* Since this view is specifically made for vertically scrolling components, we always consume the
* nested fling event just like recycler view.
*/
@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return super.dispatchNestedFling(velocityX, velocityY, true);
}
public void setOnInterceptTouchListener(@Nullable OnInterceptTouchListener onInterceptTouchListener) {
mOnInterceptTouchListener = onInterceptTouchListener;
}
@Override
public void obtainLithoViewChildren(List<LithoView> lithoViews) {
lithoViews.add(mLithoView);
}
void mount(ComponentTree contentComponentTree, final ScrollPosition scrollPosition, @Nullable ScrollStateListener scrollStateListener, boolean isIncrementalMountEnabled) {
mLithoView.setComponentTree(contentComponentTree);
mIsIncrementalMountEnabled = isIncrementalMountEnabled;
mScrollPosition = scrollPosition;
final ViewTreeObserver.OnPreDrawListener onPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
setScrollY(scrollPosition.y);
ViewTreeObserver currentViewTreeObserver = getViewTreeObserver();
if (currentViewTreeObserver.isAlive()) {
currentViewTreeObserver.removeOnPreDrawListener(this);
}
return true;
}
};
getViewTreeObserver().addOnPreDrawListener(onPreDrawListener);
mOnPreDrawListener = onPreDrawListener;
if (scrollStateListener != null) {
if (mScrollStateDetector == null) {
mScrollStateDetector = new ScrollStateDetector(this);
}
mScrollStateDetector.setListener(scrollStateListener);
}
}
void unmount() {
mLithoView.unbind();
mLithoView.setComponentTree(null);
mScrollPosition = null;
getViewTreeObserver().removeOnPreDrawListener(mOnPreDrawListener);
mOnPreDrawListener = null;
if (mScrollStateDetector != null) {
mScrollStateDetector.setListener(null);
}
}
}
17
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Unmounts a component tree from a component view.
*
* @param lithoView the view to unmount
*/
public static void unmountComponent(LithoView lithoView) {
if (!lithoView.isIncrementalMountEnabled()) {
throw new IllegalArgumentException("In order to test unmounting a Component, it needs to be mounted with " + "incremental mount enabled. Please use a mountComponent() variation that " + "accepts an incrementalMountEnabled argument");
}
// Unmounting the component by running incremental mount to a Rect that we certain won't
// contain the component.
Rect rect = new Rect(99999, 99999, 999999, 999999);
lithoView.notifyVisibleBoundsChanged(rect, true);
}
17
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Mount a component into a component view.
*
* @param context A components context
* @param lithoView The view to mount the component into
* @param component The component to mount
* @return A LithoView with the component mounted in it.
*/
public static LithoView mountComponent(ComponentContext context, LithoView lithoView, Component component) {
return mountComponent(context, lithoView, component, 100, 100);
}
17
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Mount a component into a component view.
*
* @param context A components context
* @param lithoView The view to mount the component into
* @param component The component to mount
* @param width The width of the resulting view
* @param height The height of the resulting view
* @return A LithoView with the component mounted in it.
*/
public static LithoView mountComponent(ComponentContext context, LithoView lithoView, Component component, int width, int height) {
return mountComponent(context, lithoView, component, false, false, width, height);
}
17
Source : SubComponentExtractor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public List<InspectableComponent> extract(Component input) {
final LithoView lithoView = ComponentTestHelper.mountComponent(mComponentContext, input);
return LithoViewSubComponentExtractor.subComponents().extract(lithoView);
}
17
Source : SubComponentDeepExtractor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public List<InspectableComponent> extract(Component input) {
final LithoView lithoView = ComponentTestHelper.mountComponent(mComponentContext, input);
return LithoViewSubComponentDeepExtractor.subComponentsDeeply().extract(lithoView);
}
17
Source : LithoRepresentation.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Returns the standard {@code toString} representation of the given object. It may or not the
* object's own implementation of {@code toString}.
*
* @param object the given object.
* @return the {@code toString} representation of the given object.
*/
@Override
public String toStringOf(Object object) {
if (object instanceof Component) {
final LithoView lithoView = ComponentTestHelper.mountComponent(mComponentContext, (Component) object);
return LithoViewTestHelper.viewToString(lithoView);
}
if (object instanceof Component.Builder) {
final LithoView lithoView = ComponentTestHelper.mountComponent((Component.Builder) object);
return LithoViewTestHelper.viewToString(lithoView);
}
if (object instanceof LithoView) {
return LithoViewTestHelper.viewToString((LithoView) object);
}
return super.toStringOf(object);
}
17
Source : EventTriggerTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testCanTriggerEventOnNestedComponent() {
Handle handle = new Handle();
ComponentContainer component = ComponentContainer.create(mComponentContext).componentWithTriggerHandle(handle).uniqueString("A").build();
LithoView lithoView = ComponentTestHelper.mountComponent(mComponentContext, component);
// The EventTriggers have been correctly applied to lithoView's ComponentTree
// (EventTriggersContainer),
// but this hasn't been applied to the ComponentTree inside mComponentContext.
mComponentContext = ComponentContext.withComponentScope(ComponentContext.withComponentTree(lithoView.getComponentContext(), lithoView.getComponentTree()), component, "globalKey");
replacedertThat(ComponentWithTrigger.testTriggerMethod(mComponentContext, handle)).isEqualTo("A");
}
17
Source : EventTriggerTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testCanTriggerEvent() {
Handle handle = new Handle();
ComponentWithTrigger component = ComponentWithTrigger.create(mComponentContext).handle(handle).uniqueString("A").build();
LithoView lithoView = ComponentTestHelper.mountComponent(mComponentContext, component);
// The EventTriggers have been correctly applied to lithoView's ComponentTree
// (EventTriggersContainer),
// but this hasn't been applied to the ComponentTree inside mComponentContext.
mComponentContext = ComponentContext.withComponentScope(ComponentContext.withComponentTree(lithoView.getComponentContext(), lithoView.getComponentTree()), component, "globalKey");
replacedertThat(ComponentWithTrigger.testTriggerMethod(mComponentContext, handle)).isEqualTo("A");
}
17
Source : ProgressSpecTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testDefault() {
LithoView view = getMountedView();
replacedertThat(view.getMeasuredWidth()).isGreaterThan(0);
replacedertThat(view.getMeasuredHeight()).isGreaterThan(0);
}
17
Source : EditTextSpecTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testEditTextWithText() {
final ComponentContext c = mComponentsRule.getContext();
final LithoView lithoView = ComponentTestHelper.mountComponent(EditText.create(c).textChangedEventHandler(null).textSizePx(10).text(TEXT));
final android.widget.EditText editText = (android.widget.EditText) lithoView.getChildAt(0);
replacedertThat(editText.getText().toString()).isEqualTo(TEXT);
replacedertThat(editText.getTextSize()).isEqualTo(10);
}
17
Source : IncrementalMountUtilsTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Tests {@link IncrementalMountUtils}
*/
@RunWith(LithoTestRunner.clreplaced)
public clreplaced IncrementalMountUtilsTest {
private static final int SCROLLING_VIEW_WIDTH = 100;
private static final int SCROLLING_VIEW_HEIGHT = 1000;
public TestWrapperView mWrapperView = mock(TestWrapperView.clreplaced);
public LithoView mLithoView = mock(LithoView.clreplaced);
public ViewGroup mViewGroup = mock(ViewGroup.clreplaced);
private final Rect mMountedRect = new Rect();
@Before
public void setUp() {
when(mLithoView.isIncrementalMountEnabled()).thenReturn(true);
when(mWrapperView.getWrappedView()).thenReturn(mLithoView);
when(mViewGroup.getChildCount()).thenReturn(1);
when(mViewGroup.getChildAt(0)).thenReturn(mLithoView);
when(mViewGroup.getWidth()).thenReturn(SCROLLING_VIEW_WIDTH);
when(mViewGroup.getHeight()).thenReturn(SCROLLING_VIEW_HEIGHT);
// Can't use verify as the rect is reset when it is released back to the pool, which occurs
// before we can check it.
doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
mMountedRect.set((Rect) invocation.getArguments()[0]);
return null;
}
}).when(mLithoView).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtTop() {
setupViewBounds(mLithoView, 0, -10, SCROLLING_VIEW_WIDTH, 10);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 10, SCROLLING_VIEW_WIDTH, 20));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtTopWithTranslationYPartialIn() {
setupViewBounds(mLithoView, 0, -10, SCROLLING_VIEW_WIDTH, 10);
setupViewTranslations(mLithoView, 0, 5);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 5, SCROLLING_VIEW_WIDTH, 20));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtTopWithTranslationYFullyOut() {
setupViewBounds(mLithoView, 0, -10, SCROLLING_VIEW_WIDTH, 10);
setupViewTranslations(mLithoView, 0, -15);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verify(mLithoView, never()).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtLeft() {
setupViewBounds(mLithoView, -10, 0, 10, SCROLLING_VIEW_HEIGHT);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(10, 0, 20, SCROLLING_VIEW_HEIGHT));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtLeftWithTranslationXFullyIn() {
setupViewBounds(mLithoView, -10, 0, 10, SCROLLING_VIEW_HEIGHT);
setupViewTranslations(mLithoView, 15, 0);
setupLithoViewPreviousBounds(mLithoView, 20, SCROLLING_VIEW_HEIGHT);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verify(mLithoView, never()).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtLeftWithTranslationXPartialOut() {
setupViewBounds(mLithoView, -10, 0, 10, SCROLLING_VIEW_HEIGHT);
setupViewTranslations(mLithoView, -7, 0);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(17, 0, 20, SCROLLING_VIEW_HEIGHT));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtBottom() {
setupViewBounds(mLithoView, 0, SCROLLING_VIEW_HEIGHT - 5, SCROLLING_VIEW_WIDTH, SCROLLING_VIEW_HEIGHT + 5);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 0, SCROLLING_VIEW_WIDTH, 5));
}
@Test
public void testIncrementalMountForLithoViewVisibleAtRight() {
setupViewBounds(mLithoView, SCROLLING_VIEW_WIDTH - 5, 0, SCROLLING_VIEW_WIDTH + 5, SCROLLING_VIEW_HEIGHT);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 0, 5, SCROLLING_VIEW_HEIGHT));
}
@Test
public void testIncrementalMountForLithoViewNewlyFullyVisible() {
setupViewBounds(mLithoView, 0, 10, SCROLLING_VIEW_WIDTH, 20);
setupLithoViewPreviousBounds(mLithoView, SCROLLING_VIEW_WIDTH, 5);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 0, SCROLLING_VIEW_WIDTH, 10));
}
@Test
public void testIncrementalMountForLithoViewAlreadyFullyVisible() {
setupViewBounds(mLithoView, 0, 10, SCROLLING_VIEW_WIDTH, 20);
setupLithoViewPreviousBounds(mLithoView, SCROLLING_VIEW_WIDTH, 10);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verify(mLithoView, never()).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
@Test
public void testNoIncrementalMountWhenNotEnabled() {
setupViewBounds(mLithoView, 0, SCROLLING_VIEW_HEIGHT - 5, SCROLLING_VIEW_WIDTH, SCROLLING_VIEW_HEIGHT + 5);
when(mLithoView.isIncrementalMountEnabled()).thenReturn(false);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verify(mLithoView, never()).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
@Test
public void testIncrementalMountForWrappedViewAtTop() {
when(mViewGroup.getChildAt(0)).thenReturn(mWrapperView);
setupViewBounds(mWrapperView, 0, -10, SCROLLING_VIEW_WIDTH, 10);
setupViewBounds(mLithoView, 0, 0, SCROLLING_VIEW_WIDTH, 20);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 10, SCROLLING_VIEW_WIDTH, 20));
}
@Test
public void testIncrementalMountForWrappedViewAtBottom() {
when(mViewGroup.getChildAt(0)).thenReturn(mWrapperView);
setupViewBounds(mWrapperView, 0, SCROLLING_VIEW_HEIGHT - 5, SCROLLING_VIEW_WIDTH, SCROLLING_VIEW_HEIGHT + 5);
setupViewBounds(mLithoView, 0, 0, SCROLLING_VIEW_WIDTH, 10);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 0, SCROLLING_VIEW_WIDTH, 5));
}
@Test
public void testIncrementalMountForWrappedLithoViewNewlyFullyVisible() {
when(mViewGroup.getChildAt(0)).thenReturn(mWrapperView);
setupViewBounds(mWrapperView, 0, 10, SCROLLING_VIEW_WIDTH, 20);
setupViewBounds(mLithoView, 0, 0, SCROLLING_VIEW_WIDTH, 10);
setupLithoViewPreviousBounds(mLithoView, SCROLLING_VIEW_WIDTH, 5);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verifyPerformIncrementalMountCalled(new Rect(0, 0, SCROLLING_VIEW_WIDTH, 10));
}
@Test
public void testIncrementalMountForWrappedLithoViewAlreadyFullyVisible() {
when(mViewGroup.getChildAt(0)).thenReturn(mWrapperView);
setupViewBounds(mWrapperView, 0, 10, SCROLLING_VIEW_WIDTH, 20);
setupViewBounds(mLithoView, 0, 0, SCROLLING_VIEW_WIDTH, 10);
setupLithoViewPreviousBounds(mLithoView, SCROLLING_VIEW_WIDTH, 10);
IncrementalMountUtils.performIncrementalMount(mViewGroup);
verify(mLithoView, never()).notifyVisibleBoundsChanged((Rect) any(), eq(true));
}
private static void setupViewBounds(View view, int l, int t, int r, int b) {
when(view.getLeft()).thenReturn(l);
when(view.getTop()).thenReturn(t);
when(view.getRight()).thenReturn(r);
when(view.getBottom()).thenReturn(b);
when(view.getWidth()).thenReturn(r - l);
when(view.getHeight()).thenReturn(b - t);
}
private static void setupViewTranslations(View view, float translationX, float translationY) {
when(view.getTranslationX()).thenReturn(translationX);
when(view.getTranslationY()).thenReturn(translationY);
}
private static void setupLithoViewPreviousBounds(LithoView lithoView, int width, int height) {
when(lithoView.getPreviousMountBounds()).thenReturn(new Rect(0, 0, width, height));
}
private void verifyPerformIncrementalMountCalled(Rect rect) {
replacedertThat(mMountedRect).isEqualTo(rect);
}
public static clreplaced TestWrapperView extends View implements WrapperView {
public TestWrapperView(Context context) {
super(context);
}
@Override
public View getWrappedView() {
return null;
}
}
}
17
Source : TestCrasherOnMountSpec.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@OnUnmount
static void onUnmount(ComponentContext c, LithoView mountedView) {
mountedView.setComponentTree(null);
}
17
Source : TestCrasherOnMountSpec.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@OnMount
static void onMount(ComponentContext c, LithoView lithoView) {
throw new RuntimeException("onMount crash");
}
17
Source : LithoViewDescriptor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public void init(LithoView node) throws Exception {
node.setOnDirtyMountListener(new LithoView.OnDirtyMountListener() {
@Override
public void onDirtyMount(LithoView view) {
invalidate(view);
invalidateAX(view);
}
});
}
17
Source : LithoViewDescriptor.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
public Object getChildAt(LithoView node, int index) {
return DebugComponent.getRootInstance(node);
}
16
Source : SplashActivity.java
with Apache License 2.0
from JessYanCoding
with Apache License 2.0
from JessYanCoding
@Override
public void initData() {
final ComponentContext c = new ComponentContext(this);
final LithoView lithoView = LithoView.create(this, /* context */
Text.create(c).text("Hello, World!").textSizeDip(50).build());
setContentView(lithoView);
}
16
Source : TooltipTriggerExampleComponentSpec.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
private static PopupWindow createTooltip(final ComponentContext c, final String text) {
final LithoView tooltip = LithoView.create(c, Column.create(c).paddingDip(YogaEdge.ALL, 15).backgroundColor(LITHO_PINK).child(Text.create(c).text(text).textColor(Color.WHITE)).build());
return new PopupWindow(tooltip, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
}
16
Source : ComponentDemoActivity.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Demos.DemoItem model = getDataModel();
LithoView lithoView = new LithoView(this);
if (!(model instanceof Demos.SingleDemo) || ((Demos.SingleDemo) model).componentCreator == null) {
throw new RuntimeException("Invalid model: " + model);
}
lithoView.setComponent(((Demos.SingleDemo) model).componentCreator.create(lithoView.getComponentContext()));
setContentView(lithoView);
}
16
Source : InspectableComponent.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* @return The root {@link InspectableComponent} of a LithoView.
*/
@Nullable
public static InspectableComponent getRootInstance(LithoView view) {
final DebugComponent rootInstance = DebugComponent.getRootInstance(view);
if (rootInstance == null) {
return null;
}
return new InspectableComponent(rootInstance);
}
16
Source : LithoViewRule.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Explicitly calls layout on the current root {@link LithoView}
*/
public LithoViewRule layout() {
final LithoView lithoView = getLithoView();
lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());
return this;
}
16
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Mount a component into a component view.
*
* @param context A components context
* @param lithoView The view to mount the component into
* @param component The component to mount
* @param incrementalMountEnabled States whether incremental mount is enabled
* @param width The width of the resulting view
* @param height The height of the resulting view
* @return A LithoView with the component mounted in it.
*/
public static LithoView mountComponent(ComponentContext context, LithoView lithoView, Component component, boolean incrementalMountEnabled, boolean visibilityProcessingEnabled, int width, int height) {
return mountComponent(lithoView, ComponentTree.create(context, component).incrementalMount(incrementalMountEnabled).layoutDiffing(false).visibilityProcessing(visibilityProcessingEnabled).build(), makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY));
}
16
Source : ComponentTestHelper.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
/**
* Set a root component to a component tree, and mount it into a component view.
*
* @param lithoView The view to mount the component tree into
* @param componentTree The component tree to mount
* @param component The root component
* @return A LithoView with the component tree mounted in it.
*/
public static LithoView mountComponent(LithoView lithoView, ComponentTree componentTree, Component component) {
componentTree.setRoot(component);
return mountComponent(lithoView, componentTree, makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY));
}
16
Source : StickyHeaderControllerTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testTranslateRecyclerViewChild() {
SectionsRecyclerView recycler = mock(SectionsRecyclerView.clreplaced);
RecyclerView recyclerView = mock(RecyclerView.clreplaced);
when(recycler.getRecyclerView()).thenReturn(recyclerView);
when(recyclerView.getLayoutManager()).thenReturn(mock(RecyclerView.LayoutManager.clreplaced));
mStickyHeaderController.init(recycler);
when(mHreplacedtickyHeader.findFirstVisibleItemPosition()).thenReturn(2);
when(mHreplacedtickyHeader.isSticky(2)).thenReturn(true);
ComponentTree componentTree = mock(ComponentTree.clreplaced);
when(mHreplacedtickyHeader.getComponentForStickyHeaderAt(2)).thenReturn(componentTree);
LithoView lithoView = mock(LithoView.clreplaced);
when(componentTree.getLithoView()).thenReturn(lithoView);
mStickyHeaderController.onScrolled(null, 0, 0);
verify(lithoView).setTranslationY(anyFloat());
verify(recycler, times(2)).hideStickyHeader();
}
16
Source : StickyHeaderControllerTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testTranslateStackedStickyHeaders() {
SectionsRecyclerView recycler = mock(SectionsRecyclerView.clreplaced);
RecyclerView recyclerView = mock(RecyclerView.clreplaced);
when(recycler.getRecyclerView()).thenReturn(recyclerView);
when(recyclerView.getLayoutManager()).thenReturn(mock(RecyclerView.LayoutManager.clreplaced));
mStickyHeaderController.init(recycler);
when(mHreplacedtickyHeader.findFirstVisibleItemPosition()).thenReturn(2);
when(mHreplacedtickyHeader.isSticky(2)).thenReturn(true);
when(mHreplacedtickyHeader.isSticky(3)).thenReturn(true);
when(mHreplacedtickyHeader.isValidPosition(3)).thenReturn(true);
ComponentTree componentTree = mock(ComponentTree.clreplaced);
when(mHreplacedtickyHeader.getComponentForStickyHeaderAt(2)).thenReturn(componentTree);
LithoView lithoView = mock(LithoView.clreplaced);
when(componentTree.getLithoView()).thenReturn(lithoView);
mStickyHeaderController.onScrolled(null, 0, 0);
verify(lithoView, never()).setTranslationY(nullable(Integer.clreplaced));
verify(recycler, times(2)).hideStickyHeader();
}
16
Source : ComponentQueriesTest.java
with Apache License 2.0
from facebook
with Apache License 2.0
from facebook
@Test
public void testExtractTextFromTextComponent() {
final LithoView view = ComponentTestHelper.mountComponent(mContext, Text.create(mContext).text("hello").build());
replacedertThat(view.getTextContent().getTexreplacedems()).isEqualTo(ImmutableList.<CharSequence>of("hello"));
}
See More Examples