summaryrefslogtreecommitdiff
path: root/compiler/src/test/java/android/databinding/tool/LayoutBinderTest.java
blob: c7f81ab184f54e46fb7ddc7bf0f121de157cb2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2015 The Android Open Source Project
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.databinding.tool;


import android.databinding.tool.expr.Expr;
import android.databinding.tool.expr.ExprModel;
import android.databinding.tool.expr.FieldAccessExpr;
import android.databinding.tool.expr.IdentifierExpr;
import android.databinding.tool.expr.StaticIdentifierExpr;
import android.databinding.tool.reflection.Callable;
import android.databinding.tool.reflection.java.JavaAnalyzer;
import android.databinding.tool.reflection.java.JavaClass;

import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class LayoutBinderTest {
    MockLayoutBinder mLayoutBinder;
    ExprModel mExprModel;
    @Before
    public void setUp() throws Exception {
        mLayoutBinder = new MockLayoutBinder();
        mExprModel = mLayoutBinder.getModel();
        JavaAnalyzer.initForTests();
    }

    @Test
    public void testRegisterId() {
        int originalSize = mExprModel.size();
        mLayoutBinder.addVariable("test", "java.lang.String", null);
        assertEquals(originalSize + 1, mExprModel.size());
        final Map.Entry<String, Expr> entry = findIdentifier("test");
        final Expr value = entry.getValue();
        assertEquals(value.getClass(), IdentifierExpr.class);
        final IdentifierExpr id = (IdentifierExpr) value;
        assertEquals("test", id.getName());
        assertEquals(new JavaClass(String.class), id.getResolvedType());
        assertTrue(id.isDynamic());
    }

    @Test
    public void testRegisterImport() {
        int originalSize = mExprModel.size();
        mExprModel.addImport("test", "java.lang.String", null);
        assertEquals(originalSize + 1, mExprModel.size());
        final Map.Entry<String, Expr> entry = findIdentifier("test");
        final Expr value = entry.getValue();
        assertEquals(value.getClass(), StaticIdentifierExpr.class);
        final IdentifierExpr id = (IdentifierExpr) value;
        assertEquals("test", id.getName());
        assertEquals(new JavaClass(String.class), id.getResolvedType());
        assertFalse(id.isDynamic());
    }

    @Test
    public void testParse() {
        int originalSize = mExprModel.size();
        mLayoutBinder.addVariable("user", "android.databinding.tool2.LayoutBinderTest.TestUser",
                null);
        mLayoutBinder.parse("user.name", null);
        mLayoutBinder.parse("user.lastName", null);
        assertEquals(originalSize + 3, mExprModel.size());
        final List<Expr> bindingExprs = mExprModel.getBindingExpressions();
        assertEquals(2, bindingExprs.size());
        IdentifierExpr id = mExprModel.identifier("user");
        assertTrue(bindingExprs.get(0) instanceof FieldAccessExpr);
        assertTrue(bindingExprs.get(1) instanceof FieldAccessExpr);
        assertEquals(2, id.getParents().size());
        assertTrue(bindingExprs.get(0).getChildren().contains(id));
        assertTrue(bindingExprs.get(1).getChildren().contains(id));
    }

    @Test
    public void testParseWithMethods() {
        mLayoutBinder.addVariable("user", "android.databinding.tool.LayoutBinderTest.TestUser",
                null);
        mLayoutBinder.parse("user.fullName", null);
        Expr item = mExprModel.getBindingExpressions().get(0);
        assertTrue(item instanceof FieldAccessExpr);
        IdentifierExpr id = mExprModel.identifier("user");
        FieldAccessExpr fa = (FieldAccessExpr) item;
        fa.getResolvedType();
        final Callable getter = fa.getGetter();
        assertTrue(getter.type == Callable.Type.METHOD);
        assertSame(id, fa.getTarget());
        assertTrue(fa.isDynamic());
    }

    private Map.Entry<String, Expr> findIdentifier(String name) {
        for (Map.Entry<String, Expr> entry : mExprModel.getExprMap().entrySet()) {
            if (entry.getValue() instanceof IdentifierExpr) {
                IdentifierExpr expr = (IdentifierExpr) entry.getValue();
                if (name.equals(expr.getName())) {
                    return entry;
                }
            }
        }
        return null;
    }

    static class TestUser {
        public String name;
        public String lastName;

        public String fullName() {
            return name + " " + lastName;
        }
    }
}