Skip to content

Commit

Permalink
Tests: Improve testing server errors
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Holecek <hluk@email.cz>
  • Loading branch information
hluk committed Aug 4, 2019
1 parent 6da3ca5 commit 8c3424c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
18 changes: 4 additions & 14 deletions src/tests/test_utils.h
Expand Up @@ -59,7 +59,7 @@ do { \
ferr.open(stderr, QIODevice::WriteOnly); \
ferr.write(errors_ + "\n"); \
ferr.close(); \
QVERIFY2(false, "Failed with errors above."); \
QFAIL("Failed with errors above."); \
} \
} while (false)

Expand All @@ -76,10 +76,10 @@ do { \
TEST( m_test->runClientWithError((Args() << ARGUMENTS), (EXIT_CODE), toByteArray(STDERR_CONTAINS)) );

#define WAIT_FOR_CLIPBOARD(DATA) \
QCOMPARE( waitUntilClipboardSet(DATA), QByteArray(DATA) )
TEST( m_test->verifyClipboard(DATA, "text/plain") )

#define WAIT_FOR_CLIPBOARD2(DATA, MIME) \
QCOMPARE( waitUntilClipboardSet(DATA, MIME), QByteArray(DATA) )
TEST( m_test->verifyClipboard((DATA), (MIME)) )

#define RETURN_ON_ERROR(CALLBACK, ERROR) \
do { \
Expand All @@ -103,17 +103,7 @@ do { \
} while(false)

#define WAIT_ON_OUTPUT(ARGUMENTS, OUTPUT) \
do { \
PerformanceTimer perf; \
QByteArray out_; \
const QByteArray expected_(OUTPUT); \
SleepTimer t_(8000); \
do { \
TEST( m_test->getClientOutput((Args() << ARGUMENTS), &out_) ); \
} while (out_ != expected_ && t_.sleep()); \
perf.printPerformance("WAIT_ON_OUTPUT", (Args() << ARGUMENTS)); \
QCOMPARE(QString(out_), QString(expected_)); \
} while(false)
TEST( m_test->waitOnOutput((Args() << ARGUMENTS), (OUTPUT)) )

#define SKIP_ON_ENV(ENV) \
if ( qgetenv(ENV) == "1" ) \
Expand Down
6 changes: 6 additions & 0 deletions src/tests/testinterface.h
Expand Up @@ -73,12 +73,18 @@ class TestInterface {
/// Run client with given @a arguments and read output, check stderr and exit code.
virtual QByteArray getClientOutput(const QStringList &arguments, QByteArray *stdoutActual) = 0;

/// Waits on client output.
virtual QByteArray waitOnOutput(const QStringList &arguments, const QByteArray &stdoutExpected) = 0;

/// Set clipboard through monitor process.
virtual QByteArray setClipboard(
const QByteArray &bytes,
const QString &mime = QString("text/plain"),
ClipboardMode mode = ClipboardMode::Clipboard) = 0;

/// Verify clipboard content.
virtual QByteArray verifyClipboard(const QByteArray &data, const QString &mime, bool exact = true) = 0;

/**
* Return errors/warning from server (otherwise empty output).
* If @a readAll is set, read all stderr.
Expand Down
86 changes: 46 additions & 40 deletions src/tests/tests.cpp
Expand Up @@ -138,23 +138,6 @@ QByteArray getClipboard(const QString &mime = QString("text/plain"), ClipboardMo
return (data != nullptr) ? data->data(mime) : QByteArray();
}

QByteArray waitUntilClipboardSet(const QByteArray &data, const QString &mime = QString("text/plain"), bool exact = true)
{
PerformanceTimer perf;

SleepTimer t(waitMsSetClipboard * 5);
do {
if ( exact ? getClipboard(mime) == data : getClipboard(mime).contains(data) ) {
perf.printPerformance("waitUntilClipboardSet", QStringList() << QString::fromUtf8(data) << mime);
waitFor(waitMsSetClipboard);
return data;
}
} while (t.sleep());

waitFor(waitMsSetClipboard);
return getClipboard(mime);
}

bool waitWhileFileExists(const QFile &file)
{
SleepTimer t(2000);
Expand Down Expand Up @@ -217,7 +200,7 @@ class TestInterfaceImpl final : public TestInterface {

m_server->closeReadChannel(QProcess::StandardOutput);

RETURN_ON_ERROR( readServerErrors(), "Failed to read server errors" );
RETURN_ON_ERROR( readServerErrors(), "Failed to start server" );

return waitForServerToStart();
}
Expand Down Expand Up @@ -350,7 +333,7 @@ class TestInterfaceImpl final : public TestInterface {
return printClienAndServerStderr(stderrActual, exitCode);

if (stdoutActual != stdoutExpected) {
return "Test failed: "
return "Test failed:"
+ decorateOutput("Unexpected output", stdoutActual)
+ decorateOutput("Expected output", stdoutExpected)
+ printClienAndServerStderr(stderrActual, exitCode);
Expand Down Expand Up @@ -412,13 +395,29 @@ class TestInterfaceImpl final : public TestInterface {
QGuiApplication::clipboard()->setMimeData(mimeData, qmode);

waitFor(waitMsSetClipboard);
return verifyClipboard(bytes, mime);
}

waitUntilClipboardSet(bytes, mime);
RETURN_ON_ERROR(
testClipboard(bytes, mime),
"Failed to set " + QByteArray(mode == ClipboardMode::Clipboard ? "clipboard" : "selection") );
QByteArray verifyClipboard(const QByteArray &data, const QString &mime, bool exact = true) override
{
PerformanceTimer perf;

return QByteArray();
SleepTimer t(waitMsSetClipboard * 5);
do {
if ( exact ? getClipboard(mime) == data : getClipboard(mime).contains(data) ) {
perf.printPerformance("verifyClipboard", QStringList() << QString::fromUtf8(data) << mime);
waitFor(waitMsSetClipboard);
RETURN_ON_ERROR( readServerErrors(), "Failed to set or test clipboard content" );
return QByteArray();
}
} while (t.sleep());

const QByteArray actualBytes = getClipboard(mime);
return QString("Unexpected clipboard data for MIME \"%1\":")
.arg(mime).toUtf8()
+ decorateOutput("Unexpected content", actualBytes)
+ decorateOutput("Expected content", data)
+ readServerErrors(ReadAllStderr);
}

QByteArray readServerErrors(ReadStderrFlag flag = ReadErrors) override
Expand All @@ -441,11 +440,31 @@ class TestInterfaceImpl final : public TestInterface {
if ( !testStderr(stderrActual) || exitCode != 0 )
return printClienAndServerStderr(stderrActual, exitCode);

RETURN_ON_ERROR( readServerErrors(), "Failed to read server errors" );
RETURN_ON_ERROR( readServerErrors(), "Failed gettting client output" );

return "";
}

QByteArray waitOnOutput(const QStringList &arguments, const QByteArray &stdoutExpected) override
{
PerformanceTimer perf;
QByteArray stdoutActual;

SleepTimer t_(8000);
do {
RETURN_ON_ERROR( getClientOutput(arguments, &stdoutActual), "Failed to wait on client output" );
} while (stdoutActual != stdoutExpected && t_.sleep());

if (stdoutActual == stdoutExpected)
return QByteArray();

return QString("Unexpected output for command \"%1\":")
.arg(arguments.join(' ')).toUtf8()
+ decorateOutput("Unexpected content", stdoutActual)
+ decorateOutput("Expected content", stdoutExpected)
+ readServerErrors(ReadAllStderr);
}

QByteArray cleanupTestCase() override
{
return cleanup();
Expand Down Expand Up @@ -517,7 +536,7 @@ class TestInterfaceImpl final : public TestInterface {
RETURN_ON_ERROR( setClipboard(QByteArray(), mimeText, ClipboardMode::Selection), "Failed to reset selection" );
#endif

RETURN_ON_ERROR( startServer(), "Failed to start server" );
RETURN_ON_ERROR( startServer(), "Failed to initialize server" );

// Always show main window first so that the results are consistent with desktop environments
// where user cannot hide main window (tiling window managers without tray).
Expand Down Expand Up @@ -581,19 +600,6 @@ class TestInterfaceImpl final : public TestInterface {
return p->waitForStarted(10000);
}

QByteArray testClipboard(const QByteArray &bytes, const QString &mime)
{
const QByteArray actualBytes = getClipboard(mime);
if (actualBytes != bytes) {
return QString("Test failed (clipboard data for MIME \"%1\"): ")
.arg(mime).toUtf8()
+ decorateOutput("Unexpected content", actualBytes)
+ decorateOutput("Expected content", bytes);
}

return "";
}

QByteArray waitForServerToStart()
{
SleepTimer t(15000);
Expand Down Expand Up @@ -3124,7 +3130,7 @@ void Tests::showHideLogDialog()

RUN("keys" << logDialogId << "CTRL+A" << "CTRL+C" << logDialogId, "");
const QByteArray expectedLog = "Starting callback: onStart";
QCOMPARE( waitUntilClipboardSet(expectedLog, mimeHtml, false), expectedLog );
TEST( m_test->verifyClipboard(expectedLog, mimeHtml, false) );

RUN("keys" << logDialogId << "ESCAPE" << clipboardBrowserId, "");
}
Expand Down

0 comments on commit 8c3424c

Please sign in to comment.