Improve code coverage 18/298318/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 6 Sep 2023 01:39:46 +0000 (10:39 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 6 Sep 2023 01:40:53 +0000 (10:40 +0900)
- Modify unit tests

Change-Id: I589c303a2b5bf1db82e15f38112587589c9cf0ca
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
25 files changed:
CMakeLists.txt
capi-system-media-key.pc.in
cmake/Modules/ApplyPkgConfig.cmake [new file with mode: 0644]
mock/ecore_wl2_mock.cc [deleted file]
mock/ecore_wl2_mock.h [deleted file]
mock/mock_hook.h [deleted file]
mock/module_mock.h [deleted file]
mock/test_fixture.cc [deleted file]
mock/test_fixture.h [deleted file]
packaging/capi-system-media-key.spec
src/CMakeLists.txt [new file with mode: 0644]
src/media_key.cc
tests/CMakeLists.txt [new file with mode: 0644]
tests/media-key_unittests/CMakeLists.txt [new file with mode: 0644]
tests/media-key_unittests/main.cc [new file with mode: 0644]
tests/media-key_unittests/media_key_tests.cc [new file with mode: 0644]
tests/media-key_unittests/mock/ecore_wl2_mock.cc [new file with mode: 0644]
tests/media-key_unittests/mock/ecore_wl2_mock.h [new file with mode: 0644]
tests/media-key_unittests/mock/mock_hook.h [new file with mode: 0644]
tests/media-key_unittests/mock/module_mock.h [new file with mode: 0644]
tests/media-key_unittests/mock/test_fixture.cc [new file with mode: 0644]
tests/media-key_unittests/mock/test_fixture.h [new file with mode: 0644]
unittest/CMakeLists.txt [deleted file]
unittest/main.cc [deleted file]
unittest/media_key_unit_test.cc [deleted file]

index 7243e0c95f1b5852c1556aa22dffb10599c2c539..3789e1d7045597d7dde713fcd329c68e9e45c03c 100644 (file)
@@ -1,44 +1,50 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
-PROJECT(capi-system-media-key CXX)
 
-INCLUDE(FindPkgConfig)
-
-SET(requires "dlog capi-base-common ecore ecore-input evas ecore-wl2")
-SET(pc_requires "capi-base-common")
+PROJECT(capi-system-media-key)
 
-PKG_CHECK_MODULES(capi-system-media-key REQUIRED ${requires})
+SET(CMAKE_INSTALL_PREFIX /usr)
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(PC_PREFIX ${PREFIX})
+SET(PC_NAME "capi-system-media-key")
+SET(PC_REQUIRED "capi-base-common")
+SET(PC_LDFLAGS "-lcapi-system-media-key")
 
-FOREACH(flag ${capi-system-media-key_CFLAGS})
-        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
-ENDFOREACH(flag)
-SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-zdefs")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_C_FLAGS_RELEASE "-O2")
 
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_C_FLAGS} -fPIC")
 SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
 
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
 
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SOURCES)
-ADD_LIBRARY(${PROJECT_NAME} SHARED ${SOURCES})
+SET(TARGET_MEDIA_KEY "capi-system-media-key")
 
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${MAJORVER})
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${FULLVER})
-
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${capi-system-media-key_LDFLAGS})
+INCLUDE(FindPkgConfig)
+INCLUDE(ApplyPkgConfig)
 
-SET(PC_NAME capi-system-media-key)
-SET(PC_REQUIRED ${pc_requires})
-SET(PC_LDFLAGS -lcapi-system-media-key)
-SET(PC_CFLAGS -I\${includedir}/system)
+PKG_CHECK_MODULES(DLOG_DEPS REQUIRED dlog)
+PKG_CHECK_MODULES(CAPI_BASE_COMMON_DEPS REQUIRED capi-base-common)
+PKG_CHECK_MODULES(ECORE_DEPS REQUIRED ecore)
+PKG_CHECK_MODULES(ECORE_INPUT_DEPS REQUIRED ecore-input)
+PKG_CHECK_MODULES(EVAS_DEPS REQUIRED evas)
+PKG_CHECK_MODULES(ECORE_WL2_DEPS REQUIRED ecore-wl2)
+PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0)
+PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock)
 
-CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY)
-SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.pc")
+CONFIGURE_FILE(${TARGET_MEDIA_KEY}.pc.in ${TARGET_MEDIA_KEY}.pc @ONLY)
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_MEDIA_KEY}.pc
+  DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
 
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
 INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
-        DESTINATION include/system
-        FILES_MATCHING PATTERN "*.h")
+  DESTINATION include/system
+  FILES_MATCHING PATTERN "*.h")
 
-ADD_SUBDIRECTORY(unittest)
\ No newline at end of file
+ADD_SUBDIRECTORY(src)
+ADD_SUBDIRECTORY(tests)
\ No newline at end of file
index e5aa22d93d5208d1420edd642bd16107dbc3a0ff..ed2928164b40048b9e9ecd35a3ca056cc92b03dc 100644 (file)
@@ -7,7 +7,7 @@ libdir=@LIB_INSTALL_DIR@
 includedir=/usr/include/system
 
 Name: @PC_NAME@
