2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.tools.layoutlib.create;
19 import static org.junit.Assert.*;
21 import org.junit.Test;
22 import org.objectweb.asm.ClassReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
29 * Tests {@link ClassHasNativeVisitor}.
31 public class ClassHasNativeVisitorTest {
34 public void testHasNative() throws IOException {
35 MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
37 this.getClass().getCanonicalName() + "$" + ClassWithNative.class.getSimpleName();
38 ClassReader cr = new ClassReader(className);
40 cr.accept(cv, 0 /* flags */);
41 assertArrayEquals(new String[] { "native_method" }, cv.getMethodsFound());
42 assertTrue(cv.hasNativeMethods());
46 public void testHasNoNative() throws IOException {
47 MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor();
49 this.getClass().getCanonicalName() + "$" + ClassWithoutNative.class.getSimpleName();
50 ClassReader cr = new ClassReader(className);
52 cr.accept(cv, 0 /* flags */);
53 assertArrayEquals(new String[0], cv.getMethodsFound());
54 assertFalse(cv.hasNativeMethods());
60 * Overrides {@link ClassHasNativeVisitor} to collec the name of the native methods found.
62 private static class MockClassHasNativeVisitor extends ClassHasNativeVisitor {
63 private ArrayList<String> mMethodsFound = new ArrayList<String>();
65 public String[] getMethodsFound() {
66 return mMethodsFound.toArray(new String[mMethodsFound.size()]);
70 protected void setHasNativeMethods(boolean hasNativeMethods, String methodName) {
71 if (hasNativeMethods) {
72 mMethodsFound.add(methodName);
74 super.setHasNativeMethods(hasNativeMethods, methodName);
79 * Dummy test class with a native method.
81 public static class ClassWithNative {
82 public ClassWithNative() {
85 public void callTheNativeMethod() {
89 private native void native_method();
93 * Dummy test class with no native method.
95 public static class ClassWithoutNative {
96 public ClassWithoutNative() {
99 public void someMethod() {