1 /*
2  * Copyright (C) 2017 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 
18 #include "nativeTestHelper.h"
19 #include <cstdlib>
20 #include <cstring>
21 
22 extern int register_android_hardware_cts_SensorNativeTest(JNIEnv* env);
23 extern int register_android_hardware_cts_SensorDirectReportTest(JNIEnv* env);
24 
fail(JNIEnv * env,const char * format,...)25 void fail(JNIEnv* env, const char* format, ...) {
26     va_list args;
27 
28     va_start(args, format);
29     char *msg;
30     vasprintf(&msg, format, args);
31     va_end(args);
32 
33     jclass exClass;
34     const char *className = "java/lang/AssertionError";
35     exClass = env->FindClass(className);
36     jmethodID constructor = env->GetMethodID(exClass, "<init>",
37                                              "(Ljava/lang/String;Ljava/lang/Throwable;)V");
38     jstring msgStr = env->NewStringUTF(msg);
39     jobject exception = env->NewObject(exClass, constructor, msgStr, nullptr);
40     env->Throw(static_cast<jthrowable>(exception));
41     free(msg);
42 }
43 
JNI_OnLoad(JavaVM * vm,void *)44 jint JNI_OnLoad(JavaVM *vm, void *) {
45     JNIEnv *env = NULL;
46     if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
47         return JNI_ERR;
48     }
49     if (register_android_hardware_cts_SensorNativeTest(env)) {
50         return JNI_ERR;
51     }
52     if (register_android_hardware_cts_SensorDirectReportTest(env)) {
53         return JNI_ERR;
54     }
55     return JNI_VERSION_1_4;
56 }
57