Skip to content

Commit

Permalink
Improve BindData tests
Browse files Browse the repository at this point in the history
Summary: This diff updates BindData related tests in `MountStateTest` class so that they don't pull the BindData from MountState, but instead rely on public RenderUnit API.

Reviewed By: adityasharat

Differential Revision: D46177117

fbshipit-source-id: 70993b90404f58d181e51f6a0d929d08e84b9128
  • Loading branch information
zielinskimz authored and facebook-github-bot committed May 25, 2023
1 parent 7f339dd commit e204991
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ public void onMountUnitWithNonHostView_shouldThrowException() {
public void onAttachDetachMountState_MountStateShouldCallBindersAndPassBindData() {
final Context c = RuntimeEnvironment.application;

final List<TestBinder<?>> bindOrder = new ArrayList<>();
final List<Pair<Object, Object>> bindOrder = new ArrayList<>();
final List<Pair<Object, Object>> unbindOrder = new ArrayList<>();

TestBinderWithBindData bindBinder = new TestBinderWithBindData<>(bindOrder, unbindOrder, 1);
Expand Down Expand Up @@ -934,11 +934,10 @@ public void onAttachDetachMountState_MountStateShouldCallBindersAndPassBindData(
// assert mount state is correct
assertThat(mountState.getMountItemCount()).isEqualTo(2);

// assert bind data is correct
final BindData bindData = mountState.getMountItemAt(1).getBindData();
assertThat(bindData.getFixedBindersBindData()).isNull();
assertThat(bindData.getOptionalMountBindersBindData().get(mountBinder.getClass())).isEqualTo(2);
assertThat(bindData.getAttachBindersBindData().get(bindBinder.getClass())).isEqualTo(1);
// assert that bind was called in correct order and correct bind data was returned
assertThat(bindOrder).hasSize(2);
assertThat(bindOrder.get(0).second).isEqualTo(2);
assertThat(bindOrder.get(1).second).isEqualTo(1);

mountState.detach();

Expand All @@ -960,8 +959,13 @@ public void testBinderUnmountPassBindDataOnUpdateMountItem_whenShouldUpdateRetur
final long id;
MountState mountState;

final TestBinderWithBindData1 mountBinder = new TestBinderWithBindData1(1);
final TestBinderWithBindData1 attachBinder = new TestBinderWithBindData1(2);
final List<Pair<Object, Object>> bindOrder = new ArrayList<>();
final List<Pair<Object, Object>> unbindOrder = new ArrayList<>();

final TestBinderWithBindData1 mountBinder =
new TestBinderWithBindData1(bindOrder, unbindOrder, 1);
final TestBinderWithBindData1 attachBinder =
new TestBinderWithBindData1(bindOrder, unbindOrder, 2);
{
final TestNode root = new TestNode();
final TestNode leaf = new TestNode(0, 0, 10, 10);
Expand All @@ -981,33 +985,32 @@ public void testBinderUnmountPassBindDataOnUpdateMountItem_whenShouldUpdateRetur
// assert mount state is correct
assertThat(mountState.getMountItemCount()).isEqualTo(2);

final BindData bindData = mountState.getMountItemAt(1).getBindData();

assertThat(bindData.getFixedBindersBindData()).isNull();

// assert optional mount binders bind data is correct
assertThat(bindData.getOptionalMountBindersBindData()).hasSize(1);
assertThat(bindData.getOptionalMountBindersBindData().get(mountBinder.getClass()))
.isEqualTo(1);

// assert attach binders bind data is correct
assertThat(bindData.getAttachBindersBindData()).hasSize(1);
assertThat(bindData.getAttachBindersBindData().get(attachBinder.getClass())).isEqualTo(2);
// assert that bind was called in correct order and correct bind data was returned
assertThat(bindOrder).hasSize(2);
assertThat(bindOrder.get(0).second).isEqualTo(1);
assertThat(bindOrder.get(1).second).isEqualTo(2);
}

bindOrder.clear();
unbindOrder.clear();
{
final TestNode newRoot = new TestNode();
final TestNode newLeaf = new TestNode(10, 10, 10, 10);
newLeaf.setLayoutData(new Object());

newRoot.addChild(newLeaf);

final TestBinderWithBindData1 mountBinder1 = new TestBinderWithBindData1(10);
final TestBinderWithBindData2 mountBinder2 = new TestBinderWithBindData2(3);
final TestBinderWithBindData1 mountBinder1 =
new TestBinderWithBindData1(bindOrder, unbindOrder, 10);
final TestBinderWithBindData2 mountBinder2 =
new TestBinderWithBindData2(bindOrder, unbindOrder, 3);

final TestBinderWithBindData1 attachBinder1 = new TestBinderWithBindData1(20);
final TestBinderWithBindData2 attachBinder2 = new TestBinderWithBindData2(4);
final TestBinderWithBindData3 attachBinder3 = new TestBinderWithBindData3(5);
final TestBinderWithBindData1 attachBinder1 =
new TestBinderWithBindData1(bindOrder, unbindOrder, 20);
final TestBinderWithBindData2 attachBinder2 =
new TestBinderWithBindData2(bindOrder, unbindOrder, 4);
final TestBinderWithBindData3 attachBinder3 =
new TestBinderWithBindData3(bindOrder, unbindOrder, 5);

final TestRenderUnit newRenderUnit = new TestRenderUnit();
newRenderUnit.addOptionalMountBinders(
Expand All @@ -1027,25 +1030,19 @@ public void testBinderUnmountPassBindDataOnUpdateMountItem_whenShouldUpdateRetur
// assert mount state is correct
assertThat(mountState.getMountItemCount()).isEqualTo(2);

final BindData updatedBindData = mountState.getMountItemAt(1).getBindData();

assertThat(updatedBindData.getFixedBindersBindData()).isNull();

// assert optional mount binders bind data is correct
assertThat(updatedBindData.getOptionalMountBindersBindData()).hasSize(2);
assertThat(updatedBindData.getOptionalMountBindersBindData().get(mountBinder1.getClass()))
.isEqualTo(10);
assertThat(updatedBindData.getOptionalMountBindersBindData().get(mountBinder2.getClass()))
.isEqualTo(3);

// assert attach binders bind data is correct
assertThat(updatedBindData.getAttachBindersBindData()).hasSize(3);
assertThat(updatedBindData.getAttachBindersBindData().get(attachBinder1.getClass()))
.isEqualTo(20);
assertThat(updatedBindData.getAttachBindersBindData().get(attachBinder2.getClass()))
.isEqualTo(4);
assertThat(updatedBindData.getAttachBindersBindData().get(attachBinder3.getClass()))
.isEqualTo(5);
// assert that unbind was called in correct order and correct bind data was passed after
// update
assertThat(unbindOrder).hasSize(2);
assertThat(unbindOrder.get(0).second).isEqualTo(2);
assertThat(unbindOrder.get(1).second).isEqualTo(1);

// assert that bind was called in correct order and correct bind data was returned
assertThat(bindOrder).hasSize(5);
assertThat(bindOrder.get(0).second).isEqualTo(10);
assertThat(bindOrder.get(1).second).isEqualTo(3);
assertThat(bindOrder.get(2).second).isEqualTo(20);
assertThat(bindOrder.get(3).second).isEqualTo(4);
assertThat(bindOrder.get(4).second).isEqualTo(5);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public boolean shouldUpdate(

@Override
public Object bind(Context context, View view, MODEL model, Object layoutData) {
bindOrder.add(this);
bindOrder.add(new Pair(this, bindData));
wasBound = true;
return bindData;
}
Expand Down

0 comments on commit e204991

Please sign in to comment.