LayoutBinderTest.java revision fead9ca09b117136b35bc5bf137340a754f9eddd
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *      http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package android.databinding.tool;
15
16
17import org.junit.Before;
18import org.junit.Test;
19
20import android.databinding.tool.expr.Expr;
21import android.databinding.tool.expr.ExprModel;
22import android.databinding.tool.expr.FieldAccessExpr;
23import android.databinding.tool.expr.IdentifierExpr;
24import android.databinding.tool.expr.StaticIdentifierExpr;
25import android.databinding.tool.reflection.Callable;
26import android.databinding.tool.reflection.java.JavaClass;
27
28import java.util.List;
29import java.util.Map;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertFalse;
33import static org.junit.Assert.assertSame;
34import static org.junit.Assert.assertTrue;
35
36public class LayoutBinderTest {
37    LayoutBinder mLayoutBinder;
38    ExprModel mExprModel;
39    @Before
40    public void setUp() throws Exception {
41        mLayoutBinder = new MockLayoutBinder();
42        mExprModel = mLayoutBinder.getModel();
43    }
44
45    @Test
46    public void testRegisterId() {
47        mLayoutBinder.addVariable("test", "java.lang.String");
48        assertEquals(1, mExprModel.size());
49        final Map.Entry<String, Expr> entry = mExprModel.getExprMap().entrySet().iterator().next();
50        final Expr value = entry.getValue();
51        assertEquals(value.getClass(), IdentifierExpr.class);
52        final IdentifierExpr id = (IdentifierExpr) value;
53        assertEquals("test", id.getName());
54        assertEquals(new JavaClass(String.class), id.getResolvedType());
55        assertTrue(id.isDynamic());
56    }
57
58    @Test
59    public void testRegisterImport() {
60        mExprModel.addImport("test", "java.lang.String");
61        assertEquals(1, mExprModel.size());
62        final Map.Entry<String, Expr> entry = mExprModel.getExprMap().entrySet().iterator().next();
63        final Expr value = entry.getValue();
64        assertEquals(value.getClass(), StaticIdentifierExpr.class);
65        final IdentifierExpr id = (IdentifierExpr) value;
66        assertEquals("test", id.getName());
67        assertEquals(new JavaClass(String.class), id.getResolvedType());
68        assertFalse(id.isDynamic());
69    }
70
71    @Test
72    public void testParse() {
73        mLayoutBinder.addVariable("user", "android.databinding.tool2.LayoutBinderTest.TestUser");
74        mLayoutBinder.parse("user.name");
75        mLayoutBinder.parse("user.lastName");
76        assertEquals(3, mExprModel.size());
77        final List<Expr> bindingExprs = mExprModel.getBindingExpressions();
78        assertEquals(2, bindingExprs.size());
79        IdentifierExpr id = mExprModel.identifier("user");
80        assertTrue(bindingExprs.get(0) instanceof FieldAccessExpr);
81        assertTrue(bindingExprs.get(1) instanceof FieldAccessExpr);
82        assertEquals(2, id.getParents().size());
83        assertTrue(bindingExprs.get(0).getChildren().contains(id));
84        assertTrue(bindingExprs.get(1).getChildren().contains(id));
85    }
86
87    @Test
88    public void testParseWithMethods() {
89        mLayoutBinder.addVariable("user", "android.databinding.tool.LayoutBinderTest.TestUser");
90        mLayoutBinder.parse("user.fullName");
91        Expr item = mExprModel.getBindingExpressions().get(0);
92        assertTrue(item instanceof FieldAccessExpr);
93        IdentifierExpr id = mExprModel.identifier("user");
94        FieldAccessExpr fa = (FieldAccessExpr) item;
95        fa.getResolvedType();
96        final Callable getter = fa.getGetter();
97        assertTrue(getter.type == Callable.Type.METHOD);
98        assertSame(id, fa.getChild());
99        assertTrue(fa.isDynamic());
100    }
101
102    static class TestUser {
103        public String name;
104        public String lastName;
105
106        public String fullName() {
107            return name + " " + lastName;
108        }
109    }
110}
111