add test cases for ds_tizen_global_resource 37/278637/1
authorSooChan Lim <sc1.lim@samsung.com>
Fri, 22 Jul 2022 06:24:01 +0000 (15:24 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Fri, 22 Jul 2022 14:00:44 +0000 (23:00 +0900)
Change-Id: I5ee07c9a598c6470f5703aef3d71a1cd564cd7a2

packaging/libds-tizen.spec
tests/meson.build
tests/tc_global_resource.cpp [new file with mode: 0644]

index abfe00c..4165443 100644 (file)
@@ -476,3 +476,4 @@ ninja -C builddir install
 %{_includedir}/libds-tizen/global_resource.h
 %{_libdir}/pkgconfig/libds-tizen-global-resource.pc
 %{_libdir}/libds-tizen-global-resource.so
+%{_bindir}/libds-tizen-global-resource-tests
index 43b227e..40e737e 100644 (file)
@@ -167,3 +167,24 @@ executable('libds-tizen-screen-rotation-tests',
   install_dir: libds_tizen_bindir,
   install : true
 )
+
+## global-resource tests
+tc_global_resource_files = [
+  'tc_main.cpp',
+  'tc_global_resource.cpp',
+]
+
+executable('libds-tizen-global-resource-tests',
+  [
+    tc_mock_files,
+    tc_global_resource_files
+  ],
+  dependencies: [
+    deps_test_common,
+    deps_libds_tizen_global_resource,
+    dependency('libdrm', required: true),
+    dependency('tizen-extension-client', required: true),
+  ],
+  install_dir: libds_tizen_bindir,
+  install : true
+)
diff --git a/tests/tc_global_resource.cpp b/tests/tc_global_resource.cpp
new file mode 100644 (file)
index 0000000..fbf5a4e
--- /dev/null
@@ -0,0 +1,371 @@
+#include "tc_main.h"
+#include "mockclient.h"
+#include "mockcompositor.h"
+#include <libds-tizen/global_resource.h>
+#include <tizen-extension-client-protocol.h>
+
+#define TIZEN_SURFACE_VERSION 1
+
+class MockGlobalResourceCompositor : public MockCompositor
+{
+public:
+    MockGlobalResourceCompositor()
+        : MockCompositor(&MockGlobalResourceCompositor::TestSetup, this)
+    {
+        ds_inf("%s : this(%p)", __func__, this);
+
+        // initialize the flags to check
+        bSurfaceDestroyed = false;
+
+        bDestroyed = false;
+        bGetResourceInfo = false;
+        bDestroyResourceInfo = false;
+        mResourceId = 0;
+    }
+
+    ~MockGlobalResourceCompositor()
+    {
+        ds_inf("%s : this(%p)", __func__, this);
+    }
+
+    static void TestSetup(void *data)
+    {
+        MockGlobalResourceCompositor *mockComp =
+            static_cast<MockGlobalResourceCompositor *>(data);
+        Compositor *comp = mockComp->compositor;
+
+        ds_inf("%s: mockComp(%p)", __func__, mockComp);
+
+        // new surface listener
+        mockComp->mNewSurfaceListener.notify =
+            MockGlobalResourceCompositor::NewSurfaceCallback;
+        mockComp->mNewSurfaceListener.parent = mockComp;
+        ds_compositor_add_new_surface_listener(comp->compositor,
+                &mockComp->mNewSurfaceListener);
+
+        mockComp->mGlobalResource =
+            ds_tizen_global_resource_create(comp->display);
+
+        // destroy listener
+        mockComp->mDestroyListener.notify =
+            MockGlobalResourceCompositor::DestroyCallback;
+        mockComp->mDestroyListener.parent = mockComp;
+        ds_tizen_global_resource_add_destroy_listener(mockComp->mGlobalResource,
+            &mockComp->mDestroyListener);
+
+        // get_resource_info listener
+        mockComp->mGetResourceInfoListener.notify =
+            MockGlobalResourceCompositor::GetResourceInfoCallback;
+        mockComp->mGetResourceInfoListener.parent = mockComp;
+        ds_tizen_global_resource_add_get_resource_info_listener(
+            mockComp->mGlobalResource,
+            &mockComp->mGetResourceInfoListener);
+    }
+
+    static void NewSurfaceCallback(struct wl_listener *listener, void *data)
+    {
+        MockGlobalResourceCompositor *mockComp =
+            reinterpret_cast<NewSurfaceListener *>(listener)->parent;
+        struct ds_surface *surface = static_cast<struct ds_surface *>(data);
+
+        ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
+
+        mockComp->mSurface = surface;
+
+        // del surface listener
+        mockComp->mDelSurfaceListener.notify =
+            MockGlobalResourceCompositor::DelSurfaceCallback;
+        mockComp->mDelSurfaceListener.parent = mockComp;
+        ds_surface_add_destroy_listener(surface,
+            &mockComp->mDelSurfaceListener);
+    }
+
+    static void DelSurfaceCallback(struct wl_listener *listener, void *data)
+    {
+        MockGlobalResourceCompositor *mockComp =
+            reinterpret_cast<DelSurfaceListener *>(listener)->parent;
+        struct ds_surface *surface = static_cast<struct ds_surface *>(data);
+
+        ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface);
+
+        if (ds_surface_get_wl_resource(mockComp->mSurface) ==
+            ds_surface_get_wl_resource(surface)) {
+            ds_inf("%s: surface is deleted.", __func__);
+            mockComp->bSurfaceDestroyed = true;
+        }
+    }
+
+    static void DestroyCallback(struct wl_listener *listener, void *data)
+    {
+        ds_inf("%s", __func__);
+
+        MockGlobalResourceCompositor *mockComp =
+            reinterpret_cast<DestroyListener *>(listener)->parent;
+
+        mockComp->bDestroyed = true;
+    }
+
+    static void GetResourceInfoCallback(struct wl_listener *listener,
+        void *data)
+    {
+        ds_inf("%s", __func__);
+
+        MockGlobalResourceCompositor *mockComp =
+            reinterpret_cast<GetResourceInfoListener *>(listener)->parent;
+        struct ds_tizen_global_resource_info *info =
+            static_cast<struct ds_tizen_global_resource_info *>(data);
+
+        ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
+
+        mockComp->bGetResourceInfo = true;
+
+        mockComp->mInfo = info;
+        mockComp->mSurface =
+            ds_tizen_global_resource_info_get_surface(mockComp->mInfo);
+
+        mockComp->mResourceId = ds_tizen_global_resource_get_universal_id(mockComp->mInfo);
+
+        // info destroy listener
+        mockComp->mGobalResourceInfoDestroyListener.notify =
+            MockGlobalResourceCompositor::GobalResourceInfoDestroyCallback;
+        mockComp->mGobalResourceInfoDestroyListener.parent = mockComp;
+        ds_tizen_global_resource_info_add_destroy_listener(mockComp->mInfo,
+            &mockComp->mGobalResourceInfoDestroyListener);
+    }
+
+    static void GobalResourceInfoDestroyCallback(struct wl_listener *listener,
+        void *data)
+    {
+        ds_inf("%s", __func__);
+
+        MockGlobalResourceCompositor *mockComp =
+            reinterpret_cast<GobalResourceInfoDestroyListener *>(listener)->parent;
+        struct ds_tizen_global_resource_info *info =
+            static_cast<struct ds_tizen_global_resource_info *>(data);
+
+        ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info);
+
+        if (mockComp->mInfo == info) {
+            ds_inf("%s: info is deleted.", __func__);
+            mockComp->bDestroyResourceInfo = true;
+        }
+    }
+
+public:
+    bool bSurfaceDestroyed;
+    bool bDestroyed;
+    bool bGetResourceInfo;
+    bool bDestroyResourceInfo;
+    uint32_t mResourceId;
+
+private:
+    struct ds_tizen_global_resource_info *mInfo;
+    struct ds_surface *mSurface;
+
+    struct NewSurfaceListener : ::wl_listener {
+        MockGlobalResourceCompositor *parent;
+    };
+    NewSurfaceListener mNewSurfaceListener;
+
+    struct DelSurfaceListener : ::wl_listener {
+        MockGlobalResourceCompositor *parent;
+    };
+    DelSurfaceListener mDelSurfaceListener;
+
+    struct ds_tizen_global_resource *mGlobalResource;
+
+    struct DestroyListener : ::wl_listener {
+        MockGlobalResourceCompositor *parent;
+    };
+    DestroyListener mDestroyListener;
+
+    struct GetResourceInfoListener : ::wl_listener {
+        MockGlobalResourceCompositor *parent;
+    };
+    GetResourceInfoListener mGetResourceInfoListener;
+
+    struct GobalResourceInfoDestroyListener : ::wl_listener {
+        MockGlobalResourceCompositor *parent;
+    };
+    GobalResourceInfoDestroyListener mGobalResourceInfoDestroyListener;
+};
+
+class MockGlobalResourceClient : public MockClient
+{
+public:
+    MockGlobalResourceClient()
+        : mResourceId(0),
+          compositor_res(nullptr),
+          tizen_surface_res(nullptr)
+    {}
+    MockGlobalResourceClient(const struct wl_registry_listener *listener)
+        : MockClient(listener, this)
+    {
+        ds_inf("%s", __func__);
+
+        mResourceId = 0;
+    }
+    ~MockGlobalResourceClient()
+    {
+        ds_inf("%s", __func__);
+    }
+
+    void SetWlCompositor(struct wl_compositor *global_res)
+    {
+        ds_inf("%s", __func__);
+
+        compositor_res = global_res;
+    }
+
+    struct wl_compositor *GetWlCompositor()
+    {
+        ds_inf("%s", __func__);
+
+        return compositor_res;
+    }
+
+    void SetTizenSurfaceShm(struct tizen_surface *global_res)
+    {
+        ds_inf("%s", __func__);
+
+        tizen_surface_res = global_res;
+    }
+
+    struct tizen_surface *GetTizenSurfaceShm()
+    {
+        ds_inf("%s", __func__);
+
+        return tizen_surface_res;
+    }
+
+public:
+    uint32_t mResourceId;
+
+private:
+    struct wl_compositor *compositor_res;
+    struct tizen_surface *tizen_surface_res;
+};
+
+static void
+client_registry_cb_global(void *data, struct wl_registry *registry,
+    uint32_t name, const char *interface, uint32_t version)
+{
+    ds_inf("%s", __func__);
+
+    MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
+    struct wl_compositor *compositor_res;
+    struct tizen_surface *tizen_surface_res;
+
+    if (!strcmp(interface, "wl_compositor")) {
+        compositor_res = (struct wl_compositor *)wl_registry_bind(registry,
+            name, &wl_compositor_interface, 1);
+        if (compositor_res == nullptr) {
+            ds_err("wl_registry_bind() failed. wl_compositor resource.");
+            return;
+        }
+        client->SetWlCompositor(compositor_res);
+    } else if (!strcmp(interface, "tizen_surface")) {
+        tizen_surface_res = (struct tizen_surface *)wl_registry_bind(registry,
+            name, &tizen_surface_interface, TIZEN_SURFACE_VERSION);
+        if (tizen_surface_res == nullptr) {
+            ds_err("wl_registry_bind() failed. tizen_surface resource.");
+            return;
+        }
+        client->SetTizenSurfaceShm(tizen_surface_res);
+    }
+}
+
+static void
+client_registry_cb_global_remove(void *data, struct wl_registry *registry,
+    uint32_t name)
+{
+    ds_inf("%s", __func__);
+
+    MockGlobalResourceClient *client = static_cast<MockGlobalResourceClient *>(data);
+    struct wl_compositor *compositor_res = client->GetWlCompositor();
+    struct tizen_surface *tizen_surface_res = client->GetTizenSurfaceShm();
+
+    tizen_surface_destroy(tizen_surface_res);
+    wl_compositor_destroy(compositor_res);
+}
+
+static const struct wl_registry_listener registry_listener = {
+    .global = client_registry_cb_global,
+    .global_remove = client_registry_cb_global_remove
+};
+
+static void
+client_tizen_resource_cb_resoruce_id(void *data,
+    struct tizen_resource *resource_res, uint32_t id)
+{
+    ds_inf("%s", __func__);
+
+    MockGlobalResourceClient *client =
+        static_cast<MockGlobalResourceClient *>(data);
+
+    client->mResourceId = id;
+}
+
+static const struct
+tizen_resource_listener resource_cb_listener = {
+    .resource_id = client_tizen_resource_cb_resoruce_id,
+};
+
+class GlobalResourceTest : public ::testing::Test
+{
+public:
+    void SetUp(void) override;
+    void TearDown(void) override;
+
+    MockGlobalResourceCompositor *comp;
+    MockGlobalResourceClient *client;
+    struct wl_compositor *compositor_res;
+    struct tizen_surface *tizen_surface_res;
+    struct wl_surface *surface_res;
+    struct tizen_resource *resource_res;
+};
+
+void
+GlobalResourceTest::SetUp(void)
+{
+    //ds_log_init(DS_DBG, NULL);
+
+    ds_inf("%s", __func__);
+
+    comp = new MockGlobalResourceCompositor();
+    client = new MockGlobalResourceClient(&registry_listener);
+    compositor_res = client->GetWlCompositor();
+    tizen_surface_res = client->GetTizenSurfaceShm();
+    surface_res = wl_compositor_create_surface(compositor_res);
+
+    resource_res =
+        tizen_surface_get_tizen_resource(tizen_surface_res, surface_res);
+
+    tizen_resource_add_listener(resource_res, &resource_cb_listener, client);
+
+    client->RoundTrip();
+}
+
+void
+GlobalResourceTest::TearDown(void)
+{
+    ds_inf("%s", __func__);
+
+    tizen_resource_destroy(resource_res);
+    client->RoundTrip();
+    EXPECT_TRUE(comp->bDestroyResourceInfo);
+
+    wl_surface_destroy(surface_res);
+    client->RoundTrip();
+    EXPECT_TRUE(comp->bSurfaceDestroyed);
+
+    delete client;
+    delete comp;
+}
+
+TEST_F(GlobalResourceTest, Create_P)
+{
+    EXPECT_TRUE(true);
+    EXPECT_TRUE(comp->bGetResourceInfo);
+    EXPECT_TRUE(client->mResourceId == 0);
+}