add mockcompositor and mockclient 04/232004/5
authorSooChan Lim <sc1.lim@samsung.com>
Mon, 27 Apr 2020 08:49:15 +0000 (17:49 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Mon, 27 Apr 2020 10:19:51 +0000 (19:19 +0900)
This is initial version for mockcompositor and mockclient.
The mockcompsitor process the clients' events in every some milliseconds.
The mockclient has only RoundTrip method right now.

Change-Id: I5395a55863a919535f376ca24cc0b3b750ca0554

unittests/Makefile.am
unittests/mockclient.cpp [new file with mode: 0644]
unittests/mockclient.h [new file with mode: 0644]
unittests/mockcompositor.cpp [new file with mode: 0644]
unittests/mockcompositor.h [new file with mode: 0644]
unittests/tc-mockcompositor.cpp [new file with mode: 0644]
unittests/tc-wayland-extension.h [new file with mode: 0644]

index b4ec744..183a71f 100644 (file)
@@ -1,7 +1,10 @@
 bin_PROGRAMS = libwayland-extension-unittests
 
 libwayland_extension_unittests_SOURCES = \
-       tc-main.cpp
+       tc-main.cpp \
+       mockcompositor.cpp \
+       mockclient.cpp \
+       tc-mockcompositor.cpp
 
 libwayland_extension_unittests_CXXFLAGS = \
        -I$(top_srcdir)/protocol/tizen \
diff --git a/unittests/mockclient.cpp b/unittests/mockclient.cpp
new file mode 100644 (file)
index 0000000..501b405
--- /dev/null
@@ -0,0 +1,47 @@
+/**************************************************************************
+ *
+ * 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 "mockclient.h"
+#include <iostream>
+
+MockClient::MockClient()
+{
+       display = wl_display_connect("test-wl-extension-socket");
+       if (!display)
+               std::cout << "fail to connect display\n";
+}
+
+MockClient::~MockClient()
+{
+       wl_display_disconnect(display);
+}
+
+void MockClient::RoundTrip()
+{
+       wl_display_roundtrip(display);
+}
\ No newline at end of file
diff --git a/unittests/mockclient.h b/unittests/mockclient.h
new file mode 100644 (file)
index 0000000..22777fd
--- /dev/null
@@ -0,0 +1,45 @@
+/**************************************************************************
+ *
+ * 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 _MOCKCLIENT_H_
+#define _MOCKCLIENT_H_
+
+#include <wayland-client.h>
+
+class MockClient
+{
+public:
+       MockClient();
+       ~MockClient();
+       void RoundTrip();
+
+private:
+       struct wl_display *display;
+};
+
+#endif
diff --git a/unittests/mockcompositor.cpp b/unittests/mockcompositor.cpp
new file mode 100644 (file)
index 0000000..9ed4e39
--- /dev/null
@@ -0,0 +1,109 @@
+/**************************************************************************
+ *
+ * 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);
+
+       std::cout << "res:" << wl_resource_get_class(message->resource) << " "
+                               << "op:" << message->message_opcode << " "
+                               << "name:" << message->message->name << "\n";
+}
+
+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 = wl_display_add_socket(display, "test-wl-extension-socket");
+       if (ret != 0)
+               std::cout << "fail to add socket\n";
+}
+
+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() {
+       // finish the child thread(mockcompositor).
+       alive = false;
+       th.join();
+}
+
+void MockCompositor::Process()
+{
+       std::lock_guard<std::mutex> lock(m);
+       compositor->DispatchEvents();
+}
diff --git a/unittests/mockcompositor.h b/unittests/mockcompositor.h
new file mode 100644 (file)
index 0000000..7280b2a
--- /dev/null
@@ -0,0 +1,70 @@
+/**************************************************************************
+ *
+ * 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>
+
+class Compositor
+{
+public:
+       Compositor();
+       ~Compositor();
+       void DispatchEvents();
+
+private:
+       struct wl_display *display;
+       struct wl_event_loop *loop;
+       struct wl_protocol_logger *logger;
+};
+
+class MockCompositor
+{
+public:
+       MockCompositor();
+       ~MockCompositor();
+
+       void Process();
+
+private:
+       std::thread th;
+       std::mutex m;
+       std::condition_variable cv;
+
+       bool alive = true;
+       bool ready = false;
+
+       Compositor *compositor;
+};
+
+#endif
diff --git a/unittests/tc-mockcompositor.cpp b/unittests/tc-mockcompositor.cpp
new file mode 100644 (file)
index 0000000..3d98b33
--- /dev/null
@@ -0,0 +1,61 @@
+/**************************************************************************
+ *
+ * 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 "tc-wayland-extension.h"
+#include "mockcompositor.h"
+#include "mockclient.h"
+
+class MockCompositorTest : public ::testing::Test
+{
+public:
+       void SetUp(void) override;
+       void TearDown(void) override;
+};
+
+void MockCompositorTest::SetUp(void)
+{
+       setenv("XDG_RUNTIME_DIR", "/run", 1);
+}
+
+void MockCompositorTest::TearDown(void)
+{
+       unsetenv("XDG_RUNTIME_DIR");
+}
+
+TEST_F(MockCompositorTest, NewCompositor)
+{
+       MockCompositor compositor;
+}
+
+TEST_F(MockCompositorTest, RoundTrip)
+{
+       MockCompositor compositor;
+       MockClient client;
+
+       client.RoundTrip();
+}
diff --git a/unittests/tc-wayland-extension.h b/unittests/tc-wayland-extension.h
new file mode 100644 (file)
index 0000000..f4b8dda
--- /dev/null
@@ -0,0 +1,40 @@
+/**************************************************************************
+ *
+ * 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 _TC_WAYLAND_EXTENSION_H_
+#define _TC_WAYLAND_EXTENSION_H_
+
+#include <iostream>
+#include <gmock/gmock.h>
+
+using ::testing::TestWithParam;
+using ::testing::Bool;
+using ::testing::Values;
+using ::testing::Combine;
+
+#endif
\ No newline at end of file