diff options
author | Daehyeon Jung <darrenh.jung@samsung.com> | 2020-09-02 21:00:49 +0900 |
---|---|---|
committer | Daehyeon Jung <darrenh.jung@samsung.com> | 2020-09-11 10:12:50 +0900 |
commit | ea2c34220fb97d3da9909d6b3abfbe4d50213d9b (patch) | |
tree | 83bd0465262314a8b4b766c666b12b86c6429394 | |
parent | e398d8dfb3aa603c2cc3139fd4ddbe5471abe93d (diff) | |
download | appcore-agent-ea2c34220fb97d3da9909d6b3abfbe4d50213d9b.tar.gz appcore-agent-ea2c34220fb97d3da9909d6b3abfbe4d50213d9b.tar.bz2 appcore-agent-ea2c34220fb97d3da9909d6b3abfbe4d50213d9b.zip |
Add UnitTests
Change-Id: Ibca0d8b145da96239a06f8769ec711d91888ccf1
Signed-off-by: Daehyeon Jung <darrenh.jung@samsung.com>
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | packaging/appcore-agent.spec | 5 | ||||
-rw-r--r-- | unittests/CMakeLists.txt | 19 | ||||
-rw-r--r-- | unittests/mock/appcore_mock.cc | 27 | ||||
-rw-r--r-- | unittests/mock/appcore_mock.h | 32 | ||||
-rw-r--r-- | unittests/mock/mock_hook.h | 42 | ||||
-rw-r--r-- | unittests/mock/module_mock.h | 25 | ||||
-rw-r--r-- | unittests/mock/test_fixture.cc | 21 | ||||
-rw-r--r-- | unittests/mock/test_fixture.h | 53 | ||||
-rw-r--r-- | unittests/service_app_test.cc | 143 |
10 files changed, 370 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 69fd9f5..fa6c53c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(appcore-agent C) +PROJECT(appcore-agent C CXX) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) @@ -42,3 +42,5 @@ INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appcore-agent FILES_MATCHING PATTERN "*.h" ) + +ADD_SUBDIRECTORY(unittests)
\ No newline at end of file diff --git a/packaging/appcore-agent.spec b/packaging/appcore-agent.spec index ceeab77..6a4aaae 100644 --- a/packaging/appcore-agent.spec +++ b/packaging/appcore-agent.spec @@ -14,6 +14,7 @@ BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(appcore-common) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(ecore-core) +BuildRequires: pkgconfig(gmock) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(vconf-internal-keys) @@ -76,6 +77,10 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %endif +%check +cd unittests +LD_LIBRARY_PATH=../ ctest -V + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt new file mode 100644 index 0000000..6c7fd2d --- /dev/null +++ b/unittests/CMakeLists.txt @@ -0,0 +1,19 @@ +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} TEST_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/mock MOCK_SRCS) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include/) + +ENABLE_TESTING() + +SET(TARGET_TEST "appcore-agent-test") +ADD_EXECUTABLE(${TARGET_TEST} ${TEST_SRCS} ${MOCK_SRCS}) +ADD_TEST(${TARGET_TEST} ${TARGET_TEST}) + +INCLUDE(FindPkgConfig) +pkg_check_modules(test REQUIRED aul dlog capi-appfw-app-control capi-appfw-app-common vconf ecore-core vconf-internal-keys appcore-common capi-system-info) +FOREACH(flag ${test_CFLAGS}) + SET(EXTRA_CXXFLAGS_test "${EXTRA_CXXFLAGS_test} ${flag}") +ENDFOREACH(flag) +SET_TARGET_PROPERTIES(${TARGET_TEST} PROPERTIES COMPILE_FLAGS ${EXTRA_CXXFLAGS_test}) +message(${EXTRA_CXXFLAGS_test}) + +TARGET_LINK_LIBRARIES(${TARGET_TEST} gmock appcore-agent) diff --git a/unittests/mock/appcore_mock.cc b/unittests/mock/appcore_mock.cc new file mode 100644 index 0000000..4829379 --- /dev/null +++ b/unittests/mock/appcore_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 "appcore_mock.h" +#include "mock_hook.h" +#include "test_fixture.h" + +extern "C" int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void* data) { + return MOCK_HOOK_P4(AppCoreMock, appcore_base_init, ops, argc, argv, data); +} + +extern "C" void appcore_base_fini() { + return MOCK_HOOK_P0(AppCoreMock, appcore_base_fini); +} diff --git a/unittests/mock/appcore_mock.h b/unittests/mock/appcore_mock.h new file mode 100644 index 0000000..8b1433b --- /dev/null +++ b/unittests/mock/appcore_mock.h @@ -0,0 +1,32 @@ +/* + * 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 MOCK_APPCORE_MOCK_H_ +#define MOCK_APPCORE_MOCK_H_ +#include <gmock/gmock.h> +#include <appcore_base.h> + +#include "module_mock.h" + +class AppCoreMock : public virtual ModuleMock { + public: + virtual ~AppCoreMock() {} + + MOCK_METHOD4(appcore_base_init, int(appcore_base_ops, int, char**, void*)); + MOCK_METHOD0(appcore_base_fini, void()); +}; + +#endif // MOCK_APPCORE_HOOK_H_
\ No newline at end of file diff --git a/unittests/mock/mock_hook.h b/unittests/mock/mock_hook.h new file mode 100644 index 0000000..79831d7 --- /dev/null +++ b/unittests/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 MOCK_MOCK_HOOK_H_ +#define MOCK_MOCK_HOOK_H_ + +#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 // MOCK_MOCK_HOOK_H_ diff --git a/unittests/mock/module_mock.h b/unittests/mock/module_mock.h new file mode 100644 index 0000000..0934014 --- /dev/null +++ b/unittests/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 MOCK_MODULE_MOCK_H_ +#define MOCK_MODULE_MOCK_H_ + +class ModuleMock { + public: + virtual ~ModuleMock() {} +}; + +#endif // MOCK_MODULE_MOCK_H_ diff --git a/unittests/mock/test_fixture.cc b/unittests/mock/test_fixture.cc new file mode 100644 index 0000000..27f5666 --- /dev/null +++ b/unittests/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 <memory> + +std::unique_ptr<ModuleMock> TestFixture::mock_; diff --git a/unittests/mock/test_fixture.h b/unittests/mock/test_fixture.h new file mode 100644 index 0000000..db223f1 --- /dev/null +++ b/unittests/mock/test_fixture.h @@ -0,0 +1,53 @@ +/* + * 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 MOCK_TEST_FIXTURE_H_ +#define MOCK_TEST_FIXTURE_H_ + +#include <gtest/gtest.h> + +#include <memory> +#include <stdexcept> +#include <string> +#include <utility> + +#include "module_mock.h" + +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 // MOCK_TEST_FIXTURE_H_ diff --git a/unittests/service_app_test.cc b/unittests/service_app_test.cc new file mode 100644 index 0000000..6b16ba2 --- /dev/null +++ b/unittests/service_app_test.cc @@ -0,0 +1,143 @@ +/* + * 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 <gmock/gmock.h> +#include <dlog.h> +#include <stdio.h> + +#include <chrono> +#include <thread> + +#include <bundle_internal.h> +#include <service_app.h> +#include <service_app_extension.h> +#include <service_app_internal.h> +#include <appcore_base.h> + +#include "mock/test_fixture.h" +#include "mock/appcore_mock.h" + +namespace appcore_agent { + +class Mocks : public ::testing::NiceMock<AppCoreMock> {}; + +class ServiceAppTest : public TestFixture { + public: + ServiceAppTest() : TestFixture(std::make_unique<Mocks>()) {} + virtual ~ServiceAppTest() {} + + virtual void SetUp() {} + + virtual void TearDown() {} +}; + +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; +} + +bool __service_app_create_cb(void* user_data) { + return true; +} + +bool __service_app_create_cb_loop(void* user_data) { + bool *loop = reinterpret_cast<bool*>(user_data); + + while (*loop) { + std::cout << "wait..." << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + + return true; +} + +void __service_app_terminate_cb(void* user_data) { + +} + +void __service_app_control_cb(app_control_h app_control, void* user_data) { + +} + + +TEST_F(ServiceAppTest, Basic) { + int ret = 0; + service_app_lifecycle_callback_s callback = { + .create = __service_app_create_cb, + .terminate = __service_app_terminate_cb, + .app_control = __service_app_control_cb + }; + + bundle* b = bundle_create(); + ASSERT_TRUE(b != nullptr); + bundle_add_str(b, "dummy", "dummy"); + char** argv; + int argc = bundle_export_to_argv(b, &argv); + ASSERT_TRUE(argc > 0); + + ret = service_app_main(argc, argv, &callback, nullptr); + EXPECT_THAT(ret, testing::Eq(APP_ERROR_NONE)); + + ret = service_app_main(0, nullptr, &callback, nullptr); + EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER)); + + callback.create = nullptr; + ret = service_app_main(argc, argv, &callback, nullptr); + EXPECT_THAT(ret, testing::Eq(APP_ERROR_INVALID_PARAMETER)); + + bool loop = true; + std::thread t([&]() { + callback.create = __service_app_create_cb_loop; + service_app_main(argc, argv, &callback, reinterpret_cast<void*>(&loop)); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + callback.create = __service_app_create_cb; + ret = service_app_main(1, argv, &callback, &loop); + EXPECT_THAT(ret, testing::Eq(APP_ERROR_ALREADY_RUNNING)); + + loop = false; + t.join(); + bundle_free_exported_argv(argc, &argv); + bundle_free(b); +} + +} // namespace appcore_agent + +int main(int argc, char* argv[]) { + int ret = 0; + try { + ::testing::InitGoogleTest(&argc, argv); + } catch(...) { + std::cout << "Exception occured" << std::endl; + return 1; + } + + try { + return RUN_ALL_TESTS(); + } catch(const ::testing::internal::GoogleTestFailureException& e) { + std::cout << "GoogleTestFailureException occured:" << e.what() << std::endl; + ret = 1; + } + + return ret; +} |