diff --git a/src/test/shared/undef_x11_macros.h b/src/test/shared/undef_x11_macros.h deleted file mode 100644 index 5eda6e4d5..000000000 --- a/src/test/shared/undef_x11_macros.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2024 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -/** - * This header fixes conflicts between X11 and Google Test. - * - * It should be included after headers of code under test (that use X11) - * and before including Google Test headers. - */ - -#undef None // NOSONAR -#undef Bool // NOSONAR diff --git a/src/test/unittests/platform/XWindowsClipboardTests.cpp b/src/test/unittests/platform/XWindowsClipboardTests.cpp deleted file mode 100644 index 194ca096a..000000000 --- a/src/test/unittests/platform/XWindowsClipboardTests.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2012 - 2016, 2024 Symless Ltd. - * SPDX-FileCopyrightText: (C) 2011 Nick Bolton - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#include "platform/XWindowsClipboard.h" - -#include "test/shared/undef_x11_macros.h" - -#include - -const auto None = 0L; - -class TestXWindowsClipboard : public XWindowsClipboard -{ -public: - class TestCICCCMGetClipboard : public CICCCMGetClipboard - { - public: - TestCICCCMGetClipboard() : CICCCMGetClipboard(None, None, None) - { - } - }; -}; - -TEST(XWindowsClipboardTests_CICCCMGetClipboard, ctor_default_errorNone) -{ - TestXWindowsClipboard::TestCICCCMGetClipboard clipboard; - - EXPECT_EQ(None, clipboard.error()); -} - -// TODO: fix segfault in release mode -#if 0 - -class XWindowsClipboardTests : public ::testing::Test { -protected: - void SetUp() override { - m_display = XOpenDisplay(nullptr); - int screen = DefaultScreen(m_display); - Window root = XRootWindow(m_display, screen); - - XSetWindowAttributes attr; - attr.do_not_propagate_mask = 0; - attr.override_redirect = True; - attr.cursor = Cursor(); - - m_window = XCreateWindow( - m_display, root, 0, 0, 1, 1, 0, 0, InputOnly, nullptr, 0, &attr); - - m_clipboard = std::make_unique(m_display, m_window, 0); - m_clipboard->open(0); - m_clipboard->empty(); - } - - void TearDown() override { - XDestroyWindow(m_display, m_window); - XCloseDisplay(m_display); - } - - XWindowsClipboard &getClipboard() { return *m_clipboard; } - -private: - Display *m_display; - Window m_window; - std::unique_ptr m_clipboard; -}; - -TEST_F(XWindowsClipboardTests, empty_openCalled_returnsTrue) { - XWindowsClipboard &clipboard = getClipboard(); - - bool actual = clipboard.empty(); - - EXPECT_EQ(true, actual); -} - -TEST_F(XWindowsClipboardTests, empty_singleFormat_hasReturnsFalse) { - XWindowsClipboard &clipboard = getClipboard(); - clipboard.add(XWindowsClipboard::kText, "synergy rocks!"); - - clipboard.empty(); - - bool actual = clipboard.has(XWindowsClipboard::kText); - EXPECT_FALSE(actual); -} - -TEST_F(XWindowsClipboardTests, add_newValue_valueWasStored) { - XWindowsClipboard &clipboard = getClipboard(); - - clipboard.add(IClipboard::kText, "synergy rocks!"); - - std::string actual = clipboard.get(IClipboard::kText); - EXPECT_EQ("synergy rocks!", actual); -} - -TEST_F(XWindowsClipboardTests, add_replaceValue_valueWasReplaced) { - XWindowsClipboard &clipboard = getClipboard(); - - clipboard.add(IClipboard::kText, "synergy rocks!"); - clipboard.add(IClipboard::kText, "maxivista sucks"); // haha, just kidding. - - std::string actual = clipboard.get(IClipboard::kText); - EXPECT_EQ("maxivista sucks", actual); -} - -TEST_F(XWindowsClipboardTests, close_isOpen_noErrors) { - XWindowsClipboard &clipboard = getClipboard(); - - // clipboard opened in createClipboard() - clipboard.close(); - - // can't assert anything -} - -TEST_F(XWindowsClipboardTests, has_withFormatAdded_returnsTrue) { - XWindowsClipboard &clipboard = getClipboard(); - clipboard.add(IClipboard::kText, "synergy rocks!"); - - bool actual = clipboard.has(IClipboard::kText); - - EXPECT_EQ(true, actual); -} - -TEST_F(XWindowsClipboardTests, has_withNoFormats_returnsFalse) { - XWindowsClipboard &clipboard = getClipboard(); - - bool actual = clipboard.has(IClipboard::kText); - - EXPECT_FALSE(actual); -} - -TEST_F(XWindowsClipboardTests, get_withNoFormats_returnsEmpty) { - XWindowsClipboard &clipboard = getClipboard(); - - std::string actual = clipboard.get(IClipboard::kText); - - EXPECT_EQ("", actual); -} - -TEST_F(XWindowsClipboardTests, get_withFormatAdded_returnsExpected) { - XWindowsClipboard &clipboard = getClipboard(); - clipboard.add(IClipboard::kText, "synergy rocks!"); - - std::string actual = clipboard.get(IClipboard::kText); - - EXPECT_EQ("synergy rocks!", actual); -} - -#endif diff --git a/src/unittests/platform/CMakeLists.txt b/src/unittests/platform/CMakeLists.txt index b8757f3af..318c4a3d0 100644 --- a/src/unittests/platform/CMakeLists.txt +++ b/src/unittests/platform/CMakeLists.txt @@ -9,4 +9,12 @@ if (WIN32) SOURCE MSWindowsClipboardTests.cpp WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/platform" ) +elseif(UNIX AND NOT APPLE) + create_test( + NAME XWindowsClipboardTests + DEPENDS platform + LIBS base arch + SOURCE XWindowsClipboardTests.cpp + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/platform" + ) endif() diff --git a/src/unittests/platform/XWindowsClipboardTests.cpp b/src/unittests/platform/XWindowsClipboardTests.cpp new file mode 100644 index 000000000..32f13e40c --- /dev/null +++ b/src/unittests/platform/XWindowsClipboardTests.cpp @@ -0,0 +1,77 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd. + * SPDX-FileCopyrightText: (C) 2011 Nick Bolton + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "XWindowsClipboardTests.h" + +#include "../../lib/platform/XWindowsClipboard.h" + +class TestXWindowsClipboard : public XWindowsClipboard +{ +public: + class TestCICCCMGetClipboard : public CICCCMGetClipboard + { + public: + TestCICCCMGetClipboard() : CICCCMGetClipboard(None, None, None) + { + } + }; +}; + +void XWindowsClipboardTests::defaultCtor() +{ + TestXWindowsClipboard::TestCICCCMGetClipboard clipboard; + QCOMPARE(None, clipboard.error()); +} + +// Only work on XWindows +#if !WINAPI_LIBEI && !WINAPI_PORTAL +void XWindowsClipboardTests::initTestCase() +{ + m_display = XOpenDisplay(nullptr); + int screen = DefaultScreen(m_display); + Window root = XRootWindow(m_display, screen); + + XSetWindowAttributes attr; + attr.do_not_propagate_mask = 0; + attr.override_redirect = True; + attr.cursor = Cursor(); + + m_window = XCreateWindow(m_display, root, 0, 0, 1, 1, 0, 0, InputOnly, nullptr, 0, &attr); +} + +void XWindowsClipboardTests::cleanupTestCase() +{ + XDestroyWindow(m_display, m_window); + XCloseDisplay(m_display); +} + +void XWindowsClipboardTests::open() +{ + m_clipboard = std::make_unique(m_display, m_window, 0); + QVERIFY(m_clipboard->open(0)); + QVERIFY(m_clipboard->empty()); +} + +void XWindowsClipboardTests::singleFormat() +{ + auto &clipboard = getClipboard(); + QVERIFY(clipboard.empty()); + clipboard.add(XWindowsClipboard::kText, m_testString); + QVERIFY(!clipboard.has(XWindowsClipboard::kText)); + QCOMPARE(clipboard.get(XWindowsClipboard::kText), m_testString); + + clipboard.add(XWindowsClipboard::kText, m_testString2); + QCOMPARE(clipboard.get(XWindowsClipboard::kText), m_testString2); +} + +XWindowsClipboard &XWindowsClipboardTests::getClipboard() +{ + return *m_clipboard; +} +#endif +QTEST_MAIN(XWindowsClipboardTests) diff --git a/src/unittests/platform/XWindowsClipboardTests.h b/src/unittests/platform/XWindowsClipboardTests.h new file mode 100644 index 000000000..7450c28e3 --- /dev/null +++ b/src/unittests/platform/XWindowsClipboardTests.h @@ -0,0 +1,39 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "base/Log.h" + +#if !WINAPI_LIBEI && !WINAPI_PORTAL +#include "../../lib/platform/XWindowsClipboard.h" +#endif + +#include + +class XWindowsClipboardTests : public QObject +{ + Q_OBJECT +private slots: + // Test are run in order top to bottom + void defaultCtor(); + // Tests only work on X Windows +#if !WINAPI_LIBEI && !WINAPI_PORTAL + void initTestCase(); + void cleanupTestCase(); + void open(); + void singleFormat(); +#endif +private: + Arch m_arch; + Log m_log; +#if !WINAPI_LIBEI && !WINAPI_PORTAL + const std::string m_testString = "deskflow test string"; + const std::string m_testString2 = "Another String"; + Display *m_display; + Window m_window; + XWindowsClipboard &getClipboard(); + std::unique_ptr m_clipboard; +#endif +};