Add unittests 65/235565/6
authorJihoon Kim <jihoon48.kim@samsung.com>
Fri, 5 Jun 2020 11:43:59 +0000 (20:43 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Sat, 6 Jun 2020 05:28:33 +0000 (14:28 +0900)
Change-Id: I232e9353ff10628c56b2239385d41b9a24444fe5
Signed-off-by: Jihoon Kim <jihoon48.kim@samsung.com>
CMakeLists.txt
packaging/capi-ui-inputmethod-manager.spec
tests/CMakeLists.txt [new file with mode: 0644]
tests/src/cynara_mock.cpp [new file with mode: 0644]
tests/src/cynara_mock.h [new file with mode: 0644]
tests/src/inputmethod_manager_unittests.cpp [new file with mode: 0644]
tests/src/isf_control_mock.cpp [new file with mode: 0644]
tests/src/isf_control_mock.h [new file with mode: 0644]
tests/src/main.cpp [new file with mode: 0644]

index 81bd51b..8e6cf7e 100644 (file)
@@ -134,3 +134,13 @@ ADD_CUSTOM_COMMAND(
 )
 
 ENDIF(UNIX)
+
+## Test
+IF(NOT DEFINED MINIMUM_BUILD)
+ENABLE_TESTING()
+SET(UNITTEST_INPUTMETHOD_MANAGER inputmethod_manager_unittests)
+ADD_TEST(NAME ${UNITTEST_INPUTMETHOD_MANAGER} COMMAND ${UNITTEST_INPUTMETHOD_MANAGER}
+                        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests)
+
+ADD_SUBDIRECTORY(tests)
+ENDIF(NOT DEFINED MINIMUM_BUILD)
index 01a0805..f82d59a 100644 (file)
@@ -11,9 +11,15 @@ BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(isf)
 BuildRequires:  pkgconfig(cynara-client)
 BuildRequires:  pkgconfig(cynara-session)
+BuildRequires:  pkgconfig(gmock)
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
 
+%if 0%{?gcov:1}
+BuildRequires:  lcov
+BuildRequires:  zip
+%endif
+
 %description
 Input Method Manager Library
 
@@ -33,6 +39,13 @@ Group:    Graphics & UI Framework/Input
 Input Method gcov objects
 %endif
 
+%package unittests
+Summary:    inputmethod manager tests
+Group:      Development/Libraries
+Requires:   %{name} = %{version}-%{release}
+
+%description unittests
+GTest for inputmethod manager
 
 %prep
 %setup -q
@@ -71,6 +84,13 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj
 install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %endif
 
+%check
+ctest -V --output-on-failure %{?_smp_mflags}
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -q -d . -o gcov.info
+genhtml gcov.info
+%endif
+
 %post -p /sbin/ldconfig
 
 %postun -p /sbin/ldconfig
@@ -90,3 +110,6 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %files gcov
 %{_datadir}/gcov/obj/*
 %endif
+
+%files unittests
+%{_bindir}/*
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..eb87d65
--- /dev/null
@@ -0,0 +1,52 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(gtest-inputmethod-manager CXX)
+
+# Find Packages
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs REQUIRED
+       dlog
+       capi-base-common
+       gmock
+)
+
+FOREACH(flag ${pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIE")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++11")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+
+SET(SOURCES "")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
+
+AUX_SOURCE_DIRECTORY(src SOURCES)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../src INPUTMETHOD_MANAGER_SOURCES)
+
+ADD_DEFINITIONS("-DFULLVER=\"${FULLVER}\"")
+
+ADD_EXECUTABLE(${UNITTEST_INPUTMETHOD_MANAGER}
+       ${INPUTMETHOD_MANAGER_SOURCES}
+       ${SOURCES}
+       )
+TARGET_LINK_LIBRARIES(${UNITTEST_INPUTMETHOD_MANAGER} ${GTEST_LIBRARIES} ${pkgs_LDFLAGS} ${EXTRA_LDFLAGS})
+SET_TARGET_PROPERTIES(${UNITTEST_INPUTMETHOD_MANAGER} PROPERTIES
+       COMPILE_FLAGS "-fPIE"
+       #Never add any space for LINKFLAGS
+       LINK_FLAGS "-Wl,\
+--wrap=cynara_initialize,\
+--wrap=cynara_finish,\
+--wrap=cynara_session_from_pid,\
+--wrap=cynara_check,\
+--wrap=isf_control_show_ime_list,\
+--wrap=isf_control_show_ime_selector,\
+--wrap=isf_control_is_ime_enabled,\
+--wrap=isf_control_get_all_ime_info,\
+--wrap=isf_control_get_active_ime,\
+--wrap=isf_control_prelaunch_ise")
+INSTALL(TARGETS ${UNITTEST_INPUTMETHOD_MANAGER} DESTINATION /usr/bin)
diff --git a/tests/src/cynara_mock.cpp b/tests/src/cynara_mock.cpp
new file mode 100644 (file)
index 0000000..d553442
--- /dev/null
@@ -0,0 +1,34 @@
+#include "cynara_mock.h"
+#include <string.h>
+#include <stdio.h>
+
+static int check_result = CYNARA_API_ACCESS_ALLOWED;
+
+void cynara_check_set_result(int result)
+{
+    check_result = result;
+}
+
+int __wrap_cynara_initialize(cynara** c, const cynara_configuration* conf)
+{
+    *c = (void *)0x1;
+    return 0;
+}
+
+int __wrap_cynara_finish(cynara* c)
+{
+    return 0;
+}
+
+int __wrap_cynara_check(cynara* c, const char* client, const char* client_session,
+    const char* user,
+    const char* privilege)
+{
+    //return CYNARA_API_ACCESS_ALLOWED;
+    return check_result;
+}
+
+char *__wrap_cynara_session_from_pid(pid_t pid)
+{
+    return strdup("session");
+}
diff --git a/tests/src/cynara_mock.h b/tests/src/cynara_mock.h
new file mode 100644 (file)
index 0000000..ca302bc
--- /dev/null
@@ -0,0 +1,87 @@
+#ifndef MOCK_CYNARA_H_
+#define MOCK_CYNARA_H_
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void cynara;
+typedef void* cynara_configuration;
+
+/*! \brief  indicating that API call was interrupted by user*/
+#define CYNARA_API_INTERRUPTED                  4
+
+/*! \brief  indicating access that cannot be resolved without further actions*/
+#define CYNARA_API_ACCESS_NOT_RESOLVED          3
+
+/*! \brief   indicating access that was checked is allowed */
+#define CYNARA_API_ACCESS_ALLOWED               2
+
+/*! \brief   indicating that access that was checked is denied */
+#define CYNARA_API_ACCESS_DENIED                1
+
+/*! \brief   indicating the result of the one specific API is successful */
+#define CYNARA_API_SUCCESS                      0
+
+/*! \brief   indicating that value is not present in cache */
+#define CYNARA_API_CACHE_MISS                   -1
+
+/*! \brief   indicating that pending requests reached maximum */
+#define CYNARA_API_MAX_PENDING_REQUESTS         -2
+
+/*! \brief   indicating system is running out of memory state */
+#define CYNARA_API_OUT_OF_MEMORY                -3
+
+/*! \brief   indicating the API's parameter is malformed */
+#define CYNARA_API_INVALID_PARAM                -4
+
+/*! \brief   indicating that service is not available */
+#define CYNARA_API_SERVICE_NOT_AVAILABLE        -5
+
+/*! \brief   indicating that provided method is not supported by library */
+#define CYNARA_API_METHOD_NOT_SUPPORTED         -6
+
+/*! \brief   cynara service does not allow to perform requested operation */
+#define CYNARA_API_OPERATION_NOT_ALLOWED        -7
+
+/*! \brief   cynara service failed to perform requested operation */
+#define CYNARA_API_OPERATION_FAILED             -8
+
+/*! \brief   cynara service hasn't found requested bucket */
+#define CYNARA_API_BUCKET_NOT_FOUND             -9
+
+/*! \brief   indicating an unknown error */
+#define CYNARA_API_UNKNOWN_ERROR                -10
+
+/*! \brief   indicating configuration error */
+#define CYNARA_API_CONFIGURATION_ERROR          -11
+
+/*! \brief   indicating invalid parameter in command-line */
+#define CYNARA_API_INVALID_COMMANDLINE_PARAM    -12
+
+/*! \brief   indicating that provided buffer is too short */
+#define CYNARA_API_BUFFER_TOO_SHORT             -13
+
+/*! \brief   indicating that database is corrupted */
+#define CYNARA_API_DATABASE_CORRUPTED           -14
+
+/*! \brief   indicating that user doesn't have enough permission to perform action */
+#define CYNARA_API_PERMISSION_DENIED            -15
+
+void cynara_check_set_result(int result);
+
+int __wrap_cynara_initialize(cynara** c, const cynara_configuration* conf);
+int __wrap_cynara_finish(cynara* c);
+int __wrap_cynara_check(cynara* c, const char* client, const char* client_session,
+    const char* user,
+    const char* privilege);
+
+char *__wrap_cynara_session_from_pid(pid_t pid);
+
+#ifdef __cplusplus
+}
+#endif
+#endif  /* MOCK_CYNARA_H_ */
diff --git a/tests/src/inputmethod_manager_unittests.cpp b/tests/src/inputmethod_manager_unittests.cpp
new file mode 100644 (file)
index 0000000..eca3ae0
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * 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 <inputmethod_manager.h>
+#include "cynara_mock.h"
+#include "isf_control_mock.h"
+
+namespace {
+
+#define IME_APPID "ise-default"
+
+class InputMethodManagerTest : public testing::Test {
+    public:
+        virtual void SetUp() {
+            cynara_check_set_result(CYNARA_API_ACCESS_ALLOWED);
+        }
+        virtual void TearDown() {
+        }
+};
+
+class InputMethodManagerDeniedTest : public testing::Test {
+    public:
+        virtual void SetUp() {
+            cynara_check_set_result(CYNARA_API_ACCESS_DENIED);
+        }
+        virtual void TearDown() {
+            cynara_check_set_result(CYNARA_API_ACCESS_ALLOWED);
+        }
+};
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_get_active_ime_p)
+{
+    char *app_id = nullptr;
+    int ret = ime_manager_get_active_ime(&app_id);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_NONE);
+    EXPECT_NE(app_id, nullptr);
+
+    if (app_id)
+        free(app_id);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_get_active_ime_invalid_parameter)
+{
+    int ret = ime_manager_get_active_ime(NULL);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(InputMethodManagerDeniedTest, utc_ime_manager_get_active_ime_permission_denied)
+{
+    int ret = ime_manager_get_active_ime(NULL);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_PERMISSION_DENIED);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_is_ime_enabled_p)
+{
+    bool enabled;
+    int ret = ime_manager_is_ime_enabled(IME_APPID, &enabled);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_NONE);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_is_ime_enabled_invalid_parameter)
+{
+    bool enabled;
+    int ret = ime_manager_is_ime_enabled(NULL, &enabled);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_is_ime_enabled_invalid_parameter2)
+{
+    int ret = ime_manager_is_ime_enabled(IME_APPID, NULL);
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_show_ime_list_p)
+{
+    int ret = ime_manager_show_ime_list();
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_NONE);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_show_ime_selector_p)
+{
+    int ret = ime_manager_show_ime_selector();
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_NONE);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_get_enabled_ime_count_p)
+{
+    int count = ime_manager_get_enabled_ime_count();
+    EXPECT_GT(count, 0);
+}
+
+TEST_F(InputMethodManagerDeniedTest, utc_ime_manager_get_enabled_ime_count_permission_denied)
+{
+    int count = ime_manager_get_enabled_ime_count();
+    EXPECT_EQ(count, 0);
+}
+
+TEST_F(InputMethodManagerTest, utc_ime_manager_prelaunch_ime_p)
+{
+    int ret = ime_manager_prelaunch_ime();
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_NONE);
+}
+
+TEST_F(InputMethodManagerDeniedTest, utc_ime_manager_prelaunch_ime_permission_denied)
+{
+    int ret = ime_manager_prelaunch_ime();
+    EXPECT_EQ(ret, IME_MANAGER_ERROR_PERMISSION_DENIED);
+}
+
+} // namespace
diff --git a/tests/src/isf_control_mock.cpp b/tests/src/isf_control_mock.cpp
new file mode 100644 (file)
index 0000000..4f45059
--- /dev/null
@@ -0,0 +1,54 @@
+#include "isf_control_mock.h"
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int __wrap_isf_control_show_ime_list()
+{
+    return 0;
+}
+
+int __wrap_isf_control_show_ime_selector()
+{
+    return 0;
+}
+
+int __wrap_isf_control_is_ime_enabled(const char *appid, bool *enabled)
+{
+    if (enabled)
+        *enabled = true;
+
+    return 0;
+}
+
+int __wrap_isf_control_get_all_ime_info (ime_info_s **info)
+{
+    ime_info_s *ime_info;
+    int count = 1;
+    ime_info = (ime_info_s *)calloc (count, sizeof (ime_info_s));
+    if (ime_info) {
+        for (int i = 0; i < count; i++) {
+            snprintf(ime_info[i].appid, sizeof (ime_info[i].appid), "%s", "tizenime");
+            snprintf(ime_info[i].label, sizeof (ime_info[i].label), "%s", "Tizen IME");
+            ime_info[i].is_enabled = true;
+            ime_info[i].is_preinstalled = true;
+            ime_info[i].has_option = false;
+        }
+        *info = ime_info;
+    }
+
+    return count;
+}
+
+int __wrap_isf_control_get_active_ime(char **app_id)
+{
+    if (app_id)
+        *app_id = strdup("defaultIME");
+
+    return 0;
+}
+
+int __wrap_isf_control_prelaunch_ise(void)
+{
+    return 0;
+}
diff --git a/tests/src/isf_control_mock.h b/tests/src/isf_control_mock.h
new file mode 100644 (file)
index 0000000..b700ebb
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef MOCK_ISF_CONTROL_H_
+#define MOCK_ISF_CONTROL_H_
+
+#include <sys/types.h>
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+    char appid[128];
+    char label[256];
+    bool is_enabled;
+    bool is_preinstalled;
+    int has_option; // 0: no keyboard option, 1: keyboard option is available, -1: unknown yet
+} ime_info_s;
+
+int __wrap_isf_control_show_ime_list();
+int __wrap_isf_control_show_ime_selector();
+int __wrap_isf_control_is_ime_enabled(const char *appid, bool *enabled);
+int __wrap_isf_control_get_all_ime_info (ime_info_s **info);
+int __wrap_isf_control_get_active_ime(char **app_id);
+int __wrap_isf_control_prelaunch_ise(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif  /* MOCK_ISF_CONTROL_H_ */
diff --git a/tests/src/main.cpp b/tests/src/main.cpp
new file mode 100644 (file)
index 0000000..a37d671
--- /dev/null
@@ -0,0 +1,7 @@
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+int main(int argc, char** argv) {
+    testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}