| 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 android.databinding.tool.expr.Expr; |
| 18 |
import android.databinding.tool.expr.ExprModel; |
| 19 |
import android.databinding.tool.expr.FieldAccessExpr; |
| 20 |
import android.databinding.tool.expr.IdentifierExpr; |
| 21 |
import android.databinding.tool.expr.StaticIdentifierExpr; |
| 22 |
import android.databinding.tool.reflection.Callable; |
| 23 |
import android.databinding.tool.reflection.java.JavaAnalyzer; |
| 24 |
import android.databinding.tool.reflection.java.JavaClass; |
| 25 |
|
| 26 |
import org.junit.Before; |
| 27 |
import org.junit.Test; |
| 28 |
|
| 29 |
import java.util.List; |
| 30 |
import java.util.Map; |
| 31 |
|
| 32 |
import static org.junit.Assert.assertEquals; |
| 33 |
import static org.junit.Assert.assertFalse; |
| 34 |
import static org.junit.Assert.assertSame; |
| 35 |
import static org.junit.Assert.assertTrue; |
| 36 |
|
| 37 |
public class LayoutBinderTest { |
| 38 |
MockLayoutBinder mLayoutBinder; |
| 39 |
ExprModel mExprModel; |
| 40 |
@Before |
| 41 |
public void setUp() throws Exception { |
| 42 |
mLayoutBinder = new MockLayoutBinder(); |
| 43 |
mExprModel = mLayoutBinder.getModel(); |
| 44 |
JavaAnalyzer.initForTests(); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void testRegisterId() { |
| 49 |
int originalSize = mExprModel.size(); |
| 50 |
mLayoutBinder.addVariable("test", "java.lang.String", null); |
| 51 |
assertEquals(originalSize + 1, mExprModel.size()); |
| 52 |
final Map.Entry<String, Expr> entry = findIdentifier("test"); |
| 53 |
final Expr value = entry.getValue(); |
| 54 |
assertEquals(value.getClass(), IdentifierExpr.class); |
| 55 |
final IdentifierExpr id = (IdentifierExpr) value; |
| 56 |
assertEquals("test", id.getName()); |
| 57 |
assertEquals(new JavaClass(String.class), id.getResolvedType()); |
| 58 |
assertTrue(id.isDynamic()); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testRegisterImport() { |
| 63 |
int originalSize = mExprModel.size(); |
| 64 |
mExprModel.addImport("test", "java.lang.String", null); |
| 65 |
assertEquals(originalSize + 1, mExprModel.size()); |
| 66 |
final Map.Entry<String, Expr> entry = findIdentifier("test"); |
| 67 |
final Expr value = entry.getValue(); |
| 68 |
assertEquals(value.getClass(), StaticIdentifierExpr.class); |
| 69 |
final IdentifierExpr id = (IdentifierExpr) value; |
| 70 |
assertEquals("test", id.getName()); |
| 71 |
assertEquals(new JavaClass(String.class), id.getResolvedType()); |
| 72 |
assertFalse(id.isDynamic()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void testParse() { |
| 77 |
int originalSize = mExprModel.size(); |
| 78 |
mLayoutBinder.addVariable("user", "android.databinding.tool2.LayoutBinderTest.TestUser", |
| 79 |
null); |
| 80 |
mLayoutBinder.parse("user.name", null, null); |
| 81 |
mLayoutBinder.parse("user.lastName", null, null); |
| 82 |
assertEquals(originalSize + 3, mExprModel.size()); |
| 83 |
final List<Expr> bindingExprs = mExprModel.getBindingExpressions(); |
| 84 |
assertEquals(2, bindingExprs.size()); |
| 85 |
IdentifierExpr id = mExprModel.identifier("user"); |
| 86 |
assertTrue(bindingExprs.get(0) instanceof FieldAccessExpr); |
| 87 |
assertTrue(bindingExprs.get(1) instanceof FieldAccessExpr); |
| 88 |
assertEquals(2, id.getParents().size()); |
| 89 |
assertTrue(bindingExprs.get(0).getChildren().contains(id)); |
| 90 |
assertTrue(bindingExprs.get(1).getChildren().contains(id)); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testParseWithMethods() { |
| 95 |
mLayoutBinder.addVariable("user", "android.databinding.tool.LayoutBinderTest.TestUser", |
| 96 |
null); |
| 97 |
mLayoutBinder.parse("user.fullName", null, null); |
| 98 |
Expr item = mExprModel.getBindingExpressions().get(0); |
| 99 |
assertTrue(item instanceof FieldAccessExpr); |
| 100 |
IdentifierExpr id = mExprModel.identifier("user"); |
| 101 |
FieldAccessExpr fa = (FieldAccessExpr) item; |
| 102 |
fa.getResolvedType(); |
| 103 |
final Callable getter = fa.getGetter(); |
| 104 |
assertTrue(getter.type == Callable.Type.METHOD); |
| 105 |
assertSame(id, fa.getTarget()); |
| 106 |
assertTrue(fa.isDynamic()); |
| 107 |
} |
| 108 |
|
| 109 |
private Map.Entry<String, Expr> findIdentifier(String name) { |
| 110 |
for (Map.Entry<String, Expr> entry : mExprModel.getExprMap().entrySet()) { |
| 111 |
if (entry.getValue() instanceof IdentifierExpr) { |
| 112 |
IdentifierExpr expr = (IdentifierExpr) entry.getValue(); |
| 113 |
if (name.equals(expr.getName())) { |
| 114 |
return entry; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
static class TestUser { |
| 122 |
public String name; |
| 123 |
public String lastName; |
| 124 |
|
| 125 |
public String fullName() { |
| 126 |
return name + " " + lastName; |
| 127 |
} |
| 128 |
} |
| 129 |
} |