UnnecessaryCalculationTest.java revision eebcbdd5d35e56a2c8ef37feeb65df46130d001d
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 */
16package android.databinding.testapp;
17
18import android.databinding.testapp.databinding.UnnecessaryCalculationBinding;
19import android.databinding.testapp.vo.BasicObject;
20import android.support.annotation.UiThread;
21import android.test.UiThreadTest;
22
23import java.util.concurrent.atomic.AtomicInteger;
24
25public class UnnecessaryCalculationTest extends BaseDataBinderTest<UnnecessaryCalculationBinding> {
26
27    public UnnecessaryCalculationTest() {
28        super(UnnecessaryCalculationBinding.class);
29    }
30
31    @Override
32    protected void setUp() throws Exception {
33        super.setUp();
34        initBinder();
35    }
36
37    @UiThreadTest
38    public void testDontSetUnnecessaryFlags() {
39        BasicObjWithCounter obja = new BasicObjWithCounter();
40        BasicObjWithCounter objb = new BasicObjWithCounter();
41        BasicObjWithCounter objc = new BasicObjWithCounter();
42        mBinder.setObja(obja);
43        mBinder.setObjb(objb);
44        mBinder.setObjc(objc);
45        mBinder.setA(true);
46        mBinder.setB(true);
47        mBinder.setC(false);
48        mBinder.executePendingBindings();
49        assertEquals("true", mBinder.textView.getText().toString());
50        assertEquals("true", mBinder.textView2.getText().toString());
51        assertEquals(1, obja.counter);
52        assertEquals(1, objb.counter);
53        assertEquals(0, objc.counter);
54        obja = new BasicObjWithCounter();
55        mBinder.setObja(obja);
56        mBinder.executePendingBindings();
57        assertEquals("true", mBinder.textView.getText().toString());
58        assertEquals("true", mBinder.textView2.getText().toString());
59        assertEquals(1, obja.counter);
60        assertEquals(1, objb.counter);
61        assertEquals(0, objc.counter);
62    }
63
64    private static class BasicObjWithCounter extends BasicObject {
65        int counter = 0;
66
67        @Override
68        public String boolMethod(boolean value) {
69            counter ++;
70            return super.boolMethod(value);
71        }
72    }
73
74}
75