From: SukHyung, Kang Date: Wed, 29 Jul 2020 06:33:41 +0000 (+0900) Subject: Add unit test for app event X-Git-Tag: submit/tizen/20200925.013435~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8e63442ffbc9b54af05aa70cea977346b51f67a8;p=platform%2Fcore%2Fapi%2Fapp-event.git Add unit test for app event Change-Id: I3f4ce6f04509751d4adaa39778735b85bd62055e Signed-off-by: SukHyung, Kang --- diff --git a/CMakeLists.txt b/CMakeLists.txt index dd27bc3..eb24c99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,16 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(INC_DIR include) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tests) + +ENABLE_TESTING() +SET(APP_EVENT_UNIT_TESTS app_event_unittests) +ADD_TEST(NAME ${APP_EVENT_UNIT_TESTS} COMMAND ${APP_EVENT_UNIT_TESTS} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests) + +ADD_DEPENDENCIES(${APP_EVENT_UNIT_TESTS} capi-appfw-event) INSTALL( DIRECTORY ${INC_DIR}/ DESTINATION include/appfw diff --git a/mock/aul_mock.cc b/mock/aul_mock.cc new file mode 100644 index 0000000..74affa4 --- /dev/null +++ b/mock/aul_mock.cc @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 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/aul_mock.h" +#include "mock/mock_hook.h" +#include "mock/test_fixture.h" + +extern "C" int aul_svc_get_appid_by_alias_appid(const char *alias_appid, + char **appid) { + return MOCK_HOOK_P2(AulMock, aul_svc_get_appid_by_alias_appid, + alias_appid, appid); +} diff --git a/mock/aul_mock.h b/mock/aul_mock.h new file mode 100644 index 0000000..b1ee00e --- /dev/null +++ b/mock/aul_mock.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_AUL_MOCK_H_ +#define UNIT_TESTS_MOCK_AUL_MOCK_H_ + +#include + +#include "mock/module_mock.h" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD2(aul_svc_get_appid_by_alias_appid, + int(const char *, char **)); +}; + +#endif // UNIT_TESTS_MOCK_AUL_MOCK_H_ diff --git a/mock/libeventsystem_mock.cc b/mock/libeventsystem_mock.cc new file mode 100644 index 0000000..b62ee73 --- /dev/null +++ b/mock/libeventsystem_mock.cc @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 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/libeventsystem_mock.h" +#include "mock/mock_hook.h" +#include "mock/test_fixture.h" + +extern "C" int eventsystem_register_application_event(const char *event_name, + unsigned int *reg_id, int *event_type, eventsystem_cb callback, + void *user_data) { + return MOCK_HOOK_P5(EventSystemMock, eventsystem_register_application_event, + event_name, reg_id, event_type, callback, user_data); +} + +extern "C" int eventsystem_unregister_application_event(unsigned int reg_id) { + return MOCK_HOOK_P1(EventSystemMock, eventsystem_unregister_application_event, + reg_id); +} + +extern "C" int eventsystem_send_user_event(const char *event_name, + bundle *data, bool is_trusted) { + return MOCK_HOOK_P3(EventSystemMock, eventsystem_send_user_event, + event_name, data, is_trusted); +} + +extern "C" int eventsystem_keep_last_event_data(const char *event_name) { + return MOCK_HOOK_P1(EventSystemMock, eventsystem_keep_last_event_data, + event_name); +} diff --git a/mock/libeventsystem_mock.h b/mock/libeventsystem_mock.h new file mode 100644 index 0000000..724e593 --- /dev/null +++ b/mock/libeventsystem_mock.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_EVENT_SYSTEM_MOCK_H_ +#define UNIT_TESTS_MOCK_EVENT_SYSTEM_MOCK_H_ + +#include +#include +#include + +#include "mock/module_mock.h" + +class EventSystemMock : public virtual ModuleMock { + public: + virtual ~EventSystemMock() {} + + MOCK_METHOD5(eventsystem_register_application_event, + int(const char *, unsigned int *, int *, eventsystem_cb, void *)); + + MOCK_METHOD1(eventsystem_unregister_application_event, + int(unsigned int)); + + MOCK_METHOD3(eventsystem_send_user_event, + int(const char *, bundle *, bool)); + + MOCK_METHOD1(eventsystem_keep_last_event_data, + int(const char *)); +}; + +#endif // UNIT_TESTS_MOCK_EVENT_SYSTEM_MOCK_H_ diff --git a/mock/mock_hook.h b/mock/mock_hook.h new file mode 100644 index 0000000..af27bba --- /dev/null +++ b/mock/mock_hook.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_MOCK_HOOK_H_ +#define UNIT_TESTS_MOCK_MOCK_HOOK_H_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7) +#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6, p7, p8) +#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \ + TestFixture::GetMock().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // UNIT_TESTS_MOCK_MOCK_HOOK_H_ diff --git a/mock/module_mock.h b/mock/module_mock.h new file mode 100644 index 0000000..9b19d89 --- /dev/null +++ b/mock/module_mock.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_MODULE_MOCK_H_ +#define UNIT_TESTS_MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // UNIT_TESTS_MOCK_MODULE_MOCK_H_ diff --git a/mock/test_fixture.cc b/mock/test_fixture.cc new file mode 100644 index 0000000..27f5666 --- /dev/null +++ b/mock/test_fixture.cc @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 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 "test_fixture.h" + +#include + +std::unique_ptr TestFixture::mock_; diff --git a/mock/test_fixture.h b/mock/test_fixture.h new file mode 100644 index 0000000..1ea3b8f --- /dev/null +++ b/mock/test_fixture.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_TEST_FIXTURE_H_ +#define UNIT_TESTS_MOCK_TEST_FIXTURE_H_ + +#include + +#include +#include +#include +#include + +#include "module_mock.h" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template + static T& GetMock() { + auto ptr = dynamic_cast(mock_.get()); + if (!ptr) + throw std::invalid_argument("The test does not provide mock of \"" + + std::string(typeid(T).name()) + "\""); + return *ptr; + } + + static std::unique_ptr mock_; +}; + +#endif // UNIT_TESTS_MOCK_TEST_FIXTURE_H_ diff --git a/packaging/capi-appfw-event.spec b/packaging/capi-appfw-event.spec index 6333035..e32add0 100644 --- a/packaging/capi-appfw-event.spec +++ b/packaging/capi-appfw-event.spec @@ -12,6 +12,12 @@ BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(eventsystem) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(aul) +BuildRequires: pkgconfig(gmock) + +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif %description An Application event library in Tizen C API @@ -54,6 +60,15 @@ mkdir -p gcov-obj find . -name '*.gcno' -exec cp '{}' gcov-obj ';' %endif +%check +ctest -V +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o app_event.info +genhtml app_event.info -o app_event.out +zip -r app_event.zip app_event.out app_event.info +install -m 0644 app_event.zip %{buildroot}%{_datadir}/gcov/ +%endif + %install rm -rf %{buildroot} %make_install @@ -66,6 +81,19 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %post -p /sbin/ldconfig %postun -p /sbin/ldconfig +################################################ +# app_event_unittests +################################################ +%package -n app_event_unittests +Summary: GTest for app_event +Group: Development/Libraries + +%description -n app_event_unittests +GTest for app_event + +%files -n app_event_unittests +%{_bindir}/app_event_unittests + %files %manifest %{name}.manifest %{_libdir}/libcapi-appfw-event.so.* @@ -79,5 +107,5 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %if 0%{?gcov:1} %files gcov -%{_datadir}/gcov/obj/* +%{_datadir}/gcov/* %endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ccd245c..14c5f04 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(fw_name "capi-appfw-event") PROJECT(${fw_name}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..1aa1814 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,43 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(app_event_unittests CXX) + +INCLUDE(FindPkgConfig) +pkg_check_modules(app_event_unittests REQUIRED + dlog + gmock + glib-2.0 + bundle + eventsystem + aul +) + +FOREACH(flag ${app_event_unittests_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror") + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2") + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../mock) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../) + +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../src LIB_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock MOCK_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../tests/unittests SOURCES) + +ADD_EXECUTABLE(${PROJECT_NAME} + ${SOURCES} + ${LIB_SOURCES} + ${MOCK_SOURCES} +) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${app_event_unittests_LDFLAGS} + capi-appfw-event +) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/) diff --git a/tests/unittests/test_event.cc b/tests/unittests/test_event.cc new file mode 100644 index 0000000..f47712a --- /dev/null +++ b/tests/unittests/test_event.cc @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2020 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 "app_event.h" +#include "mock/test_fixture.h" +#include "mock/libeventsystem_mock.h" +#include "mock/aul_mock.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; + +class Mocks : public ::testing::NiceMock, + public ::testing::NiceMock {}; + +class AppEventTest : public TestFixture { + public: + AppEventTest() : TestFixture(std::make_unique()) {} + virtual ~AppEventTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +static void _event_cb(const char *event_name, bundle *event_data, void *user_data) +{ + return; +} + + +TEST_F(AppEventTest, add_remove_event_handler) { + int ret; + event_handler_h event_handler; + char *_appid = nullptr; + + _appid = (char *)malloc(sizeof(char) * 11); + + if (_appid) + snprintf(_appid, 10, "%s", "testappid"); + + EXPECT_CALL(GetMock(), + aul_svc_get_appid_by_alias_appid(_, _)). + WillOnce(DoAll(SetArgPointee<1>(_appid), + Return(0))); + + EXPECT_CALL(GetMock(), + eventsystem_register_application_event(_, _, _, _, _)). + WillOnce(Return(0)); + + ret = event_add_event_handler(SYSTEM_EVENT_BATTERY_CHARGER_STATUS, _event_cb, + NULL, &event_handler); + + EXPECT_EQ(ret, EVENT_ERROR_NONE); + + EXPECT_CALL(GetMock(), + eventsystem_unregister_application_event(_)). + WillOnce(Return(0)); + + ret = event_remove_event_handler(event_handler); + + EXPECT_EQ(ret, EVENT_ERROR_NONE); +} + +TEST_F(AppEventTest, publish_app_event) { + int ret; + bundle *event_data = NULL; + + event_data = bundle_create(); + bundle_add_str(event_data, "my_event_key", "my_event_data"); + + EXPECT_CALL(GetMock(), + eventsystem_send_user_event(_, _, _)). + WillOnce(Return(0)); + + ret = event_publish_app_event("myevent", event_data); + + bundle_free(event_data); + + EXPECT_EQ(ret, EVENT_ERROR_NONE); +} + +TEST_F(AppEventTest, publish_trusted_app_event) { + int ret; + bundle *event_data = NULL; + + event_data = bundle_create(); + bundle_add_str(event_data, "my_event_key", "my_event_data"); + + EXPECT_CALL(GetMock(), + eventsystem_send_user_event(_, _, _)). + WillOnce(Return(0)); + + ret = event_publish_trusted_app_event("myevent", event_data); + + bundle_free(event_data); + + EXPECT_EQ(ret, EVENT_ERROR_NONE); +} + +TEST_F(AppEventTest, keep_last_event) { + int ret; + + EXPECT_CALL(GetMock(), + eventsystem_keep_last_event_data(_)). + WillOnce(Return(0)); + + ret = event_keep_last_event_data("myevent"); + + EXPECT_EQ(ret, EVENT_ERROR_NONE); +} diff --git a/tests/unittests/test_main.cc b/tests/unittests/test_main.cc new file mode 100644 index 0000000..46bccb2 --- /dev/null +++ b/tests/unittests/test_main.cc @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 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 + +int main(int argc, char** argv) { + int ret; + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() + << std::endl; + } + + return ret; +}