add mock client/compositor 90/278390/1
authorSooChan Lim <sc1.lim@samsung.com>
Sun, 10 Jul 2022 04:46:59 +0000 (13:46 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Wed, 20 Jul 2022 05:05:47 +0000 (14:05 +0900)
This is the mock classes for testing request/event interaction between
client and server.

Change-Id: Ide1366d47f3cae83944520c8459a62bf4d737a1f

tests/mockclient.cpp [new file with mode: 0644]
tests/mockclient.h [new file with mode: 0644]
tests/mockcompositor.cpp [new file with mode: 0644]
tests/mockcompositor.h [new file with mode: 0644]

diff --git a/tests/mockclient.cpp b/tests/mockclient.cpp
new file mode 100644 (file)
index 0000000..f9f9581
--- /dev/null
@@ -0,0 +1,70 @@
+/**************************************************************************
+ *
+ * Copyright 2022 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#include "mockclient.h"
+#include <iostream>
+
+MockClient::MockClient()
+{
+    display = wl_display_connect("test-ds-tizen");
+    if (!display)
+        ds_err("wl_display_connect() filed.");
+
+    registry = wl_display_get_registry(display);
+    if (!registry)
+        ds_err("wl_display_get_registry() filed.");
+}
+
+MockClient::MockClient(const struct wl_registry_listener *registry_listener, void *data)
+{
+    int ret;
+    display = wl_display_connect("test-ds-tizen");
+    if (!display)
+        ds_err("wl_display_connect() filed.");
+
+    registry = wl_display_get_registry(display);
+    if (!registry)
+        ds_err("wl_display_get_registry() filed.");
+
+    ret = wl_registry_add_listener(registry, registry_listener, data);
+    if (ret)
+        ds_err("wl_registry_add_listener() filed.");
+
+    wl_display_roundtrip(display);
+}
+
+MockClient::~MockClient()
+{
+    wl_registry_destroy(registry);
+    wl_display_disconnect(display);
+}
+
+void MockClient::RoundTrip()
+{
+    wl_display_roundtrip(display);
+}
\ No newline at end of file
diff --git a/tests/mockclient.h b/tests/mockclient.h
new file mode 100644 (file)
index 0000000..178b9bc
--- /dev/null
@@ -0,0 +1,48 @@
+/**************************************************************************
+ *
+ * Copyright 2022 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#ifndef _MOCKCLIENT_H_
+#define _MOCKCLIENT_H_
+
+#include <libds/log.h>
+#include <wayland-client.h>
+
+class MockClient
+{
+public:
+    MockClient();
+    MockClient(const struct wl_registry_listener *registry_listener, void *data);
+    virtual ~MockClient();
+    void RoundTrip();
+
+private:
+    struct wl_display *display;
+    struct wl_registry *registry;
+};
+
+#endif
diff --git a/tests/mockcompositor.cpp b/tests/mockcompositor.cpp
new file mode 100644 (file)
index 0000000..62fc779
--- /dev/null
@@ -0,0 +1,149 @@
+/**************************************************************************
+ *
+ * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#include "mockcompositor.h"
+#include <iostream>
+
+static void
+logger_func(void *user_data, enum wl_protocol_logger_type type,
+        const struct wl_protocol_logger_message *message)
+{
+    //Compositor *c = static_cast<Compositor *>(user_data);
+#if 0
+    std::cout << "res:" << wl_resource_get_class(message->resource) << " "
+              << "op:" << message->message_opcode << " "
+              << "name:" << message->message->name << "\n";
+#endif
+}
+
+Compositor::Compositor()
+    : display(wl_display_create()),
+      loop(wl_display_get_event_loop(display)),
+      logger(wl_display_add_protocol_logger(display, logger_func, this))
+{
+    int ret;
+
+    ret = wl_display_add_socket(display, "test-ds-tizen");
+    if (ret != 0)
+        ds_err("wl_display_add_socket() filed.");
+
+    ret = wl_display_init_shm(display);
+    if (ret != 0)
+        ds_err("wl_display_init_shm() filed.");
+
+    compositor = ds_compositor_create(display);
+    if (compositor == nullptr)
+        ds_err("ds_compositor_create() filed.");
+}
+
+Compositor::~Compositor ()
+{
+    wl_list *clients = wl_display_get_client_list(display);
+    wl_client *client = nullptr;
+
+    wl_client_for_each(client, clients) {
+        wl_client_destroy(client);
+    }
+
+    wl_protocol_logger_destroy(logger);
+    wl_display_destroy(display);
+}
+
+void Compositor::DispatchEvents()
+{
+    /* flush any pending client events */
+    wl_display_flush_clients(display);
+    /* dispatch any pending main loop events */
+    wl_event_loop_dispatch(loop, 0);
+}
+
+MockCompositor::MockCompositor()
+    : compositor(nullptr)
+{
+    th = std::thread([this](){
+        Compositor c;
+        compositor = &c;
+
+        // mockcompositor is ready
+        ready = true;
+        cv.notify_one();
+
+        while (alive) {
+            // mockcompositor process the events in every 40 milliseconds
+            std::this_thread::sleep_for(std::chrono::milliseconds(40));
+            Process();
+        }
+    });
+
+    // wait until the child thread(mockcompositor) is ready
+    {
+        std::unique_lock<std::mutex> lock(m);
+        cv.wait(lock, [this]{return ready;});
+    }
+}
+
+MockCompositor::MockCompositor(ds_test_setup_func_t setup_func, void *data)
+    : compositor(nullptr)
+{
+    th = std::thread([this, setup_func, data](){
+        Compositor c;
+        compositor = &c;
+
+        // call test setup_function
+        setup_func(data);
+
+        // mockcompositor is ready
+        ready = true;
+        cv.notify_one();
+
+        while (alive) {
+            // mockcompositor process the events in every 40 milliseconds
+            std::this_thread::sleep_for(std::chrono::milliseconds(40));
+
+            Process();
+        }
+    });
+
+    // wait until the child thread(mockcompositor) is ready
+    {
+        std::unique_lock<std::mutex> lock(m);
+        cv.wait(lock, [this]{return ready;});
+    }
+}
+
+MockCompositor::~MockCompositor() {
+    // finish the child thread(mockcompositor).
+    alive = false;
+    th.join();
+}
+
+void MockCompositor::Process()
+{
+    std::lock_guard<std::mutex> lock(m);
+    compositor->DispatchEvents();
+}
diff --git a/tests/mockcompositor.h b/tests/mockcompositor.h
new file mode 100644 (file)
index 0000000..2b03f46
--- /dev/null
@@ -0,0 +1,80 @@
+/**************************************************************************
+ *
+ * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+**************************************************************************/
+
+#ifndef _MOCKCOMPOSITOR_H_
+#define _MOCKCOMPOSITOR_H_
+
+#include <iostream>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+#include <wayland-server.h>
+#include <libds/log.h>
+#include <libds/backend.h>
+#include <libds/output.h>
+#include <libds/compositor.h>
+
+// ds_listener test function call
+typedef void (*ds_test_setup_func_t)(void *data);
+
+class Compositor
+{
+public:
+    Compositor();
+    ~Compositor();
+    void DispatchEvents();
+
+public:
+    struct wl_display *display;
+    struct wl_event_loop *loop;
+    struct wl_protocol_logger *logger;
+    struct ds_compositor *compositor;
+};
+
+class MockCompositor
+{
+public:
+    MockCompositor();
+    MockCompositor(ds_test_setup_func_t setup_func, void *data);
+    virtual ~MockCompositor();
+
+    void Process();
+
+protected:
+    Compositor *compositor;
+
+private:
+    std::thread th;
+    std::mutex m;
+    std::condition_variable cv;
+
+    bool alive = true;
+    bool ready = false;
+};
+
+#endif