Fix JNI Loading for BuildTest

Bug 2814482

BuildTest uses native code to check the CPU's features. When using
runtest.py, the library is put into /system/lib, so the library is
loaded without problems. However, running the test in CTS causes
the library not to be found, because it is not installed into the
system directory anymore. The CTS runner's classloader is used for
tests, so the native code must be compiled into the runner's jar
in order for it to be found.

Change-Id: I1922d3d2e95753fb0e1dad741178e514695dc1e3
diff --git a/tests/Android.mk b/tests/Android.mk
index 64517af..838d01d 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -24,6 +24,8 @@
 
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
+LOCAL_JNI_SHARED_LIBRARIES := libcts_jni
+
 # Resource unit tests use a private locale and some densities
 LOCAL_AAPT_FLAGS = -c xx_YY -c cs -c 32dpi -c 240dpi -c 160dpi
 
diff --git a/tests/tests/os/libbuildtest/Android.mk b/tests/jni/Android.mk
similarity index 79%
rename from tests/tests/os/libbuildtest/Android.mk
rename to tests/jni/Android.mk
index a155e50..d58416f 100644
--- a/tests/tests/os/libbuildtest/Android.mk
+++ b/tests/jni/Android.mk
@@ -16,7 +16,7 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE := libbuildtest
+LOCAL_MODULE := libcts_jni
 
 # Don't include this package in any configuration by default.
 LOCAL_MODULE_TAGS := optional
@@ -24,11 +24,19 @@
 # This isn't part of the system, so don't prelink it.
 LOCAL_PRELINK_MODULE := false
 
-LOCAL_SRC_FILES := android_os_cts_BuildTest.c
+LOCAL_SRC_FILES := \
+		CtsJniOnLoad.cpp
 
 LOCAL_C_INCLUDES := $(JNI_H_INCLUDE) 
-LOCAL_C_INCLUDES += ndk/sources/cpufeatures
 
+LOCAL_SHARED_LIBRARIES := libnativehelper liblog
+
+ifneq ($(TARGET_SIMULATOR),true)
+LOCAL_SRC_FILES += android_os_cts_CpuFeatures.cpp
+LOCAL_C_INCLUDES += ndk/sources/cpufeatures
 LOCAL_STATIC_LIBRARIES := cpufeatures
