LayoutBinderTest.java revision c96847768305d83c6bc4919432af9bd9bfe4c08e
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        int originalSize = mExprModel.size();
48        mLayoutBinder.addVariable("test", "java.lang.String");
49        assertEquals(originalSize + 1, mExprModel.size());
50        final Map.Entry<String, Expr> entry = findIdentifier("test");
51        final Expr value = entry.getValue();
52        assertEquals(value.getClass(), IdentifierExpr.class);
53        final IdentifierExpr id = (IdentifierExpr) value;
54        assertEquals("test", id.getName());
55        assertEquals(new JavaClass(String.class), id.getResolvedType());
56        assertTrue(id.isDynamic());
57    }
58
59    @Test
60    public void testRegisterImport() {
61        int originalSize = mExprModel.size();
62        mExprModel.addImport("test", "java.lang.String");
63        assertEquals(originalSize + 1, mExprModel.size());
64        final Map.Entry<String, Expr> entry = findIdentifier("test");
65        final Expr value = entry.getValue();
66        assertEquals(value.getClass(), StaticIdentifierExpr.class);
67        final IdentifierExpr id = (IdentifierExpr) value;
68        assertEquals("test", id.getName());
69        assertEquals(new JavaClass(String.class), id.getResolvedType());
70        assertFalse(id.isDynamic());
71    }
72
73    @Test
74    public void testParse() {
75        int originalSize = mExprModel.size();
76        mLayoutBinder.addVariable("user", "android.databinding.tool2.LayoutBinderTest.TestUser");
77        mLayoutBinder.parse("user.name");
78        mLayoutBinder.parse("user.lastName");
79        assertEquals(originalSize + 3, mExprModel.size());
80        final List<Expr> bindingExprs = mExprModel.getBindingExpressions();
81        assertEquals(2, bindingExprs.size());
82        IdentifierExpr id = mExprModel.identifier("user");
83        assertTrue(bindingExprs.get(0) instanceof FieldAccessExpr);
84        assertTrue(bindingExprs.get(1) instanceof FieldAccessExpr);
85        assertEquals(2, id.getParents().size());
86        assertTrue(bindingExprs.get(0).getChildren().contains(id));
87        assertTrue(bindingExprs.get(1).getChildren().contains(id));
88    }
89
90    @Test
91    public void testParseWithMethods() {
92        mLayoutBinder.addVariable("user", "android.databinding.tool.LayoutBinderTest.TestUser");
93        mLayoutBinder.parse("user.fullName");
94        Expr item = mExprModel.getBindingExpressions().get(0);
95        assertTrue(item instanceof FieldAccessExpr);
96        IdentifierExpr id = mExprModel.identifier("user");
97        FieldAccessExpr fa = (FieldAccessExpr) item;
98        fa.getResolvedType();
99        final Callable getter = fa.getGetter();
100        assertTrue(getter.type == Callable.Type.METHOD);
101        assertSame(id, fa.getChild());
102        assertTrue(fa.isDynamic());
103    }
104
105    private Map.Entry<String, Expr> findIdentifier(String name) {
106        for (Map.Entry<String, Expr> entry : mExprModel.getExprMap().entrySet()) {
107            if (entry.getValue() instanceof IdentifierExpr) {
108                IdentifierExpr expr = (IdentifierExpr) entry.getValue();
109                if (name.equals(expr.getName())) {
110                    return entry;
111                }
112            }
113        }
114        return null;
115    }
116
117    static class TestUser {
118        public String name;
119        public String lastName;
120
121        public String fullName() {
122            return name + " " + lastName;
123        }
124    }
125}
126