gtest: Add gtest for ir 37/174137/7
authorpr.jung <pr.jung@samsung.com>
Wed, 28 Mar 2018 11:02:58 +0000 (20:02 +0900)
committerpr.jung <pr.jung@samsung.com>
Fri, 30 Mar 2018 10:16:15 +0000 (19:16 +0900)
Change-Id: I26ae1223e63c650c354a3f152fe6669023ac348a
Signed-off-by: pr.jung <pr.jung@samsung.com>
CMakeLists.txt
hw/common.h
packaging/libdevice-node.spec
unittest/CMakeLists.txt [new file with mode: 0755]
unittest/gtest_hal_ir.cpp [new file with mode: 0644]

index 1aafe716945661c2b4460a02d950e664403ba3b5..0c4e82d2be873a9d05fd909a3848da3ac4b7d39f 100644 (file)
@@ -8,8 +8,8 @@ SET(INCLUDEDIR "${PREFIX}/include/${PROJECT_NAME}")
 SET(VERSION 0.1)
 
 SET(INC_DIR include)
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/${INC_DIR})
+INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})
+INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/${INC_DIR})
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(rpkgs REQUIRED dlog vconf glib-2.0)
@@ -62,3 +62,7 @@ INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hw/ DESTINATION include/hw
 
 CONFIGURE_FILE(hwcommon.pc.in hwcommon.pc @ONLY)
 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hwcommon.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
+
+IF(BUILD_GTESTS STREQUAL on)
+       ADD_SUBDIRECTORY(unittest)
+ENDIF()
index b333532750542af0ff8d21888baa78639ad213b3..76c88f47375971de188053426c4eab9241e63c5f 100644 (file)
 #ifndef __HW_COMMON_H__
 #define __HW_COMMON_H__
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include <stdint.h>
 
 #define MAKE_TAG_CONSTANT(A,B,C,D)     (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))
@@ -82,4 +86,9 @@ int hw_get_info(const char *id, const struct hw_info **info);
        __attribute__ ((visibility("default"))) \
        struct hw_info HARDWARE_INFO_SYM
 
+#ifdef __cplusplus
+}
 #endif
+
+#endif
+
index c4b92730258ef21da51d4ce6148a23380e7fa06a..782d7fd5ed91aaba64b134303b04aad49d834bea 100644 (file)
@@ -10,6 +10,9 @@ BuildRequires:  cmake
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(gio-2.0)
+
+BuildRequires: pkgconfig(gmock)
 
 %description
 development package of library to control OAL APIs
@@ -26,7 +29,10 @@ Library to control OAL APIs (devel)
 %setup -q
 cp %{SOURCE1} .
 %build
-%cmake .
+%cmake -DBUILD_GTESTS=on \
+       .
+       #eol
+
 make %{?jobs:-j%jobs}
 
 %install
@@ -39,6 +45,7 @@ make %{?jobs:-j%jobs}
 %{_libdir}/*.so.*
 %manifest %{name}.manifest
 %license LICENSE.APLv2
+%{_bindir}/gtest*
 
 %files devel
 %{_includedir}/device-node/*.h
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..e3f7305
--- /dev/null
@@ -0,0 +1,40 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(gtest-libdevice-node C CXX)
+
+ADD_DEFINITIONS("-DUSE_DLOG")
+#SET(SRCS
+#      ../hw/common.c
+#      )
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/${INC_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/hw)
+
+SET(REQUIRES_LIST ${REQUIRES_LIST}
+       glib-2.0
+       gio-2.0
+       gmock
+       dlog
+)
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(gtest_pkgs REQUIRED ${REQUIRES_LIST})
+
+FOREACH(flag ${gtest_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -fPIE")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+aux_source_directory(. sources)
+FOREACH(src ${sources})
+    GET_FILENAME_COMPONENT(src_name ${src} NAME_WE)
+    MESSAGE("${src_name}")
+    ADD_EXECUTABLE(${src_name} ${CMAKE_SOURCE_DIR}/hw/common.c ${src})
+    TARGET_LINK_LIBRARIES(${src_name} ${gtest_LDFLAGS} ${gtest_pkgs_LDFLAGS} -ldl)
+    INSTALL(TARGETS ${src_name} DESTINATION bin)
+ENDFOREACH()
+
diff --git a/unittest/gtest_hal_ir.cpp b/unittest/gtest_hal_ir.cpp
new file mode 100644 (file)
index 0000000..144a4c0
--- /dev/null
@@ -0,0 +1,106 @@
+
+#include <iostream>
+#include <gtest/gtest.h>
+#include "hw/common.h"
+#include "hw/ir.h"
+
+using namespace std;
+
+/*
+ * main class
+ */
+struct ir_device *ir_dev;
+
+class IRHalTest : public testing::Test
+{
+       public:
+               virtual void SetUp()
+               {
+                       struct hw_info *info;
+                       int ret;
+                       ret = hw_get_info(IR_HARDWARE_DEVICE_ID,
+                                       (const struct hw_info **)&info);
+
+                       if (ret < 0) {
+                               cout << "Fail to load ir hal(" << ret << ")" << endl;
+                               assert(true);
+                               return;
+                       }
+                       if (!info->open) {
+                               cout << "Failed to open ir device; open(NULL)" << endl;
+                               assert(true);
+                               return;
+                       }
+
+                       ret = info->open(info, NULL, (struct hw_common**)&ir_dev);
+                       if (ret < 0 || !ir_dev) {
+                               cout << "Failed to get ir device structure (" << ret << ")" << endl;
+                               assert(true);
+                               return;
+                       }
+
+                       return;
+               }
+
+               virtual void TearDown()
+               {
+                       struct hw_info *info;
+
+                       info = ir_dev->common.info;
+                       if (!info)
+                               free(ir_dev);
+                       else
+                               info->close((struct hw_common *)ir_dev);
+                       ir_dev = NULL;
+
+                       return;
+               }
+};
+
+/*
+ * testcase
+ */
+TEST_F(IRHalTest, InitP)
+{
+       EXPECT_NE(ir_dev, nullptr);
+}
+
+TEST_F(IRHalTest, DeinitP)
+{
+       struct ir_device *tmp;
+       struct hw_info *info;
+       int ret;
+
+       hw_get_info(IR_HARDWARE_DEVICE_ID,
+                       (const struct hw_info **)&info);
+
+       EXPECT_NE(info, nullptr);
+       EXPECT_NE(info->open, nullptr);
+       info->open(info, NULL, (struct hw_common**)&tmp);
+
+       ret = info->close((struct hw_common *)tmp);
+       EXPECT_EQ(ret, 0);
+}
+
+TEST_F(IRHalTest, IsAvailableP)
+{
+       bool val;
+
+       EXPECT_NE(ir_dev, nullptr);
+       ir_dev->is_available(&val);
+}
+
+TEST_F(IRHalTest, TransmitP)
+{
+       int pattern[5] = {100, 200, 300, 400, 500};
+
+       EXPECT_NE(ir_dev, nullptr);
+       ir_dev->transmit(pattern, 5);
+}
+
+int main(int argc, char **argv)
+{
+       testing::InitGoogleTest(&argc, argv);
+
+       return RUN_ALL_TESTS();
+}