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 "SensorTest.h"
20 
21 namespace {
22 using android::SensorTest::SensorTest;
23 
24 #define RETURN_ON_EXCEPTION() do { if (env->ExceptionCheck()) { return;} } while(false)
25 
setUp(JNIEnv *,jclass)26 jlong setUp(JNIEnv*, jclass) {
27     SensorTest *test = new SensorTest();
28     if (test != nullptr) {
29         test->SetUp();
30     }
31     return reinterpret_cast<jlong>(test);
32 }
33 
tearDown(JNIEnv *,jclass,jlong instance)34 void tearDown(JNIEnv*, jclass, jlong instance) {
35     delete reinterpret_cast<SensorTest *>(instance);
36 }
37 
test(JNIEnv * env,jclass,jlong instance)38 void test(JNIEnv* env, jclass, jlong instance) {
39     SensorTest *test = reinterpret_cast<SensorTest *>(instance);
40     ASSERT_NOT_NULL(test);
41 
42     // test if SensorTest is intialized
43     ALOGI("testInitialized");
44     test->testInitialized(env);
45     RETURN_ON_EXCEPTION();
46 
47     // test if SensorTest is intialized
48     ALOGI("testInvalidParameter");
49     test->testInvalidParameter(env);
50     RETURN_ON_EXCEPTION();
51 
52     // test sensor direct report
53     std::vector<int32_t> sensorTypes ={ASENSOR_TYPE_ACCELEROMETER, ASENSOR_TYPE_GYROSCOPE};
54     std::vector<int32_t> rates = {
55         ASENSOR_DIRECT_RATE_NORMAL, ASENSOR_DIRECT_RATE_FAST, ASENSOR_DIRECT_RATE_VERY_FAST};
56     std::vector<int32_t> channelTypes =
57         {ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY, ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER};
58     for (auto s : sensorTypes) {
59         for (auto c : channelTypes) {
60             for (auto r : rates) {
61                 ALOGI("testDirectReport: sensorType = %d, channelType = %d, ratelevel = %d",
62                       s, c, r);
63                 test->testDirectReport(env, s, c, r);
64                 RETURN_ON_EXCEPTION();
65             }
66         }
67     }
68 }
69 
70 JNINativeMethod gMethods[] = {
71     {  "nativeSetUp", "()J",
72             (void *) setUp},
73     {  "nativeTearDown", "(J)V",
74             (void *) tearDown},
75     {  "nativeTest", "(J)V",
76             (void *) test},
77 };
78 } // unamed namespace
79 
register_android_hardware_cts_SensorNativeTest(JNIEnv * env)80 int register_android_hardware_cts_SensorNativeTest(JNIEnv* env) {
81     jclass clazz = env->FindClass("android/hardware/cts/SensorNativeTest");
82     return env->RegisterNatives(clazz, gMethods,
83             sizeof(gMethods) / sizeof(JNINativeMethod));
84 }
85