--- /dev/null
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <aul.h>
+#include <aul_cmd.h>
+#include <aul_launcher_service.h>
+#include <aul_sock.h>
+#include <glib.h>
+#include <gtest/gtest.h>
+
+#include <bundle_cpp.h>
+
+#include <string>
+
+#include "mock/mock_hook.h"
+#include "mock/socket_mock.h"
+#include "mock/test_fixture.h"
+#include "src/launch.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::Invoke;
+
+namespace {
+
+constexpr const char kName[] = "test";
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+void LauncherServiceCb(const char* appid, const char* instance_id,
+ const int pid, const uint32_t serial, void* user_data) {
+}
+
+} // namespace
+
+class LauncherServiceTest : public TestFixture {
+ public:
+ LauncherServiceTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+ virtual void SetUp() {
+ if (!aul_is_initialized())
+ aul_launch_init(nullptr, nullptr);
+
+ loop_ = g_main_loop_new(nullptr, FALSE);
+
+ cmd_ = -1;
+ opt_ = -1;
+
+ ON_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+ .WillByDefault(Invoke([&](int fd, const void* buf, size_t n, int flags)
+ -> ssize_t {
+ const app_pkt_t* pkt = reinterpret_cast<const app_pkt_t*>(buf);
+ cmd_ = pkt->cmd;
+ opt_ = pkt->opt;
+
+ if (opt_ & AUL_SOCK_BUNDLE)
+ kb_ = bundle_decode(pkt->data, pkt->len);
+
+ if (cmd_ == APP_COM_LEAVE)
+ QuitMainLoop();
+
+ return n;
+ }));
+ ON_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+ .WillByDefault(
+ Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+ int ret = 0;
+ memcpy(buf, &ret, sizeof(int));
+ return sizeof(int);
+ }));
+
+ int ret = aul_launcher_service_create(kName, LauncherServiceCb, this,
+ &handle_);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_NE(handle_, nullptr);
+ }
+
+ virtual void TearDown() {
+ if (aul_is_initialized())
+ aul_launch_fini();
+
+ if (kb_ != nullptr) {
+ bundle_free(kb_);
+ kb_ = nullptr;
+ }
+
+ if (create_handle_ != nullptr) {
+ aul_launcher_service_destroy(create_handle_);
+ create_handle_ = nullptr;
+ }
+
+ if (handle_ != nullptr) {
+ aul_launcher_service_destroy(handle_);
+ handle_ = nullptr;
+ }
+
+ if (loop_ != nullptr) {
+ g_main_loop_unref(loop_);
+ loop_ = nullptr;
+ }
+ }
+
+ void RunMainLoop() {
+ g_main_loop_run(loop_);
+ }
+
+ void QuitMainLoop() {
+ g_main_loop_quit(loop_);
+ }
+
+ int cmd_ = -1;
+ int opt_ = -1;
+ bundle* kb_ = nullptr;
+ aul_launcher_service_h create_handle_ = nullptr;
+ aul_launcher_service_h handle_ = nullptr;
+
+ private:
+ GMainLoop* loop_ = nullptr;
+};
+
+TEST_F(LauncherServiceTest, aul_launcher_service_create_P) {
+ int ret = aul_launcher_service_create(kName, LauncherServiceCb, this,
+ &create_handle_);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_NE(create_handle_, nullptr);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_create_N) {
+ int ret = aul_launcher_service_create(nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_listen_P) {
+ int ret = aul_launcher_service_listen(handle_);
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_NE(kb_, nullptr);
+ tizen_base::Bundle b(kb_, false, false);
+
+ EXPECT_EQ(cmd_, APP_COM_CREATE);
+ EXPECT_EQ(opt_, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ std::string endpoint = "launcher_service:" + std::string(kName) + ":" +
+ std::to_string(getpid());
+ EXPECT_EQ(b.GetString(AUL_K_COM_ENDPOINT), endpoint);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_listen_N) {
+ int ret = aul_launcher_service_listen(nullptr);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_destroy_P) {
+ aul_launcher_service_listen(handle_);
+
+ int ret = aul_launcher_service_destroy(handle_);
+ EXPECT_EQ(ret, AUL_R_OK);
+ handle_ = nullptr;
+
+ RunMainLoop();
+
+ EXPECT_NE(kb_, nullptr);
+ tizen_base::Bundle b(kb_, false, false);
+ EXPECT_EQ(cmd_, APP_COM_LEAVE);
+ EXPECT_EQ(opt_, (AUL_SOCK_BUNDLE | AUL_SOCK_NONE));
+ std::string endpoint = "launcher_service:" + std::string(kName) + ":" +
+ std::to_string(getpid());
+ EXPECT_EQ(b.GetString(AUL_K_COM_ENDPOINT), endpoint);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_destroy_N) {
+ int ret = aul_launcher_service_destroy(nullptr);
+ EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_notify_animation_started_P) {
+ int ret = aul_launcher_service_notify_animation_started();
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd_, LAUNCHER_SERVICE_NOTIFY_ANIMATION_STARTED);
+ EXPECT_EQ(opt_, AUL_SOCK_NOREPLY);
+}
+
+TEST_F(LauncherServiceTest, aul_launcher_service_notify_animation_finished_P) {
+ int ret = aul_launcher_service_notify_animation_finished();
+ EXPECT_EQ(ret, AUL_R_OK);
+ EXPECT_EQ(cmd_, LAUNCHER_SERVICE_NOTIFY_ANIMATION_FINISHED);
+ EXPECT_EQ(opt_, AUL_SOCK_NOREPLY);
+}