1 /* 2 * Copyright (C) 2019 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 17 package com.android.tools.layoutlib.create; 18 19 import static org.junit.Assert.*; 20 21 import org.junit.Test; 22 import org.objectweb.asm.ClassReader; 23 24 import java.io.IOException; 25 import java.util.ArrayList; 26 27 28 /** 29 * Tests {@link ClassHasNativeVisitor}. 30 */ 31 public class ClassHasNativeVisitorTest { 32 33 @Test testHasNative()34 public void testHasNative() throws IOException { 35 MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor(); 36 String className = 37 this.getClass().getCanonicalName() + "$" + ClassWithNative.class.getSimpleName(); 38 ClassReader cr = new ClassReader(className); 39 40 cr.accept(cv, 0 /* flags */); 41 assertArrayEquals(new String[] { "native_method" }, cv.getMethodsFound()); 42 assertTrue(cv.hasNativeMethods()); 43 } 44 45 @Test testHasNoNative()46 public void testHasNoNative() throws IOException { 47 MockClassHasNativeVisitor cv = new MockClassHasNativeVisitor(); 48 String className = 49 this.getClass().getCanonicalName() + "$" + ClassWithoutNative.class.getSimpleName(); 50 ClassReader cr = new ClassReader(className); 51 52 cr.accept(cv, 0 /* flags */); 53 assertArrayEquals(new String[0], cv.getMethodsFound()); 54 assertFalse(cv.hasNativeMethods()); 55 } 56 57 //------- 58 59 /** 60 * Overrides {@link ClassHasNativeVisitor} to collec the name of the native methods found. 61 */ 62 private static class MockClassHasNativeVisitor extends ClassHasNativeVisitor { 63 private ArrayList<String> mMethodsFound = new ArrayList<>(); 64 getMethodsFound()65 public String[] getMethodsFound() { 66 return mMethodsFound.toArray(new String[mMethodsFound.size()]); 67 } 68 69 @Override setHasNativeMethods(boolean hasNativeMethods, String methodName)70 protected void setHasNativeMethods(boolean hasNativeMethods, String methodName) { 71 if (hasNativeMethods) { 72 mMethodsFound.add(methodName); 73 } 74 super.setHasNativeMethods(hasNativeMethods, methodName); 75 } 76 } 77 78 /** 79 * Fake test class with a native method. 80 */ 81 public static class ClassWithNative { ClassWithNative()82 public ClassWithNative() { 83 } 84 callTheNativeMethod()85 public void callTheNativeMethod() { 86 native_method(); 87 } 88 native_method()89 private native void native_method(); 90 } 91 92 /** 93 * Fake test class with no native method. 94 */ 95 public static class ClassWithoutNative { ClassWithoutNative()96 public ClassWithoutNative() { 97 } 98 someMethod()99 public void someMethod() { 100 } 101 } 102 } 103