Add support for auto-coverage in Bluetooth-Agent 02/239602/4
authoranuj.bhumiya <anuj.bhumiya@samsung.com>
Tue, 28 Jul 2020 05:27:09 +0000 (10:57 +0530)
committeranuj.bhumiya <anuj.bhumiya@samsung.com>
Fri, 31 Jul 2020 05:03:18 +0000 (10:33 +0530)
Change-Id: I53ed2243221404347d9b045860e1929b87f9b5e1
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
CMakeLists.txt
ag-agent/bluetooth-ag-agent.c
packaging/bluetooth-agent.spec
unittest/CMakeLists.txt
unittest/bluetooth-agent_test.cpp
unittest/mock/bluetooth-agent-mock.c [new file with mode: 0644]
unittest/run_coverage.sh [new file with mode: 0755]

index b7c18fa..6f8fc2f 100644 (file)
@@ -9,6 +9,6 @@ ADD_SUBDIRECTORY(pb-agent)
 ADD_SUBDIRECTORY(ag-agent)
 ADD_SUBDIRECTORY(ipsp-agent)
 ADD_SUBDIRECTORY(hid-agent)
-IF (BUILD_GTESTS)
+IF (BUILD_GCOV)
 ADD_SUBDIRECTORY(unittest)
-ENDIF (BUILD_GTESTS)
+ENDIF (BUILD_GCOV)
\ No newline at end of file
index a3ef15d..70a7c4a 100644 (file)
@@ -1314,7 +1314,7 @@ fail:
 void _bt_ag_agent_get_manufacturer_name(void *device)
 {
        FN_START;
-       char *manufacturer_name;
+       char *manufacturer_name = NULL;
        int ret;
 
        ret = system_info_get_platform_string("http://tizen.org/system/manufacturer",
@@ -1426,7 +1426,7 @@ void _bt_ag_agent_get_creg_status(void *device)
 void _bt_ag_agent_get_model_name(void *device)
 {
        FN_START;
-       char *model_name;
+       char *model_name = NULL;
        int ret;
 
        ret = system_info_get_platform_string("http://tizen.org/system/model_name", &model_name);
@@ -1451,7 +1451,7 @@ void _bt_ag_agent_get_model_name(void *device)
 void _bt_ag_agent_get_revision_information(void *device)
 {
        FN_START;
-       char *revision_info;
+       char *revision_info = NULL;
        int ret;
 
        ret = system_info_get_platform_string("http://tizen.org/system/build.string",
index 953be0e..15e27aa 100644 (file)
@@ -46,7 +46,7 @@ BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  cmake
 Requires: security-config
 %if 0%{?gcov:1}
-BuildRequires:  pkgconfig(gmock)
+BuildRequires:  gtest-devel
 BuildRequires:  lcov
 %endif
 
@@ -163,6 +163,13 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj
 install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %endif
 
+%check
+%if 0%{?gcov:1}
+pushd unittest
+./run_coverage.sh
+popd
+%endif
+
 # This usage of profile macro does NOT conflict 4.0 configurability.
 #%if "%{?profile}" != "mobile" && "%{?profile}" != "tv"
 # Original: wearable, ivi. Added: common, "undefined"
index 951996a..91497fb 100644 (file)
@@ -16,6 +16,8 @@
 # @author
 # @brief       Cmake for tests
 #
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
+
 SET(TARGET_GTEST_TESTS "gtest-bluetooth-agent")
 
 INCLUDE_DIRECTORIES(/usr/include)
@@ -36,7 +38,7 @@ SET(EXEC_PREFIX "\${prefix}")
 SET(INCLUDEDIR "/usr/include")
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(testpkgs REQUIRED dlog glib-2.0 gmock)
+pkg_check_modules(testpkgs REQUIRED dlog msg-service email-service glib-2.0)
 
 INCLUDE_DIRECTORIES(${testpkgs_INCLUDE_DIRS})
 
@@ -44,6 +46,16 @@ SET(GTEST_TESTS_SOURCES
 bluetooth-agent_test.cpp
 )
 
+SET(BLUETOOTH_AGENT_MOCK "bluetooth-agent-mock")
+SET(BLUETOOTH_AGENT_MOCK_SRCS
+       mock/bluetooth-agent-mock.c)
+SET(BLUETOOTH_AGENT_MOCK_CFLAGS "${CMAKE_C_FLAGS}" )
+ADD_LIBRARY(${BLUETOOTH_AGENT_MOCK} SHARED ${BLUETOOTH_AGENT_MOCK_SRCS})
+TARGET_LINK_LIBRARIES(${BLUETOOTH_AGENT_MOCK} -ldl)
+SET_TARGET_PROPERTIES(${BLUETOOTH_AGENT_MOCK} PROPERTIES
+       COMPILE_FLAGS ${BLUETOOTH_AGENT_MOCK_CFLAGS}
+)
+
 ADD_EXECUTABLE(${TARGET_GTEST_TESTS}
        ${GTEST_TESTS_SOURCES}
 )
@@ -51,7 +63,7 @@ ADD_EXECUTABLE(${TARGET_GTEST_TESTS}
 TARGET_LINK_LIBRARIES(${TARGET_GTEST_TESTS}
        ${testpkgs_LDFLAGS}
        ${testpkgs_LIBRARIES}
-       gmock
+       gtest
        -L${CMAKE_SOURCE_DIR}/ag-agent
        -L${CMAKE_SOURCE_DIR}/hf-agent
        -L${CMAKE_SOURCE_DIR}/hid-agent
index a8e48c2..c75d20f 100644 (file)
@@ -20,7 +20,6 @@
  * @brief       Unit-tests setup
  */
 
-#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <unistd.h>
 #include <glib.h>
 /* pb-agent header files */
 #include "bluetooth_pb_vcard.h"
 
-using ::testing::EmptyTestEventListener;
-using ::testing::InitGoogleTest;
-using ::testing::Test;
-using ::testing::TestCase;
-using ::testing::TestEventListeners;
-using ::testing::TestInfo;
-using ::testing::TestPartResult;
-using ::testing::UnitTest;
-
 static GMainLoop *mainloop = NULL;
 
 
@@ -1274,7 +1264,7 @@ TEST(BluetoothPBAgent_test, _bt_is_sim_addressbook) {
 }
 
 int main(int argc, char **argv) {
-  InitGoogleTest(&argc, argv);
+  testing::InitGoogleTest(&argc, argv);
 
   return RUN_ALL_TESTS();
 }
diff --git a/unittest/mock/bluetooth-agent-mock.c b/unittest/mock/bluetooth-agent-mock.c
new file mode 100644 (file)
index 0000000..df90f02
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2020 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.
+ *    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.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <glib.h>
+#include <gio/gio.h>
+
+
+/*Messaging Header Files*/
+#include "msg.h"
+#include "msg_transport_types.h"
+#include "msg_types.h"
+#include "email-types.h"
+
+#ifndef API
+#define API __attribute__ ((visibility("default")))
+#endif
+
+
+API int vconf_get_int(const char *in_key, const int intval)
+{
+       return 0;
+}
+
+API char *vconf_get_str(const char *in_key)
+{
+       return strdup("memory/telephony/nw_name");
+}
+
+API int email_service_end()
+{
+       return EMAIL_ERROR_NONE;
+}
+
+API int msg_open_msg_handle(msg_handle_t *handle)
+{
+       return MSG_SUCCESS;
+}
+
+API int msg_reg_sent_status_callback(msg_handle_t handle, msg_sent_status_cb cb, void *user_param)
+{
+       return MSG_SUCCESS;
+}
+
+API int msg_reg_sms_message_callback(msg_handle_t handle, msg_sms_incoming_cb cb, unsigned short port, void *user_param)
+{
+       return MSG_SUCCESS;
+}
+
+API GDBusConnection *g_bus_get_sync (GBusType bus_type,
+               GCancellable *cancellable,
+               GError **error)
+{
+       return (void *)1234;
+}
+
+API guint g_dbus_connection_register_object (GDBusConnection *connection,
+               const gchar *object_path,
+               GDBusInterfaceInfo *interface_info,
+               const GDBusInterfaceVTable *vtable,
+               gpointer user_data,
+               GDestroyNotify user_data_free_func,
+               GError **error)
+{
+       return 1234;
+}
+
+API guint g_dbus_connection_signal_subscribe (GDBusConnection *connection,
+               const gchar *sender,
+               const gchar *interface_name,
+               const gchar *member,
+               const gchar *object_path,
+               const gchar *arg0,
+               GDBusSignalFlags flags,
+               GDBusSignalCallback callback,
+               gpointer user_data,
+               GDestroyNotify user_data_free_func)
+{
+       return 0;
+}
+API void g_dbus_connection_signal_unsubscribe(GDBusConnection *connection,
+               guint subscription_id)
+{
+}
+
+API gboolean g_dbus_connection_unregister_object(GDBusConnection *connection,
+               guint registration_id)
+{
+       return TRUE;
+}
+
+API void g_object_unref(gpointer object)
+{
+}
+
+API GVariant *g_dbus_proxy_call_sync(GDBusProxy *proxy,
+               const gchar *method_name,
+               GVariant *parameters,
+               GDBusCallFlags flags,
+               gint timeout_msec,
+               GCancellable *cancellable,
+               GError **error)
+{
+       return NULL;
+}
+
+API GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection,
+               GDBusProxyFlags flags,
+               GDBusInterfaceInfo *info,
+               const gchar *name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               GCancellable *cancellable,
+               GError **error)
+{
+       return (void *)1234;
+}
+
+API GVariant *g_variant_get_child_value(GVariant *value,
+               gsize index_)
+{
+       return (void *)1234;
+}
+
+API gsize g_variant_n_children(GVariant *value)
+{
+       return 1234;
+}
+
+API void g_variant_unref(GVariant *value)
+{
+}
+
+API void g_variant_get (GVariant *value,
+               const gchar *format_string,
+               ...)
+{
+}
+
+API gint g_io_channel_unix_get_fd(GIOChannel *channel)
+{
+       return 0;
+}
diff --git a/unittest/run_coverage.sh b/unittest/run_coverage.sh
new file mode 100755 (executable)
index 0000000..dec9df1
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+export LD_LIBRARY_PATH=..:.:../ag-agent:../hf-agent:../hid-agent:../include:../ipsp-agent:../map-agent:../pb-agent:$LD_LIBRARY_PATH
+pushd ../
+RootDir=$PWD
+popd
+
+unitTestFile=gtest-bluetooth-agent
+if [ ! -x  "./$unitTestFile" ]
+then
+    echo "$unitTestFile file does not exist!"
+    exit -1
+fi
+#./$unitTestFile
+
+LD_PRELOAD=./libbluetooth-agent-mock.so ./$unitTestFile
+
+CMakeDir=${RootDir}
+CoverageDir=${RootDir}/coverage
+
+pushd $CMakeDir
+
+    for obj in `ls *.o`
+    do
+        gcov -b -c $obj
+    done
+
+    if [ -f /usr/bin/lcov ]
+    then
+        lcov -c --ignore-errors graph --no-external -b . -d . -o cov.info
+        genhtml cov.info -o ${CoverageDir}
+        echo "Coverage test result created! [${CoverageDir}]"
+    else
+        echo "lcov does not exist!"
+    fi
+popd
+