Add unit tests for complication API 55/279355/4
authorHwankyu Jhun <h.jhun@samsung.com>
Sun, 7 Aug 2022 23:40:43 +0000 (08:40 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 8 Aug 2022 03:21:16 +0000 (12:21 +0900)
To refactor complication API, unit tests are added.

Change-Id: I87c7e1d10a731b197e9e36daaba5d9a7842f7943
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
test/unit_tests/test_complication.cc [new file with mode: 0644]

diff --git a/test/unit_tests/test_complication.cc b/test/unit_tests/test_complication.cc
new file mode 100644 (file)
index 0000000..4d4c27e
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * 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_complication.h>
+#include <aul_sock.h>
+#include <gtest/gtest.h>
+#include <stdlib.h>
+
+#include <bundle_cpp.h>
+
+#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::SetArgPointee;
+using ::testing::Invoke;
+using ::testing::SaveArg;
+
+namespace {
+
+constexpr const char kAppId[] = "org.tizen.complication";
+constexpr const char kProviderAppId[] = "org.tizen.complication-provider";
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+}  // namespace
+
+class ComplicationTest : public TestFixture {
+ public:
+  ComplicationTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+  virtual void SetUp() {
+    if (aul_is_initialized())
+      return;
+
+    aul_launch_init(nullptr, nullptr);
+  }
+
+  virtual void TearDown() {
+    aul_launch_fini();
+  }
+};
+
+TEST_F(ComplicationTest, aul_complication_update_request_P) {
+  int cmd = -1;
+  int opt = -1;
+  bundle* kb = nullptr;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(1)
+      .WillOnce(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;
+        kb = bundle_decode(pkt->data, pkt->len);
+        return n;
+      }));
+  EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+      .Times(1)
+      .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+        int ret = AUL_R_OK;
+        memcpy(buf, &ret, sizeof(int));
+        return sizeof(int);
+      }));
+
+  int ret = aul_complication_update_request(kAppId, kProviderAppId, getuid());
+  EXPECT_NE(kb, nullptr);
+  tizen_base::Bundle b(kb, false, true);
+
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, COMPLICATION_UPDATE_REQUEST);
+  EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_QUEUE));
+  EXPECT_EQ(b.GetString(AUL_K_CALLER_APPID), kAppId);
+  EXPECT_EQ(b.GetString(AUL_K_APPID), kProviderAppId);
+  EXPECT_EQ(b.GetString(AUL_K_COMPLICATION_MODE), UPDATE_REQUEST);
+}
+
+TEST_F(ComplicationTest, aul_complication_update_request_N) {
+  int ret = aul_complication_update_request(kAppId, nullptr, getuid());
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+
+  ret = aul_complication_update_request(nullptr, kProviderAppId, getuid());
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+}
+
+TEST_F(ComplicationTest, aul_complication_launch_with_extra_data_P) {
+  int cmd = -1;
+  int opt = -1;
+  bundle* kb = nullptr;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(1)
+      .WillOnce(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;
+        kb = bundle_decode(pkt->data, pkt->len);
+        return n;
+      }));
+  EXPECT_CALL(GetMock<SocketMock>(), recv(_, _, _, _))
+      .Times(1)
+      .WillOnce(Invoke([](int fd, void* buf, size_t n, int flags) -> ssize_t {
+        int ret = AUL_R_OK;
+        memcpy(buf, &ret, sizeof(int));
+        return sizeof(int);
+      }));
+
+  int ret = aul_complication_launch_with_extra_data(kAppId, kProviderAppId,
+      getuid(), "KEY", "VALUE");
+  EXPECT_NE(kb, nullptr);
+  tizen_base::Bundle b(kb, false, true);
+
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, COMPLICATION_UPDATE_REQUEST);
+  EXPECT_EQ(opt, (AUL_SOCK_BUNDLE | AUL_SOCK_QUEUE));
+  EXPECT_EQ(b.GetString(AUL_K_CALLER_APPID), kAppId);
+  EXPECT_EQ(b.GetString(AUL_K_APPID), kProviderAppId);
+  EXPECT_EQ(b.GetString(AUL_K_COMPLICATION_MODE), LAUNCH_REQUEST);
+  EXPECT_EQ(b.GetString("KEY"), "VALUE");
+}
+
+TEST_F(ComplicationTest, aul_complication_launch_with_extra_data_N) {
+  int ret = aul_complication_launch_with_extra_data(kAppId, kProviderAppId,
+      getuid(), "KEY", nullptr);
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+
+  ret = aul_complication_launch_with_extra_data(kAppId, kProviderAppId,
+      getuid(), nullptr, "VALUE");
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+
+  ret = aul_complication_launch_with_extra_data(kAppId, nullptr,
+      getuid(), "KEY", "VALUE");
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+
+  ret = aul_complication_launch_with_extra_data(nullptr, kProviderAppId,
+      getuid(), "KEY", "VALUE");
+  EXPECT_EQ(ret, AUL_R_EINVAL);
+}