Skip to content

Commit

Permalink
Tests: Test copying data from clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Nov 10, 2024
1 parent accd367 commit a27564c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/platform/dummy/dummyclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void DummyClipboard::setData(ClipboardMode mode, const QVariantMap &dataMap)
QGuiApplication::clipboard()->setMimeData( createMimeData(dataMap), modeToQClipboardMode(mode) );
}

void DummyClipboard::setRawData(ClipboardMode mode, QMimeData *mimeData)
{
QGuiApplication::clipboard()->setMimeData( mimeData, modeToQClipboardMode(mode) );
}

const QMimeData *DummyClipboard::rawMimeData(ClipboardMode mode) const
{
return QGuiApplication::clipboard()->mimeData( modeToQClipboardMode(mode) );
Expand Down
1 change: 1 addition & 0 deletions src/platform/dummy/dummyclipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DummyClipboard : public PlatformClipboard
QVariantMap data(ClipboardMode mode, const QStringList &formats) const override;

void setData(ClipboardMode mode, const QVariantMap &dataMap) override;
void setRawData(ClipboardMode mode, QMimeData *mimeData) override;

const QMimeData *mimeData(ClipboardMode mode) const override;

Expand Down
1 change: 1 addition & 0 deletions src/platform/platformclipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PlatformClipboard : public QObject
* Set data to clipboard.
*/
virtual void setData(ClipboardMode mode, const QVariantMap &dataMap) = 0;
virtual void setRawData(ClipboardMode mode, QMimeData *mimeData) = 0;

virtual const QMimeData *mimeData(ClipboardMode mode) const = 0;

Expand Down
10 changes: 10 additions & 0 deletions src/platform/x11/x11platformclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ void X11PlatformClipboard::setData(ClipboardMode mode, const QVariantMap &dataMa
}
}

void X11PlatformClipboard::setRawData(ClipboardMode mode, QMimeData *mimeData)
{
if ( X11Info::isPlatformX11() ) {
DummyClipboard::setRawData(mode, mimeData);
} else {
const auto qmode = modeToQClipboardMode(mode);
WaylandClipboard::instance()->setMimeData(mimeData, qmode);
}
}

const QMimeData *X11PlatformClipboard::rawMimeData(ClipboardMode mode) const
{
if ( X11Info::isPlatformX11() )
Expand Down
1 change: 1 addition & 0 deletions src/platform/x11/x11platformclipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class X11PlatformClipboard final : public DummyClipboard
QVariantMap data(ClipboardMode mode, const QStringList &formats) const override;

void setData(ClipboardMode mode, const QVariantMap &dataMap) override;
void setRawData(ClipboardMode mode, QMimeData *mimeData) override;

bool isSelectionSupported() const override { return m_selectionSupported; }

Expand Down
93 changes: 93 additions & 0 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "platform/platformclipboard.h"
#include "platform/platformnativeinterface.h"

#include <QClipboard>
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
Expand Down Expand Up @@ -270,6 +271,7 @@ class TestInterfaceImpl final : public TestInterface {
m_env.insert("COPYQ_LOG_LEVEL", "DEBUG");
m_env.insert("COPYQ_SESSION_COLOR", defaultSessionColor);
m_env.insert("COPYQ_SESSION_NAME", "TEST");
m_env.insert("COPYQ_CLIPBOARD_COPY_TIMEOUT_MS", "2000");
}

~TestInterfaceImpl()
Expand Down Expand Up @@ -835,6 +837,41 @@ QVariantMap secretData(const QByteArray &text)
};
}

class SlowMimeData final : public QMimeData {
public:
explicit SlowMimeData(const QByteArray &data, int delayMs)
: m_data(data)
, m_delayMs(delayMs)
{}

bool hasFormat(const QString &mimeType) const override
{
return formats().contains(mimeType);
}

QStringList formats() const override
{
return {"a/a", "b/b", "c/c"};
}

protected:
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QVariant retrieveData(const QString &mimeType, QMetaType) const override
#else
QVariant retrieveData(const QString &mimeType, QVariant::Type) const override
#endif
{
if (formats().contains(mimeType)) {
waitFor(m_delayMs);
return m_data;
}
return {};
}
private:
QByteArray m_data;
int m_delayMs;
};

} // namespace

Tests::Tests(const TestInterfacePtr &test, QObject *parent)
Expand Down Expand Up @@ -4999,6 +5036,62 @@ void Tests::saveLargeItem()
RUN(args << "getItem(0)['application/x-copyq-test-data'].length", "260000\n");
}

void Tests::slowClipboard()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.clipboardFormatsToSave = function() { return ["a/a", "b/b", "c/c"] }'
},
])
)";
RUN(script, "");
WAIT_ON_OUTPUT("clipboardFormatsToSave", "a/a\nb/b\nc/c\n");

TEST( m_test->setClipboard("A", "a/a") );
WAIT_ON_OUTPUT("clipboard" << "a/a", "A");

auto clipboard = platformNativeInterface()->clipboard();
for (int i = 0; i < 3; ++i) {
QMimeData *data = new SlowMimeData(QByteArray::number(i), 501);
clipboard->setRawData(ClipboardMode::Clipboard, data);
waitFor(100);
}
WAIT_ON_OUTPUT("read('a/a', 0)", "2");
RUN("read('b/b', 0)", "2");
RUN("read('c/c', 0)", "2");
RUN("read('?', 0)", "a/a\nb/b\nc/c\n");

QMimeData *data = new SlowMimeData("X", 2001);
clipboard->setRawData(ClipboardMode::Clipboard, data);
waitFor(2000);
const auto expectedLog = R"(^.*<monitorClipboard-\d+>: Clipboard data expired, refusing to access old data$)";
QTRY_COMPARE( count(splitLines(readLogFile(maxReadLogSize)), expectedLog), 1 );
WAIT_ON_OUTPUT("read('a/a', 0)", "X");
RUN("read('b/b', 0)", "");
RUN("read('?', 0)", "a/a\n");
}

void Tests::clipboardUriList()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.clipboardFormatsToSave = function() { return [mimeUriList] }'
},
])
)";

RUN(script, "");
WAIT_ON_OUTPUT("commands().length", "1\n");

const QByteArray uris = "https://test1.example.com\nhttps://test1.example.com";
TEST( m_test->setClipboard(uris, mimeUriList) );
WAIT_ON_OUTPUT("clipboard(mimeUriList)", uris);
}

int Tests::run(
const QStringList &arguments, QByteArray *stdoutData, QByteArray *stderrData, const QByteArray &in,
const QStringList &environment)
Expand Down
3 changes: 3 additions & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ private slots:

void saveLargeItem();

void slowClipboard();
void clipboardUriList();

private:
void clearServerErrors();
int run(const QStringList &arguments, QByteArray *stdoutData = nullptr,
Expand Down

0 comments on commit a27564c

Please sign in to comment.