-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow manual viewport estimation to avoid sync initRange calls
Summary: The goal here is to have a manual bypass around sync initRange calls on the main thread: we do this by specifying a manual viewport range so that a range doesn't have to be determined before kicking off async layouts. Will follow up with Mihaela on longer term mitigation here (there are already params in flight). Reviewed By: muraziz, paveldudka Differential Revision: D16599558 fbshipit-source-id: 018a15a2aa986f5aef4235529c2a76990ab37c13
- Loading branch information
1 parent
b3dfc1b
commit 3308ee8
Showing
3 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
litho-it/src/test/java/com/facebook/litho/widget/RecyclerBinderManualRangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Copyright 2014-present Facebook, Inc. | ||
* | ||
* 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.litho.widget; | ||
|
||
import static com.facebook.litho.SizeSpec.EXACTLY; | ||
import static com.facebook.litho.SizeSpec.UNSPECIFIED; | ||
import static com.facebook.litho.SizeSpec.makeSizeSpec; | ||
import static com.facebook.litho.widget.ComponentRenderInfo.create; | ||
import static com.facebook.litho.widget.RecyclerBinderTest.NO_OP_CHANGE_SET_COMPLETE_CALLBACK; | ||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import android.os.Looper; | ||
import com.facebook.litho.ComponentContext; | ||
import com.facebook.litho.ComponentTree; | ||
import com.facebook.litho.EventHandler; | ||
import com.facebook.litho.Size; | ||
import com.facebook.litho.SizeSpec; | ||
import com.facebook.litho.testing.TestDrawableComponent; | ||
import com.facebook.litho.testing.Whitebox; | ||
import com.facebook.litho.testing.testrunner.ComponentsTestRunner; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.Shadows; | ||
import org.robolectric.shadows.ShadowLooper; | ||
|
||
/** Tests for manually specifying estimatedViewportCount to {@link RecyclerBinder} */ | ||
@RunWith(ComponentsTestRunner.class) | ||
public class RecyclerBinderManualRangeTest { | ||
|
||
@Rule public ExpectedException mExpectedException = ExpectedException.none(); | ||
|
||
private ComponentContext mComponentContext; | ||
private ShadowLooper mLayoutThreadShadowLooper; | ||
|
||
@Before | ||
public void setup() { | ||
mComponentContext = new ComponentContext(RuntimeEnvironment.application); | ||
mComponentContext.getAndroidContext().setTheme(0); | ||
|
||
mLayoutThreadShadowLooper = | ||
Shadows.shadowOf( | ||
(Looper) Whitebox.invokeMethod(ComponentTree.class, "getDefaultLayoutThreadLooper")); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
mLayoutThreadShadowLooper.runToEndOfTasks(); | ||
} | ||
|
||
@Test | ||
public void testMeasureAfterAddItems() { | ||
final int widthSpec = SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY); | ||
final int heightSpec = SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY); | ||
|
||
final RecyclerBinder recyclerBinder = | ||
new RecyclerBinder.Builder() | ||
.estimatedViewportCount(1) | ||
.rangeRatio(.5f) | ||
.build(mComponentContext); | ||
|
||
final List<RenderInfo> initialComponents = new ArrayList<>(); | ||
for (int i = 0; i < 10; i++) { | ||
initialComponents.add( | ||
create() | ||
.component( | ||
TestDrawableComponent.create(mComponentContext) | ||
.widthPx(100) | ||
.heightPx(100) | ||
.build()) | ||
.build()); | ||
} | ||
recyclerBinder.insertRangeAt(0, initialComponents); | ||
recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK); | ||
|
||
recyclerBinder.measure(new Size(), widthSpec, heightSpec, null); | ||
|
||
for (int i = 0; i < initialComponents.size(); i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isFalse(); | ||
} | ||
|
||
mLayoutThreadShadowLooper.runToEndOfTasks(); | ||
|
||
for (int i = 0; i < 2; i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isTrue(); | ||
assertThat(holder.isTreeValid()).describedAs("Holder " + i).isTrue(); | ||
RecyclerBinderTest.assertHasCompatibleLayout( | ||
recyclerBinder, i, makeSizeSpec(200, EXACTLY), makeSizeSpec(0, UNSPECIFIED)); | ||
} | ||
|
||
for (int i = 2; i < initialComponents.size(); i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isFalse(); | ||
assertThat(holder.isTreeValid()).describedAs("Holder " + i).isFalse(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAddItemsAfterMeasure() { | ||
final int widthSpec = SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY); | ||
final int heightSpec = SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY); | ||
|
||
final RecyclerBinder recyclerBinder = | ||
new RecyclerBinder.Builder() | ||
.estimatedViewportCount(1) | ||
.rangeRatio(.5f) | ||
.build(mComponentContext); | ||
|
||
recyclerBinder.measure(new Size(), widthSpec, heightSpec, null); | ||
|
||
final List<RenderInfo> initialComponents = new ArrayList<>(); | ||
for (int i = 0; i < 10; i++) { | ||
initialComponents.add( | ||
create() | ||
.component( | ||
TestDrawableComponent.create(mComponentContext) | ||
.widthPx(100) | ||
.heightPx(100) | ||
.build()) | ||
.build()); | ||
} | ||
recyclerBinder.insertRangeAt(0, initialComponents); | ||
recyclerBinder.notifyChangeSetComplete(true, NO_OP_CHANGE_SET_COMPLETE_CALLBACK); | ||
|
||
for (int i = 0; i < initialComponents.size(); i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isFalse(); | ||
} | ||
|
||
mLayoutThreadShadowLooper.runToEndOfTasks(); | ||
|
||
for (int i = 0; i < 2; i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isTrue(); | ||
assertThat(holder.isTreeValid()).describedAs("Holder " + i).isTrue(); | ||
RecyclerBinderTest.assertHasCompatibleLayout( | ||
recyclerBinder, i, makeSizeSpec(200, EXACTLY), makeSizeSpec(0, UNSPECIFIED)); | ||
} | ||
|
||
for (int i = 2; i < initialComponents.size(); i++) { | ||
final ComponentTreeHolder holder = recyclerBinder.getComponentTreeHolderAt(i); | ||
assertThat(holder.hasCompletedLatestLayout()).describedAs("Holder " + i).isFalse(); | ||
assertThat(holder.isTreeValid()).describedAs("Holder " + i).isFalse(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCanMeasureIsUnsupported() { | ||
mExpectedException.expect(RuntimeException.class); | ||
mExpectedException.expectMessage( | ||
"Cannot use manual estimated viewport count when the RecyclerBinder needs an item to determine its size!"); | ||
|
||
final RecyclerBinder recyclerBinder = | ||
new RecyclerBinder.Builder() | ||
.estimatedViewportCount(1) | ||
.rangeRatio(.5f) | ||
.canMeasure(true) | ||
.build(mComponentContext); | ||
|
||
recyclerBinder.measure( | ||
new Size(), | ||
SizeSpec.makeSizeSpec(200, UNSPECIFIED), | ||
SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY), | ||
mock(EventHandler.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters