From 1cb57f687bd68bc8df6957f3b2c37bd6b28a68d3 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 16 Jul 2020 13:38:25 +0900 Subject: Prep gmock tests Change-Id: I0a2abe76e35f0879aa4e810261b86e9c1cdb0ddf Signed-off-by: Inkyun Kil --- CMakeLists.txt | 5 +- packaging/librua.spec | 50 ++++++++ test/CMakeLists.txt | 22 ---- test/rua-test.c | 270 ---------------------------------------- tests/CMakeLists.txt | 2 + tests/mock/aul_mock.cc | 27 ++++ tests/mock/aul_mock.h | 34 +++++ tests/mock/mock_hook.h | 42 +++++++ tests/mock/module_mock.h | 25 ++++ tests/mock/test_fixture.cc | 21 ++++ tests/mock/test_fixture.h | 54 ++++++++ tests/rua-test/CMakeLists.txt | 21 ++++ tests/rua-test/rua-test.c | 270 ++++++++++++++++++++++++++++++++++++++++ tests/unittest/CMakeLists.txt | 25 ++++ tests/unittest/main.cc | 25 ++++ tests/unittest/rua_unit_test.cc | 59 +++++++++ 16 files changed, 658 insertions(+), 294 deletions(-) delete mode 100644 test/CMakeLists.txt delete mode 100644 test/rua-test.c create mode 100644 tests/CMakeLists.txt create mode 100644 tests/mock/aul_mock.cc create mode 100644 tests/mock/aul_mock.h create mode 100644 tests/mock/mock_hook.h create mode 100644 tests/mock/module_mock.h create mode 100644 tests/mock/test_fixture.cc create mode 100644 tests/mock/test_fixture.h create mode 100644 tests/rua-test/CMakeLists.txt create mode 100644 tests/rua-test/rua-test.c create mode 100644 tests/unittest/CMakeLists.txt create mode 100644 tests/unittest/main.cc create mode 100644 tests/unittest/rua_unit_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d95170..96e22b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(rua C) +PROJECT(rua C CXX) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(EXEC_PREFIX "\${prefix}") @@ -19,6 +19,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -std=c++14 -fPIE") ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"") @@ -42,4 +43,4 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_types.h DESTINATION includ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_context.h DESTINATION include/${PROJECT_NAME}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_manager.h DESTINATION include/${PROJECT_NAME}) -ADD_SUBDIRECTORY(test) +ADD_SUBDIRECTORY(tests) diff --git a/packaging/librua.spec b/packaging/librua.spec index 25bf7a9..294e1f7 100644 --- a/packaging/librua.spec +++ b/packaging/librua.spec @@ -14,9 +14,15 @@ BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(gmock) BuildRequires: pkgconfig(capi-appfw-app-control) BuildRequires: pkgconfig(pkgmgr-info) +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: zip +%endif + %description Recently used application library @@ -39,6 +45,13 @@ Recently used application library (test) cp %{SOURCE1001} . %build +%if 0%{?gcov:1} +export CFLAGS+=" -fprofile-arcs -ftest-coverage" +export CXXFLAGS+=" -fprofile-arcs -ftest-coverage" +export FFLAGS+=" -fprofile-arcs -ftest-coverage" +export LDFLAGS+=" -lgcov" +%endif + MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %cmake -DFULLVER=%{version} \ -DMAJORVER=${MAJORVER} \ @@ -46,9 +59,28 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` make %{?_smp_mflags} +%if 0%{?gcov:1} +mkdir -p gcov-obj +find . -name '*.gcno' -exec cp '{}' gcov-obj ';' +%endif + +%check +(cd tests && cd unittest && LD_LIBRARY_PATH=../../ ctest -V) +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -q -d . -o rua.info +genhtml rua.info -o rua.out +zip -r rua.zip rua.out +install -m 0644 rua.zip %{buildroot}%{_datadir}/gcov/ +%endif + %install %make_install +%if 0%{?gcov:1} +mkdir -p %{buildroot}%{_datadir}/gcov/obj +install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +%endif + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -71,3 +103,21 @@ make %{?_smp_mflags} %manifest %{name}.manifest %defattr(-,root,root,-) %{_bindir}/rua-test + +################################################# +# rua-gcov +################################################# +%if 0%{?gcov:1} +%package gcov +Summary: Simple string key-val dictionary ADT (gcov) +Group: Application Framework/Testing + +%description gcov +Simple string key-val dictionary ADT gcov objects +%endif + +%if 0%{?gcov:1} +%files gcov +%defattr(-,root,root,-) +%{_datadir}/gcov/* +%endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index 3f7d5c8..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -SET(RUATEST rua-test) -SET(RUATEST_SRCS rua-test.c) -SET(RUATEST_VENDOR samsung) -SET(RUATEST_DIR /usr/bin) -SET(RUATEST_LOCALEDIR /usr/share/locale) - -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) -PKG_CHECK_MODULES(RUATEST_PKGS REQUIRED - ) - -FOREACH(flag ${RUATEST_PKGS_CFLAGS}) - SET(RUATEST_CFLAGS "${RUATEST_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(RUATEST_CFLAGS, "${RUATEST_CFLAGS} -DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") - -ADD_EXECUTABLE(${RUATEST} ${RUATEST_SRCS}) -SET_TARGET_PROPERTIES(${RUATEST} PROPERTIES COMPILE_FLAGS "${RUATEST_CFLAGS}") -TARGET_LINK_LIBRARIES(${RUATEST} ${PROJECT_NAME} ${RUATEST_PKGS_LDFLAGS}) - -INSTALL(TARGETS ${RUATEST} DESTINATION bin) - diff --git a/test/rua-test.c b/test/rua-test.c deleted file mode 100644 index 35b2183..0000000 --- a/test/rua-test.c +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright (c) 2000 - 2016 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 -#include -#include -#include -#include -#include -#include -#include -#include - -/* For multi-user support */ -#include - -#include "rua.h" -#include "rua_internal.h" -#include "rua_stat.h" -#include "rua_stat_internal.h" - -static GMainLoop *mainloop = NULL; -static pthread_t g_thread; - -static int callback_id_table[10] = { 0, }; - -static gboolean run_test(int selected_number); -static void *__print_menu(void *arg) -{ - int test_num = 0; - int run_next = 1; - - while (run_next) { - printf("==========================================\n"); - printf(" Basic test menu \n"); - printf("==========================================\n"); - printf(" 0. EXIT\n"); - printf(" 1. Add rua history to DEFAULT USER(5001)\n"); - printf(" 2. Delete history with pkgname\n"); - printf(" 3. Load RUA history\n"); - printf(" 4. Update RUA stat\n"); - printf(" 5. Get RUA stat tags\n"); - printf(" 6. Register update callback (MAX 10)\n"); - printf(" 7. Unregister update callback\n"); - printf("------------------------------------------\n"); - int ret = scanf("%d", &test_num); - if (ret < 0) { - printf("scanf fail %d", ret); - break; - } - - run_next = run_test(test_num); - } - - return NULL; -} - -static void __update_cb(char **table, int rows, int cols, void *user_data) -{ - struct rua_rec record; - - printf("update_cb"); - int row; - for (row = 0; row < rows; ++row) { - rua_history_get_rec(&record, table, rows, cols, row); - printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time); - } - -} - -static int __add_history() -{ - int ret; - char *app_path; - char *pkgname = "org.tizen.ruatester"; - - app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname); - ret = rua_add_history_for_uid(pkgname, app_path, NULL, 5001); - return ret; -} - -static int __delete_history_with_pkgname() -{ - int ret; - char *pkgname = "org.tizen.ruatester"; - - ret = rua_delete_history_with_pkgname_for_uid(pkgname, 5001); - return ret; -} - -static int __load_rua_history() -{ - char **table = NULL; - int rows = 0; - int cols = 0; - struct rua_rec record; - - if (rua_history_load_db_for_uid(&table, &rows, &cols, 5001) || !table) { - printf("fail to load rua history \n"); - return -1; - } - - int row; - for (row = 0; row < rows; ++row) { - rua_history_get_rec(&record, table, rows, cols, row); - printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time); - } - - rua_history_unload_db(&table); - return 0; -} - -static int __update_stat() -{ - int ret; - - ret = rua_stat_update_for_uid("ruacaller", "org.tizen.ruatester", 5001); - return ret; -} - -static int __rua_stat_tag_iter_cb(const char *rua_stat_tag, void *data) -{ - printf("rua_stat_tag : %s \n", rua_stat_tag); - - return 0; -} - -static int __get_stat_tags() -{ - int ret; - ret = rua_stat_get_stat_tags_for_uid("ruacaller", __rua_stat_tag_iter_cb, NULL, 5001); - return ret; -} - -static int __register_callback() -{ - int ret; - - int i = 0; - for (i = 0; i < 10; i++) { - if (callback_id_table[i] == 0) { - int output = 0; - ret = rua_register_update_cb(__update_cb, NULL, &output); - if (ret == -1) { - printf("__register_callback error : %d \n", ret); - return -1; - } - - printf("callback id : %d \n", output); - callback_id_table[i] = output; - - return 0; - } - } - - return -1; -} - -static int __unregister_callback() -{ - int ret; - - int i = 0; - int id = 0; - printf("Enter callback_id (The number from register_callback) : "); - ret = scanf("%d", &id); - if (ret < 0) { - printf("scanf fail %d", ret); - return -1; - } - - - for (i = 0; i < 10; i++) { - if (callback_id_table[i] == id) { - ret = rua_unregister_update_cb(callback_id_table[i]); - if (ret == -1) - printf("__unregister_callback : %d \n", ret); - - callback_id_table[i] = 0; - return 0; - } - } - - return -1; -} - - -static gboolean run_test(int selected_number) -{ - int r = 0; - gboolean loop = TRUE; - switch (selected_number) { - case 0: - g_main_loop_quit(mainloop); - loop = FALSE; - break; - - case 1: - __add_history(); - break; - - case 2: - __delete_history_with_pkgname(); - break; - - case 3: - __load_rua_history(); - break; - - case 4: - __update_stat(); - break; - - case 5: - __get_stat_tags(); - break; - - case 6: - r = __register_callback(); - if (r == -1) - printf("table is full"); - break; - - case 7: - r = __unregister_callback(); - if (r == -1) - printf("id is wrong"); - - break; - - default: - break; - } - - - - return loop; -} - -int main() -{ - int ret = 0; - - mainloop = g_main_loop_new(NULL, FALSE); - - int result = pthread_create(&g_thread, NULL, __print_menu, (void *)NULL); - if (result < 0) { - printf("pthread_create failed in initialize\n"); - return 0; - } - - g_main_loop_run(mainloop); - g_main_loop_unref(mainloop); - mainloop = NULL; - - return ret; -} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3baa0d0 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_SUBDIRECTORY(rua-test) +ADD_SUBDIRECTORY(unittest) \ No newline at end of file diff --git a/tests/mock/aul_mock.cc b/tests/mock/aul_mock.cc new file mode 100644 index 0000000..ab22c6d --- /dev/null +++ b/tests/mock/aul_mock.cc @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "mock_hook.h" +#include "test_fixture.h" +#include "aul_mock.h" + + +extern "C" int aul_add_rua_history_for_uid( + bundle* arg0, uid_t arg1) { + return MOCK_HOOK_P2(AulMock, aul_add_rua_history_for_uid, arg0, arg1); +} diff --git a/tests/mock/aul_mock.h b/tests/mock/aul_mock.h new file mode 100644 index 0000000..da623ae --- /dev/null +++ b/tests/mock/aul_mock.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_AUL_MOCK_H_ +#define UNIT_TESTS_MOCK_AUL_MOCK_H_ + +#include +#include +#include + +#include "module_mock.h" + +class AulMock : public virtual ModuleMock { + public: + virtual ~AulMock() {} + + MOCK_METHOD2(aul_add_rua_history_for_uid, int(bundle*, uid_t)); + +}; + +#endif // UNIT_TESTS_MOCK_AUL_MOCK_H_ diff --git a/tests/mock/mock_hook.h b/tests/mock/mock_hook.h new file mode 100644 index 0000000..af27bba --- /dev/null +++ b/tests/mock/mock_hook.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_MOCK_HOOK_H_ +#define UNIT_TESTS_MOCK_MOCK_HOOK_H_ + +#define MOCK_HOOK_P0(MOCK_CLASS, f) \ + TestFixture::GetMock().f() +#define MOCK_HOOK_P1(MOCK_CLASS, f, p1) \ + TestFixture::GetMock().f(p1) +#define MOCK_HOOK_P2(MOCK_CLASS, f, p1, p2) \ + TestFixture::GetMock().f(p1, p2) +#define MOCK_HOOK_P3(MOCK_CLASS, f, p1, p2, p3) \ + TestFixture::GetMock().f(p1, p2, p3) +#define MOCK_HOOK_P4(MOCK_CLASS, f, p1, p2, p3, p4) \ + TestFixture::GetMock().f(p1, p2, p3, p4) +#define MOCK_HOOK_P5(MOCK_CLASS, f, p1, p2, p3, p4, p5) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5) +#define MOCK_HOOK_P6(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6) \ + TestFixture::GetMock().f(p1, p2, p3, p4, p5, p6) +#define MOCK_HOOK_P7(MOCK_CLASS, f, p1, p2, p3, p4, p5, p6, p7) \ + TestFixture::GetMock().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().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().f( \ + p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) + +#endif // UNIT_TESTS_MOCK_MOCK_HOOK_H_ diff --git a/tests/mock/module_mock.h b/tests/mock/module_mock.h new file mode 100644 index 0000000..9b19d89 --- /dev/null +++ b/tests/mock/module_mock.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_MODULE_MOCK_H_ +#define UNIT_TESTS_MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // UNIT_TESTS_MOCK_MODULE_MOCK_H_ diff --git a/tests/mock/test_fixture.cc b/tests/mock/test_fixture.cc new file mode 100644 index 0000000..27f5666 --- /dev/null +++ b/tests/mock/test_fixture.cc @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 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 "test_fixture.h" + +#include + +std::unique_ptr TestFixture::mock_; diff --git a/tests/mock/test_fixture.h b/tests/mock/test_fixture.h new file mode 100644 index 0000000..1f9d858 --- /dev/null +++ b/tests/mock/test_fixture.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020 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 UNIT_TESTS_MOCK_TEST_FIXTURE_H_ +#define UNIT_TESTS_MOCK_TEST_FIXTURE_H_ + +#include + +#include +#include +#include +#include + +#include "module_mock.h" +#include "test_fixture.h" + +class TestFixture : public ::testing::Test { + public: + explicit TestFixture(std::unique_ptr&& mock) { + mock_ = std::move(mock); + } + virtual ~TestFixture() { + mock_.reset(); + } + + virtual void SetUp() {} + virtual void TearDown() {} + + template + static T& GetMock() { + auto ptr = dynamic_cast(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 mock_; +}; + +#endif // UNIT_TESTS_MOCK_TEST_FIXTURE_H_ diff --git a/tests/rua-test/CMakeLists.txt b/tests/rua-test/CMakeLists.txt new file mode 100644 index 0000000..088189a --- /dev/null +++ b/tests/rua-test/CMakeLists.txt @@ -0,0 +1,21 @@ +SET(RUATEST rua-test) +SET(RUATEST_SRCS rua-test.c) +SET(RUATEST_VENDOR samsung) +SET(RUATEST_DIR /usr/bin) +SET(RUATEST_LOCALEDIR /usr/share/locale) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) +PKG_CHECK_MODULES(RUATEST_PKGS REQUIRED + ) + +FOREACH(flag ${RUATEST_PKGS_CFLAGS}) + SET(RUATEST_CFLAGS "${RUATEST_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(RUATEST_CFLAGS, "${RUATEST_CFLAGS} -DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") + +ADD_EXECUTABLE(${RUATEST} ${RUATEST_SRCS}) +SET_TARGET_PROPERTIES(${RUATEST} PROPERTIES COMPILE_FLAGS "${RUATEST_CFLAGS}") +TARGET_LINK_LIBRARIES(${RUATEST} ${PROJECT_NAME} ${RUATEST_PKGS_LDFLAGS}) + +INSTALL(TARGETS ${RUATEST} DESTINATION bin) diff --git a/tests/rua-test/rua-test.c b/tests/rua-test/rua-test.c new file mode 100644 index 0000000..35b2183 --- /dev/null +++ b/tests/rua-test/rua-test.c @@ -0,0 +1,270 @@ +/* + * Copyright (c) 2000 - 2016 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 +#include +#include +#include +#include +#include +#include +#include +#include + +/* For multi-user support */ +#include + +#include "rua.h" +#include "rua_internal.h" +#include "rua_stat.h" +#include "rua_stat_internal.h" + +static GMainLoop *mainloop = NULL; +static pthread_t g_thread; + +static int callback_id_table[10] = { 0, }; + +static gboolean run_test(int selected_number); +static void *__print_menu(void *arg) +{ + int test_num = 0; + int run_next = 1; + + while (run_next) { + printf("==========================================\n"); + printf(" Basic test menu \n"); + printf("==========================================\n"); + printf(" 0. EXIT\n"); + printf(" 1. Add rua history to DEFAULT USER(5001)\n"); + printf(" 2. Delete history with pkgname\n"); + printf(" 3. Load RUA history\n"); + printf(" 4. Update RUA stat\n"); + printf(" 5. Get RUA stat tags\n"); + printf(" 6. Register update callback (MAX 10)\n"); + printf(" 7. Unregister update callback\n"); + printf("------------------------------------------\n"); + int ret = scanf("%d", &test_num); + if (ret < 0) { + printf("scanf fail %d", ret); + break; + } + + run_next = run_test(test_num); + } + + return NULL; +} + +static void __update_cb(char **table, int rows, int cols, void *user_data) +{ + struct rua_rec record; + + printf("update_cb"); + int row; + for (row = 0; row < rows; ++row) { + rua_history_get_rec(&record, table, rows, cols, row); + printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time); + } + +} + +static int __add_history() +{ + int ret; + char *app_path; + char *pkgname = "org.tizen.ruatester"; + + app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname); + ret = rua_add_history_for_uid(pkgname, app_path, NULL, 5001); + return ret; +} + +static int __delete_history_with_pkgname() +{ + int ret; + char *pkgname = "org.tizen.ruatester"; + + ret = rua_delete_history_with_pkgname_for_uid(pkgname, 5001); + return ret; +} + +static int __load_rua_history() +{ + char **table = NULL; + int rows = 0; + int cols = 0; + struct rua_rec record; + + if (rua_history_load_db_for_uid(&table, &rows, &cols, 5001) || !table) { + printf("fail to load rua history \n"); + return -1; + } + + int row; + for (row = 0; row < rows; ++row) { + rua_history_get_rec(&record, table, rows, cols, row); + printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time); + } + + rua_history_unload_db(&table); + return 0; +} + +static int __update_stat() +{ + int ret; + + ret = rua_stat_update_for_uid("ruacaller", "org.tizen.ruatester", 5001); + return ret; +} + +static int __rua_stat_tag_iter_cb(const char *rua_stat_tag, void *data) +{ + printf("rua_stat_tag : %s \n", rua_stat_tag); + + return 0; +} + +static int __get_stat_tags() +{ + int ret; + ret = rua_stat_get_stat_tags_for_uid("ruacaller", __rua_stat_tag_iter_cb, NULL, 5001); + return ret; +} + +static int __register_callback() +{ + int ret; + + int i = 0; + for (i = 0; i < 10; i++) { + if (callback_id_table[i] == 0) { + int output = 0; + ret = rua_register_update_cb(__update_cb, NULL, &output); + if (ret == -1) { + printf("__register_callback error : %d \n", ret); + return -1; + } + + printf("callback id : %d \n", output); + callback_id_table[i] = output; + + return 0; + } + } + + return -1; +} + +static int __unregister_callback() +{ + int ret; + + int i = 0; + int id = 0; + printf("Enter callback_id (The number from register_callback) : "); + ret = scanf("%d", &id); + if (ret < 0) { + printf("scanf fail %d", ret); + return -1; + } + + + for (i = 0; i < 10; i++) { + if (callback_id_table[i] == id) { + ret = rua_unregister_update_cb(callback_id_table[i]); + if (ret == -1) + printf("__unregister_callback : %d \n", ret); + + callback_id_table[i] = 0; + return 0; + } + } + + return -1; +} + + +static gboolean run_test(int selected_number) +{ + int r = 0; + gboolean loop = TRUE; + switch (selected_number) { + case 0: + g_main_loop_quit(mainloop); + loop = FALSE; + break; + + case 1: + __add_history(); + break; + + case 2: + __delete_history_with_pkgname(); + break; + + case 3: + __load_rua_history(); + break; + + case 4: + __update_stat(); + break; + + case 5: + __get_stat_tags(); + break; + + case 6: + r = __register_callback(); + if (r == -1) + printf("table is full"); + break; + + case 7: + r = __unregister_callback(); + if (r == -1) + printf("id is wrong"); + + break; + + default: + break; + } + + + + return loop; +} + +int main() +{ + int ret = 0; + + mainloop = g_main_loop_new(NULL, FALSE); + + int result = pthread_create(&g_thread, NULL, __print_menu, (void *)NULL); + if (result < 0) { + printf("pthread_create failed in initialize\n"); + return 0; + } + + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + mainloop = NULL; + + return ret; +} diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt new file mode 100644 index 0000000..d4677ec --- /dev/null +++ b/tests/unittest/CMakeLists.txt @@ -0,0 +1,25 @@ +ENABLE_TESTING() +SET(TARGET_RUA_UNIT_TEST "rua-unit-test") + +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ UNIT_TESTS_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../mock/ UNIT_TESTS_SRCS) +ADD_EXECUTABLE(${TARGET_RUA_UNIT_TEST} + ${UNIT_TESTS_SRCS} +) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_RUA_UNIT_TEST} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../../include" + "${CMAKE_CURRENT_SOURCE_DIR}/../mock" +) + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(gtest_pkgs REQUIRED gmock) + +TARGET_LINK_LIBRARIES(${TARGET_RUA_UNIT_TEST} PUBLIC ${gtest_pkgs_LDFLAGS} ${PROJECT_NAME}) +SET_TARGET_PROPERTIES(${TARGET_RUA_UNIT_TEST} PROPERTIES COMPILE_FLAGS "-fPIE") +SET_TARGET_PROPERTIES(${TARGET_RUA_UNIT_TEST} PROPERTIES LINK_FLAGS "-pie") + +ADD_TEST( + NAME ${TARGET_RUA_UNIT_TEST} + COMMAND ${TARGET_RUA_UNIT_TEST} +) diff --git a/tests/unittest/main.cc b/tests/unittest/main.cc new file mode 100644 index 0000000..5f340cd --- /dev/null +++ b/tests/unittest/main.cc @@ -0,0 +1,25 @@ +// Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#include +#include + +int main(int argc, char** argv) { + int ret = -1; + try { + testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occurred" << std::endl; + } + + try { + ret = RUN_ALL_TESTS(); + } catch (const ::testing::internal::GoogleTestFailureException& e) { + ret = -1; + std::cout << "GoogleTestFailureException was thrown:" << e.what() + << std::endl; + } + + return ret; +} diff --git a/tests/unittest/rua_unit_test.cc b/tests/unittest/rua_unit_test.cc new file mode 100644 index 0000000..ccffbb9 --- /dev/null +++ b/tests/unittest/rua_unit_test.cc @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include +#include + +#include "aul_mock.h" +#include "test_fixture.h" + +using ::testing::_; +using ::testing::Return; + +class Mocks : public ::testing::NiceMock{}; + +class RuaTest : public TestFixture { + public: + RuaTest() : TestFixture(std::make_unique()) {} + virtual ~RuaTest() {} + + virtual void SetUp() { + } + + virtual void TearDown() { + } +}; + +TEST_F(RuaTest, rua_add_history_for_uid) { + EXPECT_CALL(GetMock(), + aul_add_rua_history_for_uid(_, _)). + WillOnce(Return(0)); + + int ret; + ret = rua_add_history_for_uid("test", "/app_path", "arg", 5001); + EXPECT_EQ(ret, 0); +} + +TEST_F(RuaTest, rua_delete_history_with_pkgname_N) { + int ret; + ret = rua_delete_history_with_pkgname("test"); + EXPECT_NE(ret, 0); +} + -- cgit v1.2.3