From 1d8c38fc1126c8991aabb5af77ba3a9c20bef739 Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Tue, 24 Aug 2021 15:20:53 +0900 Subject: [PATCH] Implement EventSignalSender unit test Change-Id: I473f648fd6182ac3d7ab191a5cad55c89c7767bd Signed-off-by: Ilho Kim --- src/res-copy/include/event_signal_sender.hh | 2 +- tests/mock/pkgmgr_installer_mock.cc | 25 +++++++ tests/mock/pkgmgr_installer_mock.h | 34 +++++++++ .../res-copy/src/test_event_signal_sender.cc | 74 +++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 tests/mock/pkgmgr_installer_mock.cc create mode 100644 tests/mock/pkgmgr_installer_mock.h create mode 100644 tests/unit_tests/res-copy/src/test_event_signal_sender.cc diff --git a/src/res-copy/include/event_signal_sender.hh b/src/res-copy/include/event_signal_sender.hh index 3f0c52d..495f059 100644 --- a/src/res-copy/include/event_signal_sender.hh +++ b/src/res-copy/include/event_signal_sender.hh @@ -51,4 +51,4 @@ class EventSignalSender { } // res_handler -#endif // PARAM_CHECKER_HH_ +#endif // EVENT_SIGNAL_SENDER_HH_ diff --git a/tests/mock/pkgmgr_installer_mock.cc b/tests/mock/pkgmgr_installer_mock.cc new file mode 100644 index 0000000..fbf408a --- /dev/null +++ b/tests/mock/pkgmgr_installer_mock.cc @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Samsung Electronics 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 "mock/mock_hook.h" +#include "mock/pkgmgr_installer_mock.h" +#include "mock/test_fixture.h" + +extern "C" int pkgmgr_installer_send_res_copy_signal(pkgmgr_installer *pi, + const char *pkgid, const char *status, pkgmgr_res_event_info *event_info) { + return MOCK_HOOK_P4(PkgMgrInstallerMock, + pkgmgr_installer_send_res_copy_signal, pi, pkgid, status, event_info); +} diff --git a/tests/mock/pkgmgr_installer_mock.h b/tests/mock/pkgmgr_installer_mock.h new file mode 100644 index 0000000..fe1f9ac --- /dev/null +++ b/tests/mock/pkgmgr_installer_mock.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Samsung Electronics 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 TESTS_MOCK_PKGMGR_INSTALLER_MOCK_H_ +#define TESTS_MOCK_PKGMGR_INSTALLER_MOCK_H_ + +#include +#include +#include + +#include "mock/module_mock.h" + +class PkgMgrInstallerMock : public virtual ModuleMock { + public: + virtual ~PkgMgrInstallerMock() {} + + MOCK_METHOD4(pkgmgr_installer_send_res_copy_signal, + int(pkgmgr_installer*, const char*, const char*, pkgmgr_res_event_info*)); +}; + +#endif // TESTS_MOCK_PKGMGR_INSTALLER_MOCK_H_ diff --git a/tests/unit_tests/res-copy/src/test_event_signal_sender.cc b/tests/unit_tests/res-copy/src/test_event_signal_sender.cc new file mode 100644 index 0000000..56a7d15 --- /dev/null +++ b/tests/unit_tests/res-copy/src/test_event_signal_sender.cc @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 Samsung Electronics 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 "mock/pkgmgr_installer_mock.h" +#include "mock/test_fixture.h" +#include "include/event_signal_sender.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::InvokeArgument; +using ::testing::SaveArg; + +using res_handler::EventSignalSender; + +class InstallerMocks : public ::testing::NiceMock {}; + +class EventSignalSenderTest : public TestFixture { + public: + EventSignalSenderTest() : TestFixture(std::make_unique()) {} + virtual ~EventSignalSenderTest() {} + + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(EventSignalSenderTest, SendSignals) { + EXPECT_CALL(GetMock(), + pkgmgr_installer_send_res_copy_signal(_, _, _, _)) + .WillRepeatedly(Return(0)); + + pkgmgr_installer* pi = pkgmgr_installer_offline_new(); + EventSignalSender signal(pi); + signal.SetPkgID("org.test.targetpkgid"); + signal.SetReqType(res_handler::ReqType::REQ_TYPE_NEW); + signal.SetUID(0); + signal.SetSessionID("session_id"); + + EXPECT_TRUE(signal.SendStart()); + EXPECT_TRUE(signal.SendOK()); + EXPECT_TRUE(signal.SendFail(res_handler::ErrorType::ERROR_NONE)); +} + +TEST_F(EventSignalSenderTest, SendSignalWithNullPkgMgrInstaller) { + EXPECT_CALL(GetMock(), + pkgmgr_installer_send_res_copy_signal(_, _, _, _)) + .Times(0); + + EventSignalSender signal(nullptr); + EXPECT_FALSE(signal.SendStart()); + EXPECT_FALSE(signal.SendOK()); + EXPECT_FALSE(signal.SendFail(res_handler::ErrorType::ERROR_NONE)); +} -- 2.34.1