summaryrefslogtreecommitdiffstats
path: root/tests/auto/lancelot/tst_lancelot.cpp
blob: f1c6e5c1a29b6866fbe48487feb16d182ef109a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

// Generalization dimensions TBD:
// ImageFormat: Rendering to different bit formats
// Engine: Rendering with different engines
// Painter renderhints? (set in qps itself)

#include <QtTest/QtTest>
#include <paintcommands.h>
#include <QPainter>
#include <QLibraryInfo>
#include <baselineprotocol.h>

#ifndef SRCDIR
#define SRCDIR "."
#endif

static const QLatin1String scriptsDir(SRCDIR "/scripts/");

class tst_Lancelot : public QObject
{
Q_OBJECT

public:
    tst_Lancelot();

private:
    QImage getBaseline(const QString &fileName, bool *created);
    QImage render(const QString &fileName);
    QStringList loadScriptFile(const QString &filePath);

    QString errorMsg;
    BaselineProtocol proto;

private slots:
    void initTestCase();
    void cleanupTestCase() {}

    void testRendering_data();
    void testRendering();
};

tst_Lancelot::tst_Lancelot()
{
}

void tst_Lancelot::initTestCase()
{
#if !defined(Q_OS_LINUX)
    QSKIP("For the moment, this test is only supported on Linux.", SkipAll);
#endif

    // Check the environment here, so that we can just skip on trouble!
    if (!proto.connect()) {
        QWARN(qPrintable(proto.errorMessage()));
        QSKIP("Communication with baseline image server failed.", SkipAll);
    }

    // TBD: preload scripts, to confirm reading and generate checksums
}

void tst_Lancelot::testRendering_data()
{
    // search the data directory for qps files and make a row for each.
    // filter out those marked as blacklisted (in the file itself?)
    // Rather do the above in initTestCase. Can load & split them all also.
    // And baselines too?

    QTest::addColumn<QString>("fileName");

    // ### no, don't do it like this, we want opt-in, not opt-out, for stability
    QDir qpsDir(scriptsDir);
    QStringList files = qpsDir.entryList(QStringList() << QLatin1String("*.qps"), QDir::Files | QDir::Readable);
    files.removeOne(QLatin1String("porter_duff.qps"));
    files.removeOne(QLatin1String("porter_duff2.qps"));
    files.removeOne(QLatin1String("images.qps"));
    files.removeOne(QLatin1String("sizes.qps"));

    if (files.isEmpty()) {
        QWARN("No qps files found in " + qpsDir.path().toLatin1());
        QSKIP("Aborted due to errors.", SkipAll);
    }

    foreach(QString fileName, files) {
        QString baseName = fileName.section('.', 0, -2);
        QTest::newRow(baseName.toLatin1()) << fileName;
    }
}


// We treat failures because of test environment
// (e.g. script files not found) as just warnings, and not
// QFAILs, to avoid false negatives caused by environment
// or server instability

void tst_Lancelot::testRendering()
{
    errorMsg.clear();
    QFETCH(QString, fileName);

    bool newBaselineCreated = false;
    QImage baseline = getBaseline(fileName, &newBaselineCreated);
    if (newBaselineCreated)
        QSKIP("Baseline not found; new baseline created.", SkipSingle);
    if (!errorMsg.isNull() || baseline.isNull()) {
        QWARN("Error: Failed to get baseline image. " + errorMsg.toLatin1());
        QSKIP("Aborted due to errors.", SkipSingle);
    }

    QImage rendered = render(fileName);
    if (!errorMsg.isNull() || rendered.isNull()) {
        // Indicates that it's an error in the test environment, not Qt
        QWARN("Error: Failed to render image. " + errorMsg.toLatin1());
        QSKIP("Aborted due to errors.", SkipSingle);
    }

    if (rendered.format() != baseline.format())
        baseline = baseline.convertToFormat(rendered.format());

    // The actual check:
    if (rendered != baseline) {
        QByteArray failMsg;
        proto.submitMismatch(fileName, rendered, &failMsg);

#if 1
        // Also generate & submit the diff image
        QImage diff(rendered.size(), QImage::Format_RGB32);
        diff.fill(0);
        QPainter p(&diff);
        p.drawImage(0, 0, rendered);
        p.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
        p.drawImage(0, 0, baseline);
        p.end();
        proto.submitMismatch(fileName + QLatin1String("_diff"), diff, 0);
#endif

        failMsg.prepend("Rendered image differs from baseline. ");
        QFAIL(failMsg.constData());
    }
}


QImage tst_Lancelot::getBaseline(const QString &fileName, bool *created)
{
    QImage baseline;
    BaselineProtocol::Command response;
    if (!proto.requestBaseline(fileName, &response, &baseline)) {
        errorMsg = QLatin1String("Error downloading baseline from server. ") + proto.errorMessage();
        return QImage();
    }

    if (response == BaselineProtocol::BaselineNotPresent) {
        // Here: Create new baseline!
        baseline = render(fileName);
        if (!errorMsg.isNull() || baseline.isNull()) {
            errorMsg.prepend(QLatin1String("Failed to render new baseline. "));
            return QImage();
        }
        if (!proto.submitNewBaseline(fileName, baseline)) {
            errorMsg = QLatin1String("Failed to submit new baseline to server. ") + proto.errorMessage();
            return QImage();
        }
        if (created)
            *created = true;
    }
    else if (response == BaselineProtocol::IgnoreCase) {
        // Blacklisted or similar, signal back that this item should be skipped
        // Will effectively happen with this:
        errorMsg = QLatin1String("Baseline server says skip this");
        return QImage();
    }
    else if (response != BaselineProtocol::AcceptBaseline) {
        errorMsg = QLatin1String("Unknown response from baseline server. ");
        return QImage();
    }
    return baseline;
}


QImage tst_Lancelot::render(const QString &fileName)
{
    QString filePath = scriptsDir + fileName;
    QStringList script = loadScriptFile(filePath);
    if (script.isEmpty())
        return QImage();

    QImage img(800, 800, QImage::Format_ARGB32_Premultiplied);
    QPainter p(&img);
    PaintCommands pcmd(script, 800, 800);
    pcmd.setPainter(&p);
    pcmd.setFilePath(QFileInfo(filePath).absoluteFilePath()); // eh yuck
    pcmd.runCommands();
    p.end();

    return img;
}


QStringList tst_Lancelot::loadScriptFile(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QFile::ReadOnly)) {
        errorMsg = QLatin1String("Failed to open script file ") + filePath;
        return QStringList();
    }
    //# use the new readscript function instead?
    QTextStream stream(&file);
    return stream.readAll().split(QLatin1Char('\n'), QString::SkipEmptyParts);
}

QTEST_MAIN(tst_Lancelot)
#include "tst_lancelot.moc"