From 868c14e4fcefaa522f4b89386f41f83f40f86540 Mon Sep 17 00:00:00 2001 From: SooChan Lim Date: Mon, 27 Apr 2020 17:49:15 +0900 Subject: [PATCH] add mockcompositor and mockclient 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 | 5 +- unittests/mockclient.cpp | 47 +++++++++++++ unittests/mockclient.h | 45 +++++++++++++ unittests/mockcompositor.cpp | 109 +++++++++++++++++++++++++++++++ unittests/mockcompositor.h | 70 ++++++++++++++++++++ unittests/tc-mockcompositor.cpp | 61 +++++++++++++++++ unittests/tc-wayland-extension.h | 40 ++++++++++++ 7 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 unittests/mockclient.cpp create mode 100644 unittests/mockclient.h create mode 100644 unittests/mockcompositor.cpp create mode 100644 unittests/mockcompositor.h create mode 100644 unittests/tc-mockcompositor.cpp create mode 100644 unittests/tc-wayland-extension.h diff --git a/unittests/Makefile.am b/unittests/Makefile.am index b4ec744..183a71f 100644 --- a/unittests/Makefile.am +++ b/unittests/Makefile.am @@ -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 index 0000000..501b405 --- /dev/null +++ b/unittests/mockclient.cpp @@ -0,0 +1,47 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 + +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 index 0000000..22777fd --- /dev/null +++ b/unittests/mockclient.h @@ -0,0 +1,45 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 + +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 index 0000000..9ed4e39 --- /dev/null +++ b/unittests/mockcompositor.cpp @@ -0,0 +1,109 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 + +static void +logger_func(void *user_data, enum wl_protocol_logger_type type, + const struct wl_protocol_logger_message *message) +{ + //Compositor *c = static_cast(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 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 lock(m); + compositor->DispatchEvents(); +} diff --git a/unittests/mockcompositor.h b/unittests/mockcompositor.h new file mode 100644 index 0000000..7280b2a --- /dev/null +++ b/unittests/mockcompositor.h @@ -0,0 +1,70 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 +#include +#include +#include +#include + +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 index 0000000..3d98b33 --- /dev/null +++ b/unittests/tc-mockcompositor.cpp @@ -0,0 +1,61 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 index 0000000..f4b8dda --- /dev/null +++ b/unittests/tc-wayland-extension.h @@ -0,0 +1,40 @@ +/************************************************************************** + * + * Copyright 2020 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: SooChan Lim + * + * 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 +#include + +using ::testing::TestWithParam; +using ::testing::Bool; +using ::testing::Values; +using ::testing::Combine; + +#endif \ No newline at end of file -- 2.34.1