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.BR;
17import android.databinding.testapp.databinding.MultiArgAdapterEvaluationTestBinding;
18import android.databinding.testapp.databinding.MultiArgAdapterTestBinding;
19import android.test.UiThreadTest;
20
21import static android.databinding.testapp.adapter.MultiArgTestAdapter.MultiBindingClass1;
22import static android.databinding.testapp.adapter.MultiArgTestAdapter.MultiBindingClass2;
23import static android.databinding.testapp.adapter.MultiArgTestAdapter.join;
24
25public class MultiArgAdapterEvaluationTest extends BaseDataBinderTest<MultiArgAdapterEvaluationTestBinding> {
26
27    public MultiArgAdapterEvaluationTest() {
28        super(MultiArgAdapterEvaluationTestBinding.class);
29    }
30
31    @UiThreadTest
32    public void testMultiArgIsCalled() {
33        initBinder();
34        MultiBindingClass1 obj1 = new MultiBindingClass1();
35        MultiBindingClass2 obj2 = new MultiBindingClass2();
36        obj1.setValue("a", false);
37        obj2.setValue("b", false);
38        mBinder.setObj1(obj1);
39        mBinder.setObj2(obj2);
40        mBinder.executePendingBindings();
41
42        assertEquals(mBinder.merged.getText().toString(), join(obj1.getValue(), obj2.getValue()));
43        assertEquals(mBinder.view2.getText().toString(), join(obj2.getValue()));
44        assertEquals(mBinder.view2text.getText().toString(), obj2.getValue());
45
46        String prev2 = mBinder.view2.getText().toString();
47        String prevValue = mBinder.merged.getText().toString();
48        obj1.setValue("o", false);
49        mBinder.executePendingBindings();
50        assertEquals(prevValue, mBinder.merged.getText().toString());
51        obj2.setValue("p", false);
52        mBinder.executePendingBindings();
53        assertEquals(prevValue, mBinder.merged.getText().toString());
54        // now invalidate obj1 only, obj2 should be evaluated as well
55        obj1.setValue("o2", true);
56        mBinder.executePendingBindings();
57        assertEquals(join(obj1, obj2), mBinder.merged.getText().toString());
58        assertEquals("obj2 should not be re-evaluated", prev2, mBinder.view2.getText().toString());
59        assertEquals("obj2 should not be re-evaluated", prev2,
60                mBinder.view2text.getText().toString());
61    }
62}
63