2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "qbaselinetest.h"
|
|
|
|
#include "baselineprotocol.h"
|
2012-08-13 12:13:40 +00:00
|
|
|
#include <QtCore/QDir>
|
2018-11-16 12:42:16 +00:00
|
|
|
#include <QFile>
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
#define MAXCMDLINEARGS 128
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
namespace QBaselineTest {
|
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
static char *fargv[MAXCMDLINEARGS];
|
|
|
|
static bool simfail = false;
|
|
|
|
static PlatformInfo customInfo;
|
2012-10-29 09:34:20 +00:00
|
|
|
static bool customAutoModeSet = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
static BaselineProtocol proto;
|
|
|
|
static bool connected = false;
|
|
|
|
static bool triedConnecting = false;
|
2012-10-29 09:34:20 +00:00
|
|
|
static bool dryRunMode = false;
|
2016-08-17 10:52:04 +00:00
|
|
|
static enum { UploadMissing, UploadAll, UploadNone } baselinePolicy = UploadMissing;
|
2021-11-23 12:36:59 +00:00
|
|
|
static bool abortIfUnstable = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
static QByteArray curFunction;
|
|
|
|
static ImageItemList itemList;
|
|
|
|
static bool gotBaselines;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
void handleCmdLineArgs(int *argcp, char ***argvp)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-08-13 12:13:40 +00:00
|
|
|
if (!argcp || !argvp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool showHelp = false;
|
|
|
|
|
|
|
|
int fargc = 0;
|
|
|
|
int numArgs = *argcp;
|
|
|
|
|
|
|
|
for (int i = 0; i < numArgs; i++) {
|
|
|
|
QByteArray arg = (*argvp)[i];
|
2021-11-23 12:36:59 +00:00
|
|
|
QByteArray nextArg = (i+1 < numArgs) ? (*argvp)[i+1] : nullptr;
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
if (arg == "-simfail") {
|
|
|
|
simfail = true;
|
2018-04-18 14:24:48 +00:00
|
|
|
} else if (arg == "-fuzzlevel") {
|
|
|
|
i++;
|
|
|
|
bool ok = false;
|
|
|
|
(void)nextArg.toInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
qWarning() << "-fuzzlevel requires integer parameter";
|
|
|
|
showHelp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
customInfo.insert("FuzzLevel", QString::fromLatin1(nextArg));
|
2012-08-13 12:13:40 +00:00
|
|
|
} else if (arg == "-auto") {
|
2012-10-29 09:34:20 +00:00
|
|
|
customAutoModeSet = true;
|
2012-08-13 12:13:40 +00:00
|
|
|
customInfo.setAdHocRun(false);
|
|
|
|
} else if (arg == "-adhoc") {
|
2012-10-29 09:34:20 +00:00
|
|
|
customAutoModeSet = true;
|
2012-08-13 12:13:40 +00:00
|
|
|
customInfo.setAdHocRun(true);
|
2016-08-17 10:52:04 +00:00
|
|
|
} else if (arg == "-setbaselines") {
|
|
|
|
baselinePolicy = UploadAll;
|
2021-11-23 12:36:59 +00:00
|
|
|
} else if (arg == "-keeprunning") {
|
|
|
|
abortIfUnstable = false;
|
2016-08-17 10:52:04 +00:00
|
|
|
} else if (arg == "-nosetbaselines") {
|
|
|
|
baselinePolicy = UploadNone;
|
2012-08-13 12:13:40 +00:00
|
|
|
} else if (arg == "-compareto") {
|
|
|
|
i++;
|
|
|
|
int split = qMax(0, nextArg.indexOf('='));
|
|
|
|
QByteArray key = nextArg.left(split).trimmed();
|
|
|
|
QByteArray value = nextArg.mid(split+1).trimmed();
|
|
|
|
if (key.isEmpty() || value.isEmpty()) {
|
|
|
|
qWarning() << "-compareto requires parameter of the form <key>=<value>";
|
|
|
|
showHelp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
customInfo.addOverride(key, value);
|
|
|
|
} else {
|
|
|
|
if ( (arg == "-help") || (arg == "--help") )
|
|
|
|
showHelp = true;
|
|
|
|
if (fargc >= MAXCMDLINEARGS) {
|
|
|
|
qWarning() << "Too many command line arguments!";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fargv[fargc++] = (*argvp)[i];
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 12:13:40 +00:00
|
|
|
*argcp = fargc;
|
|
|
|
*argvp = fargv;
|
|
|
|
|
|
|
|
if (showHelp) {
|
|
|
|
// TBD: arrange for this to be printed *after* QTest's help
|
|
|
|
QTextStream out(stdout);
|
|
|
|
out << "\n Baseline testing (lancelot) options:\n";
|
|
|
|
out << " -simfail : Force an image comparison mismatch. For testing purposes.\n";
|
2018-04-18 14:24:48 +00:00
|
|
|
out << " -fuzzlevel <int> : Specify the percentage of fuzziness in comparison. Overrides server default. 0 means exact match.\n";
|
2012-08-13 12:13:40 +00:00
|
|
|
out << " -auto : Inform server that this run is done by a daemon, CI system or similar.\n";
|
|
|
|
out << " -adhoc (default) : The inverse of -auto; this run is done by human, e.g. for testing.\n";
|
2021-11-23 12:36:59 +00:00
|
|
|
out << " -keeprunning : Run all tests even if the system is unstable \n";
|
2016-08-17 10:52:04 +00:00
|
|
|
out << " -setbaselines : Store ALL rendered images as new baselines. Forces replacement of previous baselines.\n";
|
|
|
|
out << " -nosetbaselines : Do not store rendered images as new baselines when previous baselines are missing.\n";
|
2012-08-13 12:13:40 +00:00
|
|
|
out << " -compareto KEY=VAL : Force comparison to baselines from a different client,\n";
|
|
|
|
out << " for example: -compareto QtVersion=4.8.0\n";
|
|
|
|
out << " Multiple -compareto client specifications may be given.\n";
|
|
|
|
out << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:36:59 +00:00
|
|
|
bool shouldAbortIfUnstable()
|
|
|
|
{
|
|
|
|
return abortIfUnstable;
|
|
|
|
}
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
void addClientProperty(const QString& key, const QString& value)
|
|
|
|
{
|
|
|
|
customInfo.insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
If a client property script is present, run it and accept its output
|
|
|
|
in the form of one 'key: value' property per line
|
|
|
|
*/
|
|
|
|
void fetchCustomClientProperties()
|
|
|
|
{
|
2018-11-16 12:42:16 +00:00
|
|
|
QFile file("hostinfo.txt");
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
return;
|
|
|
|
QTextStream in(&file);
|
|
|
|
|
|
|
|
while (!in.atEnd()) {
|
|
|
|
QString line = in.readLine().trimmed(); // ###local8bit? utf8?
|
|
|
|
if (line.startsWith(QLatin1Char('#'))) // Ignore comments in file
|
|
|
|
continue;
|
2012-08-13 12:13:40 +00:00
|
|
|
QString key, val;
|
|
|
|
int colonPos = line.indexOf(':');
|
|
|
|
if (colonPos > 0) {
|
|
|
|
key = line.left(colonPos).simplified().replace(' ', '_');
|
|
|
|
val = line.mid(colonPos+1).trimmed();
|
|
|
|
}
|
|
|
|
if (!key.isEmpty() && key.length() < 64 && val.length() < 256) // ###TBD: maximum 256 chars in value?
|
|
|
|
addClientProperty(key, val);
|
|
|
|
else
|
|
|
|
qDebug() << "Unparseable script output ignored:" << line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool connect(QByteArray *msg, bool *error)
|
|
|
|
{
|
|
|
|
if (connected) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (triedConnecting) {
|
|
|
|
// Avoid repeated connection attempts, to avoid the program using Timeout * #testItems seconds before giving up
|
2011-04-27 10:05:43 +00:00
|
|
|
*msg = "Not connected to baseline server.";
|
|
|
|
*error = true;
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
triedConnecting = true;
|
|
|
|
fetchCustomClientProperties();
|
|
|
|
// Merge the platform info set by the program with the protocols default info
|
|
|
|
PlatformInfo clientInfo = customInfo;
|
|
|
|
PlatformInfo defaultInfo = PlatformInfo::localHostInfo();
|
2021-11-23 12:36:59 +00:00
|
|
|
const auto &defaultInfoKeys = defaultInfo.keys();
|
|
|
|
for (const QString &key : defaultInfoKeys) {
|
2012-08-13 12:13:40 +00:00
|
|
|
if (!clientInfo.contains(key))
|
|
|
|
clientInfo.insert(key, defaultInfo.value(key));
|
|
|
|
}
|
2012-10-29 09:34:20 +00:00
|
|
|
if (!customAutoModeSet)
|
|
|
|
clientInfo.setAdHocRun(defaultInfo.isAdHocRun());
|
2012-08-13 12:13:40 +00:00
|
|
|
|
2022-03-15 14:44:34 +00:00
|
|
|
QString testCase = clientInfo.value(PI_TestCase);
|
2012-08-13 12:13:40 +00:00
|
|
|
if (testCase.isEmpty() && QTest::testObject() && QTest::testObject()->metaObject()) {
|
|
|
|
//qDebug() << "Trying to Read TestCaseName from Testlib!";
|
|
|
|
testCase = QTest::testObject()->metaObject()->className();
|
|
|
|
}
|
|
|
|
if (testCase.isEmpty()) {
|
|
|
|
qWarning("QBaselineTest::connect: No test case name specified, cannot connect.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-29 09:34:20 +00:00
|
|
|
if (!proto.connect(testCase, &dryRunMode, clientInfo)) {
|
2012-08-13 12:13:40 +00:00
|
|
|
*msg += "Failed to connect to baseline server: " + proto.errorMessage().toLatin1();
|
|
|
|
*error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
connected = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
bool disconnectFromBaselineServer()
|
|
|
|
{
|
|
|
|
if (proto.disconnect()) {
|
|
|
|
connected = false;
|
|
|
|
triedConnecting = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-15 14:44:34 +00:00
|
|
|
bool connectToBaselineServer(QByteArray *msg)
|
2012-08-13 12:13:40 +00:00
|
|
|
{
|
|
|
|
bool dummy;
|
|
|
|
QByteArray dummyMsg;
|
|
|
|
return connect(msg ? msg : &dummyMsg, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAutoMode(bool mode)
|
|
|
|
{
|
|
|
|
customInfo.setAdHocRun(!mode);
|
2012-10-29 09:34:20 +00:00
|
|
|
customAutoModeSet = true;
|
2012-08-13 12:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setSimFail(bool fail)
|
|
|
|
{
|
|
|
|
simfail = fail;
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:16:18 +00:00
|
|
|
void setProject(const QString &projectName)
|
|
|
|
{
|
2022-03-15 14:44:34 +00:00
|
|
|
addClientProperty(PI_Project, projectName);
|
2022-01-10 14:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setProjectImageKeys(const QStringList &keys)
|
|
|
|
{
|
|
|
|
QString keyList = keys.join(QLC(','));
|
|
|
|
addClientProperty(PI_ProjectImageKeys, keyList);
|
|
|
|
}
|
2012-08-13 12:13:40 +00:00
|
|
|
|
|
|
|
void modifyImage(QImage *img)
|
|
|
|
{
|
|
|
|
uint c0 = 0x0000ff00;
|
|
|
|
uint c1 = 0x0080ff00;
|
|
|
|
img->setPixel(1,1,c0);
|
|
|
|
img->setPixel(2,1,c1);
|
|
|
|
img->setPixel(3,1,c0);
|
|
|
|
img->setPixel(1,2,c1);
|
|
|
|
img->setPixel(1,3,c0);
|
|
|
|
img->setPixel(2,3,c1);
|
|
|
|
img->setPixel(3,3,c0);
|
|
|
|
img->setPixel(1,4,c1);
|
|
|
|
img->setPixel(1,5,c0);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
bool compareItem(const ImageItem &baseline, const QImage &img, QByteArray *msg, bool *error)
|
|
|
|
{
|
|
|
|
ImageItem item = baseline;
|
2012-08-13 12:13:40 +00:00
|
|
|
if (simfail) {
|
|
|
|
// Simulate test failure by forcing image mismatch; for testing purposes
|
|
|
|
QImage misImg = img;
|
|
|
|
modifyImage(&misImg);
|
|
|
|
item.image = misImg;
|
|
|
|
simfail = false; // One failure is typically enough
|
|
|
|
} else {
|
|
|
|
item.image = img;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
item.imageChecksums.clear();
|
2012-08-13 12:13:40 +00:00
|
|
|
item.imageChecksums.prepend(ImageItem::computeChecksum(item.image));
|
2011-04-27 10:05:43 +00:00
|
|
|
QByteArray srvMsg;
|
|
|
|
switch (baseline.status) {
|
|
|
|
case ImageItem::Ok:
|
|
|
|
break;
|
|
|
|
case ImageItem::IgnoreItem :
|
|
|
|
qDebug() << msg->constData() << "Ignored, blacklisted on server.";
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case ImageItem::BaselineNotFound:
|
2016-08-17 10:52:04 +00:00
|
|
|
if (!customInfo.overrides().isEmpty() || baselinePolicy == UploadNone) {
|
|
|
|
qWarning() << "Cannot compare to baseline: No such baseline found on server.";
|
2012-10-29 09:34:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
if (proto.submitNewBaseline(item, &srvMsg))
|
|
|
|
qDebug() << msg->constData() << "Baseline not found on server. New baseline uploaded.";
|
|
|
|
else
|
|
|
|
qDebug() << msg->constData() << "Baseline not found on server. Uploading of new baseline failed:" << srvMsg;
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qWarning() << "Unexpected reply from baseline server.";
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*error = false;
|
|
|
|
// The actual comparison of the given image with the baseline:
|
2012-08-13 12:13:40 +00:00
|
|
|
if (baseline.imageChecksums.contains(item.imageChecksums.at(0))) {
|
|
|
|
if (!proto.submitMatch(item, &srvMsg))
|
|
|
|
qWarning() << "Failed to report image match to server:" << srvMsg;
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
2012-08-13 12:13:40 +00:00
|
|
|
}
|
2016-08-17 10:52:04 +00:00
|
|
|
// At this point, we have established a legitimate mismatch
|
|
|
|
if (baselinePolicy == UploadAll) {
|
|
|
|
if (proto.submitNewBaseline(item, &srvMsg))
|
|
|
|
qDebug() << msg->constData() << "Forcing new baseline; uploaded ok.";
|
|
|
|
else
|
|
|
|
qDebug() << msg->constData() << "Forcing new baseline; uploading failed:" << srvMsg;
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-13 12:13:40 +00:00
|
|
|
bool fuzzyMatch = false;
|
|
|
|
bool res = proto.submitMismatch(item, &srvMsg, &fuzzyMatch);
|
|
|
|
if (res && fuzzyMatch) {
|
2021-11-23 12:36:59 +00:00
|
|
|
qInfo() << "Baseline server reports:" << srvMsg;
|
2012-08-13 12:13:40 +00:00
|
|
|
return true; // The server decides: a fuzzy match means no mismatch
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
*msg += "Mismatch. See report:\n " + srvMsg;
|
2012-10-29 09:34:20 +00:00
|
|
|
if (dryRunMode) {
|
|
|
|
qDebug() << "Dryrun, so ignoring" << *msg;
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-13 12:13:40 +00:00
|
|
|
bool checkImage(const QImage &img, const char *name, quint16 checksum, QByteArray *msg, bool *error, int manualdatatag)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (!connected && !connect(msg, error))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QByteArray itemName;
|
|
|
|
bool hasName = qstrlen(name);
|
2012-08-13 12:13:40 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
const char *tag = QTest::currentDataTag();
|
|
|
|
if (qstrlen(tag)) {
|
|
|
|
itemName = tag;
|
|
|
|
if (hasName)
|
|
|
|
itemName.append('_').append(name);
|
|
|
|
} else {
|
2012-08-13 12:13:40 +00:00
|
|
|
itemName = hasName ? name : "default_name";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manualdatatag > 0)
|
|
|
|
{
|
|
|
|
itemName.prepend("_");
|
|
|
|
itemName.prepend(QByteArray::number(manualdatatag));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*msg = "Baseline check of image '" + itemName + "': ";
|
|
|
|
|
|
|
|
|
|
|
|
ImageItem item;
|
|
|
|
item.itemName = QString::fromLatin1(itemName);
|
|
|
|
item.itemChecksum = checksum;
|
|
|
|
item.testFunction = QString::fromLatin1(QTest::currentTestFunction());
|
|
|
|
ImageItemList list;
|
|
|
|
list.append(item);
|
|
|
|
if (!proto.requestBaselineChecksums(QLatin1String(QTest::currentTestFunction()), &list) || list.isEmpty()) {
|
|
|
|
*msg = "Communication with baseline server failed: " + proto.errorMessage().toLatin1();
|
|
|
|
*error = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareItem(list.at(0), img, msg, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTestData &newRow(const char *dataTag, quint16 checksum)
|
|
|
|
{
|
|
|
|
if (QTest::currentTestFunction() != curFunction) {
|
|
|
|
curFunction = QTest::currentTestFunction();
|
|
|
|
itemList.clear();
|
|
|
|
gotBaselines = false;
|
|
|
|
}
|
|
|
|
ImageItem item;
|
|
|
|
item.itemName = QString::fromLatin1(dataTag);
|
|
|
|
item.itemChecksum = checksum;
|
|
|
|
item.testFunction = QString::fromLatin1(QTest::currentTestFunction());
|
|
|
|
itemList.append(item);
|
|
|
|
|
|
|
|
return QTest::newRow(dataTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool testImage(const QImage& img, QByteArray *msg, bool *error)
|
|
|
|
{
|
|
|
|
if (!connected && !connect(msg, error))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (QTest::currentTestFunction() != curFunction || itemList.isEmpty()) {
|
|
|
|
qWarning() << "Usage error: QBASELINE_TEST used without corresponding QBaselineTest::newRow()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotBaselines) {
|
|
|
|
if (!proto.requestBaselineChecksums(QString::fromLatin1(QTest::currentTestFunction()), &itemList) || itemList.isEmpty()) {
|
|
|
|
*msg = "Communication with baseline server failed: " + proto.errorMessage().toLatin1();
|
|
|
|
*error = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
gotBaselines = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString curTag = QString::fromLatin1(QTest::currentDataTag());
|
|
|
|
ImageItemList::const_iterator it = itemList.constBegin();
|
|
|
|
while (it != itemList.constEnd() && it->itemName != curTag)
|
|
|
|
++it;
|
|
|
|
if (it == itemList.constEnd()) {
|
|
|
|
qWarning() << "Usage error: QBASELINE_TEST used without corresponding QBaselineTest::newRow() for row" << curTag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return compareItem(*it, img, msg, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|