Add unittests 09/243709/2
authorInkyun Kil <inkyun.kil@samsung.com>
Wed, 9 Sep 2020 08:15:22 +0000 (17:15 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Mon, 14 Sep 2020 05:06:49 +0000 (14:06 +0900)
Change-Id: I618a062779b65ab944eec7e509d7f104e6510ab5
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
CMakeLists.txt
mock/ecore_wl2_mock.cc [new file with mode: 0644]
mock/ecore_wl2_mock.h [new file with mode: 0644]
mock/mock_hook.h [new file with mode: 0644]
mock/module_mock.h [new file with mode: 0644]
mock/test_fixture.cc [new file with mode: 0644]
mock/test_fixture.h [new file with mode: 0644]
packaging/capi-system-media-key.spec
unittest/CMakeLists.txt [new file with mode: 0644]
unittest/main.cc [new file with mode: 0644]
unittest/media_key_unit_test.cc [new file with mode: 0644]

index 37a99b58016bced7989d5077dfc57d5d42886412..7243e0c95f1b5852c1556aa22dffb10599c2c539 100644 (file)
@@ -13,7 +13,7 @@ FOREACH(flag ${capi-system-media-key_CFLAGS})
 ENDFOREACH(flag)
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline")
 
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++11")
+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")
 
@@ -40,3 +40,5 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_I
 INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
         DESTINATION include/system
         FILES_MATCHING PATTERN "*.h")
+
+ADD_SUBDIRECTORY(unittest)
\ No newline at end of file
diff --git a/mock/ecore_wl2_mock.cc b/mock/ecore_wl2_mock.cc
new file mode 100644 (file)
index 0000000..a244715
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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
new file mode 100644 (file)
index 0000000..3f1a248
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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
new file mode 100644 (file)
index 0000000..af27bba
--- /dev/null
@@ -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<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
new file mode 100644 (file)
index 0000000..9b19d89
--- /dev/null
@@ -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 (file)
index 0000000..02eae1f
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * 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
new file mode 100644 (file)
index 0000000..1f9d858
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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 792ce47890207ca396efe5c8d84e708ab1c2e788..d24fd85eb7fa0c2cc427df5e8427a4b0a023f28b 100644 (file)
@@ -13,6 +13,12 @@ BuildRequires:  pkgconfig(ecore)
 BuildRequires:  pkgconfig(ecore-input)
 BuildRequires:  pkgconfig(evas)
 BuildRequires:  pkgconfig(ecore-wl2)
+BuildRequires:  pkgconfig(gmock)
+
+%if 0%{?gcov:1}
+BuildRequires:  lcov
+BuildRequires:  zip
+%endif
 
 %description
 %{summary}.
@@ -68,6 +74,15 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj
 install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %endif
 
+%check
+(cd unittest && LD_LIBRARY_PATH=../ 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
+zip -r media_key.zip media_key.out
+install -m 0644 media_key.zip %{buildroot}%{_datadir}/gcov/
+%endif
+
 %post -p /sbin/ldconfig
 
 %postun -p /sbin/ldconfig
@@ -85,5 +100,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/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..dd9f809
--- /dev/null
@@ -0,0 +1,23 @@
+ENABLE_TESTING()
+SET(TARGET_UNIT_TEST "mediakey-unit-test")
+
+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}
+)
diff --git a/unittest/main.cc b/unittest/main.cc
new file mode 100644 (file)
index 0000000..5f340cd
--- /dev/null
@@ -0,0 +1,25 @@
+// 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
new file mode 100644 (file)
index 0000000..4a81a15
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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