Skip to content

Commit 73f13e5

Browse files
Jing-Wei Wufacebook-github-bot
authored andcommitted
Refactor LayoutSpecModelFactoryTest
Summary: Like [MountSpecModelFactoryTest](diffusion/FBS/browse/master/fbandroid/libraries/components/litho-it/src/test/java/com/facebook/litho/specmodels/processor/MountSpecModelFactoryTest.java), create SpecModel from a real LayoutSpec component instead of using mock. It would be easier to test other delegate methods in later diffs. Reviewed By: marco-cova Differential Revision: D13623075 fbshipit-source-id: dbf802781e8982ea17ef498ed9bc137d86706003
1 parent c2f032e commit 73f13e5

1 file changed

Lines changed: 47 additions & 52 deletions

File tree

litho-it/src/test/java/com/facebook/litho/specmodels/processor/LayoutSpecModelFactoryTest.java

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,86 +18,81 @@
1818

1919
import static org.assertj.core.api.Java6Assertions.assertThat;
2020
import static org.mockito.Mockito.mock;
21-
import static org.mockito.Mockito.when;
2221

22+
import com.facebook.litho.Component;
23+
import com.facebook.litho.ComponentContext;
2324
import com.facebook.litho.annotations.LayoutSpec;
25+
import com.facebook.litho.annotations.OnCreateInitialState;
26+
import com.facebook.litho.annotations.OnCreateLayout;
27+
import com.facebook.litho.annotations.Prop;
2428
import com.facebook.litho.specmodels.internal.RunMode;
2529
import com.facebook.litho.specmodels.model.DependencyInjectionHelper;
2630
import com.facebook.litho.specmodels.model.LayoutSpecModel;
31+
import com.google.testing.compile.CompilationRule;
2732
import javax.annotation.processing.Messager;
2833
import javax.lang.model.element.TypeElement;
2934
import javax.lang.model.util.Elements;
3035
import javax.lang.model.util.Types;
3136
import org.junit.Before;
37+
import org.junit.Rule;
3238
import org.junit.Test;
33-
import org.mockito.Mock;
34-
import org.mockito.MockitoAnnotations;
3539

