summaryrefslogtreecommitdiffstats
path: root/tests/HierarchyViewerTest/src/com/android/test/hierarchyviewer/MainActivityTest.java
blob: ea3710da486e23d0cf3f8632244ccaf5f771c7e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.android.test.hierarchyviewer;

import android.test.ActivityInstrumentationTestCase2;
import android.view.View;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private MainActivity mActivity;
    private View mTextView;


    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mActivity = getActivity();
        mTextView = mActivity.findViewById(R.id.textView);
    }

    private byte[] encode(View view) throws ClassNotFoundException, NoSuchMethodException,
            IllegalAccessException, InstantiationException, InvocationTargetException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024);

        Object encoder = createEncoder(baos);
        invokeMethod(View.class, view, "encode", encoder);
        invokeMethod(encoder.getClass(), encoder, "endStream");

        return baos.toByteArray();
    }

    private Object invokeMethod(Class targetClass, Object target, String methodName, Object... params)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class[] paramClasses = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            paramClasses[i] = params[i].getClass();
        }
        Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
        method.setAccessible(true);
        return method.invoke(target, params);
    }

    private Object createEncoder(ByteArrayOutputStream baos) throws ClassNotFoundException,
            NoSuchMethodException, IllegalAccessException, InvocationTargetException,
            InstantiationException {
        Class clazz = Class.forName("android.view.ViewHierarchyEncoder");
        Constructor constructor = clazz.getConstructor(ByteArrayOutputStream.class);
        return constructor.newInstance(baos);
    }

    public void testTextView() throws Exception {
        byte[] data = encode(mTextView);
        assertNotNull(data);
        assertTrue(data.length > 0);

        ViewDumpParser parser = new ViewDumpParser();
        parser.parse(data);

        List<Map<Short, Object>> views = parser.getViews();
        Map<String, Short> propertyNameTable = parser.getIds();

        assertEquals(1, views.size());
        assertNotNull(propertyNameTable);

        Map<Short, Object> textViewProperties = views.get(0);
        assertEquals("android.widget.TextView",
                textViewProperties.get(propertyNameTable.get("meta:__name__")));

        assertEquals(mActivity.getString(R.string.test),
                textViewProperties.get(propertyNameTable.get("text:text")));
    }
}