Add haltest 94/256794/7 submit/tizen/20210419.065426
authorYunmi Ha <yunmi.ha@samsung.com>
Tue, 13 Apr 2021 06:34:55 +0000 (15:34 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Fri, 16 Apr 2021 04:43:50 +0000 (13:43 +0900)
Change-Id: I71919c82656d07c14990795a2b6f099cb4104150
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
haltest/CMakeLists.txt [new file with mode: 0644]
haltest/main.cpp [new file with mode: 0644]
haltest/sensor-device.cpp [new file with mode: 0644]
haltest/sensor-haltest.h [new file with mode: 0644]
include/hal-sensor-types.h
packaging/hal-api-sensor.spec

index 697f63e..1b3e104 100644 (file)
@@ -56,3 +56,7 @@ INSTALL(
        FILES_MATCHING PATTERN "*.h"
 )
 INSTALL(FILES          ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIBDIR}/pkgconfig)
+
+IF(HALTEST STREQUAL on)
+       ADD_SUBDIRECTORY(haltest)
+ENDIF()
diff --git a/haltest/CMakeLists.txt b/haltest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..97c237c
--- /dev/null
@@ -0,0 +1,46 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(sensor-haltests CXX)
+
+SET(HALAPI_LIBRARY "hal-api-sensor")
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/${INC_DIR})
+
+SET(REQUIRES_LIST ${REQUIRES_LIST}
+    hal-api-common
+    glib-2.0
+    gio-2.0
+    gmock
+    dlog
+)
+
+FIND_LIBRARY(
+    HALAPI_LIBRARY
+    NAMES libhal-api-sensor.so
+    HINTS /usr/lib/hal /usr/lib64/hal
+    REQUIRED)
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(gtest_pkgs REQUIRED ${REQUIRES_LIST})
+
+SET(GTEST_VERSION ${gtest_pkgs_gmock_VERSION})
+STRING(REPLACE "." ";" GTEST_VERSION ${GTEST_VERSION})
+LIST(GET GTEST_VERSION 0 GTEST_VERSION_MAJOR)
+LIST(GET GTEST_VERSION 1 GTEST_VERSION_MINOR)
+ADD_DEFINITIONS("-DGTEST_VERSION_MAJOR=${GTEST_VERSION_MAJOR}")
+ADD_DEFINITIONS("-DGTEST_VERSION_MINOR=${GTEST_VERSION_MINOR}")
+
+FOREACH(flag ${gtest_pkgs_CFLAGS})
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=gnu++0x")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--gc-sections -pie")
+
+FILE(GLOB HALTESTS "*.cpp")
+
+SET(SRCS ${CMAKE_SOURCE_DIR}/haltest/main.cpp ${HALTESTS})
+ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${gtest_LDFLAGS} ${gtest_pkgs_LDFLAGS} -ldl ${HALAPI_LIBRARY})
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/hal)
diff --git a/haltest/main.cpp b/haltest/main.cpp
new file mode 100644 (file)
index 0000000..287c9f4
--- /dev/null
@@ -0,0 +1,60 @@
+#include "sensor-haltest.h"
+#include "hal-sensor.h"
+
+#define CHECK_NODEV(ret_val) \
+    if (ret_val == -ENODEV) { \
+        SKIP_MESSAGE("Not supported HAL."); \
+        return ; \
+    }
+
+class SENSOR_API : public testing::Test {
+protected:
+       static void SetUpHalTestSuite() {
+       }
+
+       void SetUp() override {}
+       void TearDown() override {}
+};
+
+TEST_F(SENSOR_API, GetBackendP)
+{
+       int ret_val;
+
+       ret_val = hal_sensor_get_backend();
+       EXPECT_EQ(ret_val, 0) << strerr("Failed to get sensor device", ret_val);
+}
+
+TEST_F(SENSOR_API, CreateP)
+{
+       int ret_val;
+
+       void **devices;
+       ret_val = hal_sensor_create(&devices);
+       if (ret_val == -ENODEV) {
+               SKIP_MESSAGE("Not supported HAL");
+               return ;
+       }
+       ASSERT_GT(ret_val, 0) << strerr("Failed to call create", ret_val);
+       ASSERT_NE(devices[0], nullptr) << "Opened devices are invalid.";
+}
+
+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/haltest/sensor-device.cpp b/haltest/sensor-device.cpp
new file mode 100644 (file)
index 0000000..93089e4
--- /dev/null
@@ -0,0 +1,173 @@
+#include <memory>
+
+#include "sensor-haltest.h"
+#include "hal-sensor.h"
+#include "hal-sensor-types.h"
+
+typedef std::vector<std::shared_ptr<sensor_device>> device_registry_t;
+
+class SENSOR_DEV :  public testing::Test {
+public:
+       static int device_count;
+       static device_registry_t devices;
+
+       static void SetUpHalTestSuite() {
+               void **sensors = nullptr;
+
+               device_count = hal_sensor_create(&sensors);
+               ASSERT_GT(device_count, 0) << "Failed to get sensor devices";
+
+               DEBUG_MESSAGE("Setup test suite : Create %d sensor devices", device_count);
+               for (int i = 0; i < device_count; i++) {
+                       devices.emplace_back(static_cast<sensor_device *>(sensors[i]));
+               }
+       }
+
+       void SetUp() override{}
+       void TearDown() override{}
+};
+
+int SENSOR_DEV::device_count = 0;
+device_registry_t SENSOR_DEV::devices;
+
+#define SKIP_NO_DEV() \
+       if (device_count <= 0) { \
+               SKIP_MESSAGE("There is no sensor devices."); \
+               return; \
+       }
+
+TEST_F(SENSOR_DEV, get_poll_fdP)
+{
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_NE((*it)->get_poll_fd(), 0);
+       }
+}
+
+TEST_F(SENSOR_DEV, get_sensorsP)
+{
+       const sensor_info_t *sensors = nullptr;
+       int ret_func;
+
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               ret_func = (*it)->get_sensors(&sensors);
+               EXPECT_GE(ret_func, 1);
+               EXPECT_NE(sensors, nullptr);
+       }
+}
+
+TEST_F(SENSOR_DEV, get_sensorsN)
+{
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->get_sensors(nullptr), SENSOR_ERROR_INVALID_PARAMETER);
+       }
+}
+/*
+TEST_F(SENSOR_DEV, enableP)
+{
+       const sensor_info_t *sensors = nullptr;
+
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_GE((*it)->get_sensors(&sensors), 1);
+               (*it)->disable(sensors[0].id);
+               EXPECT_EQ((*it)->enable(sensors[0].id), true);
+       }
+}
+
+TEST_F(SENSOR_DEV, enableN)
+{
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->enable(0), false);
+               EXPECT_EQ((*it)->enable(10), false);
+       }
+}
+
+TEST_F(SENSOR_DEV, disableP)
+{
+       const sensor_info_t *sensors = nullptr;
+
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_GE((*it)->get_sensors(&sensors), 1);
+               EXPECT_EQ((*it)->disable(sensors[0].id), true);
+    }
+
+}
+
+TEST_F(SENSOR_DEV, disableN)
+{
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->disable(0), false);
+               EXPECT_EQ((*it)->disable(10), false);
+       }
+}
+*/
+/*
+TEST_F(SENSOR_DEV, read_fdP)
+{
+       uint32_t *id = nullptr;
+       int ret_func = 0;
+
+       SKIP_NO_DEV();
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->enable(1), true);
+
+               ret_func = (*it)->read_fd(&id);
+               EXPECT_NE(id, nullptr);
+               EXPECT_NE(ret_func, 0);
+       }
+}
+
+TEST_F(SENSOR_DEV, read_fdN)
+{
+
+       SKIP_NO_DEV();
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->read_fd(nullptr), SENSOR_ERROR_INVALID_PARAMETER);
+       }
+}
+*/
+TEST_F(SENSOR_DEV, get_dataP)
+{
+       int length = 0;
+       int ret_func = 0;
+       sensor_data_t *data = NULL;
+       const sensor_info_t *sensors = nullptr;
+
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_GE((*it)->get_sensors(&sensors), 1);
+
+               ret_func = (*it)->get_data(sensors[0].id, &data, &length);
+               EXPECT_EQ(ret_func, 0);
+       }
+}
+
+TEST_F(SENSOR_DEV, get_dataN)
+{
+       int length = 0;
+       sensor_data_t *data = NULL;
+
+       SKIP_NO_DEV()
+
+       for (auto it = devices.begin(); it != devices.end(); ++it) {
+               EXPECT_EQ((*it)->get_data(0, &data, &length), SENSOR_ERROR_INVALID_PARAMETER);
+               EXPECT_EQ((*it)->get_data(1, nullptr, &length), SENSOR_ERROR_INVALID_PARAMETER);
+               EXPECT_EQ((*it)->get_data(1, &data, nullptr), SENSOR_ERROR_INVALID_PARAMETER);
+       }
+}
diff --git a/haltest/sensor-haltest.h b/haltest/sensor-haltest.h
new file mode 100644 (file)
index 0000000..033fb75
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+#ifndef __SENSOR_HALTEST_H__
+#define __SENSOR_HALTEST_H__
+
+#include <gtest/gtest.h>
+#include <string.h>
+
+// hack gtest internal for print message
+namespace testing
+{
+    namespace internal
+    {
+        enum GTestColor {
+            COLOR_DEFAULT,
+            COLOR_RED,
+            COLOR_GREEN,
+            COLOR_YELLOW
+        };
+
+        extern void ColoredPrintf(GTestColor color, const char* fmt, ...);
+    }
+}
+
+#define SKIP_MESSAGE(fmt, args...) \
+do { \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, "[  SKIPPED ] "); \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, fmt, ##args); \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, "\n"); \
+} while (0)
+
+#define DEBUG_MESSAGE(fmt, args...) \
+do { \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[    DEBUG ] "); \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_DEFAULT, fmt, ##args); \
+    testing::internal::ColoredPrintf(testing::internal::COLOR_DEFAULT, "\n"); \
+} while (0)
+
+// SetUp/TearDown function for a testsuite differ by gtest version
+#if (GTEST_VERSION_MAJOR >= 2 || (GTEST_VERSION_MAJOR == 1 && GTEST_VERSION_MINOR >= 10))
+#define SetUpHalTestSuite    SetUpTestSuite
+#define TearDownHalTestSuite TearDownTestSuite
+#else
+#define SetUpHalTestSuite    SetUpTestCase
+#define TearDownHalTestSuite TearDownTestCase
+#endif
+
+#define BUFMAX  256
+static char errbuf1[BUFMAX];
+
+static inline const char* strerr(const char* message, int eno)
+{
+    if (eno == 0)
+        return message;
+
+    if (eno < 0)
+        eno = -eno;
+
+    if (message)
+        snprintf(errbuf1, BUFMAX, "%s (%s)", message, strerror(eno));
+    else
+        snprintf(errbuf1, BUFMAX, "%s", strerror(eno));
+
+    return errbuf1;
+}
+
+#endif /* __SENSOR_HALTEST_H__ */
index fcba0cd..d373f6f 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <time.h>
+#include <tizen.h>
 
 #define SENSOR_HAL_VERSION(maj, min) ((((maj)&0xFFFF) << 24) | ((min)&0xFFFF))
 
@@ -155,6 +156,25 @@ typedef enum {
   SENSOR_DEVICE_PPG,
 } sensor_device_type;
 
+/**
+ * @brief   Enumeration for errors.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef enum {
+    SENSOR_ERROR_NONE                  = TIZEN_ERROR_NONE,                 /**< Successful */
+    SENSOR_ERROR_IO_ERROR              = TIZEN_ERROR_IO_ERROR,             /**< I/O error */
+    SENSOR_ERROR_INVALID_PARAMETER     = TIZEN_ERROR_INVALID_PARAMETER,    /**< Invalid parameter */
+    SENSOR_ERROR_NOT_SUPPORTED         = TIZEN_ERROR_NOT_SUPPORTED,        /**< Not supported */
+    SENSOR_ERROR_PERMISSION_DENIED     = TIZEN_ERROR_PERMISSION_DENIED,    /**< Permission denied */
+    SENSOR_ERROR_OUT_OF_MEMORY         = TIZEN_ERROR_OUT_OF_MEMORY,        /**< Out of memory */
+    SENSOR_ERROR_NO_DATA               = TIZEN_ERROR_NO_DATA,              /**< No data available
+                                                                                @if MOBILE (Since 3.0) @elseif WEARABLE (Since 2.3.2) @endif */
+    SENSOR_ERROR_NOT_NEED_CALIBRATION  = TIZEN_ERROR_SENSOR | 0x03,        /**< Sensor doesn't need calibration */
+    SENSOR_ERROR_OPERATION_FAILED      = TIZEN_ERROR_SENSOR | 0x06,        /**< Operation failed */
+    SENSOR_ERROR_NOT_AVAILABLE         = TIZEN_ERROR_SENSOR | 0x07,        /**< The sensor is supported, but currently not available
+                                                                                @if MOBILE (Since 3.0) @elseif WEARABLE (Since 2.3.2) @endif */
+} sensor_error_e;
+
 /*
  * A platform sensor handler is generated based on this handle
  * This id can be assigned from HAL developer. so it has to be unique in 1
index a06a530..c86626f 100644 (file)
@@ -7,7 +7,7 @@ Name:       %{name}
 Summary:    %{name} interface
 Version:    0.0.1
 Release:    1
-Group:      Development/Libraries
+Group:      System/API
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
 Source1:    %{name}.manifest
@@ -34,6 +34,14 @@ Requires:    %{name} = %{version}-%{release}
 %description -n        %{devel_name}
 %{name} Interface for product vendor developer
 
+### hal test package ########
+%package -n sensor-haltests
+Summary:       Sensor HAL(Hardware Abstraction Layer) Test Cases
+Requires:      %{name} = %{version}-%{release}
+
+%description -n sensor-haltests
+Sensor HAL(Hardware Abstraction Layer) Test Cases
+
 ### build and install #########
 %prep
 %setup -q
@@ -42,7 +50,9 @@ Requires:     %{name} = %{version}-%{release}
 %build
 cp %{SOURCE1} .
 cp %{SOURCE2} .
-cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DCMAKE_LIBDIR_PREFIX=%{_libdir}
+cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
+               -DCMAKE_LIBDIR_PREFIX=%{_libdir} \
+               -DHALTEST=on
 make %{?jobs:-j%jobs}
 
 %install
@@ -71,3 +81,8 @@ rm -rf %{buildroot}
 %license LICENSE
 %{_includedir}/hal/*.h
 %{_libdir}/pkgconfig/%{name}.pc
+
+%files -n sensor-haltests
+%manifest %{name}.manifest
+%license LICENSE
+%{_bindir}/hal/sensor-haltests