LayoutBinderTest.java revision eebcbdd5d35e56a2c8ef37feeb65df46130d001d
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    MockLayoutBinder 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", null);
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", null);
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                null);
78        mLayoutBinder.parse("user.name", null);
79        mLayoutBinder.parse("user.lastName", null);
80        assertEquals(originalSize + 3, mExprModel.size());
81        final List<Expr> bindingExprs = mExprModel.getBindingExpressions();
82        assertEquals(2, bindingExprs.size());
83        IdentifierExpr id = mExprModel.identifier("user");
84        assertTrue(bindingExprs.get(0) instanceof FieldAccessExpr);
85        assertTrue(bindingExprs.get(1) instanceof FieldAccessExpr);
86        assertEquals(2, id.getParents().size());
87        assertTrue(bindingExprs.get(0).getChildren().contains(id));
88        assertTrue(bindingExprs.get(1).getChildren().contains(id));
89    }
90
91    @Test
92    public void testParseWithMethods() {
93        mLayoutBinder.addVariable("user", "android.databinding.tool.LayoutBinderTest.TestUser",
94                null);
95        mLayoutBinder.parse("user.fullName", null);
96        Expr item = mExprModel.getBindingExpressions().get(0);
97        assertTrue(item instanceof FieldAccessExpr);
98        IdentifierExpr id = mExprModel.identifier("user");
99        FieldAccessExpr fa = (FieldAccessExpr) item;
100        fa.getResolvedType();
101        final Callable getter = fa.getGetter();
102        assertTrue(getter.type == Callable.Type.METHOD);
103        assertSame(id, fa.getChild());
104        assertTrue(fa.isDynamic());
105    }
106
107    private Map.Entry<String, Expr> findIdentifier(String name) {
108        for (Map.Entry<String, Expr> entry : mExprModel.getExprMap().entrySet()) {
109            if (entry.getValue() instanceof IdentifierExpr) {
110                IdentifierExpr expr = (IdentifierExpr) entry.getValue();
111                if (name.equals(expr.getName())) {
112                    return entry;
113                }
114            }
115        }
116        return null;
117    }
118
119    static class TestUser {
120        public String name;
121        public String lastName;
122
123        public String fullName() {
124            return name + " " + lastName;
125        }
126    }
127}
128