+else
+LOCAL_CFLAGS += -DCTS_TARGET_SIMULATOR
+endif
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/jni/CtsJniOnLoad.cpp b/tests/jni/CtsJniOnLoad.cpp
new file mode 100644
index 0000000..b6177a1
--- /dev/null
+++ b/tests/jni/CtsJniOnLoad.cpp
@@ -0,0 +1,38 @@
+/* 
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <jni.h>
+#include <stdio.h>
+
+#ifndef CTS_TARGET_SIMULATOR
+extern int register_android_os_cts_CpuFeatures(JNIEnv*);
+#endif
+
+jint JNI_OnLoad(JavaVM *vm, void *reserved) {
+    JNIEnv *env = NULL;
+
+    if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
+        return JNI_ERR;
+    }
+
+#ifndef CTS_TARGET_SIMULATOR
+    if (register_android_os_cts_CpuFeatures(env)) {
+        return JNI_ERR;
+    }
+#endif
+
+    return JNI_VERSION_1_4;
+}
diff --git a/tests/tests/os/libbuildtest/android_os_cts_BuildTest.c b/tests/jni/android_os_cts_CpuFeatures.cpp
similarity index 60%
rename from tests/tests/os/libbuildtest/android_os_cts_BuildTest.c
rename to tests/jni/android_os_cts_CpuFeatures.cpp
index 51bd991..95a0d2f 100644
--- a/tests/tests/os/libbuildtest/android_os_cts_BuildTest.c
+++ b/tests/jni/android_os_cts_CpuFeatures.cpp
@@ -18,14 +18,29 @@
 #include <jni.h>
 #include <string.h>
 
-jboolean Java_android_os_cts_BuildTest_isArmCpu(JNIEnv* env, jobject thiz)
+jboolean android_os_cts_CpuFeatures_isArmCpu(JNIEnv* env, jobject thiz)
 {
     AndroidCpuFamily cpuFamily = android_getCpuFamily();
     return cpuFamily == ANDROID_CPU_FAMILY_ARM;
 }
 
-jboolean Java_android_os_cts_BuildTest_isArm7Compatible(JNIEnv* env, jobject thiz)
+jboolean android_os_cts_CpuFeatures_isArm7Compatible(JNIEnv* env, jobject thiz)
 {
     uint64_t cpuFeatures = android_getCpuFeatures();
     return (cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7) == ANDROID_CPU_ARM_FEATURE_ARMv7;
 }
+
+static JNINativeMethod gMethods[] = {
+    {  "isArmCpu", "()Z",
+            (void *) android_os_cts_CpuFeatures_isArmCpu  },
+    {  "isArm7Compatible", "()Z",
+            (void *) android_os_cts_CpuFeatures_isArm7Compatible  },
+};
+
+int register_android_os_cts_CpuFeatures(JNIEnv* env)
+{
+    jclass clazz = env->FindClass("android/os/cts/CpuFeatures");
+
+    return env->RegisterNatives(clazz, gMethods,
+            sizeof(gMethods) / sizeof(JNINativeMethod));
+}
diff --git a/tests/src/android/os/cts/CpuFeatures.java b/tests/src/android/os/cts/CpuFeatures.java
new file mode 100644
index 0000000..f87db6a
--- /dev/null
+++ b/tests/src/android/os/cts/CpuFeatures.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os.cts;
+
+public class CpuFeatures {
+
+    public static final String ARMEABI_V7 = "armeabi-v7a";
+
+    public static final String ARMEABI = "armeabi";
+
+    static {
+        System.loadLibrary("cts_jni");
+    }
+
+    public static native boolean isArmCpu();
+
+    public static native boolean isArm7Compatible();
+}
diff --git a/tests/tests/os/Android.mk b/tests/tests/os/Android.mk
index 83f5e24..affd97d 100644
--- a/tests/tests/os/Android.mk
+++ b/tests/tests/os/Android.mk
@@ -24,7 +24,6 @@
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
 ifneq ($(TARGET_SIMULATOR),true)
-LOCAL_JNI_SHARED_LIBRARIES := libbuildtest
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 else
 LOCAL_SRC_FILES := $(filter-out %BuildTest.java,$(call all-java-files-under, src))
@@ -38,8 +37,3 @@
 #LOCAL_SDK_VERSION := current
 
 include $(BUILD_PACKAGE)
-
-ifneq ($(TARGET_SIMULATOR),true)
-# Include the associated library's makefile.
-include $(LOCAL_PATH)/libbuildtest/Android.mk
-endif
diff --git a/tests/tests/os/src/android/os/cts/BuildTest.java b/tests/tests/os/src/android/os/cts/BuildTest.java
index d9e16fb..8044752 100644
--- a/tests/tests/os/src/android/os/cts/BuildTest.java
+++ b/tests/tests/os/src/android/os/cts/BuildTest.java
@@ -25,37 +25,25 @@
 @TestTargetClass(Build.class)
 public class BuildTest extends TestCase {
 
-    private static final String ARMEABI_V7 = "armeabi-v7a";
-
-    private static final String ARMEABI = "armeabi";
-
-    static {
-        System.loadLibrary("buildtest");
-    }
-
     /** Tests that check the values of {@link Build#CPU_ABI} and {@link Build#CPU_ABI2}. */
     public void testCpuAbi() {
-        if (isArmCpu()) {
+        if (CpuFeatures.isArmCpu()) {
             assertArmCpuAbiConstants();
         }
     }
 
-    private native boolean isArmCpu();
-
     private void assertArmCpuAbiConstants() {
-        boolean isArm7Compatible = isArm7Compatible();
-        if (isArm7Compatible) {
+        if (CpuFeatures.isArm7Compatible()) {
             String message = String.format("CPU is ARM v7 compatible, so Build.CPU_ABI must be %s"
-                    + " and Build.CPU_ABI2 must be %s.", ARMEABI_V7, ARMEABI);
-            assertEquals(message, ARMEABI_V7, Build.CPU_ABI);
-            assertEquals(message, ARMEABI, Build.CPU_ABI2);
+                    + " and Build.CPU_ABI2 must be %s.", CpuFeatures.ARMEABI_V7,
+                            CpuFeatures.ARMEABI);
+            assertEquals(message, CpuFeatures.ARMEABI_V7, Build.CPU_ABI);
+            assertEquals(message, CpuFeatures.ARMEABI, Build.CPU_ABI2);
         } else {
             String message = String.format("CPU is not ARM v7 compatible, so Build.CPU_ABI must "
-                    + "be %s and Build.CPU_ABI2 must be 'unknown'.", ARMEABI);
-            assertEquals(message, ARMEABI, Build.CPU_ABI);
+                    + "be %s and Build.CPU_ABI2 must be 'unknown'.", CpuFeatures.ARMEABI);
+            assertEquals(message, CpuFeatures.ARMEABI, Build.CPU_ABI);
             assertEquals(message, "unknown", Build.CPU_ABI2);
         }
     }
-
-    private native boolean isArm7Compatible();
 }