BaseDataBinderTest.java revision 9bdb2415487832e88a05c7bd19391b05440b468e
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.ViewDataBinding;
17
18import android.content.Context;
19import android.content.pm.ActivityInfo;
20import android.os.Looper;
21import android.test.ActivityInstrumentationTestCase2;
22import android.util.Log;
23import android.view.LayoutInflater;
24
25import java.io.PrintWriter;
26import java.io.StringWriter;
27import java.lang.reflect.Method;
28
29public class BaseDataBinderTest<T extends ViewDataBinding>
30        extends ActivityInstrumentationTestCase2<TestActivity> {
31    protected Class<T> mBinderClass;
32    private int mOrientation;
33    protected T mBinder;
34
35    public BaseDataBinderTest(final Class<T> binderClass) {
36        this(binderClass, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
37    }
38
39    public BaseDataBinderTest(final Class<T> binderClass, final int orientation) {
40        super(TestActivity.class);
41        mBinderClass = binderClass;
42        mOrientation = orientation;
43    }
44
45    @Override
46    protected void setUp() throws Exception {
47        super.setUp();
48        getActivity().setRequestedOrientation(mOrientation);
49        createBinder();
50    }
51
52    public boolean isMainThread() {
53        return Looper.myLooper() == Looper.getMainLooper();
54    }
55
56    protected void createBinder() {
57        mBinder = null;
58        getActivity().runOnUiThread(new Runnable() {
59            @Override
60            public void run() {
61                Method method = null;
62                try {
63                    method = mBinderClass.getMethod("inflate", LayoutInflater.class);
64                    mBinder = (T) method.invoke(null, getActivity().getLayoutInflater());
65                    getActivity().setContentView(mBinder.getRoot());
66                } catch (Exception e) {
67                    StringWriter sw = new StringWriter();
68                    PrintWriter pw = new PrintWriter(sw);
69                    e.printStackTrace(pw);
70                    fail("Error creating binder: " + sw.toString());
71                }
72            }
73        });
74        if (!isMainThread()) {
75            getInstrumentation().waitForIdleSync();
76        }
77        assertNotNull(mBinder);
78    }
79
80    protected void assertMethod(Class<?> klass, String methodName) throws NoSuchMethodException {
81        assertEquals(klass, mBinder.getClass().getDeclaredMethod(methodName).getReturnType());
82    }
83
84    protected void assertField(Class<?> klass, String fieldName) throws NoSuchFieldException {
85        assertEquals(klass, mBinder.getClass().getDeclaredField(fieldName).getType());
86    }
87
88    protected void assertPublicField(Class<?> klass, String fieldName) throws NoSuchFieldException {
89        assertEquals(klass, mBinder.getClass().getField(fieldName).getType());
90    }
91
92    protected void assertNoField(String fieldName) {
93        Exception[] ex = new Exception[1];
94        try {
95            mBinder.getClass().getField(fieldName);
96        } catch (NoSuchFieldException e) {
97            ex[0] = e;
98        }
99        assertNotNull(ex[0]);
100    }
101}
102