Home | History | Annotate | Download | only in hierarchyviewer
      1 package com.android.test.hierarchyviewer;
      2 
      3 import android.test.ActivityInstrumentationTestCase2;
      4 import android.view.View;
      5 
      6 import java.io.ByteArrayOutputStream;
      7 import java.lang.reflect.Constructor;
      8 import java.lang.reflect.InvocationTargetException;
      9 import java.lang.reflect.Method;
     10 import java.util.List;
     11 import java.util.Map;
     12 
     13 public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
     14     private MainActivity mActivity;
     15     private View mTextView;
     16 
     17 
     18     public MainActivityTest() {
     19         super(MainActivity.class);
     20     }
     21 
     22     @Override
     23     protected void setUp() throws Exception {
     24         super.setUp();
     25 
     26         mActivity = getActivity();
     27         mTextView = mActivity.findViewById(R.id.textView);
     28     }
     29 
     30     private byte[] encode(View view) throws ClassNotFoundException, NoSuchMethodException,
     31             IllegalAccessException, InstantiationException, InvocationTargetException {
     32         ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024);
     33 
     34         Object encoder = createEncoder(baos);
     35         invokeMethod(View.class, view, "encode", encoder);
     36         invokeMethod(encoder.getClass(), encoder, "endStream");
     37 
     38         return baos.toByteArray();
     39     }
     40 
     41     private Object invokeMethod(Class targetClass, Object target, String methodName, Object... params)
     42             throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
     43         Class[] paramClasses = new Class[params.length];
     44         for (int i = 0; i < params.length; i++) {
     45             paramClasses[i] = params[i].getClass();
     46         }
     47         Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
     48         method.setAccessible(true);
     49         return method.invoke(target, params);
     50     }
     51 
     52     private Object createEncoder(ByteArrayOutputStream baos) throws ClassNotFoundException,
     53             NoSuchMethodException, IllegalAccessException, InvocationTargetException,
     54             InstantiationException {
     55         Class clazz = Class.forName("android.view.ViewHierarchyEncoder");
     56         Constructor constructor = clazz.getConstructor(ByteArrayOutputStream.class);
     57         return constructor.newInstance(baos);
     58     }
     59 
     60     public void testTextView() throws Exception {
     61         byte[] data = encode(mTextView);
     62         assertNotNull(data);
     63         assertTrue(data.length > 0);
     64 
     65         ViewDumpParser parser = new ViewDumpParser();
     66         parser.parse(data);
     67 
     68         List<Map<Short, Object>> views = parser.getViews();
     69         Map<String, Short> propertyNameTable = parser.getIds();
     70 
     71         assertEquals(1, views.size());
     72         assertNotNull(propertyNameTable);
     73 
     74         Map<Short, Object> textViewProperties = views.get(0);
     75         assertEquals("android.widget.TextView",
     76                 textViewProperties.get(propertyNameTable.get("meta:__name__")));
     77 
     78         assertEquals(mActivity.getString(R.string.test),
     79                 textViewProperties.get(propertyNameTable.get("text:text")));
     80     }
     81 }
     82