tests/auto/uic3/tst_uic3.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtCore/QDir>
       
    44 #include <QtTest/QtTest>
       
    45 #include <QtCore/QProcess>
       
    46 #include <QtCore/QByteArray>
       
    47 #include <QtCore/QString>
       
    48 #include <QtCore/QLibraryInfo>
       
    49 
       
    50 #ifndef Q_OS_WINCE
       
    51 
       
    52 class tst_uic3 : public QObject
       
    53 {
       
    54     Q_OBJECT
       
    55 
       
    56 public:
       
    57     tst_uic3();
       
    58     
       
    59 private Q_SLOTS:
       
    60     void initTestCase();
       
    61     
       
    62     void convert();
       
    63     void convert_data() const;
       
    64 
       
    65 private:
       
    66     QString workingDir() const;
       
    67 
       
    68 private:
       
    69     bool uic3Exists;
       
    70     QString command;
       
    71 };
       
    72 
       
    73 
       
    74 tst_uic3::tst_uic3()
       
    75     : uic3Exists(true)
       
    76     , command(QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/uic3"))
       
    77 {
       
    78 }
       
    79 
       
    80 void tst_uic3::initTestCase()
       
    81 {
       
    82     QProcess process;
       
    83     process.start(command, QStringList(QLatin1String("-help")));
       
    84     if (!process.waitForFinished()) {
       
    85         uic3Exists = false;
       
    86         const QString path = QString::fromLocal8Bit(qgetenv("PATH"));
       
    87         QString message = QString::fromLatin1("'%1' could not be found when run from '%2'. Path: '%3' ").
       
    88                           arg(command, QDir::currentPath(), path);
       
    89         QFAIL(qPrintable(message));
       
    90     }
       
    91     // Print version
       
    92     const QString out = QString::fromLocal8Bit(process.readAllStandardError()).remove(QLatin1Char('\r'));
       
    93     const QStringList outLines = out.split(QLatin1Char('\n'));
       
    94     QString msg = QString::fromLatin1("uic3 test built %1 running in '%2' using: ").
       
    95                   arg(QString::fromAscii(__DATE__), QDir::currentPath());
       
    96     if (!outLines.empty())
       
    97         msg += outLines.front();
       
    98     qDebug() << msg;
       
    99     process.terminate();
       
   100 
       
   101     QCOMPARE(QFileInfo(QLatin1String("baseline")).exists(), true);
       
   102     QCOMPARE(QFileInfo(QLatin1String("generated")).exists(), true);
       
   103 }
       
   104 
       
   105 void tst_uic3::convert()
       
   106 {
       
   107     if (!uic3Exists)
       
   108         QSKIP("uic3 not found in the path...", SkipAll);
       
   109 
       
   110     QFETCH(QString, originalFile);
       
   111     QFETCH(QString, generatedFile);
       
   112     QFETCH(QString, baseFile);
       
   113     QFETCH(QString, errorFile);
       
   114 
       
   115     QProcess process;
       
   116     process.setWorkingDirectory(workingDir());
       
   117 
       
   118     process.start(command, QStringList("-convert") << originalFile
       
   119                   << QString(QLatin1String("-o")) << generatedFile);
       
   120 
       
   121 
       
   122     QCOMPARE(process.exitStatus(), QProcess::NormalExit);
       
   123 
       
   124     if (process.waitForFinished()) {
       
   125         QCOMPARE(process.exitCode(), 0);
       
   126         QCOMPARE(QFileInfo(generatedFile).exists(), true);
       
   127     } else {
       
   128         QString error(QLatin1String("could not convert file: "));
       
   129         QFAIL(error.append(generatedFile).toUtf8().constData());
       
   130     }
       
   131 
       
   132     QByteArray errorOutput = process.readAllStandardError();
       
   133 
       
   134     // Compare generated file to baseline
       
   135 
       
   136     QFile bFile(baseFile);
       
   137     QFile gFile(generatedFile);
       
   138     QFile eFile(errorFile);
       
   139 
       
   140     if (!bFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
       
   141         QString err(QLatin1String("Could not read file: %1..."));
       
   142         QFAIL(err.arg(bFile.fileName()).toUtf8());
       
   143     }
       
   144 
       
   145     if (!gFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
       
   146         QString err(QLatin1String("Could not read file: %1..."));
       
   147         QFAIL(err.arg(gFile.fileName()).toUtf8());
       
   148     }
       
   149 
       
   150     if (!eFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
       
   151         QString err(QLatin1String("Could not read file: %1..."));
       
   152         QFAIL(err.arg(eFile.fileName()).toUtf8());
       
   153     }
       
   154 
       
   155     QCOMPARE(QString(bFile.readAll()), QString(gFile.readAll()));
       
   156     QCOMPARE(QString(eFile.readAll()), QString::fromLocal8Bit(errorOutput).remove(QLatin1Char('\r')));
       
   157 }
       
   158 
       
   159 void tst_uic3::convert_data() const
       
   160 {
       
   161     QTest::addColumn<QString>("originalFile");
       
   162     QTest::addColumn<QString>("generatedFile");
       
   163     QTest::addColumn<QString>("baseFile");
       
   164     QTest::addColumn<QString>("errorFile");
       
   165 
       
   166     QString cwd = workingDir().append(QDir::separator());
       
   167 
       
   168     QDir dir(cwd + QLatin1String("baseline"));
       
   169     QFileInfoList originalFiles = dir.entryInfoList(QStringList("*.ui"), QDir::Files);
       
   170 
       
   171     dir.setPath(cwd + QLatin1String("generated"));
       
   172     for (int i = 0; i < originalFiles.count(); ++i) {
       
   173         QTest::newRow(qPrintable(originalFiles.at(i).baseName()))
       
   174             << originalFiles.at(i).absoluteFilePath()
       
   175             << dir.absolutePath() + QDir::separator()
       
   176                 + originalFiles.at(i).fileName().append(QLatin1String(".4"))
       
   177             << originalFiles.at(i).absoluteFilePath().append(QLatin1String(".4"))
       
   178             << originalFiles.at(i).absoluteFilePath().append(QLatin1String(".err"));
       
   179     }
       
   180 }
       
   181 
       
   182 QString tst_uic3::workingDir() const
       
   183 {
       
   184     return QDir::cleanPath(SRCDIR);
       
   185 }
       
   186 
       
   187 QTEST_APPLESS_MAIN(tst_uic3)
       
   188 #include "tst_uic3.moc"
       
   189 #else
       
   190 QTEST_NOOP_MAIN
       
   191 #endif