Add unittests
authorChanggyu Choi <changyu.choi@samsung.com>
Thu, 13 Mar 2025 06:04:40 +0000 (15:04 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Thu, 13 Mar 2025 06:04:40 +0000 (15:04 +0900)
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
CMakeLists.txt
packaging/tizen-action-framework.spec
test/CMakeLists.txt [new file with mode: 0644]
test/unit_tests/CMakeLists.txt [new file with mode: 0644]
test/unit_tests/action_executor_factory.cc [new file with mode: 0644]
test/unit_tests/action_model_test.cc [new file with mode: 0644]
test/unit_tests/action_parameter_test.cc [new file with mode: 0644]
test/unit_tests/main.cc [new file with mode: 0644]

index 227ac757d6e74b4891e848053166897eb06265c1..33be0ce176ca1dae4d8ab2cb448a94ad50e664fc 100644 (file)
@@ -22,8 +22,11 @@ SET(TARGET_TIZEN_ACTION_PLUGIN "tizen-action-plugin")
 SET(TARGET_TIZEN_ACTION_SERVICE "tizen-action-service")
 SET(TARGET_TIZEN_ACTION_PLUGIN_DAEMON "tizen-action-plugin-daemon")
 
-#ENABLE_TESTING()
-#SET(TARGET_TIZEN_ACTION_UNIT_TEST "tizen-action-unit-test")
+ENABLE_TESTING()
+SET(TARGET_TIZEN_ACTION_UNIT_TEST "tizen-action-unit-test")
+ADD_TEST(NAME ${TARGET_TIZEN_ACTION_UNIT_TEST}
+  COMMAND ${TARGET_TIZEN_ACTION_UNIT_TEST}
+  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/unit_tests)
 
 INCLUDE(FindPkgConfig)
 INCLUDE(ApplyPkgConfig)
@@ -49,3 +52,4 @@ PKG_CHECK_MODULES(TIZEN_DATABASE_DEPS REQUIRED tizen-database)
 
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(tool)
+ADD_SUBDIRECTORY(test)
index 8a2a150d90a4ccd51fb9c5c1d25c4e41332a44cf..9ff278536aa084c36329c315d6b08dd18bd04289 100644 (file)
@@ -73,6 +73,10 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 
 %__make %{?_smp_mflags}
 
+%check
+export LD_LIBRARY_PATH="../../src/action:../../src/common"
+ctest -V %{?_smp_mflags}
+
 %install
 %make_install
 mkdir -p %{buildroot}%{_unitdir}/basic.target.wants
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c90fac8
--- /dev/null
@@ -0,0 +1 @@
+ADD_SUBDIRECTORY(unit_tests)
diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4585d9f
--- /dev/null
@@ -0,0 +1,28 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} UNIT_TEST_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/action ACTION_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../../src/common COMMON_SRCS)
+
+ADD_EXECUTABLE(${TARGET_TIZEN_ACTION_UNIT_TEST}
+  ${UNIT_TEST_SRCS}
+  ${AUL_SRCS}
+  ${COMMON_SRCS}
+  ${ACTION_SRCS})
+
+TARGET_INCLUDE_DIRECTORIES(${TARGET_TIZEN_ACTION_UNIT_TEST} PUBLIC
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}/../
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../src
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../src/action
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../src/common)
+
+APPLY_PKG_CONFIG(${TARGET_TIZEN_ACTION_UNIT_TEST} PUBLIC
+  GLIB_DEPS
+  GMOCK_DEPS
+)
+
+TARGET_LINK_LIBRARIES(${TARGET_TIZEN_ACTION_UNIT_TEST} PRIVATE ${TARGET_TIZEN_ACTION} ${TARGET_TIZEN_ACTION_COMMON})
+SET_TARGET_PROPERTIES(${TARGET_TIZEN_ACTION_UNIT_TEST} PROPERTIES
+  COMPILE_FLAGS "-fPIE")
+SET_TARGET_PROPERTIES(${TARGET_TIZEN_ACTION_UNIT_TEST} PROPERTIES
+  LINK_FLAGS "-pie")
diff --git a/test/unit_tests/action_executor_factory.cc b/test/unit_tests/action_executor_factory.cc
new file mode 100644 (file)
index 0000000..3e2bb70
--- /dev/null
@@ -0,0 +1,50 @@
+#include <gtest/gtest.h>
+#include "action_executor_factory.hh"
+#include "app_control_executor.hh"
+#include "plugin_executor.hh"
+#include "common/action_model.h"
+
+namespace action {
+
+class ActionExecutorFactoryTest : public ::testing::Test {
+ protected:
+  common::ActionModel app_control_action;
+  common::ActionModel plugin_action;
+  common::ActionModel default_action;
+
+  void SetUp() override {
+    app_control_action.SetType(common::ActionType::AppControl);
+
+    plugin_action.SetType(common::ActionType::Plugin);
+
+    default_action.SetType(static_cast<common::ActionType>(-1));
+  }
+};
+
+TEST_F(ActionExecutorFactoryTest, CreateAppControlExecutor) {
+  auto executor = ActionExecutorFactory::CreateExecutor("instance", app_control_action);
+  ASSERT_NE(executor, nullptr);
+
+  auto* app_control_executor = dynamic_cast<AppControlExecutor*>(executor.get());
+  ASSERT_NE(app_control_executor, nullptr);
+}
+
+TEST_F(ActionExecutorFactoryTest, CreatePluginExecutor) {
+  auto executor = ActionExecutorFactory::CreateExecutor("instance", plugin_action);
+  ASSERT_NE(executor, nullptr);
+
+  auto* plugin_executor = dynamic_cast<PluginExecutor*>(executor.get());
+  ASSERT_NE(plugin_executor, nullptr);
+}
+
+TEST_F(ActionExecutorFactoryTest, CreateDefaultExecutor) {
+  auto executor = ActionExecutorFactory::CreateExecutor("instance", default_action);
+  ASSERT_NE(executor, nullptr);
+
+  auto* appcontrol_executor = dynamic_cast<AppControlExecutor*>(executor.get());
+  ASSERT_EQ(appcontrol_executor, nullptr);
+  auto* plugin_executor = dynamic_cast<PluginExecutor*>(executor.get());
+  ASSERT_EQ(plugin_executor, nullptr);
+}
+
+}  // namespace action
diff --git a/test/unit_tests/action_model_test.cc b/test/unit_tests/action_model_test.cc
new file mode 100644 (file)
index 0000000..8770024
--- /dev/null
@@ -0,0 +1,49 @@
+#include <gtest/gtest.h>
+#include "common/action_model.h"
+
+using namespace common;
+
+TEST(ActionModelTest, SettersAndGetters) {
+    ActionModel model;
+
+    model.SetActionId("new_action");
+    model.SetAppId("new_app");
+    model.SetDescription("New Desc");
+    model.SetParameters({ActionParameter("param1", ParameterType::StringType, "type1", "desc1", true)});
+    model.SetPrivileges({"privilege2"});
+    model.SetType(ActionType::AppControl);
+
+    EXPECT_EQ("new_action", model.GetActionId());
+    EXPECT_EQ("new_app", model.GetAppId());
+    EXPECT_EQ("New Desc", model.GetDescription());
+    EXPECT_EQ(1, model.GetParameters().size());
+    EXPECT_EQ(1, model.GetPrivileges().size());
+    EXPECT_EQ(ActionType::AppControl, model.GetType());
+}
+
+TEST(ActionModelTest, EmptyInitialization) {
+    ActionModel model;
+    EXPECT_EQ("", model.GetActionId());
+    EXPECT_EQ("", model.GetAppId());
+    EXPECT_EQ("", model.GetDescription());
+    EXPECT_EQ(0, model.GetParameters().size());
+    EXPECT_EQ(0, model.GetPrivileges().size());
+}
+
+TEST(ActionModelTest, ParameterVectorTest) {
+    ActionModel model;
+    std::vector<ActionParameter> params = {
+        ActionParameter("name1", ParameterType::StringType, "type1", "desc1", true),
+        ActionParameter("name2", ParameterType::StringType, "type2", "desc2", false)
+    };
+    model.SetParameters(params);
+    EXPECT_EQ(2, model.GetParameters().size());
+    EXPECT_EQ("name1", model.GetParameters()[0].GetName());
+}
+
+TEST(ActionModelTest, PrivilegeVectorTest) {
+    ActionModel model;
+    model.SetPrivileges({"priv1", "priv2"});
+    EXPECT_EQ(2, model.GetPrivileges().size());
+    EXPECT_EQ("priv1", model.GetPrivileges()[0]);
+}
diff --git a/test/unit_tests/action_parameter_test.cc b/test/unit_tests/action_parameter_test.cc
new file mode 100644 (file)
index 0000000..27a8c37
--- /dev/null
@@ -0,0 +1,64 @@
+#include <gtest/gtest.h>
+#include "common/action_parameter.h"
+
+namespace common {
+
+class ActionParameterTest : public ::testing::Test {
+protected:
+    void SetUp() override {
+        param_ = new ActionParameter("test_name", ParameterType::StringType, "test_value", "test_description", true);
+    }
+
+    void TearDown() override {
+        delete param_;
+    }
+
+    ActionParameter* param_;
+};
+
+TEST_F(ActionParameterTest, GetNameTest) {
+    EXPECT_EQ(param_->GetName(), "test_name");
+}
+
+TEST_F(ActionParameterTest, SetNameTest) {
+    param_->SetName("new_name");
+    EXPECT_EQ(param_->GetName(), "new_name");
+}
+
+TEST_F(ActionParameterTest, GetTypeTest) {
+    EXPECT_EQ(param_->GetType(), ParameterType::StringType);
+}
+
+TEST_F(ActionParameterTest, SetTypeTest) {
+    param_->SetType(ParameterType::IntType);
+    EXPECT_EQ(param_->GetType(), ParameterType::IntType);
+}
+
+TEST_F(ActionParameterTest, GetValueTest) {
+    EXPECT_EQ(param_->GetValue(), "test_value");
+}
+
+TEST_F(ActionParameterTest, SetValueTest) {
+    param_->SetValue("new_value");
+    EXPECT_EQ(param_->GetValue(), "new_value");
+}
+
+TEST_F(ActionParameterTest, GetDescriptionTest) {
+    EXPECT_EQ(param_->GetDescription(), "test_description");
+}
+
+TEST_F(ActionParameterTest, SetDescriptionTest) {
+    param_->SetDescription("new_description");
+    EXPECT_EQ(param_->GetDescription(), "new_description");
+}
+
+TEST_F(ActionParameterTest, GetIsRequiredTest) {
+    EXPECT_TRUE(param_->GetIsRequired());
+}
+
+TEST_F(ActionParameterTest, SetIsRequiredTest) {
+    param_->SetIsRequired(false);
+    EXPECT_FALSE(param_->GetIsRequired());
+}
+
+}  // namespace common
diff --git a/test/unit_tests/main.cc b/test/unit_tests/main.cc
new file mode 100644 (file)
index 0000000..b824418
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2017 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.
+ */
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+
+#ifdef LOG_INTERNAL
+#include <dlog.h>
+
+extern "C" int __dlog_print(log_id_t log_id, int prio, const char* tag,
+                            const char* fmt, ...) {
+  printf("%s:", tag);
+  va_list ap;
+  va_start(ap, fmt);
+  vprintf(fmt, ap);
+  va_end(ap);
+  printf("\n");
+
+  return 0;
+}
+#endif
+
+int main(int argc, char** argv) {
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}