From 7e0182af711d412214e131babf686bf631d24500 Mon Sep 17 00:00:00 2001 From: chen0088 Date: Mon, 21 Apr 2025 14:54:59 +0800 Subject: [PATCH] add ut Signed-off-by: chen0088 --- .../common/distributedcameramgr/BUILD.gn | 3 + ..._sink_controller_channel_listener_test.cpp | 135 ++++++++++++++++++ ...ra_sink_controller_state_callback_test.cpp | 107 ++++++++++++++ ...camera_sink_data_process_listener_test.cpp | 107 ++++++++++++++ .../dcamera_sink_output_test.cpp | 14 ++ .../dcameradata/dcamera_source_input.cpp | 4 +- .../dcamera_source_input_test.cpp | 25 ++++ 7 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_channel_listener_test.cpp create mode 100644 services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_state_callback_test.cpp create mode 100644 services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_data_process_listener_test.cpp diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/BUILD.gn b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/BUILD.gn index 85302998..251ff0e9 100644 --- a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/BUILD.gn +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/BUILD.gn @@ -53,7 +53,10 @@ ohos_unittest("DCameraSinkMgrTest") { sources = [ "dcamera_sink_access_control_test.cpp", + "dcamera_sink_controller_channel_listener_test.cpp", + "dcamera_sink_controller_state_callback_test.cpp", "dcamera_sink_controller_test.cpp", + "dcamera_sink_data_process_listener_test.cpp", "dcamera_sink_data_process_test.cpp", "dcamera_sink_dev_test.cpp", "dcamera_sink_output_test.cpp", diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_channel_listener_test.cpp b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_channel_listener_test.cpp new file mode 100644 index 00000000..22192d8b --- /dev/null +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_channel_listener_test.cpp @@ -0,0 +1,135 @@ +/* + * 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 "icamera_channel_listener.h" +#include "dcamera_sink_controller.h" +#include "dcamera_sink_access_control.h" +#include "dcamera_sink_callback.h" +#include "dcamera_sink_controller_channel_listener.h" + +#include "distributed_camera_constants.h" +#include "distributed_camera_errno.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +class DCameraSinkControllerChannelListenerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr channelListener_; +}; + +void DCameraSinkControllerChannelListenerTest::SetUpTestCase(void) +{ +} + +void DCameraSinkControllerChannelListenerTest::TearDownTestCase(void) +{ +} + +void DCameraSinkControllerChannelListenerTest::SetUp(void) +{ + std::shared_ptr accessControl = std::make_shared(); + sptr sinkCallback(new DCameraSinkCallback()); + std::shared_ptr controller = + std::make_shared(accessControl, sinkCallback); + channelListener_ = std::make_shared(controller); +} + +void DCameraSinkControllerChannelListenerTest::TearDown(void) +{ + channelListener_ = nullptr; +} + +/** + * @tc.name: dcamera_sink_controller_channel_listener_test_001 + * @tc.desc: Verify the OnSessionState function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerChannelListenerTest, dcamera_sink_controller_channel_listener_test_001, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_channel_listener_test_001 enter"); + ASSERT_NE(channelListener_, nullptr); + int32_t state = 1; + std::string networkId = "networkId"; + EXPECT_NO_FATAL_FAILURE(channelListener_->OnSessionState(state, networkId)); +} + +/** + * @tc.name: dcamera_sink_controller_channel_listener_test_002 + * @tc.desc: Verify the OnSessionError function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerChannelListenerTest, dcamera_sink_controller_channel_listener_test_002, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_channel_listener_test_002 enter"); + ASSERT_NE(channelListener_, nullptr); + int32_t eventType = 0; + int32_t eventReason = 1; + std::string detail = "detail"; + EXPECT_NO_FATAL_FAILURE(channelListener_->OnSessionError(eventType, eventReason, detail)); +} + +/** + * @tc.name: dcamera_sink_controller_channel_listener_test_003 + * @tc.desc: Verify the OnDataReceived function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerChannelListenerTest, dcamera_sink_controller_channel_listener_test_003, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_channel_listener_test_003 enter"); + ASSERT_NE(channelListener_, nullptr); + std::vector> buffers; + size_t capacity = 0; + std::shared_ptr dataBuffer = std::make_shared(capacity); + buffers.push_back(dataBuffer); + EXPECT_NO_FATAL_FAILURE(channelListener_->OnDataReceived(buffers)); +} + +/** + * @tc.name: dcamera_sink_controller_channel_listener_test_004 + * @tc.desc: Verify the OnDataReceived function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerChannelListenerTest, dcamera_sink_controller_channel_listener_test_004, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_channel_listener_test_004 enter"); + std::shared_ptr contl; + auto channelListener = std::make_shared(contl); + int32_t state = 1; + std::string networkId = "networkId"; + EXPECT_NO_FATAL_FAILURE(channelListener->OnSessionState(state, networkId)); + int32_t eventType = 0; + int32_t eventReason = 1; + std::string detail = "detail"; + EXPECT_NO_FATAL_FAILURE(channelListener->OnSessionError(eventType, eventReason, detail)); + std::vector> buffers; + size_t capacity = 0; + std::shared_ptr dataBuffer = std::make_shared(capacity); + buffers.push_back(dataBuffer); + EXPECT_NO_FATAL_FAILURE(channelListener->OnDataReceived(buffers)); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_state_callback_test.cpp b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_state_callback_test.cpp new file mode 100644 index 00000000..b69ad3ab --- /dev/null +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_controller_state_callback_test.cpp @@ -0,0 +1,107 @@ +/* + * 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 "icamera_channel_listener.h" +#include "dcamera_sink_controller.h" +#include "dcamera_sink_access_control.h" +#include "dcamera_sink_callback.h" +#include "dcamera_sink_controller_state_callback.h" + +#include "distributed_camera_constants.h" +#include "distributed_camera_errno.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +class DCameraSinkControllerStateCallbackTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr stateCallback_; +}; + +void DCameraSinkControllerStateCallbackTest::SetUpTestCase(void) +{ +} + +void DCameraSinkControllerStateCallbackTest::TearDownTestCase(void) +{ +} + +void DCameraSinkControllerStateCallbackTest::SetUp(void) +{ + std::shared_ptr accessControl = std::make_shared(); + sptr sinkCallback(new DCameraSinkCallback()); + std::shared_ptr controller = + std::make_shared(accessControl, sinkCallback); + stateCallback_ = std::make_shared(controller); +} + +void DCameraSinkControllerStateCallbackTest::TearDown(void) +{ + stateCallback_ = nullptr; +} + +/** + * @tc.name: dcamera_sink_controller_state_callback_test_001 + * @tc.desc: Verify the OnStateChanged function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerStateCallbackTest, dcamera_sink_controller_state_callback_test_001, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_state_callback_test_001 enter"); + ASSERT_NE(stateCallback_, nullptr); + std::shared_ptr event = std::make_shared(); + EXPECT_NO_FATAL_FAILURE(stateCallback_->OnStateChanged(event)); +} + +/** + * @tc.name: dcamera_sink_controller_state_callback_test_002 + * @tc.desc: Verify the OnMetadataResult function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerStateCallbackTest, dcamera_sink_controller_state_callback_test_002, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_state_callback_test_002 enter"); + ASSERT_NE(stateCallback_, nullptr); + std::vector> settings; + EXPECT_NO_FATAL_FAILURE(stateCallback_->OnMetadataResult(settings)); +} + +/** + * @tc.name: dcamera_sink_controller_state_callback_test_003 + * @tc.desc: Verify the OnDataReceived function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkControllerStateCallbackTest, dcamera_sink_controller_state_callback_test_003, TestSize.Level1) +{ + DHLOGI("dcamera_sink_controller_state_callback_test_003 enter"); + std::shared_ptr contl; + auto stateCallback = std::make_shared(contl); + std::shared_ptr event = std::make_shared(); + EXPECT_NO_FATAL_FAILURE(stateCallback->OnStateChanged(event)); + std::vector> settings; + EXPECT_NO_FATAL_FAILURE(stateCallback->OnMetadataResult(settings)); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_data_process_listener_test.cpp b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_data_process_listener_test.cpp new file mode 100644 index 00000000..6d19e768 --- /dev/null +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_data_process_listener_test.cpp @@ -0,0 +1,107 @@ +/* + * 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 "dcamera_sink_data_process.h" +#include "mock_camera_channel.h" +#include "dcamera_sink_data_process_listener.h" + +#include "distributed_camera_constants.h" +#include "distributed_camera_errno.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +class DCameraSinkDataProcessListenerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr dataProcessListener_; +}; +const std::string TEST_DHID = "camera0"; + +void DCameraSinkDataProcessListenerTest::SetUpTestCase(void) +{ +} + +void DCameraSinkDataProcessListenerTest::TearDownTestCase(void) +{ +} + +void DCameraSinkDataProcessListenerTest::SetUp(void) +{ + std::shared_ptr channel = std::make_shared(); + std::shared_ptr dataProcess = + std::make_shared(TEST_DHID, channel); + dataProcessListener_ = std::make_shared(dataProcess); +} + +void DCameraSinkDataProcessListenerTest::TearDown(void) +{ + dataProcessListener_ = nullptr; +} + +/** + * @tc.name: dcamera_sink_data_process_listener_test_001 + * @tc.desc: Verify the OnProcessedVideoBuffer function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkDataProcessListenerTest, dcamera_sink_data_process_listener_test_001, TestSize.Level1) +{ + DHLOGI("dcamera_sink_data_process_listener_test_001 enter"); + ASSERT_NE(dataProcessListener_, nullptr); + size_t capacity = 1; + std::shared_ptr videoResult = std::make_shared(capacity); + EXPECT_NO_FATAL_FAILURE(dataProcessListener_->OnProcessedVideoBuffer(videoResult)); +} + +/** + * @tc.name: dcamera_sink_data_process_listener_test_002 + * @tc.desc: Verify the OnError function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkDataProcessListenerTest, dcamera_sink_data_process_listener_test_002, TestSize.Level1) +{ + DHLOGI("dcamera_sink_data_process_listener_test_002 enter"); + ASSERT_NE(dataProcessListener_, nullptr); + DataProcessErrorType errorType = DataProcessErrorType::ERROR_PIPELINE_ENCODER; + EXPECT_NO_FATAL_FAILURE(dataProcessListener_->OnError(errorType)); +} + +/** + * @tc.name: dcamera_sink_data_process_listener_test_003 + * @tc.desc: Verify the OnDataReceived function. + * @tc.type: FUNC + * @tc.require: DTS + */ +HWTEST_F(DCameraSinkDataProcessListenerTest, dcamera_sink_data_process_listener_test_003, TestSize.Level1) +{ + DHLOGI("dcamera_sink_data_process_listener_test_003 enter"); + std::shared_ptr data; + auto dataProcessListener = std::make_shared(data); + size_t capacity = 1; + std::shared_ptr videoResult = std::make_shared(capacity); + EXPECT_NO_FATAL_FAILURE(dataProcessListener->OnProcessedVideoBuffer(videoResult)); + DataProcessErrorType errorType = DataProcessErrorType::ERROR_PIPELINE_ENCODER; + EXPECT_NO_FATAL_FAILURE(dataProcessListener->OnError(errorType)); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_output_test.cpp b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_output_test.cpp index 1adafec1..8b1fcc59 100644 --- a/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_output_test.cpp +++ b/services/cameraservice/sinkservice/test/unittest/common/distributedcameramgr/dcamera_sink_output_test.cpp @@ -301,5 +301,19 @@ HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_009, TestSize.Level1) int32_t ret = operator_->PrelaunchCamera(); EXPECT_EQ(DCAMERA_OK, ret); } + +/** + * @tc.name: dcamera_sink_output_test_010 + * @tc.desc: Verify the UnInit function. + * @tc.type: FUNC + * @tc.require: I5N1JI + */ +HWTEST_F(DCameraSinkOutputTest, dcamera_sink_output_test_010, TestSize.Level1) +{ + auto callback = std::make_shared(output_); + auto dataBuffer = std::make_shared(1); + EXPECT_NO_FATAL_FAILURE(callback->OnPhotoResult(dataBuffer)); + EXPECT_NO_FATAL_FAILURE(callback->OnVideoResult(dataBuffer)); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/src/distributedcameramgr/dcameradata/dcamera_source_input.cpp b/services/cameraservice/sourceservice/src/distributedcameramgr/dcameradata/dcamera_source_input.cpp index ff3fb665..d3cd7158 100644 --- a/services/cameraservice/sourceservice/src/distributedcameramgr/dcameradata/dcamera_source_input.cpp +++ b/services/cameraservice/sourceservice/src/distributedcameramgr/dcameradata/dcamera_source_input.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -380,6 +380,8 @@ void DCameraSourceInput::OnSessionError(DCStreamType streamType, int32_t eventTy void DCameraSourceInput::OnDataReceived(DCStreamType streamType, std::vector>& buffers) { + CHECK_AND_RETURN_LOG(buffers[0] == nullptr, "the first buffer is nullptr."); + CHECK_AND_RETURN_LOG(dataProcess_[streamType] == nullptr, "dataProcess_ is nullptr."); buffers[0]->frameInfo_.offset = DCameraSoftbusLatency::GetInstance().GetTimeSyncInfo(devId_); int32_t ret = dataProcess_[streamType]->FeedStream(buffers); if (ret != DCAMERA_OK) { diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_source_input_test.cpp b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_source_input_test.cpp index 9b396a48..7e365058 100644 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_source_input_test.cpp +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_source_input_test.cpp @@ -19,6 +19,7 @@ #undef private #include "distributed_camera_errno.h" #include "mock_dcamera_source_state_listener.h" +#include "dcamera_source_input_channel_listener.h" using namespace testing::ext; @@ -435,5 +436,29 @@ HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_015, TestSize.Level1) rc = testInput_->EstablishSnapshotFrameSession(g_camIndexs); EXPECT_NE(rc, DCAMERA_OK); } + +/** + * @tc.name: dcamera_source_input_test_016 + * @tc.desc: Verify source inptut EstablishSnapshotFrameSession. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DCameraSourceInputTest, dcamera_source_input_test_016, TestSize.Level1) +{ + auto testInputListener = + std::make_shared(testInput_, DCStreamType::CONTINUOUS_FRAME); + int32_t state = 0; + std::string networkId = "networkId"; + EXPECT_NO_FATAL_FAILURE(testInputListener->OnSessionState(state, networkId)); + int32_t eventType = 0; + int32_t eventReason = 1; + std::string detail = "detail"; + EXPECT_NO_FATAL_FAILURE(testInputListener->OnSessionError(eventType, eventReason, detail)); + std::vector> buffers; + size_t capacity = 0; + std::shared_ptr dataBuffer = std::make_shared(capacity); + buffers.push_back(dataBuffer); + EXPECT_NO_FATAL_FAILURE(testInputListener->OnDataReceived(buffers)); +} } // namespace DistributedHardware } // namespace OHOS -- Gitee