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