~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/tools/uic/tst_uic.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
 
 
43
#include <QtCore/QDir>
 
44
#include <QtCore/QString>
 
45
#include <QtTest/QtTest>
 
46
#include <QtCore/QProcess>
 
47
#include <QtCore/QByteArray>
 
48
#include <QtCore/QLibraryInfo>
 
49
#include <QtCore/QTemporaryDir>
 
50
 
 
51
class tst_uic : public QObject
 
52
{
 
53
    Q_OBJECT
 
54
 
 
55
public:
 
56
    tst_uic();
 
57
 
 
58
private Q_SLOTS:
 
59
    void initTestCase();
 
60
    void cleanupTestCase();
 
61
 
 
62
    void stdOut();
 
63
 
 
64
    void run();
 
65
    void run_data() const;
 
66
 
 
67
    void compare();
 
68
    void compare_data() const;
 
69
 
 
70
private:
 
71
    const QString m_command;
 
72
    QString m_baseline;
 
73
    QTemporaryDir m_generated;
 
74
};
 
75
 
 
76
tst_uic::tst_uic()
 
77
    : m_command(QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/uic"))
 
78
{
 
79
}
 
80
 
 
81
static QByteArray msgProcessStartFailed(const QString &command, const QString &why)
 
82
{
 
83
    const QString result = QString::fromLatin1("Could not start %1: %2")
 
84
            .arg(command, why);
 
85
    return result.toLocal8Bit();
 
86
}
 
87
 
 
88
void tst_uic::initTestCase()
 
89
{
 
90
    m_baseline = QFINDTESTDATA("baseline");
 
91
    QVERIFY2(!m_baseline.isEmpty(), "Could not find 'baseline'.");
 
92
    QProcess process;
 
93
    process.start(m_command, QStringList(QLatin1String("-help")));
 
94
    QVERIFY2(process.waitForStarted(), msgProcessStartFailed(m_command, process.errorString()));
 
95
    QVERIFY(process.waitForFinished());
 
96
    QCOMPARE(process.exitStatus(), QProcess::NormalExit);
 
97
    QCOMPARE(process.exitCode(), 0);
 
98
    // Print version
 
99
    const QString out = QString::fromLocal8Bit(process.readAllStandardError()).remove(QLatin1Char('\r'));
 
100
    const QStringList outLines = out.split(QLatin1Char('\n'));
 
101
    // Print version
 
102
    QString msg = QString::fromLatin1("uic test built %1 running in '%2' using: ").
 
103
                  arg(QString::fromLatin1(__DATE__), QDir::currentPath());
 
104
    if (!outLines.empty())
 
105
        msg += outLines.front();
 
106
    qDebug("%s", qPrintable(msg));
 
107
}
 
108
 
 
109
void tst_uic::cleanupTestCase()
 
110
{
 
111
    static const char envVar[] = "UIC_KEEP_GENERATED_FILES";
 
112
    if (qgetenv(envVar).isEmpty()) {
 
113
        qDebug("Note: The environment variable '%s' can be set to keep the temporary files for error analysis.", envVar);
 
114
    } else {
 
115
        m_generated.setAutoRemove(false);
 
116
        qDebug("Keeping generated files in '%s'", qPrintable(QDir::toNativeSeparators(m_generated.path())));
 
117
    }
 
118
}
 
119
 
 
120
void tst_uic::stdOut()
 
121
{
 
122
    // Checks of everything works when using stdout and whether
 
123
    // the OS file format conventions regarding newlines are met.
 
124
    QDir baseline(m_baseline);
 
125
    const QFileInfoList baselineFiles = baseline.entryInfoList(QStringList(QLatin1String("*.ui")), QDir::Files);
 
126
    QVERIFY(!baselineFiles.isEmpty());
 
127
    QProcess process;
 
128
    process.start(m_command, QStringList(baselineFiles.front().absoluteFilePath()));
 
129
    process.closeWriteChannel();
 
130
    QVERIFY2(process.waitForStarted(), msgProcessStartFailed(m_command, process.errorString()));
 
131
    QVERIFY(process.waitForFinished());
 
132
    QCOMPARE(process.exitStatus(), QProcess::NormalExit);
 
133
    QCOMPARE(process.exitCode(), 0);
 
134
    const QByteArray output = process.readAllStandardOutput();
 
135
    QByteArray expected = "/********************************************************************************";
 
136
#ifdef Q_OS_WIN
 
137
    expected += "\r\n";
 
138
#else
 
139
    expected += '\n';
 
140
#endif
 
141
    expected += "** ";
 
142
    QVERIFY2(output.startsWith(expected), (QByteArray("Got: ") + output.toHex()).constData());
 
143
}
 
144
 
 
145
void tst_uic::run()
 
146
{
 
147
    QFETCH(QString, originalFile);
 
148
    QFETCH(QString, generatedFile);
 
149
 
 
150
    QProcess process;
 
151
    process.start(m_command, QStringList(originalFile)
 
152
        << QString(QLatin1String("-o")) << generatedFile);
 
153
    QVERIFY2(process.waitForStarted(), msgProcessStartFailed(m_command, process.errorString()));
 
154
    QVERIFY(process.waitForFinished());
 
155
    QCOMPARE(process.exitStatus(), QProcess::NormalExit);
 
156
    QCOMPARE(process.exitCode(), 0);
 
157
    QCOMPARE(QFileInfo(generatedFile).exists(), true);
 
158
}
 
159
 
 
160
void tst_uic::run_data() const
 
161
{
 
162
    QTest::addColumn<QString>("originalFile");
 
163
    QTest::addColumn<QString>("generatedFile");
 
164
 
 
165
    QDir generated(m_generated.path());
 
166
    QDir baseline(m_baseline);
 
167
    const QFileInfoList baselineFiles = baseline.entryInfoList(QStringList("*.ui"), QDir::Files);
 
168
    foreach (const QFileInfo &baselineFile, baselineFiles) {
 
169
        const QString generatedFile = generated.absolutePath()
 
170
            + QLatin1Char('/') + baselineFile.fileName()
 
171
            + QLatin1String(".h");
 
172
        QTest::newRow(qPrintable(baselineFile.baseName()))
 
173
            << baselineFile.absoluteFilePath()
 
174
            << generatedFile;
 
175
    }
 
176
}
 
177
 
 
178
 
 
179
void tst_uic::compare()
 
180
{
 
181
    QFETCH(QString, originalFile);
 
182
    QFETCH(QString, generatedFile);
 
183
 
 
184
    QFile orgFile(originalFile);
 
185
    QFile genFile(generatedFile);
 
186
 
 
187
    if (!orgFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
 
188
        QString err(QLatin1String("Could not read file: %1..."));
 
189
        QFAIL(err.arg(orgFile.fileName()).toUtf8());
 
190
    }
 
191
 
 
192
    if (!genFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
 
193
        QString err(QLatin1String("Could not read file: %1..."));
 
194
        QFAIL(err.arg(genFile.fileName()).toUtf8());
 
195
    }
 
196
 
 
197
    originalFile = orgFile.readAll();
 
198
    originalFile.replace(QRegExp(QLatin1String("Created by: Qt User Interface Compiler version [.\\d]{5,5}")), "");
 
199
 
 
200
    generatedFile = genFile.readAll();
 
201
    generatedFile.replace(QRegExp(QLatin1String("Created by: Qt User Interface Compiler version [.\\d]{5,5}")), "");
 
202
 
 
203
    QCOMPARE(generatedFile, originalFile);
 
204
}
 
205
 
 
206
void tst_uic::compare_data() const
 
207
{
 
208
    QTest::addColumn<QString>("originalFile");
 
209
    QTest::addColumn<QString>("generatedFile");
 
210
 
 
211
    QDir generated(m_generated.path());
 
212
    QDir baseline(m_baseline);
 
213
    const QFileInfoList baselineFiles = baseline.entryInfoList(QStringList("*.h"), QDir::Files);
 
214
    foreach (const QFileInfo &baselineFile, baselineFiles) {
 
215
        const QString generatedFile = generated.absolutePath()
 
216
                + QLatin1Char('/') + baselineFile.fileName();
 
217
        QTest::newRow(qPrintable(baselineFile.baseName()))
 
218
            << baselineFile.absoluteFilePath()
 
219
            << generatedFile;
 
220
    }
 
221
}
 
222
 
 
223
QTEST_MAIN(tst_uic)
 
224
#include "tst_uic.moc"