From e50ee0e27db1ab9fc4e1bc3225e538bd68bb8c2d Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 22 Apr 2025 19:37:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0n=5Fasync=5Fwork=5Fpromise?= =?UTF-8?q?=E5=92=8Cn=5Fref=E7=9A=84TDD=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../test/unittest/filemgmt_libn_test/BUILD.gn | 20 +- .../filemgmt_libn_test/include/napi_mock.h | 137 ++++++ .../src/n_async/n_async_work_promise_test.cpp | 224 +++++++++ .../src/n_async/n_ref_test.cpp | 433 ++++++++++++++++++ .../filemgmt_libn_test/src/napi_mock.cpp | 128 ++++++ 5 files changed, 939 insertions(+), 3 deletions(-) create mode 100644 interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h create mode 100644 interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_async_work_promise_test.cpp create mode 100644 interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_ref_test.cpp create mode 100644 interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp diff --git a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn index ea20a1cf9..8b51a7d16 100644 --- a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn +++ b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2023 Huawei Device Co., Ltd. +# Copyright (C) 2023-2025 Huawei Device Co., Ltd. # 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 @@ -20,15 +20,22 @@ group("unittest") { } ohos_unittest("filemgmt_libn_test") { + testonly = true + module_out_path = "file_api/file_api" include_dirs = [ "./include" ] - sources = [ "./src/n_class_test.cpp" ] + sources = [ + "./src/n_class_test.cpp", + "./src/napi_mock.cpp", + "./src/n_async/n_async_work_promise_test.cpp", + "./src/n_async/n_ref_test.cpp", + ] deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", - "//third_party/googletest:gtest_main", + "//third_party/googletest:gmock_main", ] external_deps = [ @@ -39,4 +46,11 @@ ohos_unittest("filemgmt_libn_test") { "ipc:ipc_core", "napi:ace_napi", ] + + defines = [ + "ENABLE_NAPI_MOCK", + "private=public", + ] + + cflags_cc = [ "-DENABLE_NAPI_MOCK" ] } diff --git a/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h new file mode 100644 index 000000000..653db4c93 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H +#define INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H + +#pragma once +#include +#include "n_napi.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { + +class INapiMock { +public: + virtual ~INapiMock() = default; + + virtual napi_status napi_define_class(napi_env env, const char *name, size_t length, napi_callback constructor, + void *data, size_t property_count, const napi_property_descriptor *properties, napi_value *result) = 0; + + virtual napi_status napi_create_reference( + napi_env env, napi_value value, uint32_t initial_refcount, napi_ref *result) = 0; + + virtual napi_status napi_add_env_cleanup_hook(napi_env env, void (*fun)(void *arg), void *arg) = 0; + + virtual napi_status napi_delete_reference(napi_env env, napi_ref ref) = 0; + + virtual napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) = 0; + + virtual napi_status napi_new_instance( + napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result) = 0; + + virtual napi_status napi_unwrap(napi_env env, napi_value js_object, void **result) = 0; + + virtual napi_status napi_wrap(napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, + void *finalize_hint, napi_ref *result) = 0; + + virtual napi_status napi_remove_wrap(napi_env env, napi_value js_object, void **result) = 0; + + virtual napi_status napi_create_promise(napi_env env, napi_deferred *deferred, napi_value *promise) = 0; + + virtual napi_status napi_create_async_work(napi_env env, napi_value async_resource, napi_value async_resource_name, + napi_async_execute_callback execute, napi_async_complete_callback complete, void *data, + napi_async_work *result) = 0; + + virtual napi_status napi_queue_async_work(napi_env env, napi_async_work work) = 0; + + virtual napi_status napi_create_string_utf8(napi_env env, const char *str, size_t length, napi_value *result) = 0; +}; + +class NapiMock : public INapiMock { +public: + MOCK_METHOD(napi_status, napi_define_class, + (napi_env, const char *, size_t, napi_callback, void *, size_t, const napi_property_descriptor *, napi_value *), + (override)); + + MOCK_METHOD(napi_status, napi_create_reference, (napi_env, napi_value, uint32_t, napi_ref *), (override)); + + MOCK_METHOD(napi_status, napi_add_env_cleanup_hook, (napi_env env, void (*fun)(void *arg), void *arg), (override)); + + MOCK_METHOD(napi_status, napi_delete_reference, (napi_env env, napi_ref ref), (override)); + + MOCK_METHOD(napi_status, napi_get_reference_value, (napi_env env, napi_ref ref, napi_value *result), (override)); + + MOCK_METHOD(napi_status, napi_new_instance, + (napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result), (override)); + + MOCK_METHOD(napi_status, napi_unwrap, (napi_env env, napi_value js_object, void **result), (override)); + + MOCK_METHOD(napi_status, napi_wrap, + (napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, void *finalize_hint, + napi_ref *result), + (override)); + + MOCK_METHOD(napi_status, napi_remove_wrap, (napi_env env, napi_value js_object, void **result), (override)); + + MOCK_METHOD( + napi_status, napi_create_promise, (napi_env env, napi_deferred *deferred, napi_value *promise), (override)); + + MOCK_METHOD(napi_status, napi_create_async_work, + (napi_env env, napi_value async_resource, napi_value async_resource_name, napi_async_execute_callback execute, + napi_async_complete_callback complete, void *data, napi_async_work *result), + (override)); + + MOCK_METHOD(napi_status, napi_queue_async_work, (napi_env env, napi_async_work work), (override)); + + MOCK_METHOD(napi_status, napi_create_string_utf8, + (napi_env env, const char *str, size_t length, napi_value *result), (override)); +}; + +NapiMock &GetNapiMock(); +void SetNapiMock(NapiMock *mock); +void ResetNapiMock(); + +class ScopedNapiMock { +public: + explicit ScopedNapiMock(NapiMock *mock) : mock_(mock) + { + SetNapiMock(mock_); + } + + ~ScopedNapiMock() + { + ResetNapiMock(); + } + + NapiMock &GetMock() + { + return *mock_; + } + + ScopedNapiMock(const ScopedNapiMock &) = delete; + ScopedNapiMock &operator=(const ScopedNapiMock &) = delete; + +private: + NapiMock *mock_; +}; + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_async_work_promise_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_async_work_promise_test.cpp new file mode 100644 index 000000000..360f9ccdb --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_async_work_promise_test.cpp @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * 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 +#include + +#include +#include + +#include "n_async_work_promise.h" +#include "napi_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { +using namespace std; + +class NAsyncWorkPromiseTest : public testing::Test { +public: + static void SetUpTestSuite(void) {} + static void TearDownTestSuite(void) {} + + void SetUp() override + { + mock_ = std::make_unique(); + scopedMock_ = std::make_unique(mock_.get()); + } + + void TearDown() override + { + scopedMock_.reset(); + mock_.reset(); + } + + NapiMock &GetMock() + { + return *mock_; + } + +private: + std::unique_ptr mock_; + std::unique_ptr scopedMock_; +}; + +/** + * @tc.name: NAsyncWorkPromiseTest_Schedule_001 + * @tc.desc: Test function of NAsyncWorkPromise::Schedule interface for FAILURE when napi_create_promise fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NAsyncWorkPromiseTest, NAsyncWorkPromiseTest_Schedule_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-begin NAsyncWorkPromiseTest_Schedule_001"; + string procedureName = "NAsyncWorkPromiseTest_Schedule_001"; + NContextCBExec cbExec; + NContextCBComplete cbComplete; + napi_env env = reinterpret_cast(0x1000); + NVal nval = NVal(); + + EXPECT_CALL(GetMock(), napi_create_promise(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + NAsyncWorkPromise nAsyncWorkPromise(env, nval); + auto res = nAsyncWorkPromise.Schedule(procedureName, cbExec, cbComplete); + + EXPECT_EQ(res.val_, nullptr); + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-end NAsyncWorkPromiseTest_Schedule_001"; +} + +/** + * @tc.name: NAsyncWorkPromiseTest_Schedule_002 + * @tc.desc: Test function of NAsyncWorkPromise::Schedule interface for FAILURE when napi_create_async_work fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NAsyncWorkPromiseTest, NAsyncWorkPromiseTest_Schedule_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-begin NAsyncWorkPromiseTest_Schedule_002"; + string procedureName = "NAsyncWorkPromiseTest_Schedule_002"; + NContextCBExec cbExec; + NContextCBComplete cbComplete; + napi_env env = reinterpret_cast(0x1000); + NVal nval = NVal(); + + napi_value expectedDeferred = reinterpret_cast(0x3456); + EXPECT_CALL(GetMock(), napi_create_promise(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(expectedDeferred), testing::Return(napi_ok))); + + napi_value mockResult = reinterpret_cast(0x5678); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockResult), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), + napi_create_async_work(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + NAsyncWorkPromise nAsyncWorkPromise(env, nval); + auto res = nAsyncWorkPromise.Schedule(procedureName, cbExec, cbComplete); + + EXPECT_EQ(res.val_, nullptr); + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-end NAsyncWorkPromiseTest_Schedule_002"; +} + +/** + * @tc.name: NAsyncWorkPromiseTest_Schedule_003 + * @tc.desc: Test function of NAsyncWorkPromise::Schedule interface for FAILURE when napi_queue_async_work fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NAsyncWorkPromiseTest, NAsyncWorkPromiseTest_Schedule_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-begin NAsyncWorkPromiseTest_Schedule_003"; + string procedureName = "NAsyncWorkPromiseTest_Schedule_003"; + NContextCBExec cbExec; + NContextCBComplete cbComplete; + napi_env env = reinterpret_cast(0x1000); + NVal nval = NVal(); + + napi_value expectedDeferred = reinterpret_cast(0x3456); + EXPECT_CALL(GetMock(), napi_create_promise(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(expectedDeferred), testing::Return(napi_ok))); + + napi_value mockResult = reinterpret_cast(0x5678); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockResult), testing::Return(napi_ok))); + + napi_async_work expectedAsyncWork = reinterpret_cast(0x4567); + EXPECT_CALL(GetMock(), + napi_create_async_work(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<6>(expectedAsyncWork), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_queue_async_work(testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + NAsyncWorkPromise nAsyncWorkPromise(env, nval); + auto res = nAsyncWorkPromise.Schedule(procedureName, cbExec, cbComplete); + + EXPECT_EQ(res.val_, nullptr); + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-end NAsyncWorkPromiseTest_Schedule_003"; +} + +/** + * @tc.name: NAsyncWorkPromiseTest_Schedule_004 + * @tc.desc: Test function of NAsyncWorkPromise::Schedule interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NAsyncWorkPromiseTest, NAsyncWorkPromiseTest_Schedule_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-begin NAsyncWorkPromiseTest_Schedule_004"; + string procedureName = "NAsyncWorkPromiseTest_Schedule_004"; + NContextCBExec cbExec; + NContextCBComplete cbComplete; + napi_env env = reinterpret_cast(0x1000); + NVal nval = NVal(); + + napi_value expectedDeferred = reinterpret_cast(0x3456); + EXPECT_CALL(GetMock(), napi_create_promise(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(expectedDeferred), testing::Return(napi_ok))); + + napi_value mockResult = reinterpret_cast(0x5678); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockResult), testing::Return(napi_ok))); + + napi_async_work expectedAsyncWork = reinterpret_cast(0x4567); + EXPECT_CALL(GetMock(), + napi_create_async_work(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<6>(expectedAsyncWork), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_queue_async_work(testing::_, testing::_)).WillOnce(testing::Return(napi_ok)); + + NAsyncWorkPromise nAsyncWorkPromise(env, nval); + auto res = nAsyncWorkPromise.Schedule(procedureName, cbExec, cbComplete); + + EXPECT_EQ(res.val_, expectedDeferred); + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-end NAsyncWorkPromiseTest_Schedule_004"; +} + +/** + * @tc.name: NAsyncWorkPromiseTest_Schedule_005 + * @tc.desc: Test function of NAsyncWorkPromise::Schedule interface for FAILURE when ctx_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NAsyncWorkPromiseTest, NAsyncWorkPromiseTest_Schedule_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-begin NAsyncWorkPromiseTest_Schedule_005"; + string procedureName = "NAsyncWorkPromiseTest_Schedule_005"; + NContextCBExec cbExec; + NContextCBComplete cbComplete; + napi_env env = reinterpret_cast(0x1000); + NVal nval = NVal(); + + NAsyncWorkPromise nAsyncWorkPromise(env, nval); + nAsyncWorkPromise.ctx_ = nullptr; + + auto res = nAsyncWorkPromise.Schedule(procedureName, cbExec, cbComplete); + + EXPECT_EQ(res.val_, nullptr); + GTEST_LOG_(INFO) << "NAsyncWorkPromiseTest-end NAsyncWorkPromiseTest_Schedule_005"; +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_ref_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_ref_test.cpp new file mode 100644 index 000000000..e8b3e2395 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_async/n_ref_test.cpp @@ -0,0 +1,433 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * 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 +#include + +#include +#include + +#include "n_async_work_promise.h" +#include "napi_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { +using namespace std; + +class NRefTest : public testing::Test { +public: + static void SetUpTestSuite(void) {} + static void TearDownTestSuite(void) {} + + void SetUp() override + { + mock_ = std::make_unique(); + scopedMock_ = std::make_unique(mock_.get()); + } + + void TearDown() override + { + scopedMock_.reset(); + mock_.reset(); + } + + NapiMock &GetMock() + { + return *mock_; + } + +private: + std::unique_ptr mock_; + std::unique_ptr scopedMock_; +}; + +/** + * @tc.name: NRefTest_NRef_001 + * @tc.desc: Test function of NRef::NRef interface for SUCCESS NRef with valid NVal. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value value = reinterpret_cast(0x2000); + NVal nval(env, value); + + // Set mock behaviors + napi_ref mockReference = reinterpret_cast(0x2345); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockReference), testing::Return(napi_ok))); + + NRef nref(nval); + + EXPECT_TRUE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_001"; +} + +/** + * @tc.name: NRefTest_NRef_002 + * @tc.desc: Test function of NRef::NRef interface for FAILURE NRef with invalid NVal. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_002"; + NVal nval; + + NRef nref(nval); + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_002"; +} + +/** + * @tc.name: NRefTest_NRef_003 + * @tc.desc: Test function of NRef::~NRef interface for FAILURE ~NRef with invalid NVal. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_003"; + napi_env env = reinterpret_cast(0x1000); + napi_value value = reinterpret_cast(0x2000); + NVal nval(env, value); + + napi_ref expectedRef = reinterpret_cast(0x3000); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedRef), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_delete_reference(env, expectedRef)).WillOnce(testing::Return(napi_ok)); + + { + NRef nref(nval); + } + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_003"; +} + +/** + * @tc.name: NRefTest_NRef_004 + * @tc.desc: Test function of NRef::~NRef interface for FAILURE ~NRef when ref_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_004"; + napi_env env = reinterpret_cast(0x1000); + NVal nval; + nval.env_ = env; + + NRef nref(nval); + nref.ref_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.~NRef(); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_004"; +} + +/** + * @tc.name: NRefTest_NRef_005 + * @tc.desc: Test function of NRef::~NRef interface for FAILURE ~NRef when env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_005"; + napi_value value = reinterpret_cast(0x2000); + NVal nval; + nval.val_ = value; + + NRef nref(nval); + nref.env_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.~NRef(); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_005"; +} + +/** + * @tc.name: NRefTest_NRef_006 + * @tc.desc: Test function of NRef::~NRef interface for FAILURE ~NRef when env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_NRef_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_NRef_006"; + NVal nval; + NRef nref(nval); + nref.ref_ = nullptr; + nref.env_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.~NRef(); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_NRef_006"; +} + +/** + * @tc.name: NRefTest_operator_bool_001 + * @tc.desc: Test function of NRef::operator bool interface for SUCCESS operator bool when ref_ is not nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_operator_bool_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_operator_bool_001"; + napi_env env = reinterpret_cast(0x1000); + napi_value value = reinterpret_cast(0x2000); + NVal nval(env, value); + + napi_ref expectedRef = reinterpret_cast(0x3000); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedRef), testing::Return(napi_ok))); + + NRef nref(nval); + + EXPECT_TRUE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_operator_bool_001"; +} + +/** + * @tc.name: NRefTest_operator_bool_002 + * @tc.desc: Test function of NRef::operator bool interface for FAILURE operator bool when ref_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_operator_bool_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_operator_bool_002"; + NRef nref; + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_operator_bool_002"; +} + +/** + * @tc.name: NRefTest_Deref_001 + * @tc.desc: Test function of NRef::Deref interface for SUCCESS Deref with valid reference and env. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_Deref_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_Deref_001"; + napi_env env = reinterpret_cast(0x1000); + napi_value value = reinterpret_cast(0x2000); + NVal nval(env, value); + + napi_ref expectedRef = reinterpret_cast(0x3000); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedRef), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_get_reference_value(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(value), testing::Return(napi_ok))); + + NRef nref(nval); + NVal result = nref.Deref(env); + + EXPECT_EQ(result.env_, env); + EXPECT_EQ(result.val_, value); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_Deref_001"; +} + +/** + * @tc.name: NRefTest_Deref_002 + * @tc.desc: Test function of NRef::Deref interface for FAILURE Deref when ref_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_Deref_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_Deref_002"; + napi_env env = reinterpret_cast(0x1000); + + NRef nref; + NVal result = nref.Deref(env); + + EXPECT_EQ(result.env_, nullptr); + EXPECT_EQ(result.val_, nullptr); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_Deref_002"; +} + +/** + * @tc.name: NRefTest_Deref_003 + * @tc.desc: Test function of NRef::Deref interface for FAILURE Deref when env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_Deref_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_Deref_003"; + napi_value value = reinterpret_cast(0x2000); + NVal nval; + nval.val_ = value; + + NRef nref(nval); + nref.env_ = nullptr; + + NVal result = nref.Deref(nullptr); + + EXPECT_EQ(result.env_, nullptr); + EXPECT_EQ(result.val_, nullptr); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_Deref_003"; +} + +/** + * @tc.name: NRefTest_Deref_004 + * @tc.desc: Test function of NRef::Deref interface for FAILURE Deref when ref_ and env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_Deref_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_Deref_004"; + NVal nval; + NRef nref(nval); + nref.ref_ = nullptr; + nref.env_ = nullptr; + + NVal result = nref.Deref(nullptr); + + EXPECT_EQ(result.env_, nullptr); + EXPECT_EQ(result.val_, nullptr); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_Deref_004"; +} + +/** + * @tc.name: NRefTest_DeleteJsEnv_001 + * @tc.desc: Test function of NRef::DeleteJsEnv interface for SUCCESS DeleteJsEnv with valid ref_ and env_. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_DeleteJsEnv_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_DeleteJsEnv_001"; + napi_env env = reinterpret_cast(0x1000); + napi_value value = reinterpret_cast(0x2000); + NVal nval(env, value); + + napi_ref expectedRef = reinterpret_cast(0x3000); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedRef), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).WillOnce(testing::Return(napi_ok)); + + NRef nref(nval); + nref.DeleteJsEnv(); + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_DeleteJsEnv_001"; +} + +/** + * @tc.name: NRefTest_DeleteJsEnv_002 + * @tc.desc: Test function of NRef::DeleteJsEnv interface for FAILURE DeleteJsEnv when ref_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_DeleteJsEnv_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_DeleteJsEnv_002"; + napi_env env = reinterpret_cast(0x1000); + NVal nval; + nval.env_ = env; + + NRef nref(nval); + nref.ref_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.DeleteJsEnv(); + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_DeleteJsEnv_002"; +} + +/** + * @tc.name: NRefTest_DeleteJsEnv_003 + * @tc.desc: Test function of NRef::DeleteJsEnv interface for FAILURE DeleteJsEnv when env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_DeleteJsEnv_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_DeleteJsEnv_003"; + napi_value value = reinterpret_cast(0x2000); + NVal nval; + nval.val_ = value; + + NRef nref(nval); + nref.env_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.DeleteJsEnv(); + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_DeleteJsEnv_003"; +} + +/** + * @tc.name: NRefTest_DeleteJsEnv_004 + * @tc.desc: Test function of NRef::DeleteJsEnv interface for FAILURE DeleteJsEnv when ref_ and env_ is nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NRefTest, NRefTest_DeleteJsEnv_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NRefTest-begin NRefTest_DeleteJsEnv_004"; + NVal nval; + NRef nref(nval); + nref.ref_ = nullptr; + nref.env_ = nullptr; + + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).Times(0); + + nref.DeleteJsEnv(); + + EXPECT_FALSE(static_cast(nref)); + GTEST_LOG_(INFO) << "NRefTest-end NRefTest_DeleteJsEnv_004"; +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp new file mode 100644 index 000000000..a8ee5b7b8 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * 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 "napi_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { + +namespace { +std::mutex g_mockMutex; +NapiMock *g_currentMock = nullptr; +NapiMock g_defaultMock; +} // namespace + +NapiMock &GetNapiMock() +{ + std::lock_guard lock(g_mockMutex); + return g_currentMock ? *g_currentMock : g_defaultMock; +} + +void SetNapiMock(NapiMock *mock) +{ + std::lock_guard lock(g_mockMutex); + g_currentMock = mock; +} + +void ResetNapiMock() +{ + SetNapiMock(nullptr); +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS + +#ifdef ENABLE_NAPI_MOCK +extern "C" { +using namespace OHOS::FileManagement::LibN::Test; + +napi_status napi_define_class(napi_env env, const char *name, size_t length, napi_callback constructor, void *data, + size_t property_count, const napi_property_descriptor *properties, napi_value *result) +{ + return GetNapiMock().napi_define_class(env, name, length, constructor, data, property_count, properties, result); +} + +napi_status napi_create_reference(napi_env env, napi_value value, uint32_t initial_refcount, napi_ref *result) +{ + return GetNapiMock().napi_create_reference(env, value, initial_refcount, result); +} + +napi_status napi_add_env_cleanup_hook(napi_env env, void (*fun)(void *arg), void *arg) +{ + return GetNapiMock().napi_add_env_cleanup_hook(env, fun, arg); +} + +napi_status napi_delete_reference(napi_env env, napi_ref ref) +{ + return GetNapiMock().napi_delete_reference(env, ref); +} + +napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) +{ + return GetNapiMock().napi_get_reference_value(env, ref, result); +} + +napi_status napi_new_instance( + napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result) +{ + return GetNapiMock().napi_new_instance(env, constructor, argc, argv, result); +} + +napi_status napi_unwrap(napi_env env, napi_value js_object, void **result) +{ + return GetNapiMock().napi_unwrap(env, js_object, result); +} + +napi_status napi_wrap(napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, + void *finalize_hint, napi_ref *result) +{ + return GetNapiMock().napi_wrap(env, js_object, native_object, finalize_cb, finalize_hint, result); +} + +napi_status napi_remove_wrap(napi_env env, napi_value js_object, void **result) +{ + return GetNapiMock().napi_remove_wrap(env, js_object, result); +} + +napi_status napi_create_promise(napi_env env, napi_deferred *deferred, napi_value *promise) +{ + return GetNapiMock().napi_create_promise(env, deferred, promise); +} + +napi_status napi_create_async_work(napi_env env, napi_value async_resource, napi_value async_resource_name, + napi_async_execute_callback execute, napi_async_complete_callback complete, void *data, napi_async_work *result) +{ + return GetNapiMock().napi_create_async_work( + env, async_resource, async_resource_name, execute, complete, data, result); +} + +napi_status napi_queue_async_work(napi_env env, napi_async_work work) +{ + return GetNapiMock().napi_queue_async_work(env, work); +} + +napi_status napi_create_string_utf8(napi_env env, const char *str, size_t length, napi_value *result) +{ + return GetNapiMock().napi_create_string_utf8(env, str, length, result); +} + +} // extern "C" +#endif \ No newline at end of file -- Gitee