From e5f2ccef12250dad94034c6f94c66329c1b742f2 Mon Sep 17 00:00:00 2001 From: bailu1992 Date: Fri, 9 Aug 2024 16:53:39 +0800 Subject: [PATCH] report_data_callback File coverage rate Signed-off-by: bailu1992 --- test/unittest/coverage/BUILD.gn | 33 ++++- .../coverage/report_data_callback_test.cpp | 122 ++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 test/unittest/coverage/report_data_callback_test.cpp diff --git a/test/unittest/coverage/BUILD.gn b/test/unittest/coverage/BUILD.gn index 458a4c68..e0c6461b 100644 --- a/test/unittest/coverage/BUILD.gn +++ b/test/unittest/coverage/BUILD.gn @@ -42,7 +42,38 @@ ohos_unittest("SensorBasicDataChannelTest") { "ipc:ipc_single", ] } + +ohos_unittest("ReportDataCallbackTest") { + module_out_path = "sensor/coverage" + + sources = + [ "$SUBSYSTEM_DIR/test/unittest/coverage/report_data_callback_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/kits/c", + "$SUBSYSTEM_DIR/frameworks/native/include", + "$SUBSYSTEM_DIR/interfaces/inner_api", + ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native:libsensor_client", + "$SUBSYSTEM_DIR/frameworks/native:ohsensor", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gmock", + "googletest:gtest_main", + "hilog:libhilog", + ] +} + group("unittest") { testonly = true - deps = [ ":SensorBasicDataChannelTest" ] + deps = [ + ":ReportDataCallbackTest", + ":SensorBasicDataChannelTest", + ] } diff --git a/test/unittest/coverage/report_data_callback_test.cpp b/test/unittest/coverage/report_data_callback_test.cpp new file mode 100644 index 00000000..201bac50 --- /dev/null +++ b/test/unittest/coverage/report_data_callback_test.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024 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 "report_data_callback.h" +#include "sensor_errors.h" + +#undef LOG_TAG +#define LOG_TAG "ReportDataCallbackTest" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; + +namespace { + SensorData* g_sensorData = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; +} // namespace + +class SensorBasicDataChannelTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void SensorBasicDataChannelTest::SetUpTestCase() {} + +void SensorBasicDataChannelTest::TearDownTestCase() +{ + if (g_sensorData != nullptr) { + delete[] g_sensorData; + g_sensorData = nullptr; + } +} + +void SensorBasicDataChannelTest::SetUp() {} + +void SensorBasicDataChannelTest::TearDown() {} + +HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_001, TestSize.Level1) +{ + SEN_HILOGI("ReportDataCallbackTest_001 in"); + + ReportDataCallback reportDataCallback = ReportDataCallback(); + ReportDataCallback *cb = nullptr; + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, cb); + ASSERT_EQ(ret, ERROR); + + sptr callback = new (std::nothrow) ReportDataCallback(); + if (callback->eventsBuf_.circularBuf != nullptr) { + delete[] callback->eventsBuf_.circularBuf; + callback->eventsBuf_.circularBuf = nullptr; + } + callback->eventsBuf_.circularBuf = nullptr; + ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERROR); +} + +HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_002, TestSize.Level1) +{ + SEN_HILOGI("ReportDataCallbackTest_002 in"); + + ReportDataCallback reportDataCallback = ReportDataCallback(); + sptr callback = new (std::nothrow) ReportDataCallback(); + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN + 1; + callback->eventsBuf_.writePosition = 1; + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERROR); + + callback->eventsBuf_.eventNum = 1; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN + 1; + ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERROR); +} + +HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_003, TestSize.Level1) +{ + SEN_HILOGI("ReportDataCallbackTest_003 in"); + ReportDataCallback reportDataCallback = ReportDataCallback(); + sptr callback = new (std::nothrow) ReportDataCallback(); + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN; + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERR_OK); +} + +HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_004, TestSize.Level1) +{ + SEN_HILOGI("ReportDataCallbackTest_004 in"); + ReportDataCallback reportDataCallback = ReportDataCallback(); + sptr callback = new (std::nothrow) ReportDataCallback(); + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = 1; + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERR_OK); +} + +HWTEST_F(SensorBasicDataChannelTest, ReportDataCallbackTest_005, TestSize.Level1) +{ + SEN_HILOGI("ReportDataCallbackTest_005 in"); + ReportDataCallback reportDataCallback = ReportDataCallback(); + sptr callback = new (std::nothrow) ReportDataCallback(); + callback->eventsBuf_.eventNum = CIRCULAR_BUF_LEN; + callback->eventsBuf_.writePosition = CIRCULAR_BUF_LEN - 1; + int32_t ret = reportDataCallback.ReportEventCallback(g_sensorData, callback); + ASSERT_EQ(ret, ERR_OK); +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file -- Gitee