MultiArgAdapterTest.java revision 4d4979490e1fa374c0d7f3599fed0a9e83a579d0
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
14package android.databinding.testapp;
15
16import android.databinding.testapp.adapter.MultiArgTestAdapter;
17import android.databinding.testapp.databinding.MultiArgAdapterTestBinding;
18import android.test.UiThreadTest;
19import android.view.View;
20import android.databinding.testapp.BR;
21import static android.databinding.testapp.adapter.MultiArgTestAdapter.*;
22
23public class MultiArgAdapterTest extends BaseDataBinderTest<MultiArgAdapterTestBinding> {
24
25    public MultiArgAdapterTest() {
26        super(MultiArgAdapterTestBinding.class);
27    }
28
29    @UiThreadTest
30    public void testMultiArgIsCalled() {
31        initBinder();
32        MultiBindingClass1 obj1 = new MultiBindingClass1();
33        MultiBindingClass2 obj2 = new MultiBindingClass2();
34        MultiBindingClass1 obj3 = new MultiBindingClass1();
35        MultiBindingClass2 obj4 = new MultiBindingClass2();
36        obj1.setValue("a", false);
37        obj2.setValue("b", false);
38        obj3.setValue("c", false);
39        obj4.setValue("d", false);
40        mBinder.setObj1(obj1);
41        mBinder.setObj2(obj2);
42        mBinder.setObj3(obj3);
43        mBinder.setObj4(obj4);
44        mBinder.executePendingBindings();
45
46        assertEquals(mBinder.merged.getText().toString(), join(obj1, obj2));
47        assertEquals(mBinder.view2.getText().toString(), join(obj2));
48        assertEquals(mBinder.view3.getText().toString(), join(obj3));
49        assertEquals(mBinder.view4.getText().toString(), join(obj4));
50        String prev2 = mBinder.view2.getText().toString();
51        String prevValue = mBinder.merged.getText().toString();
52        obj1.setValue("o", false);
53        mBinder.executePendingBindings();
54        assertEquals(prevValue, mBinder.merged.getText().toString());
55        obj2.setValue("p", false);
56        mBinder.executePendingBindings();
57        assertEquals(prevValue, mBinder.merged.getText().toString());
58        // now invalidate obj1 only, obj2 should be evaluated as well
59        obj1.notifyPropertyChanged(BR._all);
60        String prev3 = mBinder.view3.getText().toString();
61        String prev4 = mBinder.view4.getText().toString();
62        obj3.setValue("q", false);
63        obj4.setValue("r", false);
64        mBinder.executePendingBindings();
65        assertEquals(join(obj1, obj2), mBinder.merged.getText().toString());
66        assertEquals("obj2 should not be re-evaluated", prev2, mBinder.view2.getText().toString());
67        // make sure 3 and 4 are not invalidated
68        assertEquals("obj3 should not be re-evaluated", prev3, mBinder.view3.getText().toString());
69        assertEquals("obj4 should not be re-evaluated", prev4, mBinder.view4.getText().toString());
70    }
71}
72