1 /* 2 * Copyright (C) 2016 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.berberis.jnitests; 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4; 20 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertTrue; 26 27 @RunWith(AndroidJUnit4.class) 28 public final class JniTests { 29 static { 30 System.loadLibrary("berberis_jni_tests"); 31 } 32 intFromJNI()33 static native int intFromJNI(); 34 35 @Test testReturnInt()36 public void testReturnInt() { 37 assertEquals(42, intFromJNI()); 38 } 39 isJNIOnLoadCalled()40 static native boolean isJNIOnLoadCalled(); 41 42 @Test testOnLoadCalled()43 public void testOnLoadCalled() { 44 assertTrue(isJNIOnLoadCalled()); 45 } 46 checkGetVersion()47 static native boolean checkGetVersion(); 48 49 @Test testGetVersion()50 public void testGetVersion() { 51 assertTrue(checkGetVersion()); 52 } 53 checkJavaVMCorrespondsToJNIEnv()54 static native boolean checkJavaVMCorrespondsToJNIEnv(); 55 56 @Test testJavaVMCorrespondsToJNIEnv()57 public void testJavaVMCorrespondsToJNIEnv() { 58 assertTrue(checkJavaVMCorrespondsToJNIEnv()); 59 } 60 callRegisterNatives()61 static native boolean callRegisterNatives(); add42(int x)62 static native int add42(int x); 63 64 @Test testRegisterNatives()65 public void testRegisterNatives() { 66 assertTrue(callRegisterNatives()); 67 assertEquals(84, add42(42)); 68 } 69 add(int x, int y)70 static int add(int x, int y) { 71 return x + y; 72 } 73 callAdd(int x, int y)74 static native int callAdd(int x, int y); 75 76 @Test testCallStaticIntMethod()77 public void testCallStaticIntMethod() { 78 assertEquals(84, callAdd(42, 42)); 79 } 80 callAddA(int x, int y)81 static native int callAddA(int x, int y); 82 83 @Test testCallStaticIntMethodA()84 public void testCallStaticIntMethodA() { 85 assertEquals(84, callAddA(42, 42)); 86 } 87 callIntFromJNI()88 static int callIntFromJNI() { 89 return intFromJNI(); 90 } 91 callCallIntFromJNI()92 static native int callCallIntFromJNI(); 93 94 @Test testCallNativeCallJavaCallNative()95 public void testCallNativeCallJavaCallNative() { 96 assertEquals(42, callCallIntFromJNI()); 97 } 98 } 99