|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Java6Assertions.assertThat; |
20 | 20 | import static org.mockito.Mockito.mock; |
21 | | -import static org.mockito.Mockito.when; |
22 | 21 |
|
| 22 | +import com.facebook.litho.Component; |
| 23 | +import com.facebook.litho.ComponentContext; |
23 | 24 | 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; |
24 | 28 | import com.facebook.litho.specmodels.internal.RunMode; |
25 | 29 | import com.facebook.litho.specmodels.model.DependencyInjectionHelper; |
26 | 30 | import com.facebook.litho.specmodels.model.LayoutSpecModel; |
| 31 | +import com.google.testing.compile.CompilationRule; |
27 | 32 | import javax.annotation.processing.Messager; |
28 | 33 | import javax.lang.model.element.TypeElement; |
29 | 34 | import javax.lang.model.util.Elements; |
30 | 35 | import javax.lang.model.util.Types; |
31 | 36 | import org.junit.Before; |
| 37 | +import org.junit.Rule; |
32 | 38 | import org.junit.Test; |
33 | | -import org.mockito.Mock; |
34 | | -import org.mockito.MockitoAnnotations; |
35 | 39 |
|
36 | 40 | /** Tests {@link LayoutSpecModelFactory} */ |
37 | 41 | 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"; |
40 | 46 |
|
41 | | - @Mock private Messager mMessager; |
| 47 | + @Rule public CompilationRule mCompilationRule = new CompilationRule(); |
42 | 48 |
|
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; |
49 | 54 |
|
50 | 55 | @Before |
51 | 56 | 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()); |
57 | 61 |
|
58 | | - @Test |
59 | | - public void testCreate() { |
60 | | - LayoutSpecModel layoutSpecModel = |
| 62 | + mLayoutSpecModel = |
61 | 63 | mFactory.create( |
62 | | - mElements, |
63 | | - mTypes, |
64 | | - mTypeElement, |
65 | | - mMessager, |
| 64 | + elements, |
| 65 | + types, |
| 66 | + typeElement, |
| 67 | + mock(Messager.class), |
66 | 68 | RunMode.normal(), |
67 | 69 | mDependencyInjectionHelper, |
68 | 70 | null); |
| 71 | + } |
69 | 72 |
|
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()) |
74 | 79 | .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); |
82 | 85 | } |
83 | 86 |
|
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) {} |
96 | 92 |
|
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 | + } |
102 | 97 | } |
103 | 98 | } |
0 commit comments