Add sample unit test for CServiceMain and fix related errors 62/229662/4
authorJi-hoon Lee <dalton.lee@samsung.com>
Thu, 2 Apr 2020 13:55:41 +0000 (22:55 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Wed, 8 Apr 2020 07:42:57 +0000 (16:42 +0900)
Change-Id: I280242fb68e90473688abba06a5b333f62225b61

CMakeLists.txt
inc/application_manager_aul.h
inc/service_config.h
src/application_manager_aul.cpp
src/service_config.cpp
src/service_main.cpp
tests/utc/CMakeLists.txt
tests/utc/service-main/CMakeLists.txt [new file with mode: 0644]
tests/utc/service-main/test_service_main.cpp [new file with mode: 0644]

index 61c3363..5bd511e 100644 (file)
@@ -67,7 +67,6 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc)
 
 SET(SRCS
-               src/main.cpp
                src/service_config.cpp
                src/application_manager_aul.cpp
                src/preference_manager_vconf.cpp
@@ -76,6 +75,7 @@ SET(SRCS
                src/service_plugin.cpp
                src/service_ipc_dbus.cpp
                src/service_ipc_dbus_dispatcher.cpp
+               src/main.cpp
 )
 ADD_EXECUTABLE(${BINNAME} ${SRCS})
 
index b0b3dd3..e4edbba 100644 (file)
@@ -26,6 +26,9 @@ extern "C" {
 
 class CApplicationManagerAul : public IApplicationManager {
 public:
+       CApplicationManagerAul();
+       ~CApplicationManagerAul();
+
        virtual bool is_application_running(int pid) override;
        virtual bool is_application_running(const std::string& appid) override;
        virtual bool bring_app_to_foreground(const std::string& appid) override;
index 18fccbc..d77a4a0 100644 (file)
@@ -59,7 +59,23 @@ extern "C"
 #define VOICE_KEY_SUPPORT_MODE_STRING_TAP_TO_TALK "tap_to_talk"
 #define VOICE_KEY_SUPPORT_MODE_STRING_ALL "all"
 
-typedef struct {
+typedef struct ma_assistant_info_s {
+       ma_assistant_info_s() :
+               app_id{nullptr},
+               name{nullptr},
+               icon_path{nullptr},
+               wakeup_list{0x00, },
+               wakeup_language{0x00, },
+               cnt_wakeup{0},
+               supported_lang{0x00, },
+               cnt_lang{0},
+               wakeup_engine{nullptr},
+               custom_ui_option{false},
+               /* TODO: Define these two default values somewhere else */
+               voice_key_support_mode{VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK},
+               voice_key_tap_duration{0.0f} /* Meaning not set */
+       {}
+
        const char* app_id;
        const char* name;
        const char* icon_path;
index 5cf8855..b3863ad 100644 (file)
 #include <aul.h>
 #include <aul_svc.h>
 
+CApplicationManagerAul::CApplicationManagerAul()
+{
+}
+
+CApplicationManagerAul::~CApplicationManagerAul()
+{
+}
+
 bool CApplicationManagerAul::is_application_running(int pid)
 {
        int status = aul_app_get_status_bypid(pid);
index 44c4eaa..97b90ee 100644 (file)
@@ -71,20 +71,6 @@ int CServiceConfig::parse_assistant_info(service_config_assistant_info_cb callba
        /* alloc assistant info */
        ma_assistant_info_s temp;
 
-       temp.app_id = NULL;
-       temp.name = NULL;
-       temp.icon_path = NULL;
-       memset(temp.wakeup_list, 0x00, sizeof(temp.wakeup_list));
-       memset(temp.wakeup_language, 0x00, sizeof(temp.wakeup_language));
-       temp.cnt_wakeup = 0;
-       memset(temp.supported_lang, 0x00, sizeof(temp.supported_lang));
-       temp.cnt_lang = 0;
-       temp.wakeup_engine = NULL;
-       temp.custom_ui_option = false;
-       /* TODO: Define these two default values somewhere else */
-       temp.voice_key_support_mode = VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK;
-       temp.voice_key_tap_duration = 0.0f; /* Meaning not set */
-
        while (cur != NULL) {
                if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_LANGUAGE_SET)) {
                        xmlNodePtr child_node = cur->xmlChildrenNode;
index 270f14d..86b4001 100644 (file)
@@ -586,17 +586,19 @@ int CServiceMain::initialize_service_plugin(void)
        if (0 == mServiceConfig.get_assistant_info(mas_assistant_info_cb, this)) {
                for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
                        int inner_loop;
-                       if (mClientInfo[loop].used &&
+                       if (mClientInfo[loop].used && mClientInfo[loop].appid &&
                                0 < strlen(mClientInfo[loop].appid)) {
                                mServiceConfig.load_custom_wake_words(mClientInfo[loop].appid,
                                        mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language);
-                               if (0 < strlen(mClientInfo[loop].wakeup_engine)) {
+                               if (mClientInfo[loop].wakeup_engine &&
+                                       0 < strlen(mClientInfo[loop].wakeup_engine)) {
                                        mServicePlugin.set_assistant_wakeup_engine(
                                                mClientInfo[loop].appid,
                                                mClientInfo[loop].wakeup_engine);
                                }
                                for (inner_loop = 0; inner_loop < MAX_WAKEUP_WORDS_NUM; inner_loop++) {
-                                       if (0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
+                                       if (mClientInfo[loop].wakeup_word[inner_loop] &&
+                                               0 < strlen(mClientInfo[loop].wakeup_word[inner_loop])) {
                                                MAS_LOGD("Registering wakeup word %s for app %s",
                                                        mClientInfo[loop].wakeup_word[inner_loop], mClientInfo[loop].appid);
                                                if (0 != mServicePlugin.add_assistant_wakeup_word(
@@ -608,7 +610,8 @@ int CServiceMain::initialize_service_plugin(void)
                                        }
                                }
                                for (inner_loop = 0; inner_loop < MAX_SUPPORTED_LANGUAGE_NUM; inner_loop++) {
-                                       if (0 < strlen(mClientInfo[loop].supported_language[inner_loop])) {
+                                       if (mClientInfo[loop].supported_language[inner_loop] &&
+                                               0 < strlen(mClientInfo[loop].supported_language[inner_loop])) {
                                                MAS_LOGD("Adding language %s for app %s",
                                                        mClientInfo[loop].supported_language[inner_loop], mClientInfo[loop].appid);
                                                if (0 != mServicePlugin.add_assistant_language(
index 887d8a3..332b649 100644 (file)
@@ -3,3 +3,4 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 ADD_SUBDIRECTORY(config)
 ADD_SUBDIRECTORY(client-manager)
 ADD_SUBDIRECTORY(audio-manager)
+ADD_SUBDIRECTORY(service-main)
diff --git a/tests/utc/service-main/CMakeLists.txt b/tests/utc/service-main/CMakeLists.txt
new file mode 100644 (file)
index 0000000..69a4d83
--- /dev/null
@@ -0,0 +1,59 @@
+LINK_DIRECTORIES(${CMAKE_BINARY_DIR})
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}
+       ${CMAKE_SOURCE_DIR}/inc/
+       )
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wno-unused-function -Wno-sign-compare")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIE")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++11")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}")
+
+ADD_DEFINITIONS("-DFULLVER=\"${FULLVER}\"")
+
+SET(TEST_SOURCES
+       test_service_main.cpp
+       ${CMAKE_SOURCE_DIR}/src/service_main.cpp
+       ${CMAKE_SOURCE_DIR}/src/service_ipc_dbus.cpp
+       ${CMAKE_SOURCE_DIR}/src/service_ipc_dbus_dispatcher.cpp
+       ${CMAKE_SOURCE_DIR}/src/service_plugin.cpp
+       ${CMAKE_SOURCE_DIR}/src/service_config.cpp
+       ${CMAKE_SOURCE_DIR}/src/client_manager.cpp
+)
+
+# Find Packages
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs REQUIRED
+       capi-appfw-application
+       capi-appfw-preference
+       capi-appfw-package-manager
+       capi-appfw-service-application
+       capi-message-port
+       multi-assistant
+       dlog
+       libxml-2.0
+       ecore
+       pkgmgr-info
+)
+
+FOREACH(flag ${pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+FIND_PACKAGE(GTest REQUIRED)
+INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})
+LINK_DIRECTORIES(${GTEST_LIBRARY_DIRS})
+
+SET(TEST_SERVICE_MAIN test-service-main)
+ADD_EXECUTABLE(${TEST_SERVICE_MAIN}
+       ${TEST_SOURCES}
+       )
+
+TARGET_LINK_LIBRARIES(${TEST_SERVICE_MAIN} -ldl ${GTEST_LIBRARIES} pthread
+       ${EXTRA_LDFLAGS} ${pkgs_LDFLAGS})
+
+INSTALL(TARGETS ${TEST_SERVICE_MAIN} DESTINATION bin)
+
+ADD_TEST(NAME ${TEST_SERVICE_MAIN} COMMAND ${TEST_SERVICE_MAIN})
diff --git a/tests/utc/service-main/test_service_main.cpp b/tests/utc/service-main/test_service_main.cpp
new file mode 100644 (file)
index 0000000..1ff69ef
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2018 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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 "service_main.h"
+
+#include <string>
+#include <unistd.h>
+#include <stdlib.h>
+
+class CDummyApplicationManager : public IApplicationManager
+{
+public:
+       CDummyApplicationManager() {};
+       ~CDummyApplicationManager() {};
+
+       bool is_application_running(int pid) override { return false; }
+       bool is_application_running(const std::string& appid) override { return false; }
+       bool bring_app_to_foreground(const std::string& appid) override
+       {
+               brought_to_foreground_appid = appid;
+               return true;
+       }
+       bool launch_app_async(const std::string& appid, bool background) override
+       {
+               launched_appid = appid;
+               launched_option_background = background;
+               return true;
+       }
+       boost::optional<std::string> get_appid_by_pid(int pid) override { return boost::optional<std::string>{}; }
+public:
+       boost::optional<std::string> launched_appid;
+       boost::optional<bool> launched_option_background;
+       boost::optional<std::string> brought_to_foreground_appid;;
+};
+
+class CDummyPreferenceManager : public IPreferenceManager
+{
+public:
+       CDummyPreferenceManager() {}
+       ~CDummyPreferenceManager() {}
+
+       boost::optional<std::string> get_string(const std::string& key) override
+       {
+               boost::optional<std::string> ret;
+               const auto& iter = key_value_string.find(key);
+               if (iter != key_value_string.end()) {
+                       ret = iter->second;
+               }
+               return ret;
+       }
+       boost::optional<int> get_int(const std::string& key) override
+       {
+               boost::optional<int> ret;
+               const auto& iter = key_value_int.find(key);
+               if (iter != key_value_int.end()) {
+                       ret = iter->second;
+               }
+               return ret;
+       }
+       boost::optional<bool> get_bool(const std::string& key) override
+       {
+               boost::optional<bool> ret;
+               const auto& iter = key_value_bool.find(key);
+               if (iter != key_value_bool.end()) {
+                       ret = iter->second;
+               }
+               return ret;
+       }
+
+       bool register_changed_callback(
+                       const std::string& key, preference_changed_cb calback, void* user_data) override { return true; }
+       bool unregister_changed_callback(
+                       const std::string& key, preference_changed_cb callback) override { return true; }
+public:
+       std::map<std::string, std::string> key_value_string;
+       std::map<std::string, int> key_value_int;
+       std::map<std::string, bool> key_value_bool;
+};
+
+class DefaultFixure : public testing::Test
+{
+public:
+       DefaultFixure() {
+       }
+       virtual ~DefaultFixure() {
+       }
+       void SetUp() override {
+       }
+       void TearDown() override {
+       }
+       CDummyApplicationManager application_manager;
+       CDummyPreferenceManager preference_manager;
+
+       CServiceMain service{application_manager, preference_manager};
+};
+
+TEST_F(DefaultFixure, BringsDefaultAssistantToFrontOnAssistantActivatedPreprocessingStateEvent) {
+       std::string assistant_appid{"Arbitrary Assistant"};
+       ma_assistant_info_s info;
+       info.app_id = assistant_appid.c_str();
+       service.add_assistant_info(&info);
+       service.mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
+
+       bool brought = false;
+       if (application_manager.brought_to_foreground_appid) {
+               brought = true;
+       }
+
+       ASSERT_TRUE(brought);
+}
+
+int main(int argc, char** argv) {
+       const char* seconds = getenv("TEST_SLEEP_SECONDS");
+       if (seconds) {
+               sleep(atoi(seconds));
+       }
+       testing::InitGoogleTest(&argc, argv);
+       int ret = RUN_ALL_TESTS();
+       return ret;
+}