-Description: @PACKAGE_DESCRIPTION@
+Description: Media Key Library
 Version: @VERSION@
 Requires: @PC_REQUIRED@
 Libs: -L${libdir} @PC_LDFLAGS@
diff --git a/cmake/Modules/ApplyPkgConfig.cmake b/cmake/Modules/ApplyPkgConfig.cmake
new file mode 100644 (file)
index 0000000..793670b
--- /dev/null
@@ -0,0 +1,35 @@
+# Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+#
+# This function applies external (out of source tree) dependencies
+# to given target. Arguments are:
+#   TARGET - valid cmake target
+#   PRIVACY - dependency can be inherited by dependent targets or not:
+#     PUBLIC - this should be used by default, cause compile/link flags passing
+#     PRIVATE - do not passes any settings to dependent targets,
+#               may be usefull for static libraries from the inside of the project
+# Argument ARGV2 and following are supposed to be names of checked pkg config
+# packages. This function will use variables created by check_pkg_modules().
+#  - ${DEP_NAME}_LIBRARIES
+#  - ${DEP_NAME}_INCLUDE_DIRS
+#  - ${DEP_NAME}_CFLAGS
+#
+FUNCTION(APPLY_PKG_CONFIG TARGET PRIVACY)
+  MATH(EXPR DEST_INDEX "${ARGC}-1")
+  FOREACH(I RANGE 2 ${DEST_INDEX})
+    IF(NOT ${ARGV${I}}_FOUND)
+      MESSAGE(FATAL_ERROR "Not found dependency - ${ARGV${I}}_FOUND")
+    ENDIF(NOT ${ARGV${I}}_FOUND)
+    TARGET_LINK_LIBRARIES(${TARGET} ${PRIVACY} "${${ARGV${I}}_LIBRARIES}")
+    TARGET_INCLUDE_DIRECTORIES(${TARGET} ${PRIVACY} SYSTEM "${${ARGV${I}}_INCLUDE_DIRS}")
+    STRING(REPLACE ";" " " CFLAGS_STR "${${ARGV${I}}_CFLAGS}")
+    SET(CFLAGS_LIST ${CFLAGS_STR})
+    SEPARATE_ARGUMENTS(CFLAGS_LIST)
+    FOREACH(OPTION ${CFLAGS_LIST})
+      TARGET_COMPILE_OPTIONS(${TARGET} ${PRIVACY} ${OPTION})
+    ENDFOREACH(OPTION)
+    SET_TARGET_PROPERTIES(${TARGET} PROPERTIES SKIP_BUILD_RPATH true)
+  ENDFOREACH(I RANGE 2 ${DEST_INDEX})
+ENDFUNCTION(APPLY_PKG_CONFIG TARGET PRIVACY)
diff --git a/mock/ecore_wl2_mock.cc b/mock/ecore_wl2_mock.cc
deleted file mode 100644 (file)
index a244715..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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 "ecore_wl2_mock.h"
-
-#include <gio/gio.h>
-
-#include "mock_hook.h"
-#include "test_fixture.h"
-
-
-extern "C" Eina_Bool ecore_wl2_window_keygrab_set(Ecore_Wl2_Window* win, const char* key, int mod, int not_mod, int priority, Ecore_Wl2_Window_Keygrab_Mode grab_mode) {
-  return MOCK_HOOK_P6(EcoreWl2Mock, ecore_wl2_window_keygrab_set, win, key, mod, not_mod, priority, grab_mode);
-}
-
-extern "C" Ecore_Event_Handler* ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void* data) {
-  return MOCK_HOOK_P3(EcoreWl2Mock, ecore_event_handler_add, type, func, data);
-}
-
-extern "C" void* ecore_event_handler_del(Ecore_Event_Handler* event_handler) {
-  return MOCK_HOOK_P1(EcoreWl2Mock, ecore_event_handler_del, event_handler);
-}
-
-extern "C" Eina_Bool ecore_wl2_window_keygrab_unset(Ecore_Wl2_Window* win, const char* key, int mod, int any_mod) {
-  return MOCK_HOOK_P4(EcoreWl2Mock, ecore_wl2_window_keygrab_unset, win, key, mod, any_mod);
-}
\ No newline at end of file
diff --git a/mock/ecore_wl2_mock.h b/mock/ecore_wl2_mock.h
deleted file mode 100644 (file)
index 3f1a248..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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_ECORE_WL2_MOCK_H_
-#define UNIT_TESTS_MOCK_ECORE_WL2_MOCK_H_
-
-#include <gmock/gmock.h>
-#include <Eina.h>
-#include <Ecore.h>
-#include <Ecore_Wl2.h>
-
-#include "module_mock.h"
-
-typedef Eina_Bool(*Ecore_Event_Handler_Cb)(
-    void* data, int type, void* event);
-
-class EcoreWl2Mock : public virtual ModuleMock {
- public:
-  virtual ~EcoreWl2Mock() {}
-
-  MOCK_METHOD6(ecore_wl2_window_keygrab_set,
-      Eina_Bool (Ecore_Wl2_Window*, const char*, int, int, int, Ecore_Wl2_Window_Keygrab_Mode));
-  MOCK_METHOD3(ecore_event_handler_add,
-      Ecore_Event_Handler* (int, Ecore_Event_Handler_Cb, const void*));
-  MOCK_METHOD1(ecore_event_handler_del,
-      void*(Ecore_Event_Handler*));
-  MOCK_METHOD4(ecore_wl2_window_keygrab_unset,
-      Eina_Bool(Ecore_Wl2_Window*, const char*, int , int));
-};
-
-#endif  // UNIT_TESTS_MOCK_ECORE_WL2_MOCK_H_
diff --git a/mock/mock_hook.h b/mock/mock_hook.h
deleted file mode 100644 (file)
index af27bba..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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<MOCK_CLASS>().f()
-#define MOCK_HOOK_P1(MOCK_CLASS, f, p1)                                        \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1)
-#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2)                                    \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2)
-#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3)                                \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3)
-#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4)                            \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4)
-#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5)                        \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5)
-#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6)                    \
-    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6)
-#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7)                \
-    TestFixture::GetMock<MOCK_CLASS>().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<MOCK_CLASS>().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<MOCK_CLASS>().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
deleted file mode 100644 (file)
index 9b19d89..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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
deleted file mode 100644 (file)
index 02eae1f..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * 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"
-
-std::unique_ptr<ModuleMock> TestFixture::mock_;
diff --git a/mock/test_fixture.h b/mock/test_fixture.h
deleted file mode 100644 (file)
index 1f9d858..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 <gtest/gtest.h>
-
-#include <memory>
-#include <stdexcept>
-#include <string>
-#include <utility>
-
-#include "module_mock.h"
-#include "test_fixture.h"
-
-class TestFixture : public ::testing::Test {
- public:
-  explicit TestFixture(std::unique_ptr<ModuleMock>&& mock) {
-    mock_ = std::move(mock);
-  }
-  virtual ~TestFixture() {
-    mock_.reset();
-  }
-
-  virtual void SetUp() {}
-  virtual void TearDown() {}
-
-  template <typename T>
-  static T& GetMock() {
-    auto ptr = dynamic_cast<T*>(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<ModuleMock> mock_;
-};
-
-#endif  // UNIT_TESTS_MOCK_TEST_FIXTURE_H_
index 6a05a250aaaf85b91c4498850621629526b33a9f..1bcc2cd120549ff7a54c07903d010e3a050ebfe7 100644 (file)
@@ -14,6 +14,7 @@ BuildRequires:  pkgconfig(ecore-input)
 BuildRequires:  pkgconfig(evas)
 BuildRequires:  pkgconfig(ecore-wl2)
 BuildRequires:  pkgconfig(gmock)
+BuildRequires:  pkgconfig(glib-2.0)
 
 %if 0%{?gcov:1}
 BuildRequires:  lcov
@@ -86,7 +87,7 @@ setup() {
 
 test_main() {
     echo "test_main start"
-    /usr/bin/mediakey-unittest
+    /usr/bin/media-key-unittests
 }
 
 teardown() {
@@ -106,7 +107,7 @@ mkdir -p %{buildroot}%{_bindir}/tizen-unittests/%{name}
 install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/%{name}/
 
 %check
-(cd unittest && LD_LIBRARY_PATH=../ ctest -V)
+(cd tests/media-key_unittests && LD_LIBRARY_PATH=../../src ctest -V)
 %if 0%{?gcov:1}
 lcov -c --ignore-errors graph --no-external -q -d . -o media_key.info
 genhtml media_key.info -o media_key.out
@@ -130,7 +131,7 @@ install -m 0644 media_key.zip %{buildroot}%{_datadir}/gcov/obj/
 %{_libdir}/libcapi-system-media-key.so
 
 %files unittests
-%{_bindir}/mediakey-unittest
+%{_bindir}/media-key-unittests
 %{_bindir}/tizen-unittests/%{name}/run-unittest.sh
 
 %if 0%{?gcov:1}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644 (file)
index 0000000..bb5f766
--- /dev/null
@@ -0,0 +1,22 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} MEDIA_KEY_SRCS)
+
+ADD_LIBRARY(${TARGET_MEDIA_KEY} SHARED ${MEDIA_KEY_SRCS})
+
+SET_TARGET_PROPERTIES(${TARGET_MEDIA_KEY} PROPERTIES SOVERSION ${MAJORVER})
+SET_TARGET_PROPERTIES(${TARGET_MEDIA_KEY} PROPERTIES VERSION ${FULLVER})
+
+TARGET_INCLUDE_DIRECTORIES(${TARGET_MEDIA_KEY} PUBLIC
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}/../
+  ${CMAKE_CURRENT_SOURCE_DIR}/../include/)
+
+APPLY_PKG_CONFIG(${TARGET_MEDIA_KEY} PUBLIC
+  DLOG_DEPS
+  CAPI_BASE_COMMON_DEPS
+  ECORE_DEPS
+  ECORE_INPUT_DEPS
+  EVAS_DEPS
+  ECORE_WL2_DEPS
+)
+
+INSTALL(TARGETS ${TARGET_MEDIA_KEY} DESTINATION ${LIB_INSTALL_DIR})
\ No newline at end of file
index d6a933201c068f12c1e08982f602304056d5d974..8ef575dfc6637e92cb074c9a18e198191beaadb9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2020 - 2023 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "include/media_key.h"
+
 #include <Ecore.h>
 #include <Ecore_Input.h>
 #include <Ecore_Wl2.h>
@@ -24,7 +26,6 @@
 #include <string>
 #include <thread>
 
-#include "media_key.h"
 #include "log_private.hh"
 
 #ifdef EXPORT_API
@@ -68,6 +69,7 @@ class MediaKey {
 
     cb_ = nullptr;
     data_ = nullptr;
+    Dispose();
   }
 
  private:
@@ -90,9 +92,11 @@ class MediaKey {
     ecore_wl2_init();
 
     if (!ecore_wl2_display_connect(NULL)) {
+      // LCOV_EXCL_START
       _E("Failed to connect to wl2 display");
       ecore_wl2_shutdown();
       return;
+      // LCOV_EXCL_STOP
     }
 
     disposed_ = false;
@@ -125,12 +129,12 @@ class MediaKey {
     key_down_handler_ = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
         MediaKeyPressCb, this);
     if (!key_down_handler_)
-      _E("Failed to register key down event handler");
+      _E("Failed to register key down event handler");  // LCOV_EXCL_LINE
 
     key_up_handler_ = ecore_event_handler_add(ECORE_EVENT_KEY_UP,
         MediaKeyReleaseCb, this);
     if (!key_up_handler_)
-      _E("Failed to register key up event handler");
+      _E("Failed to register key up event handler");  // LCOV_EXCL_LINE
 
     grabbed_ = true;
     return 0;
@@ -151,7 +155,7 @@ class MediaKey {
       _D("key(%s)", i->first.c_str());
       auto ret = ecore_wl2_window_keygrab_unset(NULL, i->first.c_str(), 0, 0);
       if (ret != EINA_TRUE)
-        _W("Failed to ungrab key(%s)", i->first.c_str());
+        _W("Failed to ungrab key(%s)", i->first.c_str());  // LCOV_EXCL_LINE
     }
 
     grabbed_ = false;
@@ -164,8 +168,10 @@ class MediaKey {
   static Eina_Bool MediaKeyPressCb(void* data, int type, void* event) {
     Evas_Event_Key_Down* ev = static_cast<Evas_Event_Key_Down*>(event);
     if (ev == nullptr) {
+      // LCOV_EXCL_START
       _E("Invalid parameter");
       return ECORE_CALLBACK_RENEW;
+      // LCOV_EXCL_STOP
     }
 
     auto* handle = static_cast<MediaKey*>(data);
@@ -185,8 +191,10 @@ class MediaKey {
   static Eina_Bool MediaKeyReleaseCb(void* data, int type, void* event) {
     Evas_Event_Key_Up* ev = static_cast<Evas_Event_Key_Up*>(event);
     if (ev == nullptr) {
+      // LCOV_EXCL_START
       _E("Invalid parameter");
       return ECORE_CALLBACK_RENEW;
+      // LCOV_EXCL_STOP
     }
 
     auto* handle = static_cast<MediaKey*>(data);
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5c37e6d
--- /dev/null
@@ -0,0 +1 @@
+ADD_SUBDIRECTORY(media-key_unittests)
\ No newline at end of file
diff --git a/tests/media-key_unittests/CMakeLists.txt b/tests/media-key_unittests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..fe3ae40
--- /dev/null
@@ -0,0 +1,31 @@
+ENABLE_TESTING()
+SET(TARGET_MEDIA_KEY_UNITTESTS "media-key-unittests")
+
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock/ MOCK_SRCS)
+
+ADD_EXECUTABLE(${TARGET_MEDIA_KEY_UNITTESTS}
+  ${UNIT_TESTS_SRCS}
+  ${MOCK_SRCS}
+)
+
+TARGET_INCLUDE_DIRECTORIES(${TARGET_MEDIA_KEY_UNITTESTS} PUBLIC
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+  ${CMAKE_CURRENT_SOURCE_DIR}/
+)
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(gtest_pkgs REQUIRED gmock)
+
+TARGET_LINK_LIBRARIES(${TARGET_MEDIA_KEY_UNITTESTS} PUBLIC
+  ${TARGET_MEDIA_KEY})
+
+APPLY_PKG_CONFIG(${TARGET_MEDIA_KEY_UNITTESTS} PUBLIC
+  DLOG_DEPS
+  GLIB_DEPS
+  GMOCK_DEPS
+)
+
+ADD_TEST(NAME ${TARGET_MEDIA_KEY_UNITTESTS} COMMAND ${TARGET_MEDIA_KEY_UNITTESTS})
+
+INSTALL(PROGRAMS ${TARGET_MEDIA_KEY_UNITTESTS} DESTINATION ${BIN_INSTALL_DIR})
diff --git a/tests/media-key_unittests/main.cc b/tests/media-key_unittests/main.cc
new file mode 100644 (file)
index 0000000..89711ff
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include <dlog.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+extern "C" int __dlog_print(log_id_t log_id, int prio, const char* tag,
+                            const char* fmt, ...) {
+  printf("%s:", tag);
+  va_list ap;
+  va_start(ap, fmt);
+  vprintf(fmt, ap);
+  va_end(ap);
+  printf("\n");
+  return 0;
+}
+
+int main(int argc, char** argv) {
+  int ret = -1;
+  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;
+}
diff --git a/tests/media-key_unittests/media_key_tests.cc b/tests/media-key_unittests/media_key_tests.cc
new file mode 100644 (file)
index 0000000..c250ca4
--- /dev/null
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 2020 - 2023 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 <Ecore_Input.h>
+#include <Evas.h>
+#include <gtest/gtest.h>
+#include <glib.h>
+#include <media_key.h>
+
+#include "mock/ecore_wl2_mock.h"
+#include "mock/test_fixture.h"
+
+using ::testing::_;
+using ::testing::Return;
+using ::testing::Invoke;
+
+namespace {
+
+std::vector<std::string> keys = {
+    "XF86AudioPlay",    "XF86AudioStop",      "XF86AudioPause",
+    "XF86AudioNext",    "XF86AudioPrev",      "XF86AudioRewind",
+    "XF86AudioForward", "XF86AudioPlayPause", "XF86AudioMedia" };
+
+int dummy;
+class Mocks : public ::testing::NiceMock<EcoreWl2Mock> {};
+
+class EcoreEventHandler {
+ public:
+  EcoreEventHandler(int type, Ecore_Event_Handler_Cb func, void* user_data)
+      : type_(type), func_(func), user_data_(user_data) {}
+
+  void Invoke() {
+    if (type_ == ECORE_EVENT_KEY_DOWN) {
+      for (int i = 0; i < 9; i++) {
+        Evas_Event_Key_Down event_key_down = {0};
+        event_key_down.keyname = const_cast<char*>(keys[i].c_str());
+        func_(user_data_, type_, &event_key_down);
+      }
+    } else if (type_ == ECORE_EVENT_KEY_UP) {
+      for (int i = 0; i < 9; i++) {
+        Evas_Event_Key_Up event_key_up = { 0 };
+        event_key_up.keyname = const_cast<char*>(keys[i].c_str());
+        func_(user_data_, type_, &event_key_up);
+      }
+    }
+  }
+
+ private:
+  int type_;
+  Ecore_Event_Handler_Cb func_;
+  void* user_data_;
+};
+
+int fake_ecore_wl2_init() {
+  return 1;
+}
+
+int fake_ecore_wl2_shutdown() {
+  return 0;
+}
+
+Ecore_Wl2_Display* fake_ecore_wl2_display_connect(const char* name) {
+  return reinterpret_cast<Ecore_Wl2_Display*>(&dummy);
+}
+
+void fake_ecore_wl2_display_disconnect(Ecore_Wl2_Display* display) {
+}
+
+Ecore_Wl2_Display* fake_ecore_wl2_connected_display_get(const char* name) {
+  return reinterpret_cast<Ecore_Wl2_Display*>(&dummy);
+}
+
+Eina_Bool fake_ecore_wl2_window_keygrab_set(
+    Ecore_Wl2_Window* window, const char* key, int mod, int not_mod,
+    int priority, Ecore_Wl2_Window_Keygrab_Mode grab_mode) {
+  return EINA_TRUE;
+}
+
+Eina_Bool fake_ecore_wl2_window_keygrab_set_return_false(
+    Ecore_Wl2_Window* window, const char* key, int mod, int not_mod,
+    int priority, Ecore_Wl2_Window_Keygrab_Mode grab_mode) {
+  return EINA_FALSE;
+}
+
+Eina_Bool fake_ecore_wl2_window_keygrab_unset(Ecore_Wl2_Window* window,
+                                              const char* key, int mod,
+                                              int any_mod) {
+  return EINA_TRUE;
+}
+
+Ecore_Event_Handler* fake_ecore_event_handler_add(int type,
+                                                  Ecore_Event_Handler_Cb func,
+                                                  const void* data) {
+  auto* event = new EcoreEventHandler(type, func, const_cast<void*>(data));
+  g_idle_add(
+      [](gpointer user_data) {
+        auto* handle = static_cast<EcoreEventHandler*>(user_data);
+        handle->Invoke();
+        delete handle;
+        return G_SOURCE_REMOVE;
+      },
+      event);
+
+  return reinterpret_cast<Ecore_Event_Handler*>(&dummy);
+}
+
+void* fake_ecore_event_handler_del(Ecore_Event_Handler* event_handler) {
+  return reinterpret_cast<void*>(&dummy);
+}
+
+}  // namespace
+
+class MediaKeyTest : public TestFixture {
+ public:
+  MediaKeyTest() : TestFixture(std::make_unique<Mocks>()) {}
+  virtual ~MediaKeyTest() {}
+
+  virtual void SetUp() {
+    EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_init())
+        .WillRepeatedly(Invoke(fake_ecore_wl2_init));
+    EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_shutdown())
+        .WillRepeatedly(Invoke(fake_ecore_wl2_shutdown));
+    EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_connect(_))
+        .WillRepeatedly(Invoke(fake_ecore_wl2_display_connect));
+    EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_display_disconnect(_))
+        .WillRepeatedly(Invoke(fake_ecore_wl2_display_disconnect));
+    EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_wl2_connected_display_get(_))
+        .WillRepeatedly(Invoke(fake_ecore_wl2_connected_display_get));
+
+    loop_ = g_main_loop_new(nullptr, FALSE);
+  }
+
+  virtual void TearDown() {
+    if (loop_ != nullptr) {
+      g_main_loop_unref(loop_);
+      loop_ = nullptr;
+    }
+  }
+
+  void RunMainLoop() {
+    g_main_loop_run(loop_);
+  }
+
+  void QuitMainLoop() {
+    g_main_loop_quit(loop_);
+  }
+
+  int count_ = 0;
+
+ private:
+  GMainLoop* loop_ = nullptr;
+};
+
+static void MediaKeyEventCb(media_key_e key, media_key_event_e status,
+                            void* user_data) {
+  auto* test = static_cast<MediaKeyTest*>(user_data);
+  test->count_++;
+  if (test->count_ == static_cast<int>(keys.size() * 2))
+    test->QuitMainLoop();
+}
+
+TEST_F(MediaKeyTest, media_key_reserve_P) {
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+              ecore_wl2_window_keygrab_set(_, _, _, _, _, _))
+      .Times(9)
+      .WillRepeatedly(Invoke(fake_ecore_wl2_window_keygrab_set));
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_event_handler_add(_, _, _))
+      .Times(2)
+      .WillRepeatedly(Invoke(fake_ecore_event_handler_add));
+
+  count_ = 0;
+  int ret = media_key_reserve(MediaKeyEventCb, this);
+  EXPECT_EQ(ret, MEDIA_KEY_ERROR_NONE);
+  RunMainLoop();
+  EXPECT_EQ(count_, static_cast<int>(keys.size() * 2));
+
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+              ecore_wl2_window_keygrab_unset(_, _, _, _))
+      .Times(9)
+      .WillRepeatedly(Invoke(fake_ecore_wl2_window_keygrab_unset));
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(), ecore_event_handler_del(_))
+      .Times(2)
+      .WillRepeatedly(Invoke(fake_ecore_event_handler_del));
+
+  ret = media_key_release();
+  EXPECT_EQ(ret, MEDIA_KEY_ERROR_NONE);
+}
+
+TEST_F(MediaKeyTest, media_key_reserve_N1) {
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+              ecore_wl2_window_keygrab_set(_, _, _, _, _, _))
+      .Times(1)
+      .WillRepeatedly(Invoke(fake_ecore_wl2_window_keygrab_set_return_false));
+  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
+              ecore_wl2_window_keygrab_unset(_, _, _, _))
+      .Times(9)
+      .WillRepeatedly(Invoke(fake_ecore_wl2_window_keygrab_unset));
+
+  int ret = media_key_reserve(MediaKeyEventCb, this);
+  EXPECT_EQ(ret, MEDIA_KEY_ERROR_OPERATION_FAILED);
+}
+
+TEST_F(MediaKeyTest, media_key_reserve_N2) {
+  int ret = media_key_reserve(nullptr, nullptr);
+  EXPECT_EQ(ret, MEDIA_KEY_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(MediaKeyTest, media_key_release_P) {
+  int ret = media_key_release();
+  EXPECT_EQ(ret, MEDIA_KEY_ERROR_NONE);
+}
diff --git a/tests/media-key_unittests/mock/ecore_wl2_mock.cc b/tests/media-key_unittests/mock/ecore_wl2_mock.cc
new file mode 100644 (file)
index 0000000..7bf16a7
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2020 - 2023 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/ecore_wl2_mock.h"
+
+#include "mock/mock_hook.h"
+#include "mock/test_fixture.h"
+
+extern "C" int ecore_wl2_init() {
+  return MOCK_HOOK_P0(EcoreWl2Mock, ecore_wl2_init);
+}
+
+extern "C" int ecore_wl2_shutdown() {
+  return MOCK_HOOK_P0(EcoreWl2Mock, ecore_wl2_shutdown);
+}
+
+extern "C" Ecore_Wl2_Display* ecore_wl2_display_connect(const char* name) {
+  return MOCK_HOOK_P1(EcoreWl2Mock, ecore_wl2_display_connect, name);
+}
+
+extern "C" void ecore_wl2_display_disconnect(Ecore_Wl2_Display* display) {
+  MOCK_HOOK_P1(EcoreWl2Mock, ecore_wl2_display_disconnect, display);
+}
+
+extern "C" Ecore_Wl2_Display* ecore_wl2_connected_display_get(
+    const char* name) {
+  return MOCK_HOOK_P1(EcoreWl2Mock, ecore_wl2_connected_display_get, name);
+}
+
+extern "C" Eina_Bool ecore_wl2_window_keygrab_set(
+    Ecore_Wl2_Window* win, const char* key, int mod, int not_mod, int priority,
+    Ecore_Wl2_Window_Keygrab_Mode grab_mode) {
+  return MOCK_HOOK_P6(EcoreWl2Mock, ecore_wl2_window_keygrab_set, win, key, mod,
+                      not_mod, priority, grab_mode);
+}
+
+extern "C" Eina_Bool ecore_wl2_window_keygrab_unset(Ecore_Wl2_Window* win,
+                                                    const char* key, int mod,
+                                                    int any_mod) {
+  return MOCK_HOOK_P4(EcoreWl2Mock, ecore_wl2_window_keygrab_unset, win, key,
+                      mod, any_mod);
+}
+
+extern "C" Ecore_Event_Handler* ecore_event_handler_add(
+    int type, Ecore_Event_Handler_Cb func, const void* data) {
+  return MOCK_HOOK_P3(EcoreWl2Mock, ecore_event_handler_add, type, func, data);
+}
+
+extern "C" void* ecore_event_handler_del(Ecore_Event_Handler* event_handler) {
+  return MOCK_HOOK_P1(EcoreWl2Mock, ecore_event_handler_del, event_handler);
+}
diff --git a/tests/media-key_unittests/mock/ecore_wl2_mock.h b/tests/media-key_unittests/mock/ecore_wl2_mock.h
new file mode 100644 (file)
index 0000000..751e135
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2020 - 2023 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 MEDIA_KEY_UNITTESTS_MOCK_ECORE_WL2_MOCK_H_
+#define MEDIA_KEY_UNITTESTS_MOCK_ECORE_WL2_MOCK_H_
+
+#include <Ecore.h>
+#include <Ecore_Wl2.h>
+#include <Eina.h>
+#include <gmock/gmock.h>
+
+#include "mock/module_mock.h"
+
+typedef Eina_Bool(*Ecore_Event_Handler_Cb)(
+    void* data, int type, void* event);
+
+class EcoreWl2Mock : public virtual ModuleMock {
+ public:
+  virtual ~EcoreWl2Mock() {}
+
+  MOCK_METHOD0(ecore_wl2_init, int());
+  MOCK_METHOD0(ecore_wl2_shutdown, int());
+  MOCK_METHOD1(ecore_wl2_display_connect, Ecore_Wl2_Display*(const char*));
+  MOCK_METHOD1(ecore_wl2_display_disconnect, void(Ecore_Wl2_Display*));
+  MOCK_METHOD1(ecore_wl2_connected_display_get,
+               Ecore_Wl2_Display*(const char*));
+  MOCK_METHOD6(ecore_wl2_window_keygrab_set,
+               Eina_Bool(Ecore_Wl2_Window*, const char*, int, int, int,
+                         Ecore_Wl2_Window_Keygrab_Mode));
+  MOCK_METHOD4(ecore_wl2_window_keygrab_unset,
+               Eina_Bool(Ecore_Wl2_Window*, const char*, int, int));
+  MOCK_METHOD3(ecore_event_handler_add,
+               Ecore_Event_Handler*(int, Ecore_Event_Handler_Cb, const void*));
+  MOCK_METHOD1(ecore_event_handler_del, void*(Ecore_Event_Handler*));
+};
+
+#endif  // MEDIA_KEY_UNITTESTS_MOCK_ECORE_WL2_MOCK_H_
diff --git a/tests/media-key_unittests/mock/mock_hook.h b/tests/media-key_unittests/mock/mock_hook.h
new file mode 100644 (file)
index 0000000..03fc8f7
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2020 - 2023 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 MEDIA_KEY_UNITTESTS_MOCK_MOCK_HOOK_H_
+#define MEDIA_KEY_UNITTESTS_MOCK_MOCK_HOOK_H_
+
+#define MOCK_HOOK_P0(MOCK_CLASS, f)                                            \
+    TestFixture::GetMock<MOCK_CLASS>().f()
+#define MOCK_HOOK_P1(MOCK_CLASS, f, p1)                                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1)
+#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2)                                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2)
+#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3)                                \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3)
+#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4)                            \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4)
+#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5)                        \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5)
+#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6)                    \
+    TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6)
+#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7)                \
+    TestFixture::GetMock<MOCK_CLASS>().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<MOCK_CLASS>().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<MOCK_CLASS>().f(                                      \
+        p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
+
+#endif  // MEDIA_KEY_UNITTESTS_MOCK_MOCK_HOOK_H_
diff --git a/tests/media-key_unittests/mock/module_mock.h b/tests/media-key_unittests/mock/module_mock.h
new file mode 100644 (file)
index 0000000..b782b64
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2020 - 2023 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 MEDIA_KEY_UNITTESTS_MOCK_MODULE_MOCK_H_
+#define MEDIA_KEY_UNITTESTS_MOCK_MODULE_MOCK_H_
+
+class ModuleMock {
+ public:
+  virtual ~ModuleMock() {}
+};
+
+#endif  // MEDIA_KEY_UNITTESTS_MOCK_MODULE_MOCK_H_
diff --git a/tests/media-key_unittests/mock/test_fixture.cc b/tests/media-key_unittests/mock/test_fixture.cc
new file mode 100644 (file)
index 0000000..4e66e18
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2020 - 2023 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/test_fixture.h"
+
+std::unique_ptr<ModuleMock> TestFixture::mock_;
diff --git a/tests/media-key_unittests/mock/test_fixture.h b/tests/media-key_unittests/mock/test_fixture.h
new file mode 100644 (file)
index 0000000..dfbf18f
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2020 - 2023 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 MEDIA_KEY_UNITTESTS_MOCK_TEST_FIXTURE_H_
+#define MEDIA_KEY_UNITTESTS_MOCK_TEST_FIXTURE_H_
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include "mock/module_mock.h"
+#include "mock/test_fixture.h"
+
+class TestFixture : public ::testing::Test {
+ public:
+  explicit TestFixture(std::unique_ptr<ModuleMock>&& mock) {
+    mock_ = std::move(mock);
+  }
+  virtual ~TestFixture() {
+    mock_.reset();
+  }
+
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+
+  template <typename T>
+  static T& GetMock() {
+    auto ptr = dynamic_cast<T*>(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<ModuleMock> mock_;
+};
+
+#endif  // MEDIA_KEY_UNITTESTS_MOCK_TEST_FIXTURE_H_
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
deleted file mode 100644 (file)
index 92be4de..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-ENABLE_TESTING()
-SET(TARGET_UNIT_TEST "mediakey-unittest")
-
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS)
-AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock/ UNIT_TESTS_SRCS)
-ADD_EXECUTABLE(${TARGET_UNIT_TEST}
-  ${UNIT_TESTS_SRCS}
-)
-
-TARGET_INCLUDE_DIRECTORIES(${TARGET_UNIT_TEST} PUBLIC
-  "${CMAKE_CURRENT_SOURCE_DIR}/../include"
-  "${CMAKE_CURRENT_SOURCE_DIR}/../mock"
-)
-
-INCLUDE(FindPkgConfig)
-PKG_CHECK_MODULES(gtest_pkgs REQUIRED gmock)
-
-TARGET_LINK_LIBRARIES(${TARGET_UNIT_TEST} PUBLIC ${gtest_pkgs_LDFLAGS} ${PROJECT_NAME})
-
-ADD_TEST(
-  NAME ${TARGET_UNIT_TEST}
-  COMMAND ${TARGET_UNIT_TEST}
-)
-
-INSTALL(PROGRAMS ${TARGET_UNIT_TEST} DESTINATION ${BIN_INSTALL_DIR})
diff --git a/unittest/main.cc b/unittest/main.cc
deleted file mode 100644 (file)
index 5f340cd..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by a apache 2.0 license that can be
-// found in the LICENSE file.
-
-#include <gtest/gtest.h>
-#include <gmock/gmock.h>
-
-int main(int argc, char** argv) {
-  int ret = -1;
-  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;
-}
diff --git a/unittest/media_key_unit_test.cc b/unittest/media_key_unit_test.cc
deleted file mode 100644 (file)
index 4a81a15..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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 <gtest/gtest.h>
-#include <media_key.h>
-#include <Evas.h>
-#include <Ecore_Input.h>
-
-#include "test_fixture.h"
-#include "ecore_wl2_mock.h"
-
-using ::testing::_;
-using ::testing::Return;
-using ::testing::Invoke;
-
-class Mocks : public ::testing::NiceMock<EcoreWl2Mock> {};
-
-class MediaKeyTest : public TestFixture {
- public:
-  MediaKeyTest() : TestFixture(std::make_unique<Mocks>()) {}
-  virtual ~MediaKeyTest() {}
-
-  virtual void SetUp() {
-  }
-
-  virtual void TearDown() {
-  }
-};
-
-Ecore_Event_Handler* __ecore_event_handler_add_fake(int type, Ecore_Event_Handler_Cb func,
-    const void* data) {
-  if (type == ECORE_EVENT_KEY_DOWN || type == ECORE_EVENT_KEY_UP) {
-    func(nullptr, 0, nullptr);
-  }
-  return nullptr;
-}
-
-TEST_F(MediaKeyTest, media_key_reserve_N1) {
-  int ret = media_key_reserve(
-      [](media_key_e key, media_key_event_e status, void* user_data) -> void {
-          }, nullptr);
-  EXPECT_EQ(ret, MEDIA_KEY_ERROR_OPERATION_FAILED);
-}
-
-TEST_F(MediaKeyTest, media_key_reserve_P) {
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      ecore_wl2_window_keygrab_set(_, _, _, _, _, _)).
-          WillRepeatedly(Return(EINA_TRUE));
-  EXPECT_CALL(GetMock<EcoreWl2Mock>(),
-      ecore_event_handler_add(_, _, _)).
-          WillRepeatedly(Invoke(__ecore_event_handler_add_fake));
-
-  int ret = media_key_reserve(
-      [](media_key_e key, media_key_event_e status, void* user_data) -> void {
-          }, nullptr);
-  EXPECT_EQ(ret, MEDIA_KEY_ERROR_NONE);
-}
-
-TEST_F(MediaKeyTest, media_key_reserve_N2) {
-  int ret = media_key_reserve(nullptr, nullptr);
-  EXPECT_EQ(ret, MEDIA_KEY_ERROR_INVALID_PARAMETER);
-}
-
-TEST_F(MediaKeyTest, media_key_release) {
-  int ret = media_key_release();
-  EXPECT_EQ(ret, MEDIA_KEY_ERROR_NONE);
-}
\ No newline at end of file