ClassHasNativeVisitorTest.java revision 91512f579e7a1bf91d23254471ee47d3a88d342f
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tools.layoutlib.create;
18
19import static org.junit.Assert.*;
20
21import org.junit.Test;
22import org.objectweb.asm.ClassReader;
23
24import java.io.IOException;
25import java.util.ArrayList;
26
27
28/**
29 * Tests {@link ClassHasNativeVisitor}.
30 */
31public class ClassHasNativeVisitorTest {
32
33    @Test
34    public void testHasNative() throws IOException {
35        MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
36        ClassReader cr = new ClassReader(
37                "com.android.tools.layoutlib.create.ClassHasNativeVisitorTest$ClassWithNative");
38
39        cr.accept(cv, 0 /* flags */);
40        assertArrayEquals(new String[] { "native_method" }, cv.getMethodsFound());
41        assertTrue(cv.hasNativeMethods());
42    }
43
44    @Test
45    public void testHasNoNative() throws IOException {
46        MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
47        ClassReader cr = new ClassReader(
48                "com.android.tools.layoutlib.create.ClassHasNativeVisitorTest$ClassWithoutNative");
49
50        cr.accept(cv, 0 /* flags */);
51        assertArrayEquals(new String[0], cv.getMethodsFound());
52        assertFalse(cv.hasNativeMethods());
53    }
54
55    /**
56     * Overrides {@link ClassHasNativeVisitor} to collec the name of the native methods found.
57     */
58    private static class MockClassHasNativeVisitor extends ClassHasNativeVisitor {
59        private ArrayList<String> mMethodsFound = new ArrayList<String>();
60
61        public String[] getMethodsFound() {
62            return mMethodsFound.toArray(new String[mMethodsFound.size()]);
63        }
64
65        @Override
66        protected void setHasNativeMethods(boolean hasNativeMethods, String methodName) {
67            if (hasNativeMethods) {
68                mMethodsFound.add(methodName);
69            }
70            super.setHasNativeMethods(hasNativeMethods, methodName);
71        }
72    }
73
74    /**
75     * Dummy test class with a native method.
76     */
77    public static class ClassWithNative {
78        public ClassWithNative() {
79        }
80
81        public void callTheNativeMethod() {
82            native_method();
83        }
84
85        private native void native_method();
86    }
87
88    /**
89     * Dummy test class with no native method.
90     */
91    public static class ClassWithoutNative {
92        public ClassWithoutNative() {
93        }
94
95        public void someMethod() {
96        }
97    }
98}
99