xref: /aosp_15_r20/cts/tests/tests/binder_ndk/libbinder_ndk_test/test_ibinder_jni.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "Cts-NdkBinderTest"
17 
18 #include <android/binder_ibinder.h>
19 #include <android/binder_ibinder_jni.h>
20 #include <gtest/gtest.h>
21 #include <nativetesthelper_jni/utils.h>
22 
23 #include "utilities.h"
24 
NothingClass_onCreate(void * args)25 void* NothingClass_onCreate(void* args) { return args; }
NothingClass_onDestroy(void *)26 void NothingClass_onDestroy(void* /*userData*/) {}
NothingClass_onTransact(AIBinder *,transaction_code_t,const AParcel *,AParcel *)27 binder_status_t NothingClass_onTransact(AIBinder*, transaction_code_t,
28                                         const AParcel*, AParcel*) {
29   return STATUS_UNKNOWN_ERROR;
30 }
31 
32 static AIBinder_Class* kNothingClass =
33     AIBinder_Class_define("nothing", NothingClass_onCreate,
34                           NothingClass_onDestroy, NothingClass_onTransact);
35 
36 class NdkBinderTest_AIBinder_Jni : public NdkBinderTest {};
37 
TEST_F(NdkBinderTest_AIBinder_Jni,ConvertJni)38 TEST_F(NdkBinderTest_AIBinder_Jni, ConvertJni) {
39   JNIEnv* env = GetEnv();
40   ASSERT_NE(nullptr, env);
41 
42   AIBinder* binder = AIBinder_new(kNothingClass, nullptr);
43   EXPECT_NE(nullptr, binder);
44 
45   jobject object = AIBinder_toJavaBinder(env, binder);
46   EXPECT_NE(nullptr, object);
47 
48   AIBinder* fromJavaBinder = AIBinder_fromJavaBinder(env, object);
49   EXPECT_EQ(binder, fromJavaBinder);
50 
51   AIBinder_decStrong(binder);
52   AIBinder_decStrong(fromJavaBinder);
53 }
54