diff options
author | Hwankyu Jhun <h.jhun@samsung.com> | 2023-08-29 15:39:54 +0900 |
---|---|---|
committer | Hwankyu Jhun <h.jhun@samsung.com> | 2023-09-05 10:02:26 +0900 |
commit | 598c8566792fa71002befe1bed1335574e22f2ff (patch) | |
tree | 37ea396b34d15a6c279fe32043319c33f9f3d1ca | |
parent | 7e6e7b8db539fa21504008560d7b2af0bf136efb (diff) | |
download | app-manager-598c8566792fa71002befe1bed1335574e22f2ff.tar.gz app-manager-598c8566792fa71002befe1bed1335574e22f2ff.tar.bz2 app-manager-598c8566792fa71002befe1bed1335574e22f2ff.zip |
Improve code coverage
- Adds unit tests
Change-Id: I644a671e00dc507e0b98c82df2320b0ebfc46a02
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
22 files changed, 3389 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d458fb4..5aaf919 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ PKG_CHECK_MODULES(CAPI_BASE_COMMON_DEPS REQUIRED capi-base-common) PKG_CHECK_MODULES(CYNARA_CLIENT_DEPS REQUIRED cynara-client) PKG_CHECK_MODULES(DLOG_DEPS REQUIRED dlog) PKG_CHECK_MODULES(GLIB_DEPS REQUIRED glib-2.0) +PKG_CHECK_MODULES(GMOCK_DEPS REQUIRED gmock) PKG_CHECK_MODULES(PKGMGR_DEPS REQUIRED pkgmgr) PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info) PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf) @@ -56,3 +57,4 @@ INSTALL(DIRECTORY include/ DESTINATION include/appfw ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(tool) +ADD_SUBDIRECTORY(tests) diff --git a/packaging/capi-appfw-app-manager.spec b/packaging/capi-appfw-app-manager.spec index 58d9108..e0a20fe 100644 --- a/packaging/capi-appfw-app-manager.spec +++ b/packaging/capi-appfw-app-manager.spec @@ -14,9 +14,16 @@ BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(gmock) + Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif + %description The Application Manager API provides functions to get information about running applications. @@ -28,6 +35,14 @@ Requires: %{name} = %{version} %description devel The Application Manager API provides functions to get information about running applications. (DEV) +%package unittests +Summary: GTest for app-manager API +Group: Development/Libraries +Requires: %{name} + +%description unittests +GTest for app-manager API + %if 0%{?gcov:1} %package gcov Summary: Application Manager API(gcov) @@ -53,6 +68,16 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %cmake . -DFULLVER=%{version} -DMAJORVER=${MAJORVER} %__make %{?jobs:-j%jobs} +%check +export LD_LIBRARY_PATH="../../src" +ctest --verbose %{?_smp_mflags} +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o app-manager.info +genhtml app-manager.info -o app-manager.out +zip -r app-manager.zip app-manager.out +install -m 0644 app-manager.zip %{buildroot}%{_datadir}/gcov/ +%endif + %install rm -rf %{buildroot} %make_install @@ -64,6 +89,47 @@ mkdir -p "$gcno_obj_dir" find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' %endif +cat << EOF > run-unittest.sh +#!/bin/sh +GCOV_PATH="/tmp/home/abuild/rpmbuild/BUILD" +PAKCAGE="%{name}-%{version}" + +set_perm() { + ## Sets the permission for applications + /usr/bin/find /tmp/home/ -print | /usr/bin/xargs -n1 /usr/bin/chsmack -a "System::Run" &> /dev/null + /usr/bin/find /tmp/home/ -print | /usr/bin/xargs -n1 /usr/bin/chsmack -a "System::Run" -t &> /dev/null + /usr/bin/chmod -R 777 /tmp/home/ +} + +setup() { + echo "setup start" + /usr/bin/mkdir -p "${GCOV_PATH}/${PACKAGE}" + set_perm +} + +test_main() { + echo "test_main start" + export "GCOV_PREFIX=/tmp" + /usr/bin/app-manager_unittests +} + +teardown() { + echo "teardown start" + set_perm +} + +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}/ + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -80,7 +146,11 @@ find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' %{_libdir}/libcapi-appfw-app-manager.so %{_libdir}/pkgconfig/*.pc +%files unittests +%{_bindir}/app-manager_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..5e93821 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(app-manager_unittests) diff --git a/tests/app-manager_unittests/CMakeLists.txt b/tests/app-manager_unittests/CMakeLists.txt new file mode 100644 index 0000000..5ff893a --- /dev/null +++ b/tests/app-manager_unittests/CMakeLists.txt @@ -0,0 +1,30 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} UNIT_TEST_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock MOCK_SRCS) + +ADD_EXECUTABLE(${TARGET_APP_MANAGER_UNITTESTS} + ${UNIT_TEST_SRCS} + ${MOCK_SRCS}) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_APP_MANAGER_UNITTESTS} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/mock/ + ${CMAKE_CURRENT_SOURCE_DIR}/../ + ${CMAKE_CURRENT_SOURCE_DIR}/../../ + ${CMAKE_CURRENT_SOURCE_DIR}/../../include/) + +APPLY_PKG_CONFIG(${TARGET_APP_MANAGER_UNITTESTS} PUBLIC + AUL_DEPS + DLOG_DEPS + GLIB_DEPS + GMOCK_DEPS +) + +TARGET_LINK_LIBRARIES(${TARGET_APP_MANAGER_UNITTESTS} PUBLIC + ${TARGET_APP_MANAGER}) + +SET_TARGET_PROPERTIES(${TARGET_APP_MANAGER_UNITTESTS} + PROPERTIES COMPILE_FLAGS "-fPIE") +SET_TARGET_PROPERTIES(${TARGET_APP_MANAGER_UNITTESTS} + PROPERTIES LINK_FLAGS "-pie") + +INSTALL(TARGETS ${TARGET_APP_MANAGER_UNITTESTS} DESTINATION bin) diff --git a/tests/app-manager_unittests/app_context_tests.cc b/tests/app-manager_unittests/app_context_tests.cc new file mode 100644 index 0000000..2b5a92a --- /dev/null +++ b/tests/app-manager_unittests/app_context_tests.cc @@ -0,0 +1,262 @@ +/* + * Copyright (c) 2023 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 <aul.h> +#include <app_manager.h> +#include <app_manager_extension.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> + +#include <memory> + +#include "app-manager_unittests/mock/aul_mock.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; + +namespace { + +constexpr const char kAppId[] = "org.tizen.hello"; +constexpr const char kPackageId[] = "org.tizen.hello"; + +int dummy; +app_context_h app_context_ = reinterpret_cast<app_context_h>(&dummy); + +class Mocks : public ::testing::NiceMock<AulMock> {}; + +int fake_aul_app_context_destroy(aul_app_context_h app_context) { + return AUL_R_OK; +} + +int fake_aul_app_context_get_app_id(aul_app_context_h app_context, + char** app_id) { + *app_id = strdup(kAppId); + return AUL_R_OK; +} + +int fake_aul_app_context_get_pid(aul_app_context_h app_context, pid_t* pid) { + *pid = getpid(); + return AUL_R_OK; +} + +int fake_aul_app_context_get_pkg_id(aul_app_context_h app_context, + char** pkg_id) { + *pkg_id = strdup(kPackageId); + return AUL_R_OK; +} + +int fake_aul_app_context_get_status(aul_app_context_h app_context, + int* status) { + *status = STATUS_VISIBLE; + return AUL_R_OK; +} + +int fake_aul_app_context_is_running(aul_app_context_h app_context, + bool* is_running) { + *is_running = true; + return AUL_R_OK; +} + +int fake_aul_app_context_is_equal(aul_app_context_h lhs, aul_app_context_h rhs, + bool* equal) { + *equal = true; + return AUL_R_OK; +} + +int fake_aul_app_context_is_sub_app(aul_app_context_h app_context, + bool* is_sub_app) { + *is_sub_app = false; + return AUL_R_OK; +} + +int fake_aul_app_context_clone(aul_app_context_h app_context, + aul_app_context_h* clone) { + static aul_app_context_h context = app_context; + *clone = context; + return AUL_R_OK; +} + +} // namespace + +class AppContextTest : public TestFixture { + public: + AppContextTest() : TestFixture(std::make_unique<Mocks>()) {} + virtual ~AppContextTest() {} + + void SetUp() override {} + void TearDown() override {} +}; + +TEST_F(AppContextTest, app_context_destroy_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_destroy(_)) + .WillOnce(Invoke(fake_aul_app_context_destroy)); + + int ret = app_context_destroy(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppContextTest, app_context_destroy_N) { + int ret = app_context_destroy(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_get_package_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_app_id(_, _)) + .WillOnce(Invoke(fake_aul_app_context_get_app_id)); + + char* app_id = nullptr; + int ret = app_context_get_package(app_context_, &app_id); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + auto auto_free = + std::unique_ptr<char, decltype(std::free)*>(app_id, std::free); + ASSERT_EQ(std::string(app_id), kAppId); +} + +TEST_F(AppContextTest, app_context_get_package_N) { + int ret = app_context_get_package(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_get_app_id_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_app_id(_, _)) + .WillOnce(Invoke(fake_aul_app_context_get_app_id)); + + char* app_id = nullptr; + int ret = app_context_get_app_id(app_context_, &app_id); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + auto auto_free = + std::unique_ptr<char, decltype(std::free)*>(app_id, std::free); + ASSERT_EQ(std::string(app_id), kAppId); +} + +TEST_F(AppContextTest, app_context_get_app_id_N) { + int ret = app_context_get_app_id(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_get_pid_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_pid(_, _)) + .WillOnce(Invoke(fake_aul_app_context_get_pid)); + + pid_t pid = -1; + int ret = app_context_get_pid(app_context_, &pid); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(pid, -1); +} + +TEST_F(AppContextTest, app_context_get_pid_N) { + int ret = app_context_get_pid(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_get_package_id_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_pkg_id(_, _)) + .WillOnce(Invoke(fake_aul_app_context_get_pkg_id)); + + char* package_id = nullptr; + int ret = app_context_get_package_id(app_context_, &package_id); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + auto auto_free = + std::unique_ptr<char, decltype(std::free)*>(package_id, std::free); + ASSERT_EQ(std::string(package_id), kPackageId); +} + +TEST_F(AppContextTest, app_context_get_package_id_N) { + int ret = app_context_get_package_id(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_get_app_state_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_status(_, _)) + .WillOnce(Invoke(fake_aul_app_context_get_status)); + + app_state_e state; + int ret = app_context_get_app_state(app_context_, &state); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(state, APP_STATE_FOREGROUND); +} + +TEST_F(AppContextTest, app_context_get_app_state_N) { + int ret = app_context_get_app_state(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_is_terminated_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_is_running(_, _)) + .WillOnce(Invoke(fake_aul_app_context_is_running)); + + bool terminated = true; + int ret = app_context_is_terminated(app_context_, &terminated); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(terminated, false); +} + +TEST_F(AppContextTest, app_context_is_terminated_N) { + int ret = app_context_is_terminated(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_is_equal_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_is_equal(_, _, _)) + .WillOnce(Invoke(fake_aul_app_context_is_equal)); + + bool equal = false; + int ret = app_context_is_equal(app_context_, app_context_, &equal); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(equal, true); +} + +TEST_F(AppContextTest, app_context_is_equal_N) { + int ret = app_context_is_equal(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_is_sub_app_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_is_sub_app(_, _)) + .WillOnce(Invoke(fake_aul_app_context_is_sub_app)); + + bool is_sub_app = true; + int ret = app_context_is_sub_app(app_context_, &is_sub_app); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(is_sub_app, false); +} + +TEST_F(AppContextTest, app_context_is_sub_app_N) { + int ret = app_context_is_sub_app(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppContextTest, app_context_clone_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_clone(_, _)) + .WillOnce(Invoke(fake_aul_app_context_clone)); + + app_context_h clone = nullptr; + int ret = app_context_clone(&clone, app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(clone, nullptr); +} + +TEST_F(AppContextTest, app_context_clone_N) { + int ret = app_context_clone(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} diff --git a/tests/app-manager_unittests/app_info_tests.cc b/tests/app-manager_unittests/app_info_tests.cc new file mode 100644 index 0000000..1237344 --- /dev/null +++ b/tests/app-manager_unittests/app_info_tests.cc @@ -0,0 +1,912 @@ +/* + * Copyright (c) 2023 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 <app_manager.h> +#include <app_manager_extension.h> +#include <aul.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +#include <climits> +#include <memory> + +#include "app-manager_unittests/mock/aul_mock.hh" +#include "app-manager_unittests/mock/cynara_mock.hh" +#include "app-manager_unittests/mock/glibc_mock.hh" +#include "app-manager_unittests/mock/pkgmgr_info_mock.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; + +namespace { + +constexpr const char kAppId[] = "org.tizen.hello"; +constexpr const char kPackageId[] = "org.tizen.helloworld"; +constexpr const char kExec[] = + "/opt/usr/home/owner/apps_rw/org.tizen.helloworld/bin/hello"; +constexpr const char kLabel[] = "Hello"; +constexpr const char kLocalizedLabel[] = "안녕하세요"; +constexpr const char kIcon[] = + "/opt/usr/home/owner/apps_rw/org.tizen.helloworld/shared/res/hello.png"; +constexpr const char kAppType[] = "c++app"; +constexpr const char kMetadataKey[] = "http://tizen.org/metadata/port"; +constexpr const char kCategory[] = "clock"; + +int dummy; +pkgmgrinfo_appinfo_h appinfo_ = reinterpret_cast<pkgmgrinfo_appinfo_h>(&dummy); +pkgmgrinfo_pkginfo_h pkginfo_ = reinterpret_cast<pkgmgrinfo_pkginfo_h>(&dummy); + +class Mocks : public ::testing::NiceMock<PkgmgrInfoMock>, + public ::testing::NiceMock<AulMock>, + public ::testing::NiceMock<CynaraMock>, + public ::testing::NiceMock<GlibcMock> {}; + +int fake_aul_app_get_appid_bypid(int pid, char* appid, int len) { + snprintf(appid, len, "%s", kAppId); + return AUL_R_OK; +} + +int fake_aul_svc_get_appid_by_alias_appid(const char* alias_appid, + char** appid) { + return AUL_SVC_RET_OK; +} + +int fake_pkgmgrinfo_appinfo_get_usr_appinfo(const char* appid, uid_t uid, + pkgmgrinfo_appinfo_h* handle) { + if (strcmp(appid, kPackageId) == 0) + return PMINFO_R_ERROR; + + *handle = appinfo_; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) { + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_pkginfo_get_usr_pkginfo(const char* pkgid, uid_t uid, + pkgmgrinfo_pkginfo_h* handle) { + *handle = pkginfo_; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle) { + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, + char** mainappid) { + static char buf[256]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kAppId); + + *mainappid = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, char** exec) { + static char buf[PATH_MAX]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kExec); + + *exec = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, + char** label) { + static char buf[256]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kLabel); + + *label = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_usr_get_localed_label(const char* appid, + const char* locale, uid_t uid, + char** label) { + *label = strdup(kLocalizedLabel); + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, char** icon) { + static char buf[PATH_MAX]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kIcon); + + *icon = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, + char** pkgname) { + static char buf[256]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kPackageId); + + *pkgname = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, + char** type) { + static char buf[12]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kAppType); + + *type = buf; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_foreach_metadata( + pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_metadata_list_cb callback, + void* user_data) { + callback(kMetadataKey, "test", user_data); + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, + bool* nodisplay) { + *nodisplay = false; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h handle, + bool* enabled) { + *enabled = true; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h handle, + bool* onboot) { + *onboot = false; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, + bool* preload) { + *preload = true; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_is_support_ambient(pkgmgrinfo_appinfo_h handle, + bool* support_ambient) { + *support_ambient = true; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_appinfo_h* clone) { + *clone = handle; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_filter_create(pkgmgrinfo_appinfo_filter_h* handle) { + *handle = reinterpret_cast<pkgmgrinfo_appinfo_filter_h>(&dummy); + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_filter_destroy(pkgmgrinfo_appinfo_filter_h handle) { + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_metadata_filter_create( + pkgmgrinfo_appinfo_metadata_filter_h* handle) { + *handle = reinterpret_cast<pkgmgrinfo_appinfo_metadata_filter_h>(&dummy); + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_metadata_filter_destroy( + pkgmgrinfo_appinfo_metadata_filter_h handle) { + return PMINFO_R_OK; +} + +} // namespace + +class AppInfoTest : public TestFixture { + public: + AppInfoTest() : TestFixture(std::make_unique<Mocks>()) {} + virtual ~AppInfoTest() {} + + void SetUp() override { + EXPECT_CALL(GetMock<AulMock>(), aul_app_get_appid_bypid(_, _, _)) + .WillRepeatedly(Invoke(fake_aul_app_get_appid_bypid)); + EXPECT_CALL(GetMock<AulMock>(), aul_svc_get_appid_by_alias_appid(_, _)) + .WillRepeatedly(Invoke(fake_aul_svc_get_appid_by_alias_appid)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_get_usr_appinfo)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_destroy_appinfo(_)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_destroy_appinfo)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_filter_create(_)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_filter_create)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_filter_destroy(_)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_filter_destroy)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_metadata_filter_create(_)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_metadata_filter_create)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_metadata_filter_destroy(_)) + .WillRepeatedly( + Invoke(fake_pkgmgrinfo_appinfo_metadata_filter_destroy)); + + int ret = app_info_create(kAppId, &app_info_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_create(&app_info_filter_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_metadata_filter_create(&app_info_metadata_filter_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + } + + void TearDown() override { + if (app_info_metadata_filter_ != nullptr) { + app_info_metadata_filter_destroy(app_info_metadata_filter_); + app_info_metadata_filter_ = nullptr; + } + + if (app_info_filter_ != nullptr) { + app_info_filter_destroy(app_info_filter_); + app_info_filter_ = nullptr; + } + + if (app_info_ != nullptr) { + app_info_destroy(app_info_); + app_info_ = nullptr; + } + } + + app_info_h app_info_ = nullptr; + app_info_filter_h app_info_filter_ = nullptr; + app_info_metadata_filter_h app_info_metadata_filter_ = nullptr; +}; + +TEST_F(AppInfoTest, app_info_create_P1) { + app_info_h app_info = nullptr; + int ret = app_info_create(kAppId, &app_info); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(app_info, nullptr); + app_info_destroy(app_info); +} + +TEST_F(AppInfoTest, app_info_create_P2) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_pkginfo_get_usr_pkginfo(_, _, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_pkginfo_get_usr_pkginfo)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_pkginfo_destroy_pkginfo(_)) + .WillOnce(Invoke(fake_pkgmgrinfo_pkginfo_destroy_pkginfo)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_pkginfo_get_mainappid(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_pkginfo_get_mainappid)); + + app_info_h app_info = nullptr; + int ret = app_info_create(kPackageId, &app_info); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(app_info, nullptr); + app_info_destroy(app_info); +} + +TEST_F(AppInfoTest, app_info_create_N) { + int ret = app_info_create(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_destroy_P) { + int ret = app_info_destroy(app_info_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + app_info_ = nullptr; +} + +TEST_F(AppInfoTest, app_info_destroy_N) { + int ret = app_info_destroy(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_app_id_P) { + char* app_id = nullptr; + int ret = app_info_get_app_id(app_info_, &app_id); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(app_id), kAppId); + free(app_id); +} + +TEST_F(AppInfoTest, app_info_get_app_id_N) { + int ret = app_info_get_app_id(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_exec_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_exec(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_get_exec)); + + char* exec = nullptr; + int ret = app_info_get_exec(app_info_, &exec); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(exec), kExec); + free(exec); +} + +TEST_F(AppInfoTest, app_info_get_exec_N) { + int ret = app_info_get_exec(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_label_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_label(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_get_label)); + + char* label = nullptr; + int ret = app_info_get_label(app_info_, &label); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(label), kLabel); + free(label); +} + +TEST_F(AppInfoTest, app_info_get_label_N) { + int ret = app_info_get_label(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_localed_label_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_usr_get_localed_label(_, _, _, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_usr_get_localed_label)); + + char* label = nullptr; + int ret = app_info_get_localed_label(kAppId, "ko_KR", &label); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(label), kLocalizedLabel); + free(label); +} + +TEST_F(AppInfoTest, app_info_get_localed_label_N) { + int ret = app_info_get_localed_label(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_icon_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_icon(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_get_icon)); + + char* icon = nullptr; + int ret = app_info_get_icon(app_info_, &icon); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(icon, nullptr); + ASSERT_EQ(std::string(icon), kIcon); + free(icon); +} + +TEST_F(AppInfoTest, app_info_get_icon_N) { + int ret = app_info_get_icon(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_package_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_pkgname(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_get_pkgname)); + + char* package = nullptr; + int ret = app_info_get_package(app_info_, &package); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(package, nullptr); + ASSERT_EQ(std::string(package), kPackageId); + free(package); +} + +TEST_F(AppInfoTest, app_info_get_package_N) { + int ret = app_info_get_package(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_get_type_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_apptype(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_get_apptype)); + + char* type = nullptr; + int ret = app_info_get_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(type, nullptr); + ASSERT_EQ(std::string(type), kAppType); + free(type); +} + +TEST_F(AppInfoTest, app_info_get_type_N) { + int ret = app_info_get_type(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +int fake_pkgmgrinfo_appinfo_get_component(pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) { + *component = PMINFO_UI_APP; + return PMINFO_R_OK; +} + +TEST_F(AppInfoTest, app_info_get_app_component_type_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_component(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) -> int { + *component = PMINFO_UI_APP; + return PMINFO_R_OK; + })); + app_info_app_component_type_e type; + int ret = app_info_get_app_component_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(type, APP_INFO_APP_COMPONENT_TYPE_UI_APP); + + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_component(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) -> int { + *component = PMINFO_SVC_APP; + return PMINFO_R_OK; + })); + ret = app_info_get_app_component_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(type, APP_INFO_APP_COMPONENT_TYPE_SERVICE_APP); + + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_component(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) -> int { + *component = PMINFO_WIDGET_APP; + return PMINFO_R_OK; + })); + ret = app_info_get_app_component_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(type, APP_INFO_APP_COMPONENT_TYPE_WIDGET_APP); + + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_component(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) -> int { + *component = PMINFO_WATCH_APP; + return PMINFO_R_OK; + })); + ret = app_info_get_app_component_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(type, APP_INFO_APP_COMPONENT_TYPE_WATCH_APP); + + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_component(_, _)) + .WillOnce(Invoke([](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_component* component) -> int { + *component = PMINFO_COMPONENT_BASED_APP; + return PMINFO_R_OK; + })); + ret = app_info_get_app_component_type(app_info_, &type); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(type, APP_INFO_APP_COMPONENT_TYPE_COMPONENT_BASED_APP); +} + +TEST_F(AppInfoTest, app_info_get_app_component_type_N) { + int ret = app_info_get_app_component_type(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_foreach_metadata_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_foreach_metadata(_, _, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_foreach_metadata)); + + bool touched = false; + int ret = app_info_foreach_metadata(app_info_, + [](const char* key, const char* value, void* user_data) { + auto* flag = static_cast<bool*>(user_data); + if (strcmp(key, kMetadataKey) == 0) + *flag = true; + + return true; + }, static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppInfoTest, app_info_foreach_metadata_N) { + int ret = app_info_foreach_metadata(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_nodisplay_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_is_nodisplay(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_is_nodisplay)); + + bool nodisplay = true; + int ret = app_info_is_nodisplay(app_info_, &nodisplay); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(nodisplay, false); +} + +TEST_F(AppInfoTest, app_info_is_nodisplay_N) { + int ret = app_info_is_nodisplay(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_enabled_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_is_enabled(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_is_enabled)); + + bool enabled = false; + int ret = app_info_is_enabled(app_info_, &enabled); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(enabled, true); +} + +TEST_F(AppInfoTest, app_info_is_enabled_N) { + int ret = app_info_is_enabled(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_equal_P) { + bool equal = false; + int ret = app_info_is_equal(app_info_, app_info_, &equal); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(equal, true); +} + +TEST_F(AppInfoTest, app_info_is_equal_N) { + int ret = app_info_is_equal(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_onboot_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_is_onboot(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_is_onboot)); + + bool onboot = true; + int ret = app_info_is_onboot(app_info_, &onboot); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(onboot, false); +} + +TEST_F(AppInfoTest, app_info_is_onboot_N) { + int ret = app_info_is_onboot(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_preload_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_is_preload(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_is_preload)); + + bool preload = false; + int ret = app_info_is_preload(app_info_, &preload); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(preload, true); +} + +TEST_F(AppInfoTest, app_info_is_preload_N) { + int ret = app_info_is_preload(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_is_support_ambient_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_is_support_ambient(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_is_support_ambient)); + + bool support_ambient = false; + int ret = app_info_is_support_ambient(app_info_, &support_ambient); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(support_ambient, true); +} + +TEST_F(AppInfoTest, app_info_is_support_ambient_N) { + int ret = app_info_is_support_ambient(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_clone_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_clone_appinfo(_, _)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_clone_appinfo)); + + app_info_h handle = nullptr; + int ret = app_info_clone(&handle, app_info_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + app_info_destroy(handle); +} + +TEST_F(AppInfoTest, app_info_clone_N) { + int ret = app_info_clone(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_foreach_category_P) { + EXPECT_CALL(GetMock<CynaraMock>(), cynara_initialize(_, _)) + .WillOnce( + Invoke([&](cynara** pp_cynara, const cynara_configuration* p_conf) { + *pp_cynara = reinterpret_cast<cynara*>(&dummy); + return CYNARA_API_SUCCESS; + })); + EXPECT_CALL(GetMock<CynaraMock>(), cynara_check(_, _, _, _, _)) + .WillOnce(Invoke( + [&](cynara* p_cynara, const char* client, const char* client_session, + const char* user, const char* privilege) { + return CYNARA_API_ACCESS_ALLOWED; + })); + EXPECT_CALL(GetMock<CynaraMock>(), cynara_finish(_)) + .WillOnce(Invoke([&](cynara* p_cynara) { return CYNARA_API_SUCCESS; })); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_foreach_category(_, _, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_app_category_list_cb callback, + void* user_data) { + callback(kCategory, user_data); + return PMINFO_R_OK; + })); + + bool touched = false; + int ret = app_info_foreach_category( + app_info_, + [](const char* category, void* user_data) { + bool* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppInfoTest, app_info_foreach_category_N) { + int ret = app_info_foreach_category(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_create_P) { + app_info_filter_h handle = nullptr; + int ret = app_info_filter_create(&handle); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + app_info_filter_destroy(handle); +} + +TEST_F(AppInfoTest, app_info_filter_create_N) { + int ret = app_info_filter_create(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_destroy_P) { + int ret = app_info_filter_destroy(app_info_filter_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + app_info_filter_ = nullptr; +} + +TEST_F(AppInfoTest, app_info_filter_destroy_N) { + int ret = app_info_filter_destroy(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_add_bool_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_filter_add_bool(_, _, _)) + .WillRepeatedly( + Invoke([&](pkgmgrinfo_appinfo_filter_h handle, const char* property, + const bool value) { + return PMINFO_R_OK; + })); + + int ret = app_info_filter_add_bool(app_info_filter_, + PACKAGE_INFO_PROP_APP_NODISPLAY, true); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_bool(app_info_filter_, + PACKAGE_INFO_PROP_APP_TASKMANAGE, true); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_bool(app_info_filter_, + PACKAGE_INFO_PROP_APP_DISABLED, true); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppInfoTest, app_info_filter_add_bool_N) { + int ret = app_info_filter_add_bool(nullptr, nullptr, false); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_add_string_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_filter_add_string(_, _, _)) + .WillRepeatedly( + Invoke([&](pkgmgrinfo_appinfo_filter_h handle, const char* key, + const char* value) { + return PMINFO_R_OK; + })); + + int ret = app_info_filter_add_string(app_info_filter_, + PACKAGE_INFO_PROP_APP_ID, kAppId); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_string(app_info_filter_, PACKAGE_INFO_PROP_APP_TYPE, + kAppType); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_string(app_info_filter_, + PACKAGE_INFO_PROP_APP_CATEGORY, kCategory); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_string( + app_info_filter_, PACKAGE_INFO_PROP_APP_INSTALLED_STORAGE, "internal"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_info_filter_add_string( + app_info_filter_, PACKAGE_INFO_PROP_APP_COMPONENT_TYPE, "uiapp"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppInfoTest, app_info_filter_add_string_N) { + int ret = app_info_filter_add_string(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_count_appinfo_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_filter_count(_, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_filter_h handle, int* count) { + *count = 1; + return PMINFO_R_OK; + })); + + int appinfo_count = 0; + int ret = app_info_filter_count_appinfo(app_info_filter_, &appinfo_count); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(appinfo_count, 1); +} + +TEST_F(AppInfoTest, app_info_filter_count_appinfo_N) { + int ret = app_info_filter_count_appinfo(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_filter_foreach_appinfo_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(_, _, _, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_filter_h handle, + pkgmgrinfo_app_list_cb callback, void* user_data, + uid_t uid) { + callback(app_info_, user_data); + return PMINFO_R_OK; + })); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_appid(_, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_h handle, char** appid) { + static char buf[256]; + snprintf(buf, sizeof(buf), "%s", kAppId); + *appid = buf; + return PMINFO_R_OK; + })); + + bool touched = false; + int ret = app_info_filter_foreach_appinfo( + app_info_filter_, + [](app_info_h handle, void* user_data) { + bool* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + reinterpret_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppInfoTest, app_info_filter_foreach_appinfo_N) { + int ret = app_info_filter_foreach_appinfo(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_create_P) { + app_info_metadata_filter_h handle = nullptr; + int ret = app_info_metadata_filter_create(&handle); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + + app_info_metadata_filter_destroy(handle); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_create_N) { + int ret = app_info_metadata_filter_create(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_destroy_P) { + int ret = app_info_metadata_filter_destroy(app_info_metadata_filter_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + app_info_metadata_filter_ = nullptr; +} + +TEST_F(AppInfoTest, app_info_metadata_filter_destroy_N) { + int ret = app_info_metadata_filter_destroy(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_add_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_metadata_filter_add(_, _, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_metadata_filter_h filter, + const char* key, + const char* value) { + return PMINFO_R_OK; + })); + + int ret = app_info_metadata_filter_add(app_info_metadata_filter_, + kMetadataKey, "true"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_add_N) { + int ret = app_info_metadata_filter_add(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_foreach_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_usr_metadata_filter_foreach(_, _, _, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_metadata_filter_h filter, + pkgmgrinfo_app_list_cb callback, void* user_data, + uid_t uid) { + callback(app_info_, user_data); + return PMINFO_R_OK; + })); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_appid(_, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_h handle, char** appid) { + static char buf[256]; + snprintf(buf, sizeof(buf), "%s", kAppId); + *appid = buf; + return PMINFO_R_OK; + })); + + bool touched = false; + int ret = app_info_metadata_filter_foreach( + app_info_metadata_filter_, + [](app_info_h handle, void* user_data) { + bool* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + reinterpret_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppInfoTest, app_info_metadata_filter_foreach_N) { + int ret = app_info_metadata_filter_foreach(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppInfoTest, app_info_foreach_res_control_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_foreach_res_control(_, _, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_h app_info, + pkgmgrinfo_app_res_control_list_cb callback, + void* user_data) { + callback("org.tizen.appfw.res", "1.0", "2.0", "true", user_data); + return PMINFO_R_OK; + })); + + bool touched = false; + int ret = app_info_foreach_res_control( + app_info_, + [](const char* res_type, const char* min_res_version, + const char* max_res_version, const char* auto_close, void* user_data) { + bool* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + reinterpret_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppInfoTest, app_info_foreach_res_control_N) { + int ret = app_info_foreach_res_control(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} diff --git a/tests/app-manager_unittests/app_manager_tests.cc b/tests/app-manager_unittests/app_manager_tests.cc new file mode 100644 index 0000000..2ae926b --- /dev/null +++ b/tests/app-manager_unittests/app_manager_tests.cc @@ -0,0 +1,1002 @@ +/* + * Copyright (c) 2023 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 <aul.h> +#include <app_manager.h> +#include <app_manager_extension.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> +#include <glib.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> + +#include <memory> + +#include "app-manager_unittests/mock/aul_mock.hh" +#include "app-manager_unittests/mock/glibc_mock.hh" +#include "app-manager_unittests/mock/pkgmgr_info_mock.hh" +#include "app-manager_unittests/mock/package_manager_mock.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::Invoke; + +namespace { + +constexpr const char kAppId[] = "org.tizen.hello"; +constexpr const char kPackageId[] = "org.tizen.helloworld"; +constexpr const char kSharedDataPath[] = + "/home/owner/apps_rw/org.tizen.helloworld/shared/data/"; +constexpr const char kSharedResourcePath[] = + "/home/owner/apps_rw/org.tizen.helloworld/shared/res/"; +constexpr const char kSharedTrustedPath[] = + "/home/owner/apps_rw/org.tizen.helloworld/shared/trusted/"; +constexpr const char kExternalSharedDataPath[] = + "/home/owner/media/external/org.tizen.helloworld/shared/data/"; + +int dummy; +app_context_h app_context_ = reinterpret_cast<app_context_h>(&dummy); +pkgmgrinfo_appinfo_h app_info_ = reinterpret_cast<pkgmgrinfo_appinfo_h>(&dummy); + +class Mocks : public ::testing::NiceMock<AulMock>, + public ::testing::NiceMock<GlibcMock>, + public ::testing::NiceMock<PkgmgrInfoMock>, + public ::testing::NiceMock<PackageManagerMock> {}; + +template <typename T> +class Event { + public: + Event(T cb, void* user_data) : cb_(cb), user_data_(user_data) {} + + void Invoke() { + cb_(app_context_, user_data_); + } + + T GetCallback() const { + return cb_; + } + + void* GetUserData() const { + return user_data_; + } + + private: + T cb_; + void* user_data_; +}; + +int fake_aul_app_context_create(const char* appid, + aul_app_context_h* app_context) { + *app_context = reinterpret_cast<aul_app_context_h>(&dummy); + return AUL_R_OK; +} + +int fake_aul_app_context_destroy(aul_app_context_h app_context) { + return AUL_R_OK; +} + +int fake_aul_app_context_get_app_id(aul_app_context_h app_context, + char** app_id) { + *app_id = strdup(kAppId); + return AUL_R_OK; +} + +int fake_aul_app_context_get_pid(aul_app_context_h app_context, pid_t* pid) { + *pid = getpid(); + return AUL_R_OK; +} + +int fake_aul_app_context_is_sub_app(aul_app_context_h app_context, + bool* is_sub_app) { + *is_sub_app = false; + return AUL_R_OK; +} + +int fake_aul_app_manager_foreach_app_context( + aul_app_manager_app_context_cb callback, void* user_data) { + callback(app_context_, user_data); + return AUL_R_OK; +} + +int fake_aul_app_event_create(aul_app_event_launched_cb launched_cb, + aul_app_event_terminated_cb terminated_cb, + void* user_data, aul_app_event_h* app_event) { + *app_event = reinterpret_cast<aul_app_event_h>(&dummy); + auto* launched_event = + new Event<aul_app_event_launched_cb>(launched_cb, user_data); + g_idle_add( + [](gpointer data) { + auto* event = static_cast<Event<aul_app_event_launched_cb>*>(data); + event->Invoke(); + delete event; + return G_SOURCE_REMOVE; + }, + launched_event); + + auto* terminated_event = + new Event<aul_app_event_terminated_cb>(terminated_cb, user_data); + g_idle_add( + [](gpointer data) { + auto* event = static_cast<Event<aul_app_event_terminated_cb>*>(data); + event->Invoke(); + delete event; + return G_SOURCE_REMOVE; + }, + terminated_event); + return AUL_R_OK; +} + +int fake_aul_app_event_destroy(aul_app_event_h app_event) { + return AUL_R_OK; +} + +int fake_aul_app_get_appid_bypid(int pid, char* appid, int len) { + snprintf(appid, len, "%s", kAppId); + return AUL_R_OK; +} + +int fake_pkgmgrinfo_appinfo_get_usr_appinfo(const char* appid, uid_t uid, + pkgmgrinfo_appinfo_h* handle) { + if (appid == nullptr) + return PMINFO_R_EINVAL; + + if (strcmp(appid, kPackageId) == 0) + return PMINFO_R_ERROR; + + *handle = app_info_; + return PMINFO_R_OK; +} + +int fake_pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) { + return PMINFO_R_OK; +} + +pkgmgr_client* fake_pkgmgr_client_new(pkgmgr_client_type type) { + return reinterpret_cast<pkgmgr_client*>(&dummy); +} + +int fake_pkgmgr_client_free(pkgmgr_client* client) { + return 0; +} + +} // namespace + +class AppManagerTest : public TestFixture { + public: + AppManagerTest() : TestFixture(std::make_unique<Mocks>()) {} + virtual ~AppManagerTest() {} + + void SetUp() override { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_app_id(_, _)) + .WillRepeatedly(Invoke(fake_aul_app_context_get_app_id)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_pid(_, _)) + .WillRepeatedly(Invoke(fake_aul_app_context_get_pid)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_is_sub_app(_, _)) + .WillRepeatedly(Invoke(fake_aul_app_context_is_sub_app)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_create(_, _)) + .WillRepeatedly(Invoke(fake_aul_app_context_create)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_destroy(_)) + .WillRepeatedly(Invoke(fake_aul_app_context_destroy)); + EXPECT_CALL(GetMock<PackageManagerMock>(), pkgmgr_client_new(_)) + .WillRepeatedly(Invoke(fake_pkgmgr_client_new)); + EXPECT_CALL(GetMock<PackageManagerMock>(), pkgmgr_client_free(_)) + .WillRepeatedly(Invoke(fake_pkgmgr_client_free)); + + loop_ = g_main_loop_new(nullptr, FALSE); + + int ret = app_manager_get_app_context(kAppId, &app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_manager_event_create(&event_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + } + + void TearDown() override { + if (loop_ != nullptr) { + g_main_loop_unref(loop_); + loop_ = nullptr; + } + + if (app_context_ != nullptr) { + app_context_destroy(app_context_); + app_context_ = nullptr; + } + + if (event_ != nullptr) { + app_manager_event_destroy(event_); + event_ = nullptr; + } + } + + void RunMainLoop() { + g_main_loop_run(loop_); + } + + void QuitMainLoop() { + g_main_loop_quit(loop_); + } + + int count_ = 0; + app_context_h app_context_ = nullptr; + app_manager_event_h event_ = nullptr; + + private: + GMainLoop* loop_ = nullptr; +}; + +TEST_F(AppManagerTest, app_manager_set_app_context_event_cb_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_manager_foreach_app_context(_, _)) + .WillOnce(Invoke(fake_aul_app_manager_foreach_app_context)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_event_create(_, _, _, _)) + .WillOnce(Invoke(fake_aul_app_event_create)); + EXPECT_CALL(GetMock<AulMock>(), aul_app_event_destroy(_)) + .WillOnce(Invoke(fake_aul_app_event_destroy)); + + count_ = 0; + int ret = app_manager_set_app_context_event_cb( + [](app_context_h app_context, app_context_event_e event, + void* user_data) { + auto* test = static_cast<AppManagerTest*>(user_data); + test->count_++; + if (test->count_ == 2) + test->QuitMainLoop(); + }, this); + RunMainLoop(); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(count_, 2); + app_manager_unset_app_context_event_cb(); +} + +TEST_F(AppManagerTest, app_manager_set_app_context_event_cb_N) { + int ret = app_manager_set_app_context_event_cb(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_foreach_app_context_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_manager_foreach_app_context(_, _)) + .WillOnce(Invoke(fake_aul_app_manager_foreach_app_context)); + + bool touched = false; + int ret = app_manager_foreach_app_context( + [](app_context_h app_context, void* user_data) { + auto* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppManagerTest, app_manager_foreach_app_context_N) { + int ret = app_manager_foreach_app_context(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_foreach_runnning_app_context_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_manager_foreach_all_app_context(_, _)) + .WillOnce(Invoke(fake_aul_app_manager_foreach_app_context)); + + bool touched = false; + int ret = app_manager_foreach_running_app_context( + [](app_context_h app_context, void* user_data) { + auto* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppManagerTest, app_manager_foreach_running_app_context_N) { + int ret = app_manager_foreach_running_app_context(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_app_context_P) { + app_context_h app_context = nullptr; + int ret = app_manager_get_app_context(kAppId, &app_context); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(app_context, nullptr); + app_context_destroy(app_context); +} + +TEST_F(AppManagerTest, app_manager_get_app_context_N) { + int ret = app_manager_get_app_context(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_resume_app_P1) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_inst_id(_, _)) + .WillOnce( + Invoke([&](aul_app_context_h app_context, char** inst_id) { + *inst_id = strdup("test_id"); + return AUL_R_OK; + })); + + EXPECT_CALL(GetMock<AulMock>(), aul_resume_app_by_instance_id(_, _)) + .WillOnce(Invoke( + [&](const char* appid, const char* inst_id) { return AUL_R_OK; })); + + int ret = app_manager_resume_app(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_resume_app_P2) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_get_inst_id(_, _)) + .WillOnce( + Invoke([&](aul_app_context_h app_context, char** inst_id) { + *inst_id = nullptr; + return AUL_R_ERROR; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_app_is_running(_)) + .WillOnce(Invoke([&](const char* appid) { return 1; })); + EXPECT_CALL(GetMock<AulMock>(), aul_resume_app(_)) + .WillOnce(Invoke( + [&](const char* appid) { return AUL_R_OK; })); + + int ret = app_manager_resume_app(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_resume_app_N) { + int ret = app_manager_resume_app(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_foreach_app_info_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_get_usr_installed_list(_, _, _)) + .WillOnce(Invoke( + [&](pkgmgrinfo_app_list_cb callback, uid_t uid, void* user_data) { + callback(app_info_, user_data); + return PMINFO_R_OK; + })); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_get_appid(_, _)) + .WillOnce(Invoke([&](pkgmgrinfo_appinfo_h handle, char** appid) { + static char buf[256]; + if (buf[0] == '\0') snprintf(buf, sizeof(buf), "%s", kAppId); + + *appid = buf; + return PMINFO_R_OK; + })); + + bool touched = false; + int ret = app_manager_foreach_app_info( + [](app_info_h app_info, void* user_data) { + auto* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppManagerTest, app_manager_foreach_app_info_N) { + int ret = app_manager_foreach_app_info(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_app_info_P) { + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), + pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)) + .WillRepeatedly(Invoke(fake_pkgmgrinfo_appinfo_get_usr_appinfo)); + EXPECT_CALL(GetMock<PkgmgrInfoMock>(), pkgmgrinfo_appinfo_destroy_appinfo(_)) + .WillOnce(Invoke(fake_pkgmgrinfo_appinfo_destroy_appinfo)); + + app_info_h app_info = nullptr; + int ret = app_manager_get_app_info(kAppId, &app_info); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(app_info, nullptr); + + app_info_destroy(app_info); +} + +TEST_F(AppManagerTest, app_manager_get_app_info_N) { + int ret = app_manager_get_app_info(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_app_id_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_get_appid_bypid(_, _, _)) + .WillOnce(Invoke(fake_aul_app_get_appid_bypid)); + + char* app_id = nullptr; + int ret = app_manager_get_app_id(getpid(), &app_id); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(app_id), kAppId); + free(app_id); +} + +TEST_F(AppManagerTest, app_manager_terminate_app_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_terminate_pid(_)) + .WillOnce(Invoke([&](int pid) { return AUL_R_OK; })); + + int ret = app_manager_terminate_app(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_terminate_app_N) { + int ret = app_manager_terminate_app(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_request_terminate_bg_app_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_terminate_bgapp_pid(_)) + .WillOnce(Invoke([&](int pid) { return AUL_R_OK; })); + + int ret = app_manager_request_terminate_bg_app(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_request_terminate_bg_app_N) { + int ret = app_manager_request_terminate_bg_app(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_is_running_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_is_running(_)) + .WillOnce(Invoke([&](const char* appid) { return 1; })); + + bool running = false; + int ret = app_manager_is_running(kAppId, &running); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(running, true); +} + +TEST_F(AppManagerTest, app_manager_is_running_N) { + int ret = app_manager_is_running(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_shared_data_path_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_get_app_shared_data_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + *path = strdup(kSharedDataPath); + return AUL_R_OK; + })); + + char* path = nullptr; + int ret = app_manager_get_shared_data_path(kAppId, &path); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(path), kSharedDataPath); + free(path); +} + +TEST_F(AppManagerTest, app_manager_get_shared_data_path_N) { + int ret = app_manager_get_shared_data_path(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_shared_resource_path_P) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_shared_resource_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + *path = strdup(kSharedResourcePath); + return AUL_R_OK; + })); + + char* path = nullptr; + int ret = app_manager_get_shared_resource_path(kAppId, &path); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(path), kSharedResourcePath); + free(path); +} + +TEST_F(AppManagerTest, app_manager_get_shared_resource_path_N) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_shared_resource_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + return AUL_R_EINVAL; + })); + + int ret = app_manager_get_shared_resource_path(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_shared_trusted_path_P) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_shared_trusted_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + *path = strdup(kSharedTrustedPath); + return AUL_R_OK; + })); + + char* path = nullptr; + int ret = app_manager_get_shared_trusted_path(kAppId, &path); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(path), kSharedTrustedPath); + free(path); +} + +TEST_F(AppManagerTest, app_manager_get_shared_trusted_path_N) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_shared_trusted_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + return AUL_R_EINVAL; + })); + + int ret = app_manager_get_shared_trusted_path(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_external_shared_data_path_P) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_external_shared_data_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + *path = strdup(kExternalSharedDataPath); + return AUL_R_OK; + })); + + char* path = nullptr; + int ret = app_manager_get_external_shared_data_path(kAppId, &path); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(std::string(path), kExternalSharedDataPath); + free(path); +} + +TEST_F(AppManagerTest, app_manager_get_external_shared_data_path_N) { + EXPECT_CALL(GetMock<AulMock>(), + aul_get_app_external_shared_data_path_by_appid(_, _)) + .WillOnce(Invoke([&](const char* appid, char** path) { + return AUL_R_EINVAL; + })); + + int ret = app_manager_get_external_shared_data_path(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_set_splash_screen_display_P) { + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_enable_splash_screen(_, _)) + .WillOnce(Invoke([&](pkgmgr_client* client, const char* appid) { + return PKGMGR_R_OK; + })); + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_disable_splash_screen(_, _)) + .WillOnce(Invoke([&](pkgmgr_client* client, const char* appid) { + return PKGMGR_R_OK; + })); + + int ret = app_manager_set_splash_screen_display(kAppId, true); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_manager_set_splash_screen_display(kAppId, false); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_set_splash_screen_display_N) { + int ret = app_manager_set_splash_screen_display(nullptr, true); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_set_app_icon_P) { + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_set_app_icon(_, _, _)) + .WillOnce(Invoke([&](pkgmgr_client* client, char* appid, + char* icon_path) { + return PKGMGR_R_OK; + })); + + int ret = app_manager_set_app_icon(kAppId, "icon.png"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_set_app_icon_N) { + int ret = app_manager_set_app_icon(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_event_create_P) { + app_manager_event_h handle = nullptr; + int ret = app_manager_event_create(&handle); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + app_manager_event_destroy(handle); +} + +TEST_F(AppManagerTest, app_manager_event_create_N) { + int ret = app_manager_event_create(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_event_set_status_P) { + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_set_status_type(_, _)) + .WillRepeatedly(Invoke( + [&](pkgmgr_client* client, int status_type) { return PKGMGR_R_OK; })); + + int ret = app_manager_event_set_status(event_, + APP_MANAGER_EVENT_STATUS_TYPE_ENABLE); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + + ret = app_manager_event_set_status(event_, + APP_MANAGER_EVENT_STATUS_TYPE_DISABLE); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_event_set_status_N) { + int ret = app_manager_event_set_status(nullptr, + APP_MANAGER_EVENT_STATUS_TYPE_ENABLE); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_set_event_cb_P) { + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_listen_app_status(_, _, _)) + .WillOnce(Invoke([&](pkgmgr_client* client, pkgmgr_app_handler callback, + void* user_data) { + auto* start_event = new Event<pkgmgr_app_handler>(callback, user_data); + g_idle_add( + [](gpointer data) { + auto* event = static_cast<Event<pkgmgr_app_handler>*>(data); + auto cb = event->GetCallback(); + cb(5001, 1, "tpk", kPackageId, kAppId, "start", "enable_app", + "enable", event->GetUserData()); + delete event; + return G_SOURCE_REMOVE; + }, + start_event); + + auto* end_event = new Event<pkgmgr_app_handler>(callback, user_data); + g_idle_add( + [](gpointer data) { + auto* event = static_cast<Event<pkgmgr_app_handler>*>(data); + auto cb = event->GetCallback(); + cb(5001, 1, "tpk", kPackageId, kAppId, "end", "ok", "enable", + event->GetUserData()); + delete event; + return G_SOURCE_REMOVE; + }, + end_event); + return 1; + })); + + count_ = 0; + int ret = app_manager_set_event_cb( + event_, + [](const char* type, const char* appid, + app_manager_event_type_e event_type, + app_manager_event_state_e event_state, app_manager_event_h handle, + void* user_data) { + auto* test = static_cast<AppManagerTest*>(user_data); + test->count_++; + if (test->count_ == 2) + test->QuitMainLoop(); + }, + this); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + RunMainLoop(); + ASSERT_EQ(count_, 2); +} + +TEST_F(AppManagerTest, app_manager_set_event_cb_N) { + int ret = app_manager_set_event_cb(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_unset_event_cb_P) { + EXPECT_CALL(GetMock<PackageManagerMock>(), + pkgmgr_client_remove_listen_status(_)) + .WillOnce(Invoke([&](pkgmgr_client* client) { return PKGMGR_R_OK; })); + + int ret = app_manager_unset_event_cb(event_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_unset_event_cb_N) { + int ret = app_manager_unset_event_cb(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_event_destroy_P) { + int ret = app_manager_event_destroy(event_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + event_ = nullptr; +} + +TEST_F(AppManagerTest, app_manager_event_destroy_N) { + int ret = app_manager_event_destroy(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_set_and_unset_app_context_status_cb_P) { + EXPECT_CALL(GetMock<AulMock>(), + aul_app_event_create_with_appid(_, _, _, _, _)) + .WillOnce( + Invoke([&](const char* appid, aul_app_event_launched_cb launched_cb, + aul_app_event_terminated_cb terminated_cb, void* user_data, + aul_app_event_h* app_event) { + auto* launched_event = + new Event<aul_app_event_launched_cb>(launched_cb, user_data); + g_idle_add( + [](gpointer data) { + auto* event = + static_cast<Event<aul_app_event_launched_cb>*>(data); + event->Invoke(); + delete event; + return G_SOURCE_REMOVE; + }, + launched_event); + + auto* terminated_event = new Event<aul_app_event_terminated_cb>( + terminated_cb, user_data); + g_idle_add( + [](gpointer data) { + auto* event = + static_cast<Event<aul_app_event_terminated_cb>*>(data); + event->Invoke(); + delete event; + return G_SOURCE_REMOVE; + }, + terminated_event); + + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_app_event_destroy(_)) + .WillOnce(Invoke([&](aul_app_event_h app_event) { return AUL_R_OK; })); + + auto callback = [](app_context_h app_context, app_context_status_e status, + void* user_data) { + auto* test = static_cast<AppManagerTest*>(user_data); + test->count_++; + if (test->count_ == 2) test->QuitMainLoop(); + }; + + count_ = 0; + int ret = app_manager_set_app_context_status_cb(callback, kAppId, this); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + RunMainLoop(); + ASSERT_EQ(count_, 2); + + ret = app_manager_unset_app_context_status_cb(callback, kAppId); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_set_and_unset_app_context_status_cb_N) { + int ret = app_manager_set_app_context_status_cb(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); + + ret = app_manager_unset_app_context_status_cb(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_app_context_by_instance_id_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_create_with_inst_id(_, _, _)) + .WillOnce(Invoke([&](const char* appid, const char* inst_id, + aul_app_context_h* app_context) { + *app_context = reinterpret_cast<aul_app_context_h>(&dummy); + return AUL_R_OK; + })); + + app_context_h handle = nullptr; + int ret = + app_manager_get_app_context_by_instance_id(kAppId, "test-id", &handle); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + app_context_destroy(handle); +} + +TEST_F(AppManagerTest, app_manager_get_app_context_by_instance_id_N) { + int ret = + app_manager_get_app_context_by_instance_id(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_get_focused_app_context_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_get_focused_pid(_)) + .WillOnce(Invoke([&](int* pid) { + *pid = getpid(); + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_app_context_create_with_pid(_, _)) + .WillOnce(Invoke([&](int pid, aul_app_context_h* app_context) { + *app_context = reinterpret_cast<aul_app_context_h>(&dummy); + return AUL_R_OK; + })); + + app_context_h handle = nullptr; + int ret = app_manager_get_focused_app_context(&handle); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_NE(handle, nullptr); + + app_context_destroy(handle); +} + +TEST_F(AppManagerTest, app_manager_get_focused_app_context_N) { + int ret = app_manager_get_focused_app_context(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_attach_window_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_attach(_, _)) + .WillOnce(Invoke([&](const char* parent_app_id, + const char* child_app_id) { + if (parent_app_id == nullptr || child_app_id == nullptr) + return AUL_R_EINVAL; + + return AUL_R_OK; + })); + + int ret = app_manager_attach_window("org.tizen.parent", "org.tizen.child"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_attach_window_N) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_attach(_, _)) + .WillOnce( + Invoke([&](const char* parent_app_id, const char* child_app_id) { + if (parent_app_id == nullptr || child_app_id == nullptr) + return AUL_R_EINVAL; + + return AUL_R_OK; + })); + + int ret = app_manager_attach_window(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_detach_window_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_detach(_)) + .WillOnce(Invoke([&](const char* app_id) { + if (app_id == nullptr) return AUL_R_EINVAL; + return AUL_R_OK; + })); + + int ret = app_manager_detach_window("org.tizen.child"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_detach_window_N) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_detach(_)) + .WillOnce(Invoke([&](const char* app_id) { + if (app_id == nullptr) return AUL_R_EINVAL; + return AUL_R_OK; + })); + + int ret = app_manager_detach_window(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_foreach_visible_app_context_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_manager_foreach_all_app_context(_, _)) + .WillOnce( + Invoke([&](aul_app_manager_app_context_cb callback, void* user_data) { + callback(app_context_, user_data); + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_window_stack_get(_)) + .WillOnce(Invoke([&](aul_window_stack_h* handle) { + *handle = reinterpret_cast<aul_window_stack_h>(&dummy); + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_window_stack_foreach(_, _, _)) + .WillOnce(Invoke([&](aul_window_stack_h handle, + void (*callback)(aul_window_info_h info, void* data), + void* user_data) { + aul_window_info_h info = reinterpret_cast<aul_window_info_h>(&dummy); + callback(info, user_data); + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_window_stack_del(_)) + .WillOnce(Invoke([&](aul_window_stack_h handle) { return AUL_R_OK; })); + EXPECT_CALL(GetMock<AulMock>(), aul_window_info_get_visibility(_, _)) + .WillOnce(Invoke([&](aul_window_info_h info, int* visibility) { + *visibility = 1; + return AUL_R_OK; + })); + EXPECT_CALL(GetMock<AulMock>(), aul_window_info_get_pid(_, _)) + .WillOnce(Invoke([&](aul_window_info_h info, int* pid) { + *pid = getpid(); + return AUL_R_OK; + })); + + bool touched = false; + int ret = app_manager_foreach_visible_app_context( + [](app_context_h app_context, void* user_data) { + bool* flag = static_cast<bool*>(user_data); + *flag = true; + return true; + }, + static_cast<void*>(&touched)); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); + ASSERT_EQ(touched, true); +} + +TEST_F(AppManagerTest, app_manager_foreach_visible_app_context_N) { + int ret = app_manager_foreach_visible_app_context(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_add_app_group_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_group_add(_)) + .WillOnce(Invoke([&](int win_id) { return AUL_R_OK; })); + + int ret = app_manager_add_app_group(1); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_add_app_group_N) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_group_add(_)) + .WillOnce(Invoke([&](int win_id) { return AUL_R_EINVAL; })); + + int ret = app_manager_add_app_group(0); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_remove_app_group_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_group_remove(_)) + .WillOnce(Invoke([&](int win_id) { return AUL_R_OK; })); + + int ret = app_manager_remove_app_group(1); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_remove_app_group_N) { + EXPECT_CALL(GetMock<AulMock>(), aul_app_group_remove(_)) + .WillOnce(Invoke([&](int win_id) { return AUL_R_EINVAL; })); + + int ret = app_manager_remove_app_group(0); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_attach_window_below_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_attach_below(_, _)) + .WillOnce( + Invoke([&](const char* parent_app_id, const char* child_app_id) { + if (parent_app_id == nullptr || child_app_id == nullptr) + return AUL_R_EINVAL; + + return AUL_R_OK; + })); + + int ret = + app_manager_attach_window_below("org.tizen.parent", "org.tizen.child"); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_attach_window_below_N) { + EXPECT_CALL(GetMock<AulMock>(), aul_window_attach_below(_, _)) + .WillOnce( + Invoke([&](const char* parent_app_id, const char* child_app_id) { + if (parent_app_id == nullptr || child_app_id == nullptr) + return AUL_R_EINVAL; + + return AUL_R_OK; + })); + + int ret = app_manager_attach_window_below(nullptr, nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} + +TEST_F(AppManagerTest, app_manager_terminate_app_without_restarting_P) { + EXPECT_CALL(GetMock<AulMock>(), aul_terminate_pid_without_restart(_)) + .WillOnce(Invoke([&](int pid) { return AUL_R_OK; })); + + int ret = app_manager_terminate_app_without_restarting(app_context_); + ASSERT_EQ(ret, APP_MANAGER_ERROR_NONE); +} + +TEST_F(AppManagerTest, app_manager_terminate_app_without_restarting_N) { + int ret = app_manager_terminate_app_without_restarting(nullptr); + ASSERT_EQ(ret, APP_MANAGER_ERROR_INVALID_PARAMETER); +} diff --git a/tests/app-manager_unittests/main.cc b/tests/app-manager_unittests/main.cc new file mode 100644 index 0000000..ffae1fc --- /dev/null +++ b/tests/app-manager_unittests/main.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 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 <dlog.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> +#include <stdarg.h> +#include <stdio.h> + +extern "C" int __dlog_sec_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; +} + +extern "C" int dlog_vprint(log_priority prio, const char* tag, const char* fmt, + va_list ap) { + printf("%s:", tag); + vprintf(fmt, ap); + printf("\n"); + return 0; +} + +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; +} + +int main(int argc, char** argv) { + try { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); + } catch (std::exception const &e) { + std::cout << "test_main caught exception: " << e.what() << std::endl; + return -1; + } +} diff --git a/tests/app-manager_unittests/mock/aul_mock.cc b/tests/app-manager_unittests/mock/aul_mock.cc new file mode 100644 index 0000000..9d0f7a5 --- /dev/null +++ b/tests/app-manager_unittests/mock/aul_mock.cc @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/aul_mock.hh" + +#include "app-manager_unittests/mock/mock_hook.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +// aul_app_manager.h +extern "C" int aul_app_manager_foreach_app_context( + aul_app_manager_app_context_cb callback, void* user_data) { + return MOCK_HOOK_P2(AulMock, aul_app_manager_foreach_app_context, callback, + user_data); +} + +extern "C" int aul_app_manager_foreach_all_app_context( + aul_app_manager_app_context_cb callback, void* user_data) { + return MOCK_HOOK_P2(AulMock, aul_app_manager_foreach_all_app_context, + callback, user_data); +} + +extern "C" int aul_app_context_create(const char* app_id, + aul_app_context_h* app_context) { + return MOCK_HOOK_P2(AulMock, aul_app_context_create, app_id, app_context); +} + +extern "C" int aul_app_context_create_with_inst_id( + const char *app_id, const char *inst_id, aul_app_context_h *app_context) { + return MOCK_HOOK_P3(AulMock, aul_app_context_create_with_inst_id, app_id, + inst_id, app_context); +} + +extern "C" int aul_app_context_create_with_pid(int pid, + aul_app_context_h* app_context) { + return MOCK_HOOK_P2(AulMock, aul_app_context_create_with_pid, pid, + app_context); +} + +extern "C" int aul_app_context_destroy(aul_app_context_h app_context) { + return MOCK_HOOK_P1(AulMock, aul_app_context_destroy, app_context); +} + +extern "C" int aul_app_context_clone(aul_app_context_h app_context, + aul_app_context_h* clone) { + return MOCK_HOOK_P2(AulMock, aul_app_context_clone, app_context, clone); +} + +extern "C" int aul_app_context_get_app_id(aul_app_context_h app_context, + char** app_id) { + return MOCK_HOOK_P2(AulMock, aul_app_context_get_app_id, app_context, app_id); +} + +extern "C" int aul_app_context_get_pkg_id(aul_app_context_h app_context, + char** pkg_id) { + return MOCK_HOOK_P2(AulMock, aul_app_context_get_pkg_id, app_context, pkg_id); +} + +extern "C" int aul_app_context_get_inst_id(aul_app_context_h app_context, + char** inst_id) { + return MOCK_HOOK_P2(AulMock, aul_app_context_get_inst_id, app_context, + inst_id); +} + +extern "C" int aul_app_context_get_pid(aul_app_context_h app_context, + int* pid) { + return MOCK_HOOK_P2(AulMock, aul_app_context_get_pid, app_context, pid); +} + +extern "C" int aul_app_context_get_status(aul_app_context_h app_context, + int* status) { + return MOCK_HOOK_P2(AulMock, aul_app_context_get_status, app_context, status); +} + +extern "C" int aul_app_context_is_sub_app(aul_app_context_h app_context, + bool* is_sub_app) { + return MOCK_HOOK_P2(AulMock, aul_app_context_is_sub_app, app_context, + is_sub_app); +} + +extern "C" int aul_app_context_is_running(aul_app_context_h app_context, + bool* is_running) { + return MOCK_HOOK_P2(AulMock, aul_app_context_is_running, app_context, + is_running); +} + +extern "C" int aul_app_context_is_equal(aul_app_context_h lhs, + aul_app_context_h rhs, bool* equal) { + return MOCK_HOOK_P3(AulMock, aul_app_context_is_equal, lhs, rhs, equal); +} + +// aul_app_event.h +extern "C" int aul_app_event_create_with_appid( + const char* app_id, aul_app_event_launched_cb launched_cb, + aul_app_event_terminated_cb terminated_cb, void* user_data, + aul_app_event_h* app_event) { + return MOCK_HOOK_P5(AulMock, aul_app_event_create_with_appid, app_id, + launched_cb, terminated_cb, user_data, app_event); +} + +extern "C" int aul_app_event_create(aul_app_event_launched_cb launched_cb, + aul_app_event_terminated_cb terminated_cb, + void* user_data, + aul_app_event_h* app_event) { + return MOCK_HOOK_P4(AulMock, aul_app_event_create, launched_cb, terminated_cb, + user_data, app_event); +} + +extern "C" int aul_app_event_destroy(aul_app_event_h app_event) { + return MOCK_HOOK_P1(AulMock, aul_app_event_destroy, app_event); +} + +// aul_window.h +extern "C" int aul_window_stack_get(aul_window_stack_h* handle) { + return MOCK_HOOK_P1(AulMock, aul_window_stack_get, handle); +} + +extern "C" int aul_window_stack_del(aul_window_stack_h handle) { + return MOCK_HOOK_P1(AulMock, aul_window_stack_del, handle); +} + +extern "C" int aul_window_stack_foreach(aul_window_stack_h handle, + void (*iter_cb)(aul_window_info_h info, + void* data), + void* data) { + return MOCK_HOOK_P3(AulMock, aul_window_stack_foreach, handle, iter_cb, data); +} + +extern "C" int aul_window_info_get_visibility(aul_window_info_h info, + int* visibility) { + return MOCK_HOOK_P2(AulMock, aul_window_info_get_visibility, info, + visibility); +} + +extern "C" int aul_window_info_get_pid(aul_window_info_h info, int* pid) { + return MOCK_HOOK_P2(AulMock, aul_window_info_get_pid, info, pid); +} + +extern "C" int aul_window_get_focused_pid(int* pid) { + return MOCK_HOOK_P1(AulMock, aul_window_get_focused_pid, pid); +} + +extern "C" int aul_window_attach(const char* parent_app_id, + const char* child_app_id) { + return MOCK_HOOK_P2(AulMock, aul_window_attach, parent_app_id, child_app_id); +} + +extern "C" int aul_window_attach_below(const char* parent_app_id, + const char* child_app_id) { + return MOCK_HOOK_P2(AulMock, aul_window_attach_below, parent_app_id, + child_app_id); +} + +extern "C" int aul_window_detach(const char* app_id) { + return MOCK_HOOK_P1(AulMock, aul_window_detach, app_id); +} + +// aul.h +extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int len) { + return MOCK_HOOK_P3(AulMock, aul_app_get_appid_bypid, pid, appid, len); +} + +extern "C" int aul_resume_app_by_instance_id(const char* appid, + const char* inst_id) { + return MOCK_HOOK_P2(AulMock, aul_resume_app_by_instance_id, appid, inst_id); +} + +extern "C" int aul_app_is_running(const char* appid) { + return MOCK_HOOK_P1(AulMock, aul_app_is_running, appid); +} + +extern "C" int aul_resume_app(const char* appid) { + return MOCK_HOOK_P1(AulMock, aul_resume_app, appid); +} + +extern "C" int aul_terminate_pid(int pid) { + return MOCK_HOOK_P1(AulMock, aul_terminate_pid, pid); +} + +extern "C" int aul_terminate_bgapp_pid(int pid) { + return MOCK_HOOK_P1(AulMock, aul_terminate_bgapp_pid, pid); +} + +extern "C" int aul_get_app_shared_data_path_by_appid(const char* appid, + char** path) { + return MOCK_HOOK_P2(AulMock, aul_get_app_shared_data_path_by_appid, appid, + path); +} + +extern "C" int aul_get_app_shared_resource_path_by_appid(const char* appid, + char** path) { + return MOCK_HOOK_P2(AulMock, aul_get_app_shared_resource_path_by_appid, appid, + path); +} + +extern "C" int aul_get_app_shared_trusted_path_by_appid(const char* appid, + char** path) { + return MOCK_HOOK_P2(AulMock, aul_get_app_shared_trusted_path_by_appid, appid, + path); +} + +extern "C" int aul_get_app_external_shared_data_path_by_appid(const char* appid, + char** path) { + return MOCK_HOOK_P2(AulMock, aul_get_app_external_shared_data_path_by_appid, + appid, path); +} + +extern "C" int aul_terminate_pid_without_restart(int pid) { + return MOCK_HOOK_P1(AulMock, aul_terminate_pid_without_restart, pid); +} + +// aul_svc.h +extern "C" int aul_svc_get_appid_by_alias_appid(const char* alias_appid, + char** appid) { + return MOCK_HOOK_P2(AulMock, aul_svc_get_appid_by_alias_appid, alias_appid, + appid); +} + +// aul_app_group.h + +extern "C" int aul_app_group_add(int win_id) { + return MOCK_HOOK_P1(AulMock, aul_app_group_add, win_id); +} + +extern "C" int aul_app_group_remove(int win_id) { + return MOCK_HOOK_P1(AulMock, aul_app_group_remove, win_id); +} diff --git a/tests/app-manager_unittests/mock/aul_mock.hh b/tests/app-manager_unittests/mock/aul_mock.hh new file mode 100644 index 0000000..73e2372 --- /dev/null +++ b/tests/app-manager_unittests/mock/aul_mock.hh @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_AUL_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_AUL_MOCK_HH_ + +#include <aul.h> +#include <aul_app_event.h> +#include <aul_app_group.h> +#include <aul_app_manager.h> +#include <aul_svc.h> +#include <aul_window.h> +#include <gmock/gmock.h> + +#include "app-manager_unittests/mock/module_mock.hh" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD2(aul_app_manager_foreach_app_context, + int(aul_app_manager_app_context_cb, void*)); + MOCK_METHOD2(aul_app_manager_foreach_all_app_context, + int(aul_app_manager_app_context_cb, void*)); + MOCK_METHOD2(aul_app_context_create, int(const char*, aul_app_context_h*)); + MOCK_METHOD1(aul_app_context_destroy, int(aul_app_context_h)); + MOCK_METHOD2(aul_app_context_get_app_id, int(aul_app_context_h, char**)); + MOCK_METHOD2(aul_app_context_get_pid, int(aul_app_context_h, pid_t*)); + MOCK_METHOD2(aul_app_context_get_pkg_id, int(aul_app_context_h, char**)); + MOCK_METHOD2(aul_app_context_get_status, int(aul_app_context_h, int*)); + MOCK_METHOD2(aul_app_context_is_running, int(aul_app_context_h, bool*)); + MOCK_METHOD3(aul_app_context_is_equal, + int(aul_app_context_h, aul_app_context_h, bool*)); + MOCK_METHOD2(aul_app_context_is_sub_app, int(aul_app_context_h, bool*)); + MOCK_METHOD2(aul_app_context_clone, + int(aul_app_context_h, aul_app_context_h*)); + MOCK_METHOD3(aul_app_context_create_with_inst_id, + int(const char*, const char*, aul_app_context_h*)); + MOCK_METHOD2(aul_app_context_get_inst_id, int(aul_app_context_h, char**)); + MOCK_METHOD2(aul_app_context_create_with_pid, int(pid_t, aul_app_context_h*)); + + MOCK_METHOD4(aul_app_event_create, + int(aul_app_event_launched_cb, aul_app_event_terminated_cb, + void*, aul_app_event_h*)); + MOCK_METHOD1(aul_app_event_destroy, int(aul_app_event_h)); + MOCK_METHOD5(aul_app_event_create_with_appid, + int(const char*, aul_app_event_launched_cb, + aul_app_event_terminated_cb, void*, aul_app_event_h*)); + + MOCK_METHOD2(aul_window_info_get_visibility, int(aul_window_info_h, int*)); + MOCK_METHOD2(aul_window_info_get_pid, int(aul_window_info_h, int*)); + MOCK_METHOD1(aul_window_stack_get, int(aul_window_stack_h*)); + MOCK_METHOD3(aul_window_stack_foreach, + int(aul_window_stack_h, void (*)(aul_window_info_h, void*), + void*)); + MOCK_METHOD1(aul_window_stack_del, int(aul_window_stack_h)); + MOCK_METHOD1(aul_window_get_focused_pid, int(int*)); + MOCK_METHOD2(aul_window_attach, int(const char*, const char*)); + MOCK_METHOD1(aul_window_detach, int(const char*)); + MOCK_METHOD2(aul_window_attach_below, int(const char*, const char*)); + + MOCK_METHOD3(aul_app_get_appid_bypid, int(int, char*, int)); + MOCK_METHOD2(aul_svc_get_appid_by_alias_appid, int(const char*, char**)); + MOCK_METHOD2(aul_resume_app_by_instance_id, int(const char*, const char*)); + MOCK_METHOD1(aul_app_is_running, int(const char*)); + MOCK_METHOD1(aul_resume_app, int(const char*)); + MOCK_METHOD1(aul_terminate_pid, int(int)); + MOCK_METHOD1(aul_terminate_bgapp_pid, int(int)); + MOCK_METHOD2(aul_get_app_shared_data_path_by_appid, int(const char*, char**)); + MOCK_METHOD2(aul_get_app_shared_resource_path_by_appid, + int(const char*, char**)); + MOCK_METHOD2(aul_get_app_shared_trusted_path_by_appid, + int(const char*, char**)); + MOCK_METHOD2(aul_get_app_external_shared_data_path_by_appid, + int(const char*, char**)); + MOCK_METHOD1(aul_terminate_pid_without_restart, int(int)); + + MOCK_METHOD1(aul_app_group_add, int(int)); + MOCK_METHOD1(aul_app_group_remove, int(int)); +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_AUL_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/cynara_mock.cc b/tests/app-manager_unittests/mock/cynara_mock.cc new file mode 100644 index 0000000..032aa5f --- /dev/null +++ b/tests/app-manager_unittests/mock/cynara_mock.cc @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/cynara_mock.hh" + +#include "app-manager_unittests/mock/mock_hook.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +extern "C" int cynara_initialize(cynara** pp_cynara, + const cynara_configuration* p_conf) { + return MOCK_HOOK_P2(CynaraMock, cynara_initialize, pp_cynara, p_conf); +} + +extern "C" int cynara_check(cynara* p_cynara, const char* client, + const char* client_session, const char* user, + const char* privilege) { + return MOCK_HOOK_P5(CynaraMock, cynara_check, p_cynara, client, + client_session, user, privilege); +} + +extern "C" int cynara_finish(cynara* p_cynara) { + return MOCK_HOOK_P1(CynaraMock, cynara_finish, p_cynara); +} diff --git a/tests/app-manager_unittests/mock/cynara_mock.hh b/tests/app-manager_unittests/mock/cynara_mock.hh new file mode 100644 index 0000000..fa9883a --- /dev/null +++ b/tests/app-manager_unittests/mock/cynara_mock.hh @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_CYNARA_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_CYNARA_MOCK_HH_ + +#include <cynara-client.h> + +#include <gmock/gmock.h> + +#include "app-manager_unittests/mock/module_mock.hh" + +class CynaraMock : public virtual ModuleMock { + public: + virtual ~CynaraMock() {} + + MOCK_METHOD2(cynara_initialize, int(cynara**, const cynara_configuration*)); + MOCK_METHOD5(cynara_check, int(cynara*, const char*, const char*, const char*, + const char*)); + MOCK_METHOD1(cynara_finish, int(cynara*)); +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_CYNARA_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/glibc_mock.cc b/tests/app-manager_unittests/mock/glibc_mock.cc new file mode 100644 index 0000000..2fa10fd --- /dev/null +++ b/tests/app-manager_unittests/mock/glibc_mock.cc @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/glibc_mock.hh" + +#include <stdio.h> + +#include "app-manager_unittests/mock/mock_hook.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +extern "C" uid_t getuid() { + return 5001; +} + +extern "C" ssize_t read(int fd, void* buf, size_t count) { + return static_cast<ssize_t>(snprintf( + static_cast<char*>(buf), count, "User::Pkg::%s", "org.tizen.helloworld")); +} + +extern "C" int close(int fd) { + return 0; +} diff --git a/tests/app-manager_unittests/mock/glibc_mock.hh b/tests/app-manager_unittests/mock/glibc_mock.hh new file mode 100644 index 0000000..b434c8d --- /dev/null +++ b/tests/app-manager_unittests/mock/glibc_mock.hh @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_GLIBC_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_GLIBC_MOCK_HH_ + +#include <fcntl.h> +#include <gmock/gmock.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "app-manager_unittests/mock/module_mock.hh" + +class GlibcMock : public virtual ModuleMock { + public: + virtual ~GlibcMock() {} + + MOCK_METHOD0(getuid, uid_t()); + MOCK_METHOD2(open, int(const char*, int)); + MOCK_METHOD3(read, ssize_t(int, void*, size_t)); + MOCK_METHOD1(close, int(int)); +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_GLIBC_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/mock_hook.hh b/tests/app-manager_unittests/mock/mock_hook.hh new file mode 100644 index 0000000..04a0c30 --- /dev/null +++ b/tests/app-manager_unittests/mock/mock_hook.hh @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_MOCK_HOOK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_MOCK_HOOK_HH_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock<MOCK_CLASS>().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6, p7) +#define MOCK_HOOK_P8(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8) \ + TestFixture::GetMock<MOCK_CLASS>().f(p1, p2, p3, p4, p5, p6, p7, p8) +#define MOCK_HOOK_P10(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) \ + TestFixture::GetMock<MOCK_CLASS>().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // APP_MANAGER_UNITTESTS_MOCK_MOCK_HOOK_HH_ diff --git a/tests/app-manager_unittests/mock/module_mock.hh b/tests/app-manager_unittests/mock/module_mock.hh new file mode 100644 index 0000000..fcf0e94 --- /dev/null +++ b/tests/app-manager_unittests/mock/module_mock.hh @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_MODULE_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_MODULE_MOCK_HH_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_MODULE_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/package_manager_mock.cc b/tests/app-manager_unittests/mock/package_manager_mock.cc new file mode 100644 index 0000000..95e1270 --- /dev/null +++ b/tests/app-manager_unittests/mock/package_manager_mock.cc @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/package_manager_mock.hh" + +#include "app-manager_unittests/mock/mock_hook.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +extern "C" pkgmgr_client* pkgmgr_client_new(pkgmgr_client_type type) { + return MOCK_HOOK_P1(PackageManagerMock, pkgmgr_client_new, type); +} + +extern "C" int pkgmgr_client_free(pkgmgr_client* client) { + return MOCK_HOOK_P1(PackageManagerMock, pkgmgr_client_free, client); +} + +extern "C" int pkgmgr_client_enable_splash_screen(pkgmgr_client* client, + const char* appid) { + return MOCK_HOOK_P2(PackageManagerMock, pkgmgr_client_enable_splash_screen, + client, appid); +} + +extern "C" int pkgmgr_client_disable_splash_screen(pkgmgr_client* client, + const char* appid) { + return MOCK_HOOK_P2(PackageManagerMock, pkgmgr_client_disable_splash_screen, + client, appid); +} + +extern "C" int pkgmgr_client_set_app_icon(pkgmgr_client* client, char* appid, + char* icon) { + return MOCK_HOOK_P3(PackageManagerMock, pkgmgr_client_set_app_icon, client, + appid, icon); +} + +extern "C" int pkgmgr_client_set_status_type(pkgmgr_client* client, + int status_type) { + return MOCK_HOOK_P2(PackageManagerMock, pkgmgr_client_set_status_type, client, + status_type); +} + +extern "C" int pkgmgr_client_listen_app_status(pkgmgr_client* client, + pkgmgr_app_handler app_handler, + void* user_data) { + return MOCK_HOOK_P3(PackageManagerMock, pkgmgr_client_listen_app_status, + client, app_handler, user_data); +} + +extern "C" int pkgmgr_client_remove_listen_status(pkgmgr_client* client) { + return MOCK_HOOK_P1(PackageManagerMock, pkgmgr_client_remove_listen_status, + client); +} diff --git a/tests/app-manager_unittests/mock/package_manager_mock.hh b/tests/app-manager_unittests/mock/package_manager_mock.hh new file mode 100644 index 0000000..c7b9d5c --- /dev/null +++ b/tests/app-manager_unittests/mock/package_manager_mock.hh @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_PAKCAGE_MANAGER_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_PAKCAGE_MANAGER_MOCK_HH_ + +#include <package-manager.h> + +#include <gmock/gmock.h> + +#include "app-manager_unittests/mock/module_mock.hh" + +class PackageManagerMock : public virtual ModuleMock { + public: + virtual ~PackageManagerMock() {} + + MOCK_METHOD1(pkgmgr_client_new, pkgmgr_client*(pkgmgr_client_type)); + MOCK_METHOD1(pkgmgr_client_free, int(pkgmgr_client*)); + MOCK_METHOD2(pkgmgr_client_enable_splash_screen, + int(pkgmgr_client*, const char*)); + MOCK_METHOD2(pkgmgr_client_disable_splash_screen, + int(pkgmgr_client*, const char*)); + MOCK_METHOD3(pkgmgr_client_set_app_icon, int(pkgmgr_client*, char*, char*)); + MOCK_METHOD2(pkgmgr_client_set_status_type, int(pkgmgr_client*, int)); + MOCK_METHOD3(pkgmgr_client_listen_app_status, + int(pkgmgr_client*, pkgmgr_app_handler, void*)); + MOCK_METHOD1(pkgmgr_client_remove_listen_status, int(pkgmgr_client*)); +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_PACKAGE_MANAGER_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/pkgmgr_info_mock.cc b/tests/app-manager_unittests/mock/pkgmgr_info_mock.cc new file mode 100644 index 0000000..217c199 --- /dev/null +++ b/tests/app-manager_unittests/mock/pkgmgr_info_mock.cc @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/pkgmgr_info_mock.hh" + +#include "app-manager_unittests/mock/mock_hook.hh" +#include "app-manager_unittests/mock/test_fixture.hh" + +extern "C" int pkgmgrinfo_appinfo_get_appid(pkgmgrinfo_appinfo_h handle, + char** appid) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_appid, handle, + appid); +} + +extern "C" int pkgmgrinfo_appinfo_get_usr_installed_list( + pkgmgrinfo_app_list_cb callback, uid_t uid, void* user_data) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_usr_installed_list, + callback, uid, user_data); +} + +extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo( + const char* appid, uid_t uid, pkgmgrinfo_appinfo_h* handle) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_usr_appinfo, appid, + uid, handle); +} + +extern "C" int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_destroy_appinfo, + handle); +} + +extern "C" int pkgmgrinfo_appinfo_get_exec(pkgmgrinfo_appinfo_h handle, + char** exec) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_exec, handle, + exec); +} + +extern "C" int pkgmgrinfo_appinfo_get_label(pkgmgrinfo_appinfo_h handle, + char** label) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_label, handle, + label); +} + +extern "C" int pkgmgrinfo_appinfo_usr_get_localed_label(const char* appid, + const char* locale, + uid_t uid, + char** label) { + return MOCK_HOOK_P4(PkgmgrInfoMock, pkgmgrinfo_appinfo_usr_get_localed_label, + appid, locale, uid, label); +} + +extern "C" int pkgmgrinfo_appinfo_get_icon(pkgmgrinfo_appinfo_h handle, + char** icon) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_icon, handle, + icon); +} + +extern "C" int pkgmgrinfo_appinfo_get_pkgname(pkgmgrinfo_appinfo_h handle, + char** pkgname) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_pkgname, handle, + pkgname); +} + +extern "C" int pkgmgrinfo_appinfo_get_apptype(pkgmgrinfo_appinfo_h handle, + char** app_type) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_apptype, handle, + app_type); +} + +extern "C" int pkgmgrinfo_appinfo_get_component( + pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_component* component) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_get_component, handle, + component); +} + +extern "C" int pkgmgrinfo_appinfo_foreach_metadata( + pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_metadata_list_cb callback, + void* user_data) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_foreach_metadata, + handle, callback, user_data); +} + +extern "C" int pkgmgrinfo_appinfo_is_nodisplay(pkgmgrinfo_appinfo_h handle, + bool* nodisplay) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_is_nodisplay, handle, + nodisplay); +} + +extern "C" int pkgmgrinfo_appinfo_is_enabled(pkgmgrinfo_appinfo_h handle, + bool* enabled) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_is_enabled, handle, + enabled); +} + +extern "C" int pkgmgrinfo_appinfo_is_onboot(pkgmgrinfo_appinfo_h handle, + bool* onboot) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_is_onboot, handle, + onboot); +} + +extern "C" int pkgmgrinfo_appinfo_is_preload(pkgmgrinfo_appinfo_h handle, + bool* preload) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_is_preload, handle, + preload); +} + +extern "C" int pkgmgrinfo_appinfo_is_support_ambient( + pkgmgrinfo_appinfo_h handle, bool* support_ambient) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_is_support_ambient, + handle, support_ambient); +} + +extern "C" int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle, + pkgmgrinfo_appinfo_h* clone) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_clone_appinfo, handle, + clone); +} + +extern "C" int pkgmgrinfo_appinfo_foreach_category( + pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_category_list_cb callback, + void* user_data) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_foreach_category, + handle, callback, user_data); +} + +extern "C" int pkgmgrinfo_appinfo_filter_create( + pkgmgrinfo_appinfo_filter_h* handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_filter_create, handle); +} + +extern "C" int pkgmgrinfo_appinfo_filter_destroy( + pkgmgrinfo_appinfo_filter_h handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_filter_destroy, + handle); +} + +extern "C" int pkgmgrinfo_appinfo_filter_add_bool( + pkgmgrinfo_appinfo_filter_h handle, const char* key, bool value) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_filter_add_bool, + handle, key, value); +} + +extern "C" int pkgmgrinfo_appinfo_filter_add_string( + pkgmgrinfo_appinfo_filter_h handle, const char* key, const char* value) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_filter_add_string, + handle, key, value); +} + +extern "C" int pkgmgrinfo_appinfo_filter_count( + pkgmgrinfo_appinfo_filter_h handle, int* count) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_appinfo_filter_count, handle, + count); +} + +extern "C" int pkgmgrinfo_appinfo_usr_filter_foreach_appinfo( + pkgmgrinfo_appinfo_filter_h handle, pkgmgrinfo_app_list_cb callback, + void* user_data, uid_t uid) { + return MOCK_HOOK_P4(PkgmgrInfoMock, + pkgmgrinfo_appinfo_usr_filter_foreach_appinfo, handle, + callback, user_data, uid); +} + +extern "C" int pkgmgrinfo_appinfo_metadata_filter_create( + pkgmgrinfo_appinfo_metadata_filter_h* handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_appinfo_metadata_filter_create, + handle); +} + +extern "C" int pkgmgrinfo_appinfo_metadata_filter_destroy( + pkgmgrinfo_appinfo_metadata_filter_h handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, + pkgmgrinfo_appinfo_metadata_filter_destroy, handle); +} + +extern "C" int pkgmgrinfo_appinfo_metadata_filter_add( + pkgmgrinfo_appinfo_metadata_filter_h handle, const char* key, + const char* value) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_metadata_filter_add, + handle, key, value); +} + +extern "C" int pkgmgrinfo_appinfo_usr_metadata_filter_foreach( + pkgmgrinfo_appinfo_metadata_filter_h handle, + pkgmgrinfo_app_list_cb callback, void* user_data, uid_t uid) { + return MOCK_HOOK_P4(PkgmgrInfoMock, + pkgmgrinfo_appinfo_usr_metadata_filter_foreach, handle, + callback, user_data, uid); +} + +extern "C" int pkgmgrinfo_appinfo_foreach_res_control( + pkgmgrinfo_appinfo_h handle, pkgmgrinfo_app_res_control_list_cb callback, + void* user_data) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_appinfo_foreach_res_control, + handle, callback, user_data); +} + +extern "C" int pkgmgrinfo_pkginfo_get_usr_pkginfo( + const char* pkgid, uid_t uid, pkgmgrinfo_pkginfo_h* handle) { + return MOCK_HOOK_P3(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_usr_pkginfo, pkgid, + uid, handle); +} + +extern "C" int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, + char** mainappid) { + return MOCK_HOOK_P2(PkgmgrInfoMock, pkgmgrinfo_pkginfo_get_mainappid, handle, + mainappid); +} + +extern "C" int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle) { + return MOCK_HOOK_P1(PkgmgrInfoMock, pkgmgrinfo_pkginfo_destroy_pkginfo, + handle); +} diff --git a/tests/app-manager_unittests/mock/pkgmgr_info_mock.hh b/tests/app-manager_unittests/mock/pkgmgr_info_mock.hh new file mode 100644 index 0000000..3670dfc --- /dev/null +++ b/tests/app-manager_unittests/mock/pkgmgr_info_mock.hh @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_PKGMGR_INFO_MOCK_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_PKGMGR_INFO_MOCK_HH_ + +#include <pkgmgr-info.h> + +#include <gmock/gmock.h> + +#include "app-manager_unittests/mock/module_mock.hh" + +class PkgmgrInfoMock : public virtual ModuleMock { + public: + virtual ~PkgmgrInfoMock() {} + + MOCK_METHOD2(pkgmgrinfo_appinfo_get_appid, int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD3(pkgmgrinfo_appinfo_get_usr_installed_list, + int(pkgmgrinfo_app_list_cb, uid_t, void*)); + MOCK_METHOD3(pkgmgrinfo_appinfo_get_usr_appinfo, + int(const char*, uid_t, pkgmgrinfo_appinfo_h*)); + MOCK_METHOD1(pkgmgrinfo_appinfo_destroy_appinfo, int(pkgmgrinfo_appinfo_h)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_exec, int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_label, int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD4(pkgmgrinfo_appinfo_usr_get_localed_label, + int(const char*, const char*, uid_t, char**)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_icon, int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_pkgname, + int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_apptype, + int(pkgmgrinfo_appinfo_h, char**)); + MOCK_METHOD2(pkgmgrinfo_appinfo_get_component, + int(pkgmgrinfo_appinfo_h, pkgmgrinfo_app_component*)); + MOCK_METHOD3(pkgmgrinfo_appinfo_foreach_metadata, + int(pkgmgrinfo_appinfo_h, pkgmgrinfo_app_metadata_list_cb, + void*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_is_nodisplay, + int(pkgmgrinfo_appinfo_h, bool*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_is_enabled, int(pkgmgrinfo_appinfo_h, bool*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_is_onboot, int(pkgmgrinfo_appinfo_h, bool*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_is_preload, int(pkgmgrinfo_appinfo_h, bool*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_is_support_ambient, + int(pkgmgrinfo_appinfo_h, bool*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_clone_appinfo, + int(pkgmgrinfo_appinfo_h, pkgmgrinfo_appinfo_h*)); + MOCK_METHOD3(pkgmgrinfo_appinfo_foreach_category, + int(pkgmgrinfo_appinfo_h, pkgmgrinfo_app_category_list_cb, + void*)); + MOCK_METHOD1(pkgmgrinfo_appinfo_filter_create, + int(pkgmgrinfo_appinfo_filter_h*)); + MOCK_METHOD1(pkgmgrinfo_appinfo_filter_destroy, + int(pkgmgrinfo_appinfo_filter_h)); + MOCK_METHOD3(pkgmgrinfo_appinfo_filter_add_bool, + int(pkgmgrinfo_appinfo_filter_h, const char*, const bool)); + MOCK_METHOD3(pkgmgrinfo_appinfo_filter_add_string, + int(pkgmgrinfo_appinfo_filter_h, const char*, const char*)); + MOCK_METHOD2(pkgmgrinfo_appinfo_filter_count, + int(pkgmgrinfo_appinfo_filter_h, int*)); + MOCK_METHOD4(pkgmgrinfo_appinfo_usr_filter_foreach_appinfo, + int(pkgmgrinfo_appinfo_filter_h, pkgmgrinfo_app_list_cb, void*, + uid_t)); + MOCK_METHOD1(pkgmgrinfo_appinfo_metadata_filter_create, + int(pkgmgrinfo_appinfo_metadata_filter_h*)); + MOCK_METHOD1(pkgmgrinfo_appinfo_metadata_filter_destroy, + int(pkgmgrinfo_appinfo_metadata_filter_h)); + MOCK_METHOD3(pkgmgrinfo_appinfo_metadata_filter_add, + int(pkgmgrinfo_appinfo_metadata_filter_h, const char*, + const char*)); + MOCK_METHOD4(pkgmgrinfo_appinfo_usr_metadata_filter_foreach, + int(pkgmgrinfo_appinfo_metadata_filter_h, pkgmgrinfo_app_list_cb, + void*, uid_t)); + MOCK_METHOD3(pkgmgrinfo_appinfo_foreach_res_control, + int(pkgmgrinfo_appinfo_h, pkgmgrinfo_app_res_control_list_cb, + void*)); + + MOCK_METHOD3(pkgmgrinfo_pkginfo_get_usr_pkginfo, + int(const char*, uid_t, pkgmgrinfo_pkginfo_h*)); + MOCK_METHOD2(pkgmgrinfo_pkginfo_get_mainappid, + int(pkgmgrinfo_pkginfo_h, char**)); + MOCK_METHOD1(pkgmgrinfo_pkginfo_destroy_pkginfo, int(pkgmgrinfo_pkginfo_h)); +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_PKGMGR_INFO_MOCK_HH_ diff --git a/tests/app-manager_unittests/mock/test_fixture.cc b/tests/app-manager_unittests/mock/test_fixture.cc new file mode 100644 index 0000000..905e9f9 --- /dev/null +++ b/tests/app-manager_unittests/mock/test_fixture.cc @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 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 "app-manager_unittests/mock/test_fixture.hh" + +#include <memory> + +std::unique_ptr<ModuleMock> TestFixture::mock_; diff --git a/tests/app-manager_unittests/mock/test_fixture.hh b/tests/app-manager_unittests/mock/test_fixture.hh new file mode 100644 index 0000000..6d82c73 --- /dev/null +++ b/tests/app-manager_unittests/mock/test_fixture.hh @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef APP_MANAGER_UNITTESTS_MOCK_TEST_FIXTURE_HH_ +#define APP_MANAGER_UNITTESTS_MOCK_TEST_FIXTURE_HH_ + +#include <gtest/gtest.h> + +#include <memory> +#include <stdexcept> +#include <string> +#include <utility> + +#include "app-manager_unittests/mock/module_mock.hh" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr<ModuleMock>&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template <typename T> + static T& GetMock() { + auto ptr = dynamic_cast<T*>(mock_.get()); + if (!ptr) + throw std::invalid_argument("The test does not provide mock of \"" + + std::string(typeid(T).name()) + "\""); + return *ptr; + } + + static std::unique_ptr<ModuleMock> mock_; +}; + +#endif // APP_MANAGER_UNITTESTS_MOCK_TEST_FIXTURE_HH_ |