| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <qbaselinetest.h> |
| 30 | |
| 31 | #include <QtCore/QCoreApplication> |
| 32 | #include <QtCore/QDirIterator> |
| 33 | #include <QtCore/QDebug> |
| 34 | #include <QtCore/QProcess> |
| 35 | #include <QtGui/QImage> |
| 36 | |
| 37 | #include <algorithm> |
| 38 | |
| 39 | QString blockify(const QByteArray& s) |
| 40 | { |
| 41 | const char* indent = "\n | "; |
| 42 | QByteArray block = s.trimmed(); |
| 43 | block.replace('\n', indent); |
| 44 | block.prepend(indent); |
| 45 | block.append('\n'); |
| 46 | return block; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | class tst_Scenegraph : public QObject |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | |
| 54 | public: |
| 55 | tst_Scenegraph(); |
| 56 | |
| 57 | private Q_SLOTS: |
| 58 | void initTestCase(); |
| 59 | void cleanup(); |
| 60 | void testNoTextRendering_data(); |
| 61 | void testNoTextRendering(); |
| 62 | void testRendering_data(); |
| 63 | void testRendering(); |
| 64 | |
| 65 | private: |
| 66 | void setupTestSuite(const QByteArray& filter = QByteArray()); |
| 67 | void runTest(const QStringList& extraArgs = QStringList()); |
| 68 | bool renderAndGrab(const QString& qmlFile, const QStringList& extraArgs, QImage *screenshot, QString *errMsg); |
| 69 | quint16 checksumFileOrDir(const QString &path); |
| 70 | |
| 71 | QString testSuitePath; |
| 72 | QString grabberPath; |
| 73 | int consecutiveErrors; // Not test failures (image mismatches), but system failures (so no image at all) |
| 74 | bool aborted; // This run given up because of too many system failures |
| 75 | bool usingRhi; |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | tst_Scenegraph::tst_Scenegraph() |
| 80 | : consecutiveErrors(0), aborted(false) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void tst_Scenegraph::initTestCase() |
| 86 | { |
| 87 | QString dataDir = QFINDTESTDATA("../data/."); |
| 88 | if (dataDir.isEmpty()) |
| 89 | dataDir = QStringLiteral("data"); |
| 90 | QFileInfo fi(dataDir); |
| 91 | if (!fi.exists() || !fi.isDir() || !fi.isReadable()) |
| 92 | QSKIP("Test suite data directory missing or unreadable: " + fi.canonicalFilePath().toLatin1()); |
| 93 | testSuitePath = fi.canonicalFilePath(); |
| 94 | |
| 95 | #if defined(Q_OS_WIN) |
| 96 | grabberPath = QFINDTESTDATA("qmlscenegrabber.exe"); |
| 97 | #elif defined(Q_OS_DARWIN) |
| 98 | grabberPath = QFINDTESTDATA("qmlscenegrabber.app/Contents/MacOS/qmlscenegrabber"); |
| 99 | #else |
| 100 | grabberPath = QFINDTESTDATA("qmlscenegrabber"); |
| 101 | #endif |
| 102 | if (grabberPath.isEmpty()) |
| 103 | grabberPath = QCoreApplication::applicationDirPath() + "/qmlscenegrabber"; |
| 104 | |
| 105 | const char *backendVarName = "QT_QUICK_BACKEND"; |
| 106 | const QString backend = qEnvironmentVariable(backendVarName, QString::fromLatin1("default")); |
| 107 | QBaselineTest::addClientProperty(QString::fromLatin1(backendVarName), backend); |
| 108 | |
| 109 | #if defined(Q_OS_WIN) |
| 110 | const char *defaultRhiBackend = "d3d11"; |
| 111 | #elif defined(Q_OS_DARWIN) |
| 112 | const char *defaultRhiBackend = "metal"; |
| 113 | #else |
| 114 | const char *defaultRhiBackend = "opengl"; |
| 115 | #endif |
| 116 | usingRhi = qEnvironmentVariableIntValue("QSG_RHI") != 0; |
| 117 | QString stack; |
| 118 | if (usingRhi) { |
| 119 | const QString rhiBackend = qEnvironmentVariable("QSG_RHI_BACKEND", QString::fromLatin1(defaultRhiBackend)); |
| 120 | stack = QString::fromLatin1("RHI_%1").arg(rhiBackend); |
| 121 | } else { |
| 122 | stack = qEnvironmentVariable("QT_QUICK_BACKEND", QString::fromLatin1("DirectGL")); |
| 123 | } |
| 124 | QBaselineTest::addClientProperty(QString::fromLatin1("GraphicsStack"), stack); |
| 125 | |
| 126 | QByteArray msg; |
| 127 | if (!QBaselineTest::connectToBaselineServer(&msg)) |
| 128 | QSKIP(msg); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void tst_Scenegraph::cleanup() |
| 133 | { |
| 134 | // Allow subsystems time to settle |
| 135 | if (!aborted) |
| 136 | QTest::qWait(20); |
| 137 | } |
| 138 | |
| 139 | void tst_Scenegraph::testNoTextRendering_data() |
| 140 | { |
| 141 | setupTestSuite("text/"); |
| 142 | consecutiveErrors = 0; |
| 143 | aborted = false; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void tst_Scenegraph::testNoTextRendering() |
| 148 | { |
| 149 | runTest(QStringList() << "-notext"); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void tst_Scenegraph::testRendering_data() |
| 154 | { |
| 155 | setupTestSuite(); |
| 156 | consecutiveErrors = 0; |
| 157 | aborted = false; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void tst_Scenegraph::testRendering() |
| 162 | { |
| 163 | runTest(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void tst_Scenegraph::setupTestSuite(const QByteArray& filter) |
| 168 | { |
| 169 | QTest::addColumn<QString>("qmlFile"); |
| 170 | int numItems = 0; |
| 171 | |
| 172 | QStringList ignoreItems; |
| 173 | QFile ignoreFile(testSuitePath + "/Ignore"); |
| 174 | if (ignoreFile.open(QIODevice::ReadOnly)) { |
| 175 | while (!ignoreFile.atEnd()) { |
| 176 | QByteArray line = ignoreFile.readLine().trimmed(); |
| 177 | if (!line.isEmpty() && !line.startsWith('#')) |
| 178 | ignoreItems += line; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | QStringList itemFiles; |
| 183 | QDirIterator it(testSuitePath, QDirIterator::Subdirectories); |
| 184 | while (it.hasNext()) { |
| 185 | QString fp = it.next(); |
| 186 | if (fp.endsWith(".qml")) { |
| 187 | QString itemName = fp.mid(testSuitePath.length() + 1); |
| 188 | if (!ignoreItems.contains(itemName) && (filter.isEmpty() || !itemName.startsWith(filter))) |
| 189 | itemFiles.append(it.filePath()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | std::sort(itemFiles.begin(), itemFiles.end()); |
| 194 | for (const QString &filePath : qAsConst(itemFiles)) { |
| 195 | QByteArray itemName = filePath.mid(testSuitePath.length() + 1).toLatin1(); |
| 196 | QBaselineTest::newRow(itemName, checksumFileOrDir(filePath)) << filePath; |
| 197 | numItems++; |
| 198 | } |
| 199 | |
| 200 | if (!numItems) |
| 201 | QSKIP("No .qml test files found in " + testSuitePath.toLatin1()); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void tst_Scenegraph::runTest(const QStringList& extraArgs) |
| 206 | { |
| 207 | // qDebug() << "Rendering" << QTest::currentDataTag(); |
| 208 | |
| 209 | if (aborted) |
| 210 | QSKIP("System too unstable."); |
| 211 | |
| 212 | QFETCH(QString, qmlFile); |
| 213 | |
| 214 | QImage screenShot; |
| 215 | QString errorMessage; |
| 216 | if (renderAndGrab(qmlFile, extraArgs, &screenShot, &errorMessage)) { |
| 217 | consecutiveErrors = 0; |
| 218 | } |
| 219 | else { |
| 220 | if (++consecutiveErrors >= 3) |
| 221 | aborted = true; // Just give up if screen grabbing fails 3 times in a row |
| 222 | QFAIL(qPrintable("QuickView grabbing failed: " + errorMessage)); |
| 223 | } |
| 224 | |
| 225 | QBASELINE_TEST(screenShot); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | bool tst_Scenegraph::renderAndGrab(const QString& qmlFile, const QStringList& extraArgs, QImage *screenshot, QString *errMsg) |
| 230 | { |
| 231 | bool usePipe = true; // Whether to transport the grabbed image using temp. file or pipe. TBD: cmdline option |
| 232 | QProcess grabber; |
| 233 | grabber.setProcessChannelMode(QProcess::ForwardedErrorChannel); |
| 234 | QStringList args = extraArgs; |
| 235 | QString tmpfile = usePipe ? QString("-") : QString("/tmp/qmlscenegrabber-%1-out.ppm").arg(QCoreApplication::applicationPid()); |
| 236 | args << qmlFile << "-o" << tmpfile; |
| 237 | grabber.start(grabberPath, args, QIODevice::ReadOnly); |
| 238 | grabber.waitForFinished(17000); //### hardcoded, must be larger than the scene timeout in qmlscenegrabber |
| 239 | if (grabber.state() != QProcess::NotRunning) { |
| 240 | grabber.terminate(); |
| 241 | grabber.waitForFinished(3000); |
| 242 | } |
| 243 | QImage img; |
| 244 | bool res = usePipe ? img.load(&grabber, "ppm") : img.load(tmpfile); |
| 245 | if (!res || img.isNull()) { |
| 246 | if (errMsg) { |
| 247 | QString s("Failed to grab screen. qmlscenegrabber exitcode: %1. Process error: %2. Stderr:%3"); |
| 248 | *errMsg = s.arg(grabber.exitCode()).arg(grabber.errorString()).arg(blockify(grabber.readAllStandardError())); |
| 249 | } |
| 250 | if (!usePipe) |
| 251 | QFile::remove(tmpfile); |
| 252 | return false; |
| 253 | } |
| 254 | if (screenshot) |
| 255 | *screenshot = img; |
| 256 | if (!usePipe) |
| 257 | QFile::remove(tmpfile); |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | quint16 tst_Scenegraph::checksumFileOrDir(const QString &path) |
| 263 | { |
| 264 | QFileInfo fi(path); |
| 265 | if (!fi.exists() || !fi.isReadable()) |
| 266 | return 0; |
| 267 | if (fi.isFile()) { |
| 268 | QFile f(path); |
| 269 | f.open(QIODevice::ReadOnly); |
| 270 | QByteArray contents = f.readAll(); |
| 271 | return qChecksum(contents.constData(), uint(contents.size())); |
| 272 | } |
| 273 | if (fi.isDir()) { |
| 274 | static const QStringList nameFilters = QStringList() << "*.qml" << "*.cpp" << "*.png" << "*.jpg"; |
| 275 | quint16 cs = 0; |
| 276 | const auto entryList = QDir(fi.filePath()).entryList(nameFilters, |
| 277 | QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); |
| 278 | for (const QString &item : entryList) |
| 279 | cs ^= checksumFileOrDir(path + QLatin1Char('/') + item); |
| 280 | return cs; |
| 281 | } |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | #define main _realmain |
| 287 | QTEST_MAIN(tst_Scenegraph) |
| 288 | #undef main |
| 289 | |
| 290 | int main(int argc, char *argv[]) |
| 291 | { |
| 292 | QBaselineTest::handleCmdLineArgs(&argc, &argv); |
| 293 | return _realmain(argc, argv); |
| 294 | } |
| 295 | |
| 296 | #include "tst_scenegraph.moc" |