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