1 /* 2 * Copyright (C) 2014 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 import java.lang.reflect.Method; 18 import java.lang.System; 19 20 // This is named Main as it is a copy of JniTest, so that we can re-use the native implementations 21 // from libarttest. 22 class Main { main(String[] args)23 public static void main(String[] args) { 24 testFindClassOnAttachedNativeThread(); 25 testFindFieldOnAttachedNativeThread(); 26 testCallStaticVoidMethodOnSubClass(); 27 testGetMirandaMethod(); 28 testZeroLengthByteBuffers(); 29 testByteMethod(); 30 testShortMethod(); 31 testBooleanMethod(); 32 testCharMethod(); 33 testEnvironment(); 34 } 35 testFindClassOnAttachedNativeThread()36 public static native void testFindClassOnAttachedNativeThread(); 37 38 public static boolean testFindFieldOnAttachedNativeThreadField; 39 testFindFieldOnAttachedNativeThread()40 public static void testFindFieldOnAttachedNativeThread() { 41 testFindFieldOnAttachedNativeThreadNative(); 42 if (!testFindFieldOnAttachedNativeThreadField) { 43 throw new AssertionError(); 44 } 45 } 46 testFindFieldOnAttachedNativeThreadNative()47 private static native void testFindFieldOnAttachedNativeThreadNative(); 48 testCallStaticVoidMethodOnSubClass()49 private static void testCallStaticVoidMethodOnSubClass() { 50 testCallStaticVoidMethodOnSubClassNative(); 51 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) { 52 throw new AssertionError(); 53 } 54 } 55 testCallStaticVoidMethodOnSubClassNative()56 private static native void testCallStaticVoidMethodOnSubClassNative(); 57 58 private static class testCallStaticVoidMethodOnSubClass_SuperClass { 59 private static boolean executed = false; execute()60 private static void execute() { 61 executed = true; 62 } 63 } 64 65 private static class testCallStaticVoidMethodOnSubClass_SubClass 66 extends testCallStaticVoidMethodOnSubClass_SuperClass { 67 } 68 testGetMirandaMethodNative()69 private static native Method testGetMirandaMethodNative(); 70 testGetMirandaMethod()71 private static void testGetMirandaMethod() { 72 Method m = testGetMirandaMethodNative(); 73 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) { 74 throw new AssertionError(); 75 } 76 } 77 testZeroLengthByteBuffers()78 private static native void testZeroLengthByteBuffers(); 79 80 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface { inAbstract()81 public boolean inAbstract() { 82 return true; 83 } 84 } 85 86 private static interface testGetMirandaMethod_MirandaInterface { inInterface()87 public boolean inInterface(); 88 } 89 90 // Test sign-extension for values < 32b 91 byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8, byte b9, byte b10)92 native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, 93 byte b8, byte b9, byte b10); 94 testByteMethod()95 public static void testByteMethod() { 96 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 }; 97 for (int i = 0; i < returns.length; i++) { 98 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6, 99 (byte)(-7), (byte)8, (byte)(-9), (byte)10); 100 if (returns[i] != result) { 101 System.out.println("Run " + i + " with " + returns[i] + " vs " + result); 102 throw new AssertionError(); 103 } 104 } 105 } 106 shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, short s8, short s9, short s10)107 native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, 108 short s8, short s9, short s10); 109 testShortMethod()110 private static void testShortMethod() { 111 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 }; 112 for (int i = 0; i < returns.length; i++) { 113 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6, 114 (short)(-7), (short)8, (short)(-9), (short)10); 115 if (returns[i] != result) { 116 System.out.println("Run " + i + " with " + returns[i] + " vs " + result); 117 throw new AssertionError(); 118 } 119 } 120 } 121 122 // Test zero-extension for values < 32b 123 booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, boolean b8, boolean b9, boolean b10)124 native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, 125 boolean b8, boolean b9, boolean b10); 126 testBooleanMethod()127 public static void testBooleanMethod() { 128 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) { 129 throw new AssertionError(); 130 } 131 132 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) { 133 throw new AssertionError(); 134 } 135 } 136 charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8, char c9, char c10)137 native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, 138 char c8, char c9, char c10); 139 testCharMethod()140 private static void testCharMethod() { 141 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000, 142 (char)34000 }; 143 for (int i = 0; i < returns.length; i++) { 144 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345, 145 (char)3456); 146 if (returns[i] != result) { 147 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result); 148 throw new AssertionError(); 149 } 150 } 151 } 152 testEnvironment()153 private static void testEnvironment() { 154 String osArch = System.getProperty("os.arch"); 155 if (!"os.arch".equals(osArch)) { 156 throw new AssertionError("unexpected value for os.arch: " + osArch); 157 } 158 // TODO: improve the build script to get these running as well. 159 // if (!"cpu_abi".equals(Build.CPU_ABI)) { 160 // throw new AssertionError("unexpected value for cpu_abi"); 161 // } 162 // if (!"cpu_abi2".equals(Build.CPU_ABI2)) { 163 // throw new AssertionError("unexpected value for cpu_abi2"); 164 // } 165 // String[] expectedSupportedAbis = {"supported1", "supported2", "supported3"}; 166 // if (Arrays.equals(expectedSupportedAbis, Build.SUPPORTED_ABIS)) { 167 // throw new AssertionError("unexpected value for supported_abis"); 168 // } 169 } 170 } 171 172 public class NativeBridgeMain { main(String[] args)173 static public void main(String[] args) throws Exception { 174 System.out.println("Ready for native bridge tests."); 175 176 System.loadLibrary("arttest"); 177 178 Main.main(null); 179 } 180 } 181