1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 #include "ffi_remote_data.h" 18 19 using namespace testing; 20 using namespace testing::ext; 21 using namespace OHOS::FFI; 22 23 namespace { 24 class FfiDataTest : public testing::Test { 25 public: RunLocalTest()26 static void RunLocalTest() 27 { 28 TestFfiManager(); 29 } 30 private: 31 static void TestFfiManager(); 32 }; 33 34 class RemoteDataSample : public OHOS::FFI::RemoteData { 35 DECL_TYPE(RemoteDataSample, OHOS::FFI::RemoteData) 36 public: RemoteDataSample(int id)37 RemoteDataSample(int id): RemoteData(id) { 38 num = id; 39 } 40 int num = 0; 41 }; 42 43 class FfiContainRemoteData : public OHOS::FFI::FFIData { 44 DECL_TYPE(FfiContainRemoteData, OHOS::FFI::FFIData) 45 public: 46 FfiContainRemoteData() = default; 47 OHOS::sptr<RemoteDataSample> remoteData{}; 48 }; 49 TestFfiManager()50void FfiDataTest::TestFfiManager() 51 { 52 auto mgr = FFIDataManager::GetInstance(); 53 EXPECT_TRUE(mgr); 54 auto ffiData = FFIData::Create<FfiContainRemoteData>(); 55 EXPECT_TRUE(ffiData); 56 auto id = ffiData->GetID(); 57 int num = 100; 58 ffiData->remoteData = RemoteData::Create<RemoteDataSample>(num); 59 mgr->StoreFFIData(ffiData); 60 OHOS::sptr<FfiContainRemoteData> temp = FFIData::GetData<FfiContainRemoteData>(id); 61 EXPECT_TRUE(temp); 62 EXPECT_TRUE(temp->remoteData); 63 if (temp->remoteData->num == num) { 64 EXPECT_TRUE(true); 65 } else { 66 EXPECT_TRUE(false); 67 } 68 mgr->RemoveFFIData(id); 69 } 70 TEST_F(FfiDataTest,Types)71TEST_F(FfiDataTest, Types) 72 { 73 RunLocalTest(); 74 } 75 }