From: Jihoon Kim Date: Wed, 7 Sep 2022 02:27:23 +0000 (+0900) Subject: Add MA setting unittest code X-Git-Tag: accepted/tizen/unified/20230606.140926~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a08d674872d9542d394e4a619c219b20a5568dd;p=platform%2Fcore%2Fuifw%2Fmulti-assistant.git Add MA setting unittest code Change-Id: I5d59d736a25b8a972d5777f4c7dc1342d2c539fb Signed-off-by: Jihoon Kim --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6af9b05..1c2757b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,3 +67,13 @@ INSTALL(FILES ${CMAKE_SOURCE_DIR}/ma-config.xml DESTINATION ${TZ_SYS_RO_SHARE}/m INSTALL(FILES ${CMAKE_SOURCE_DIR}/org.tizen.multiassistant.mauiclient.service DESTINATION ${TZ_SYS_RO_SHARE}/dbus-1/services) INSTALL(FILES ${CMAKE_SOURCE_DIR}/multi-assistant.info DESTINATION ${TZ_SYS_RO_SHARE}/parser-plugins) + +## Test +IF(NOT DEFINED MINIMUM_BUILD) +ENABLE_TESTING() +SET(UTC_MA multi-assistant-unittests) +ADD_TEST(NAME ${UTC_MA} COMMAND ${UTC_MA} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests) + +ADD_SUBDIRECTORY(tests) +ENDIF(NOT DEFINED MINIMUM_BUILD) diff --git a/packaging/multi-assistant.spec b/packaging/multi-assistant.spec index 5ab826a..61d9b30 100644 --- a/packaging/multi-assistant.spec +++ b/packaging/multi-assistant.spec @@ -26,8 +26,14 @@ BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(libxml-2.0) BuildRequires: pkgconfig(pkgmgr-installer) BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(gmock) BuildRequires: cmake +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif + %description Multi assistant client library and daemon @@ -49,6 +55,13 @@ Requires: %{name} = %{version}-%{release} %description ui-devel Multi assistant manager header files for MA development. +%package unittests +Summary: multi assistant unittests +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description unittests +GTest for multi assistant %if 0%{?gcov:1} %package gcov @@ -96,6 +109,42 @@ mkdir -p "$gcno_obj_dir" find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' %endif +cat << EOF > run-unittest.sh +#!/bin/sh +setup() { + echo "setup start" +} + +test_main() { + echo "test_main start" + /usr/bin/multi-assistant-unittests +} + +teardown() { + echo "teardown start" +} + +main() { + setup + test_main + teardown +} + +main "\$*" +EOF + +mkdir -p %{buildroot}%{_bindir}/tizen-unittests/%{name} +install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/%{name} + +%check +%if 0%{?gcov:1} +ctest --output-on-failure %{?_smp_mflags} +lcov -c --ignore-errors graph --no-external -q -d . -o %{name}.info +genhtml %{name}.info -o %{name}.out +zip -r %{name}.zip %{name}.out %{name}.info +install -m 0644 %{name}.zip %{buildroot}%{_datadir}/gcov/ +%endif + %post /sbin/ldconfig @@ -132,7 +181,11 @@ mkdir -p %{_libdir}/multiassistant/ma %{_includedir}/multi_assistant_common.h %{_includedir}/multi_assistant_settings.h +%files unittests +%{_bindir}/multi-assistant-unittests +%{_bindir}/tizen-unittests/%{name}/run-unittest.sh + %if 0%{?gcov:1} %files gcov -%{_datadir}/gcov/obj/* +%{_datadir}/gcov/* %endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..5ca1fe7 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,63 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(ma_gtest CXX) + +# Find Packages +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED + dlog + ecore + glib-2.0 + capi-base-common + capi-system-info + bundle + capi-message-port + dbus-1 + cynara-client + cynara-session + libxml-2.0 + vconf + json-glib-1.0 + libtzplatform-config + libxml-2.0 + vconf + gmock +) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIE") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -std=c++11") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}") + +SET(SOURCES "") + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include) + +AUX_SOURCE_DIRECTORY(src SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../common MA_COMMON_SOURCES) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../client MA_CLIENT_SOURCES) + +ADD_DEFINITIONS("-DFULLVER=\"${FULLVER}\"") + +ADD_EXECUTABLE(${UTC_MA} + ${MA_COMMON_SOURCES} + ${MA_CLIENT_SOURCES} + ${SOURCES} + ) +TARGET_LINK_LIBRARIES(${UTC_MA} ${GTEST_LIBRARIES} ${pkgs_LDFLAGS} ${EXTRA_LDFLAGS}) +SET_TARGET_PROPERTIES(${UTC_MA} PROPERTIES + COMPILE_FLAGS "-fPIE" + #Never add any space for LINKFLAGS + LINK_FLAGS "-Wl,\ +--wrap=vconf_set_bool,\ +--wrap=vconf_get_bool,\ +--wrap=vconf_set_str,\ +--wrap=vconf_get_str") + +INSTALL(TARGETS ${UTC_MA} DESTINATION /usr/bin) diff --git a/tests/src/common.h b/tests/src/common.h new file mode 100644 index 0000000..a2d1f54 --- /dev/null +++ b/tests/src/common.h @@ -0,0 +1,16 @@ +#ifndef __MA_TEST_COMMON_H__ +#define __MA_TEST_COMMON_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +static const char *TEST_VOICE_ASSISTANT_APPID = "org.tizen.bixby-test"; +static const char *multiple_mode_vconf_key = "db/multi-assistant/multiple_mode"; + +#ifdef __cplusplus +} +#endif + +#endif /* __MA_TEST_COMMON_H__ */ \ No newline at end of file diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 0000000..a37d671 --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,7 @@ +#include +#include + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/src/multiassistant_setting_multiple_off_unittest.cpp b/tests/src/multiassistant_setting_multiple_off_unittest.cpp new file mode 100644 index 0000000..9e455ab --- /dev/null +++ b/tests/src/multiassistant_setting_multiple_off_unittest.cpp @@ -0,0 +1,76 @@ +// +// Copyright (c) 2022 Samsung Electronics Co., Ltd. +// +// 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 +#include +#include +#include +#include +#include + +#include "common.h" + +class MultiAssistantSettingMultipleOffTest : public testing::Test { + public: + virtual void SetUp() { + vconf_set_bool(multiple_mode_vconf_key, false); + } + virtual void TearDown() { + } +}; + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_is_multiple_mode_error_none) +{ + bool mode; + int ret = ma_settings_is_multiple_mode(&mode); + + EXPECT_EQ(ret, MA_ERROR_NONE); + EXPECT_EQ(mode, false); +} + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_get_current_voice_assistant_operation_failed) +{ + char *appid; + int ret = ma_settings_get_current_voice_assistant(&appid); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_change_voice_assistant_error_none) +{ + int ret = ma_settings_change_voice_assistant(TEST_VOICE_ASSISTANT_APPID); + EXPECT_EQ(ret, MA_ERROR_NONE); +} + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_get_voice_assistant_enabled_not_supported) +{ + bool enabled; + + int ret = ma_settings_get_voice_assistant_enabled(TEST_VOICE_ASSISTANT_APPID, &enabled); + EXPECT_EQ(ret, MA_ERROR_NOT_SUPPORTED); +} + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_set_voice_assistant_enabled_not_supported) +{ + bool enabled; + int ret = ma_settings_get_voice_assistant_enabled(TEST_VOICE_ASSISTANT_APPID, &enabled); + EXPECT_EQ(ret, MA_ERROR_NOT_SUPPORTED); +} + +TEST_F(MultiAssistantSettingMultipleOffTest, utc_ma_settings_get_default_voice_assistant_operation_failed) +{ + char *appid = nullptr; + int ret = ma_settings_get_default_voice_assistant(&appid); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} diff --git a/tests/src/multiassistant_setting_multiple_on_unittest.cpp b/tests/src/multiassistant_setting_multiple_on_unittest.cpp new file mode 100644 index 0000000..3131ad0 --- /dev/null +++ b/tests/src/multiassistant_setting_multiple_on_unittest.cpp @@ -0,0 +1,114 @@ +// +// Copyright (c) 2022 Samsung Electronics Co., Ltd. +// +// 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 +#include +#include +#include +#include +#include + +#include "common.h" + +class MultiAssistantSettingMultipleOnTest : public testing::Test { + public: + virtual void SetUp() { + vconf_set_bool(multiple_mode_vconf_key, true); + } + virtual void TearDown() { + vconf_set_bool(multiple_mode_vconf_key, false); + } +}; + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_is_multiple_mode_invalid_parameter) +{ + int ret = ma_settings_is_multiple_mode(NULL); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_is_multiple_mode_error_none) +{ + bool mode; + int ret = ma_settings_is_multiple_mode(&mode); + + EXPECT_EQ(ret, MA_ERROR_NONE); + EXPECT_EQ(mode, true); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_get_current_voice_assistant_invalid_parameter) +{ + int ret = ma_settings_get_current_voice_assistant(NULL); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_get_current_voice_assistant_operation_failed) +{ + char *appid; + int ret = ma_settings_get_current_voice_assistant(&appid); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_change_voice_assistant_invalid_parameter) +{ + int ret = ma_settings_change_voice_assistant(NULL); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_change_voice_assistant_operation_failed) +{ + int ret = ma_settings_change_voice_assistant(TEST_VOICE_ASSISTANT_APPID); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_get_voice_assistant_enabled_invalid_parameter) +{ + bool enabled; + + int ret = ma_settings_get_voice_assistant_enabled(TEST_VOICE_ASSISTANT_APPID, NULL); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); + + ret = ma_settings_get_voice_assistant_enabled(NULL, &enabled); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); + + ret = ma_settings_get_voice_assistant_enabled(NULL, NULL); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_get_voice_assistant_enabled_not_supported) +{ + bool enabled; + + int ret = ma_settings_get_voice_assistant_enabled(TEST_VOICE_ASSISTANT_APPID, &enabled); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_set_voice_assistant_enabled_invalid_parameter) +{ + int ret = ma_settings_set_voice_assistant_enabled(NULL, true); + EXPECT_EQ(ret, MA_ERROR_INVALID_PARAMETER); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_set_voice_assistant_enabled_operation_failed) +{ + int ret = ma_settings_set_voice_assistant_enabled(TEST_VOICE_ASSISTANT_APPID, true); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} + +TEST_F(MultiAssistantSettingMultipleOnTest, utc_ma_settings_get_default_voice_assistant_operation_failed) +{ + char *appid = nullptr; + int ret = ma_settings_get_default_voice_assistant(&appid); + EXPECT_EQ(ret, MA_ERROR_OPERATION_FAILED); +} diff --git a/tests/src/vconf_mock.cpp b/tests/src/vconf_mock.cpp new file mode 100644 index 0000000..d4b978a --- /dev/null +++ b/tests/src/vconf_mock.cpp @@ -0,0 +1,53 @@ +#include "vconf_mock.h" +#include +#include +#include +#include +#include + +static bool multiple_mode = false; + +static std::map vconfStrMap; +static std::map vconfBoolMap; + +#define VCONF_OK 0 +#define VCONF_ERROR -1 + +void set_multiple_mode(bool mode) +{ + multiple_mode = mode; +} + +EXPORT_API int __wrap_vconf_set_bool(const char *key, const int val) +{ + vconfBoolMap[key] = val; + return VCONF_OK; +} + +EXPORT_API int __wrap_vconf_get_bool(const char *key, int *val) +{ + if (vconfBoolMap.find(key) != vconfBoolMap.end()) { + if (val) { + *val = vconfBoolMap[key]; + } + return VCONF_OK; + } + else { + return VCONF_ERROR; + } +} + +EXPORT_API int __wrap_vconf_set_str(const char *key, const char *val) +{ + vconfStrMap[key] = std::string(val ? val : ""); + return VCONF_OK; +} + +EXPORT_API char *__wrap_vconf_get_str(const char *key) +{ + if (vconfBoolMap.find(key) != vconfBoolMap.end()) { + return strdup(vconfStrMap[key].c_str()); + } + else + return NULL; +} diff --git a/tests/src/vconf_mock.h b/tests/src/vconf_mock.h new file mode 100644 index 0000000..ef3f449 --- /dev/null +++ b/tests/src/vconf_mock.h @@ -0,0 +1,21 @@ +#ifndef MOCK_VCONF_H_ +#define MOCK_VCONF_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void set_multiple_mode(bool mode); + +int __wrap_vconf_set_bool(const char *key, const int val); +int __wrap_vconf_get_bool(const char *key, int *val); +int __wrap_vconf_set_str(const char *key, const char *val); +char *__wrap_vconf_get_str(const char *key); + +#ifdef __cplusplus +} +#endif +#endif /* MOCK_VCONF_H_ */