BindingAdapterTestBase.java revision 4d4979490e1fa374c0d7f3599fed0a9e83a579d0
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.ViewDataBinding;
19import android.databinding.testapp.vo.BindingAdapterBindingObject;
20
21import java.lang.reflect.InvocationTargetException;
22import java.lang.reflect.Method;
23
24public class BindingAdapterTestBase<T extends ViewDataBinding, V extends BindingAdapterBindingObject>
25        extends BaseDataBinderTest<T> {
26    private Class<V> mBindingObjectClass;
27
28    protected V mBindingObject;
29
30    private Method mSetMethod;
31
32    public BindingAdapterTestBase(Class<T> binderClass, Class<V> observableClass, int layoutId) {
33        super(binderClass);
34        mBindingObjectClass = observableClass;
35        try {
36            mSetMethod = binderClass.getDeclaredMethod("setObj", observableClass);
37        } catch (NoSuchMethodException e) {
38            throw new RuntimeException(e);
39        }
40    }
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45        initBinder(new Runnable() {
46            @Override
47            public void run() {
48                try {
49                    mBindingObject = mBindingObjectClass.newInstance();
50                    mSetMethod.invoke(getBinder(), mBindingObject);
51                    getBinder().executePendingBindings();
52                } catch (IllegalAccessException e) {
53                    throw new RuntimeException(e);
54                } catch (InvocationTargetException e) {
55                    throw new RuntimeException(e);
56                } catch (InstantiationException e) {
57                    throw new RuntimeException(e);
58                }
59
60            }
61        });
62    }
63
64    protected void changeValues() throws Throwable {
65        runTestOnUiThread(new Runnable() {
66            @Override
67            public void run() {
68                mBindingObject.changeValues();
69                getBinder().executePendingBindings();
70            }
71        });
72    }
73}
74