171eb6908f2689bd17000237eb645896c36da0138George Mount/*
271eb6908f2689bd17000237eb645896c36da0138George Mount * Copyright (C) 2015 The Android Open Source Project
371eb6908f2689bd17000237eb645896c36da0138George Mount *
471eb6908f2689bd17000237eb645896c36da0138George Mount * Licensed under the Apache License, Version 2.0 (the "License");
571eb6908f2689bd17000237eb645896c36da0138George Mount * you may not use this file except in compliance with the License.
671eb6908f2689bd17000237eb645896c36da0138George Mount * You may obtain a copy of the License at
771eb6908f2689bd17000237eb645896c36da0138George Mount *
871eb6908f2689bd17000237eb645896c36da0138George Mount *      http://www.apache.org/licenses/LICENSE-2.0
971eb6908f2689bd17000237eb645896c36da0138George Mount *
1071eb6908f2689bd17000237eb645896c36da0138George Mount * Unless required by applicable law or agreed to in writing, software
1171eb6908f2689bd17000237eb645896c36da0138George Mount * distributed under the License is distributed on an "AS IS" BASIS,
1271eb6908f2689bd17000237eb645896c36da0138George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1371eb6908f2689bd17000237eb645896c36da0138George Mount * See the License for the specific language governing permissions and
1471eb6908f2689bd17000237eb645896c36da0138George Mount * limitations under the License.
1571eb6908f2689bd17000237eb645896c36da0138George Mount */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.testapp;
1771eb6908f2689bd17000237eb645896c36da0138George Mount
18fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.ViewDataBinding;
19fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.testapp.vo.BindingAdapterBindingObject;
2071eb6908f2689bd17000237eb645896c36da0138George Mount
2171eb6908f2689bd17000237eb645896c36da0138George Mountimport java.lang.reflect.InvocationTargetException;
2271eb6908f2689bd17000237eb645896c36da0138George Mountimport java.lang.reflect.Method;
2371eb6908f2689bd17000237eb645896c36da0138George Mount
244c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mountpublic class BindingAdapterTestBase<T extends ViewDataBinding, V extends BindingAdapterBindingObject>
2571eb6908f2689bd17000237eb645896c36da0138George Mount        extends BaseDataBinderTest<T> {
2671eb6908f2689bd17000237eb645896c36da0138George Mount    private Class<V> mBindingObjectClass;
2771eb6908f2689bd17000237eb645896c36da0138George Mount
2871eb6908f2689bd17000237eb645896c36da0138George Mount    protected V mBindingObject;
2971eb6908f2689bd17000237eb645896c36da0138George Mount
3071eb6908f2689bd17000237eb645896c36da0138George Mount    private Method mSetMethod;
3171eb6908f2689bd17000237eb645896c36da0138George Mount
3271eb6908f2689bd17000237eb645896c36da0138George Mount    public BindingAdapterTestBase(Class<T> binderClass, Class<V> observableClass, int layoutId) {
334c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mount        super(binderClass);
3471eb6908f2689bd17000237eb645896c36da0138George Mount        mBindingObjectClass = observableClass;
3571eb6908f2689bd17000237eb645896c36da0138George Mount        try {
3671eb6908f2689bd17000237eb645896c36da0138George Mount            mSetMethod = binderClass.getDeclaredMethod("setObj", observableClass);
3771eb6908f2689bd17000237eb645896c36da0138George Mount        } catch (NoSuchMethodException e) {
3871eb6908f2689bd17000237eb645896c36da0138George Mount            throw new RuntimeException(e);
3971eb6908f2689bd17000237eb645896c36da0138George Mount        }
4071eb6908f2689bd17000237eb645896c36da0138George Mount    }
4171eb6908f2689bd17000237eb645896c36da0138George Mount
4271eb6908f2689bd17000237eb645896c36da0138George Mount    @Override
4371eb6908f2689bd17000237eb645896c36da0138George Mount    protected void setUp() throws Exception {
4471eb6908f2689bd17000237eb645896c36da0138George Mount        super.setUp();
453f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar        initBinder(new Runnable() {
463f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar            @Override
473f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar            public void run() {
483f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                try {
493f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    mBindingObject = mBindingObjectClass.newInstance();
503f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    mSetMethod.invoke(getBinder(), mBindingObject);
513f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    getBinder().executePendingBindings();
523f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                } catch (IllegalAccessException e) {
533f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    throw new RuntimeException(e);
543f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                } catch (InvocationTargetException e) {
553f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    throw new RuntimeException(e);
563f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                } catch (InstantiationException e) {
573f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                    throw new RuntimeException(e);
5871eb6908f2689bd17000237eb645896c36da0138George Mount                }
593f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar
603f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar            }
613f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar        });
6271eb6908f2689bd17000237eb645896c36da0138George Mount    }
6371eb6908f2689bd17000237eb645896c36da0138George Mount
6471eb6908f2689bd17000237eb645896c36da0138George Mount    protected void changeValues() throws Throwable {
6571eb6908f2689bd17000237eb645896c36da0138George Mount        runTestOnUiThread(new Runnable() {
6671eb6908f2689bd17000237eb645896c36da0138George Mount            @Override
6771eb6908f2689bd17000237eb645896c36da0138George Mount            public void run() {
6871eb6908f2689bd17000237eb645896c36da0138George Mount                mBindingObject.changeValues();
693f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                getBinder().executePendingBindings();
7071eb6908f2689bd17000237eb645896c36da0138George Mount            }
7171eb6908f2689bd17000237eb645896c36da0138George Mount        });
7271eb6908f2689bd17000237eb645896c36da0138George Mount    }
7371eb6908f2689bd17000237eb645896c36da0138George Mount}
74