ExprTest.java revision 019c36b97c7c172ac03997f6bf170e65b2ed7fe4
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.tool.expr;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import android.databinding.tool.LayoutBinder;
23import android.databinding.tool.MockLayoutBinder;
24import android.databinding.tool.reflection.ModelAnalyzer;
25import android.databinding.tool.reflection.ModelClass;
26import android.databinding.tool.reflection.java.JavaAnalyzer;
27
28import java.util.BitSet;
29import java.util.List;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertTrue;
33
34public class ExprTest{
35    private static class DummyExpr extends Expr {
36        String mKey;
37        public DummyExpr(String key, DummyExpr... children) {
38            super(children);
39            mKey = key;
40        }
41
42        @Override
43        protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
44            return modelAnalyzer.findClass(Integer.class);
45        }
46
47        @Override
48        protected List<Dependency> constructDependencies() {
49            return constructDynamicChildrenDependencies();
50        }
51
52        @Override
53        protected String computeUniqueKey() {
54            return mKey + super.computeUniqueKey();
55        }
56
57        @Override
58        public boolean isDynamic() {
59            return true;
60        }
61    }
62
63    @Before
64    public void setUp() throws Exception {
65        JavaAnalyzer.initForTests();
66    }
67
68    @Test(expected=IllegalStateException.class)
69    public void testBadExpr() {
70        Expr expr = new Expr() {
71            @Override
72            protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
73                return modelAnalyzer.findClass(Integer.class);
74            }
75
76            @Override
77            protected List<Dependency> constructDependencies() {
78                return constructDynamicChildrenDependencies();
79            }
80        };
81        expr.getUniqueKey();
82    }
83
84    @Test
85    public void testBasicInvalidationFlag() {
86        LayoutBinder lb = new MockLayoutBinder();
87        ExprModel model = lb.getModel();
88        model.seal();
89        DummyExpr d = new DummyExpr("a");
90        d.setModel(model);
91        d.setId(3);
92        d.enableDirectInvalidation();
93        assertTrue(d.getInvalidFlags().get(3));
94        BitSet clone = (BitSet) model.getInvalidateAnyBitSet().clone();
95        clone.and(d.getInvalidFlags());
96        assertEquals(1, clone.cardinality());
97    }
98
99    @Test
100    public void testCannotBeInvalidated() {
101        LayoutBinder lb = new MockLayoutBinder();
102        ExprModel model = lb.getModel();
103        model.seal();
104        DummyExpr d = new DummyExpr("a");
105        d.setModel(model);
106        d.setId(3);
107        // +1 for invalidate all flag
108        assertEquals(1, d.getInvalidFlags().cardinality());
109        assertEquals(model.getInvalidateAnyBitSet(), d.getInvalidFlags());
110    }
111
112    @Test
113    public void testInvalidationInheritance() {
114        ExprModel model = new ExprModel();
115        DummyExpr a = model.register(new DummyExpr("a"));
116        DummyExpr b = model.register(new DummyExpr("b"));
117        DummyExpr c = model.register(new DummyExpr("c", a, b));
118        a.enableDirectInvalidation();
119        b.enableDirectInvalidation();
120        c.setBindingExpression(true);
121        model.seal();
122        assertFlags(c, a, b);
123    }
124
125    @Test
126    public void testInvalidationInheritance2() {
127        ExprModel model = new ExprModel();
128        DummyExpr a = model.register(new DummyExpr("a"));
129        DummyExpr b = model.register(new DummyExpr("b", a));
130        DummyExpr c = model.register(new DummyExpr("c", b));
131        a.enableDirectInvalidation();
132        b.enableDirectInvalidation();
133        c.setBindingExpression(true);
134        model.seal();
135        assertFlags(c, a, b);
136    }
137
138    @Test
139    public void testShouldReadFlags() {
140        ExprModel model = new ExprModel();
141        DummyExpr a = model.register(new DummyExpr("a"));
142        a.enableDirectInvalidation();
143        a.setBindingExpression(true);
144        model.seal();
145        assertFlags(a, a);
146    }
147
148    @Test
149    public void testShouldReadDependencyFlags() {
150        ExprModel model = new ExprModel();
151        DummyExpr a = model.register(new DummyExpr("a"));
152        DummyExpr b = model.register(new DummyExpr("b", a));
153        DummyExpr c = model.register(new DummyExpr("c", b));
154        a.enableDirectInvalidation();
155        b.enableDirectInvalidation();
156        b.setBindingExpression(true);
157        c.setBindingExpression(true);
158        model.seal();
159        assertFlags(b, a, b);
160        assertFlags(c, a, b);
161    }
162
163    private void assertFlags(Expr a, Expr... exprs) {
164        BitSet bitSet = a.getShouldReadFlags();
165        for (Expr expr : exprs) {
166            BitSet clone = (BitSet) bitSet.clone();
167            clone.and(expr.getInvalidFlags());
168            assertEquals("should read flags of " + a.getUniqueKey() + " should include " + expr
169                    .getUniqueKey(), expr.getInvalidFlags(), clone);
170        }
171
172        BitSet composite = new BitSet();
173        for (Expr expr : exprs) {
174            composite.or(expr.getInvalidFlags());
175        }
176        assertEquals("composite flags should match", composite, bitSet);
177    }
178}
179