3640
/** Tests {@link LayoutSpecModelFactory} */
3741
public class LayoutSpecModelFactoryTest {
38-
private static final String TEST_QUALIFIED_SPEC_NAME = "com.facebook.litho.TestSpec";
39-
private static final String TEST_QUALIFIED_COMPONENT_NAME = "com.facebook.litho.Test";
42+
private static final String TEST_QUALIFIED_SPEC_NAME =
43+
"com.facebook.litho.specmodels.processor.LayoutSpecModelFactoryTest.TestLayoutSpec";
44+
private static final String TEST_QUALIFIED_COMPONENT_NAME =
45+
"com.facebook.litho.specmodels.processor.LayoutSpecModelFactoryTest.TestLayoutComponentName";
4046

41-
@Mock private Messager mMessager;
47+
@Rule public CompilationRule mCompilationRule = new CompilationRule();
4248

43-
Elements mElements = mock(Elements.class);
44-
Types mTypes = mock(Types.class);
45-
TypeElement mTypeElement = mock(TypeElement.class);
46-
DependencyInjectionHelper mDependencyInjectionHelper = mock(DependencyInjectionHelper.class);
47-
LayoutSpecModelFactory mFactory = new LayoutSpecModelFactory();
48-
LayoutSpec mLayoutSpec = mock(LayoutSpec.class);
49+
private final LayoutSpecModelFactory mFactory = new LayoutSpecModelFactory();
50+
private final DependencyInjectionHelper mDependencyInjectionHelper =
51+
mock(DependencyInjectionHelper.class);
52+
53+
private LayoutSpecModel mLayoutSpecModel;
4954

5055
@Before
5156
public void setUp() {
52-
MockitoAnnotations.initMocks(this);
53-
when(mElements.getDocComment(mTypeElement)).thenReturn("");
54-
when(mTypeElement.getQualifiedName()).thenReturn(new MockName(TEST_QUALIFIED_SPEC_NAME));
55-
when(mTypeElement.getAnnotation(LayoutSpec.class)).thenReturn(mLayoutSpec);
56-
}
57+
Elements elements = mCompilationRule.getElements();
58+
Types types = mCompilationRule.getTypes();
59+
TypeElement typeElement =
60+
elements.getTypeElement(LayoutSpecModelFactoryTest.TestLayoutSpec.class.getCanonicalName());
5761

58-
@Test
59-
public void testCreate() {
60-
LayoutSpecModel layoutSpecModel =
62+
mLayoutSpecModel =
6163
mFactory.create(
62-
mElements,
63-
mTypes,
64-
mTypeElement,
65-
mMessager,
64+
elements,
65+
types,
66+
typeElement,
67+
mock(Messager.class),
6668
RunMode.normal(),
6769
mDependencyInjectionHelper,
6870
null);
71+
}
6972

70-
assertThat(layoutSpecModel.getSpecName()).isEqualTo("TestSpec");
71-
assertThat(layoutSpecModel.getComponentName()).isEqualTo("Test");
72-
assertThat(layoutSpecModel.getSpecTypeName().toString()).isEqualTo(TEST_QUALIFIED_SPEC_NAME);
73-
assertThat(layoutSpecModel.getComponentTypeName().toString())
73+
@Test
74+
public void testCreate() {
75+
assertThat(mLayoutSpecModel.getSpecName()).isEqualTo("TestLayoutSpec");
76+
assertThat(mLayoutSpecModel.getComponentName()).isEqualTo("TestLayoutComponentName");
77+
assertThat(mLayoutSpecModel.getSpecTypeName().toString()).isEqualTo(TEST_QUALIFIED_SPEC_NAME);
78+
assertThat(mLayoutSpecModel.getComponentTypeName().toString())
7479
.isEqualTo(TEST_QUALIFIED_COMPONENT_NAME);
75-
76-
assertThat(layoutSpecModel.getDelegateMethods()).isEmpty();
77-
78-
assertThat(layoutSpecModel.getProps()).isEmpty();
79-
80-
assertThat(layoutSpecModel.hasInjectedDependencies()).isTrue();
81-
assertThat(layoutSpecModel.getDependencyInjectionHelper()).isSameAs(mDependencyInjectionHelper);
80+
assertThat(mLayoutSpecModel.getDelegateMethods()).hasSize(2);
81+
assertThat(mLayoutSpecModel.getProps()).hasSize(2);
82+
assertThat(mLayoutSpecModel.hasInjectedDependencies()).isTrue();
83+
assertThat(mLayoutSpecModel.getDependencyInjectionHelper())
84+
.isSameAs(mDependencyInjectionHelper);
8285
}
8386

84-
@Test
85-
public void testCreateWithSpecifiedName() {
86-
when(mLayoutSpec.value()).thenReturn("TestComponentName");
87-
LayoutSpecModel layoutSpecModel =
88-
mFactory.create(
89-
mElements,
90-
mTypes,
91-
mTypeElement,
92-
mMessager,
93-
RunMode.normal(),
94-
mDependencyInjectionHelper,
95-
null);
87+
@LayoutSpec(value = "TestLayoutComponentName")
88+
static class TestLayoutSpec {
89+
90+
@OnCreateInitialState
91+
static void createInitialState(@Prop int prop1) {}
9692

97-
assertThat(layoutSpecModel.getSpecName()).isEqualTo("TestSpec");
98-
assertThat(layoutSpecModel.getComponentName()).isEqualTo("TestComponentName");
99-
assertThat(layoutSpecModel.getSpecTypeName().toString()).isEqualTo(TEST_QUALIFIED_SPEC_NAME);
100-
assertThat(layoutSpecModel.getComponentTypeName().toString())
101-
.isEqualTo("com.facebook.litho.TestComponentName");
93+
@OnCreateLayout
94+
static Component onCreateLayout(ComponentContext c, @Prop int prop2) {
95+
return null;
96+
}
10297
}
10398
}

0 commit comments

Comments
 (0)