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")
INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION include/system
FILES_MATCHING PATTERN "*.h")
+
+ADD_SUBDIRECTORY(unittest)
\ No newline at end of file
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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_
--- /dev/null
+/*
+ * 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_
--- /dev/null
+/*
+ * 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_
--- /dev/null
+/*
+ * 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_;
--- /dev/null
+/*
+ * 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_
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}.
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
%if 0%{?gcov:1}
%files gcov
-%{_datadir}/gcov/obj/*
+%{_datadir}/gcov/*
%endif
--- /dev/null
+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}
+)
--- /dev/null
+// 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;
+}
--- /dev/null
+/*
+ * 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