ExprTest.java revision e52882df6130221462bf07f5f2b52de5c4b0f8de
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;
27import android.databinding.tool.writer.KCode;
28
29import java.util.BitSet;
30import java.util.List;
31
32import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertTrue;
34
35public class ExprTest{
36    private static class DummyExpr extends Expr {
37        String mKey;
38        public DummyExpr(String key, DummyExpr... children) {
39            super(children);
40            mKey = key;
41        }
42
43        @Override
44        protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
45            return modelAnalyzer.findClass(Integer.class);
46        }
47
48        @Override
49        protected List<Dependency> constructDependencies() {
50            return constructDynamicChildrenDependencies();
51        }
52
53        @Override
54        protected String computeUniqueKey() {
55            return mKey + super.computeUniqueKey();
56        }
57
58        @Override
59        protected KCode generateCode() {
60            return new KCode();
61        }
62
63        @Override
64        public boolean isDynamic() {
65            return true;
66        }
67    }
68
69    @Before
70    public void setUp() throws Exception {
71        JavaAnalyzer.initForTests();
72    }
73
74    @Test(expected=Throwable.class)
75    public void testBadExpr() {
76        Expr expr = new Expr() {
77            @Override
78            protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
79                return modelAnalyzer.findClass(Integer.class);
80            }
81
82            @Override
83            protected List<Dependency> constructDependencies() {
84                return constructDynamicChildrenDependencies();
85            }
86
87            @Override
88            protected KCode generateCode() {
89                return new KCode();
90            }
91        };
92        expr.getUniqueKey();
93    }
94
95    @Test
96    public void testBasicInvalidationFlag() {
97        LayoutBinder lb = new MockLayoutBinder();
98        ExprModel model = lb.getModel();
99        model.seal();
100        DummyExpr d = new DummyExpr("a");
101        d.setModel(model);
102        d.setId(3);
103        d.enableDirectInvalidation();
104        assertTrue(d.getInvalidFlags().get(3));
105        BitSet clone = (BitSet) model.getInvalidateAnyBitSet().clone();
106        clone.and(d.getInvalidFlags());
107        assertEquals(1, clone.cardinality());
108    }
109
110    @Test
111    public void testCannotBeInvalidated() {
112        LayoutBinder lb = new MockLayoutBinder();
113        ExprModel model = lb.getModel();
114        model.seal();
115        DummyExpr d = new DummyExpr("a");
116        d.setModel(model);
117        d.setId(3);
118        // +1 for invalidate all flag
119        assertEquals(1, d.getInvalidFlags().cardinality());
120        assertEquals(model.getInvalidateAnyBitSet(), d.getInvalidFlags());
121    }
122
123    @Test
124    public void testInvalidationInheritance() {
125        ExprModel model = new ExprModel();
126        DummyExpr a = model.register(new DummyExpr("a"));
127        DummyExpr b = model.register(new DummyExpr("b"));
128        DummyExpr c = model.register(new DummyExpr("c", a, b));
129        a.enableDirectInvalidation();
130        b.enableDirectInvalidation();
131        c.setBindingExpression(true);
132        model.seal();
133        assertFlags(c, a, b);
134    }
135
136    @Test
137    public void testInvalidationInheritance2() {
138        ExprModel model = new ExprModel();
139        DummyExpr a = model.register(new DummyExpr("a"));
140        DummyExpr b = model.register(new DummyExpr("b", a));
141        DummyExpr c = model.register(new DummyExpr("c", b));
142        a.enableDirectInvalidation();
143        b.enableDirectInvalidation();
144        c.setBindingExpression(true);
145        model.seal();
146        assertFlags(c, a, b);
147    }
148
149    @Test
150    public void testShouldReadFlags() {
151        ExprModel model = new ExprModel();
152        DummyExpr a = model.register(new DummyExpr("a"));
153        a.enableDirectInvalidation();
154        a.setBindingExpression(true);
155        model.seal();
156        assertFlags(a, a);
157    }
158
159    @Test
160    public void testShouldReadDependencyFlags() {
161        ExprModel model = new ExprModel();
162        DummyExpr a = model.register(new DummyExpr("a"));
163        DummyExpr b = model.register(new DummyExpr("b", a));
164        DummyExpr c = model.register(new DummyExpr("c", b));
165        a.enableDirectInvalidation();
166        b.enableDirectInvalidation();
167        b.setBindingExpression(true);
168        c.setBindingExpression(true);
169        model.seal();
170        assertFlags(b, a, b);
171        assertFlags(c, a, b);
172    }
173
174    private void assertFlags(Expr a, Expr... exprs) {
175        BitSet bitSet = a.getShouldReadFlags();
176        for (Expr expr : exprs) {
177            BitSet clone = (BitSet) bitSet.clone();
178            clone.and(expr.getInvalidFlags());
179            assertEquals("should read flags of " + a.getUniqueKey() + " should include " + expr
180                    .getUniqueKey(), expr.getInvalidFlags(), clone);
181        }
182
183        BitSet composite = new BitSet();
184        for (Expr expr : exprs) {
185            composite.or(expr.getInvalidFlags());
186        }
187        assertEquals("composite flags should match", composite, bitSet);
188    }
189}
190