• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 package android.databinding.tool;
15 
16 
17 import org.junit.Before;
18 import org.junit.Test;
19 
20 import android.databinding.tool.expr.Expr;
21 import android.databinding.tool.expr.ExprModel;
22 import android.databinding.tool.expr.FieldAccessExpr;
23 import android.databinding.tool.expr.IdentifierExpr;
24 import android.databinding.tool.expr.StaticIdentifierExpr;
25 import android.databinding.tool.reflection.Callable;
26 import android.databinding.tool.reflection.java.JavaClass;
27 
28 import java.util.List;
29 import java.util.Map;
30 
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertSame;
34 import static org.junit.Assert.assertTrue;
35 
36 public class LayoutBinderTest {
37     LayoutBinder mLayoutBinder;
38     ExprModel mExprModel;
39     @Before
setUp()40     public void setUp() throws Exception {
41         mLayoutBinder = new MockLayoutBinder();
42         mExprModel = mLayoutBinder.getModel();
43     }
44 
45     @Test
testRegisterId()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
testRegisterImport()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
testParse()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
testParseWithMethods()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 
findIdentifier(String name)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 
fullName()123         public String fullName() {
124             return name + " " + lastName;
125         }
126     }
127 }
128