tests/auto/declarative/qmlvisual/tst_qmlvisual.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <qtest.h>
       
    42 #include <QDeclarativeView>
       
    43 #include <QApplication>
       
    44 #include <QLibraryInfo>
       
    45 #include <QDir>
       
    46 #include <QDebug>
       
    47 #include <QProcess>
       
    48 #include <QFile>
       
    49 
       
    50 enum Mode { Record, RecordNoVisuals, RecordSnapshot, Play, TestVisuals, RemoveVisuals, UpdateVisuals, UpdatePlatformVisuals, Test };
       
    51 
       
    52 static QString testdir;
       
    53 class tst_qmlvisual : public QObject
       
    54 {
       
    55     Q_OBJECT
       
    56 public:
       
    57     tst_qmlvisual();
       
    58 
       
    59     static QString toTestScript(const QString &, Mode=Test);
       
    60     static QString viewer();
       
    61 
       
    62     static QStringList findQmlFiles(const QDir &d);
       
    63 private slots:
       
    64     void visual_data();
       
    65     void visual();
       
    66 
       
    67 private:
       
    68     QString qmlruntime;
       
    69 };
       
    70 
       
    71 
       
    72 tst_qmlvisual::tst_qmlvisual()
       
    73 {
       
    74     qmlruntime = viewer();
       
    75 }
       
    76 
       
    77 QString tst_qmlvisual::viewer()
       
    78 {
       
    79     QString binaries = QLibraryInfo::location(QLibraryInfo::BinariesPath);
       
    80 
       
    81     QString qmlruntime;
       
    82 
       
    83 #if defined(Q_WS_MAC)
       
    84     qmlruntime = QDir(binaries).absoluteFilePath("QMLViewer.app/Contents/MacOS/QMLViewer");
       
    85 #elif defined(Q_WS_WIN) || defined(Q_WS_S60)
       
    86     qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer.exe");
       
    87 #else
       
    88     qmlruntime = QDir(binaries).absoluteFilePath("qmlviewer");
       
    89 #endif
       
    90 
       
    91     return qmlruntime;
       
    92 }
       
    93 
       
    94 void tst_qmlvisual::visual_data()
       
    95 {
       
    96     QTest::addColumn<QString>("file");
       
    97     QTest::addColumn<QString>("testdata");
       
    98 
       
    99     QStringList files;
       
   100     if (qgetenv("QMLVISUAL_ALL") != "")
       
   101         files << findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
       
   102     else {
       
   103         //these are newly added tests we want to try out in CI (then move to the stable list)
       
   104         files << QT_TEST_SOURCE_DIR "/animation/qtbug10586/qtbug10586.qml";
       
   105         files << QT_TEST_SOURCE_DIR "/qdeclarativeborderimage/animated.qml";
       
   106         files << QT_TEST_SOURCE_DIR "/qdeclarativeflipable/test-flipable.qml";
       
   107         files << QT_TEST_SOURCE_DIR "/qdeclarativepositioners/usingRepeater.qml";
       
   108         files << QT_TEST_SOURCE_DIR "/animation/parentAnimation2/parentAnimation2.qml";
       
   109 
       
   110         //these are tests we think are stable and useful enough to be run by the CI system
       
   111         files << QT_TEST_SOURCE_DIR "/animation/bindinganimation/bindinganimation.qml";
       
   112         files << QT_TEST_SOURCE_DIR "/animation/loop/loop.qml";
       
   113         files << QT_TEST_SOURCE_DIR "/animation/parallelAnimation/parallelAnimation-visual.qml";
       
   114         files << QT_TEST_SOURCE_DIR "/animation/parentAnimation/parentAnimation-visual.qml";
       
   115         files << QT_TEST_SOURCE_DIR "/animation/reanchor/reanchor.qml";
       
   116         files << QT_TEST_SOURCE_DIR "/animation/scriptAction/scriptAction-visual.qml";
       
   117         files << QT_TEST_SOURCE_DIR "/qdeclarativemousearea/drag.qml";
       
   118         files << QT_TEST_SOURCE_DIR "/fillmode/fillmode.qml";
       
   119 
       
   120         // new tests
       
   121         files << QT_TEST_SOURCE_DIR "/qdeclarativemousearea/mousearea-flickable.qml";
       
   122 
       
   123         //these reliably fail in CI, for unknown reasons
       
   124         //files << QT_TEST_SOURCE_DIR "/animation/easing/easing.qml";
       
   125         //files << QT_TEST_SOURCE_DIR "/animation/pauseAnimation/pauseAnimation-visual.qml";
       
   126         //files << QT_TEST_SOURCE_DIR "/qdeclarativeborderimage/borders.qml";
       
   127         //files << QT_TEST_SOURCE_DIR "/qdeclarativeborderimage/animated-smooth.qml";
       
   128 
       
   129         //these reliably fail on Linux because of color interpolation (different float rounding)
       
   130 #if !defined(Q_WS_X11) && !defined(Q_WS_QWS)
       
   131         files << QT_TEST_SOURCE_DIR "/animation/colorAnimation/colorAnimation-visual.qml";
       
   132         files << QT_TEST_SOURCE_DIR "/animation/propertyAction/propertyAction-visual.qml";
       
   133 #endif
       
   134 
       
   135         //this is unstable because the MouseArea press-and-hold timer is not synchronized to the animation framework.
       
   136         //files << QT_TEST_SOURCE_DIR "/qdeclarativemousearea/mousearea-visual.qml";
       
   137     }
       
   138 
       
   139     foreach (const QString &file, files) {
       
   140         QString testdata = toTestScript(file);
       
   141         if (testdata.isEmpty())
       
   142             continue;
       
   143 
       
   144         QTest::newRow(file.toLatin1().constData()) << file << testdata;
       
   145     }
       
   146 }
       
   147 
       
   148 void tst_qmlvisual::visual()
       
   149 {
       
   150     QFETCH(QString, file);
       
   151     QFETCH(QString, testdata);
       
   152 
       
   153     QStringList arguments;
       
   154     arguments << "-script" << testdata
       
   155               << "-scriptopts" << "play,testimages,testerror,exitoncomplete,exitonfailure" 
       
   156               << file;
       
   157 #ifdef Q_WS_QWS
       
   158     arguments << "-qws";
       
   159 #endif
       
   160 
       
   161     QProcess p;
       
   162     p.start(qmlruntime, arguments);
       
   163     QVERIFY(p.waitForFinished());
       
   164     if (p.exitCode() != 0)
       
   165         qDebug() << p.readAllStandardError();
       
   166     QCOMPARE(p.exitStatus(), QProcess::NormalExit);
       
   167     QCOMPARE(p.exitCode(), 0);
       
   168 }
       
   169 
       
   170 QString tst_qmlvisual::toTestScript(const QString &file, Mode mode)
       
   171 {
       
   172     if (!file.endsWith(".qml"))
       
   173         return QString();
       
   174 
       
   175     int index = file.lastIndexOf(QDir::separator());
       
   176     if (index == -1)
       
   177         index = file.lastIndexOf('/');
       
   178     if (index == -1)
       
   179         return QString();
       
   180 
       
   181     const char* platformsuffix=0; // platforms with different fonts
       
   182 #if defined(Q_WS_MACX)
       
   183     platformsuffix = "-MAC";
       
   184 #elif defined(Q_WS_X11)
       
   185     platformsuffix = "-X11";
       
   186 #elif defined(Q_WS_WIN32)
       
   187     platformsuffix = "-WIN";
       
   188 #elif defined(Q_WS_QWS)
       
   189     platformsuffix = "-QWS";
       
   190 #elif defined(Q_WS_S60)
       
   191     platformsuffix = "-S60";
       
   192 #endif
       
   193 
       
   194     QString testdata = file.left(index + 1) + 
       
   195                        QString("data");
       
   196     QString testname = file.mid(index + 1, file.length() - index - 5);
       
   197 
       
   198     if (platformsuffix && (mode == UpdatePlatformVisuals || QFile::exists(testdata+QLatin1String(platformsuffix)+QDir::separator()+testname+".qml"))) {
       
   199         QString platformdir = testdata + QLatin1String(platformsuffix);
       
   200         if (mode == UpdatePlatformVisuals) {
       
   201             Q_ASSERT(QDir().mkpath(platformdir));
       
   202             // Copy from base
       
   203             QDir dir(testdata,testname+".*");
       
   204             dir.setFilter(QDir::Files);
       
   205             QFileInfoList list = dir.entryInfoList();
       
   206             for (int i = 0; i < list.size(); ++i) {
       
   207                 QFile in(list.at(i).filePath());
       
   208                 Q_ASSERT(in.open(QIODevice::ReadOnly));
       
   209                 QFile out(platformdir + QDir::separator() + list.at(i).fileName());
       
   210                 Q_ASSERT(out.open(QIODevice::WriteOnly));
       
   211                 out.write(in.readAll());
       
   212             }
       
   213         }
       
   214         testdata = platformdir;
       
   215     }
       
   216 
       
   217     testdata += QDir::separator() + testname;
       
   218 
       
   219     return testdata;
       
   220 }
       
   221 
       
   222 QStringList tst_qmlvisual::findQmlFiles(const QDir &d)
       
   223 {
       
   224     QStringList rv;
       
   225 
       
   226     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"), 
       
   227                                     QDir::Files);
       
   228     foreach (const QString &file, files) {
       
   229         if (file.at(0).isLower()) {
       
   230             rv << d.absoluteFilePath(file);
       
   231         }
       
   232     }
       
   233 
       
   234     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot | 
       
   235                                    QDir::NoSymLinks);
       
   236     foreach (const QString &dir, dirs) {
       
   237         if (dir.left(4) == "data")
       
   238             continue;
       
   239 
       
   240         QDir sub = d;
       
   241         sub.cd(dir);
       
   242         rv << findQmlFiles(sub);
       
   243     }
       
   244 
       
   245     return rv;
       
   246 }
       
   247 
       
   248 void action(Mode mode, const QString &file)
       
   249 {
       
   250     Q_ASSERT(mode != Test);
       
   251 
       
   252     QString testdata = tst_qmlvisual::toTestScript(file,mode);
       
   253 
       
   254     QStringList arguments;
       
   255     switch (mode) {
       
   256         case Test:
       
   257             // Don't run qml
       
   258             break;
       
   259         case Record:
       
   260             arguments << "-script" << testdata
       
   261                   << "-scriptopts" << "record,testimages,saveonexit"
       
   262                   << file;
       
   263             break;
       
   264         case RecordNoVisuals:
       
   265             arguments << "-script" << testdata
       
   266                   << "-scriptopts" << "record,saveonexit"
       
   267                   << file;
       
   268             break;
       
   269         case RecordSnapshot:
       
   270             arguments << "-script" << testdata
       
   271                   << "-scriptopts" << "record,testimages,snapshot,saveonexit"
       
   272                   << file;
       
   273             break;
       
   274         case Play:
       
   275             arguments << "-script" << testdata
       
   276                   << "-scriptopts" << "play,testimages,testerror,exitoncomplete"
       
   277                   << file;
       
   278             break;
       
   279         case TestVisuals:
       
   280         arguments << "-script" << testdata
       
   281                   << "-scriptopts" << "play"
       
   282                   << file;
       
   283             break;
       
   284         case UpdateVisuals:
       
   285         case UpdatePlatformVisuals:
       
   286             arguments << "-script" << testdata
       
   287                   << "-scriptopts" << "play,record,testimages,exitoncomplete,saveonexit"
       
   288                   << file;
       
   289             break;
       
   290         case RemoveVisuals:
       
   291             arguments << "-script" << testdata
       
   292                   << "-scriptopts" << "play,record,exitoncomplete,saveonexit"
       
   293                   << file;
       
   294             break;
       
   295     }
       
   296     if (!arguments.isEmpty()) {
       
   297         QProcess p;
       
   298         p.setProcessChannelMode(QProcess::ForwardedChannels);
       
   299         p.start(tst_qmlvisual::viewer(), arguments);
       
   300         p.waitForFinished();
       
   301     }
       
   302 }
       
   303 
       
   304 void usage()
       
   305 {
       
   306     fprintf(stderr, "\n");
       
   307     fprintf(stderr, "QML related options\n");
       
   308     fprintf(stderr, " -listtests                  : list all the tests seen by tst_qmlvisual, and then exit immediately\n");
       
   309     fprintf(stderr, " -record file                : record new test data for file\n");
       
   310     fprintf(stderr, " -recordnovisuals file       : record new test data for file, but ignore visuals\n");
       
   311     fprintf(stderr, " -recordsnapshot file        : record new snapshot for file (like record but only records a single frame and then exits)\n");
       
   312     fprintf(stderr, " -play file                  : playback test data for file, printing errors\n");
       
   313     fprintf(stderr, " -testvisuals file           : playback test data for file, without errors\n");
       
   314     fprintf(stderr, " -updatevisuals file         : playback test data for file, accept new visuals for file\n");
       
   315     fprintf(stderr, " -updateplatformvisuals file : playback test data for file, accept new visuals for file only on current platform (MacOSX/Win32/X11/QWS/S60)\n");
       
   316     fprintf(stderr, "\n"
       
   317         "Visual tests are recordings of manual interactions with a QML test,\n"
       
   318         "that can then be run automatically. To record a new test, run:\n"
       
   319         "\n"
       
   320         "  tst_qmlvisual -record yourtestdir/yourtest.qml\n"
       
   321         "\n"
       
   322         "This records everything you do (try to keep it short).\n"
       
   323         "To play back a test, run:\n"
       
   324         "\n"
       
   325         "  tst_qmlvisual -play yourtestdir/yourtest.qml\n"
       
   326         "\n"
       
   327         "Your test may include QML code to test itself, reporting any error to an\n"
       
   328         "'error' property on the root object - the test will fail if this property\n"
       
   329         "gets set to anything non-empty.\n"
       
   330         "\n"
       
   331         "If your test changes slightly but is still correct (check with -play), you\n"
       
   332         "can update the visuals by running:\n"
       
   333         "\n"
       
   334         "  tst_qmlvisual -updatevisuals yourtestdir/yourtest.qml\n"
       
   335         "\n"
       
   336         "If your test includes platform-sensitive visuals (eg. text in system fonts),\n"
       
   337         "you should create platform-specific visuals, using -updateplatformvisuals\n"
       
   338         "instead.\n"
       
   339         "\n"
       
   340         "If you ONLY wish to use the 'error' property, you can record your test with\n"
       
   341         "-recordnovisuals, or discard existing visuals with -removevisuals; the test\n"
       
   342         "will then only fail on a syntax error, crash, or non-empty 'error' property.\n"
       
   343     );
       
   344 }
       
   345 
       
   346 int main(int argc, char **argv)
       
   347 {
       
   348     QApplication app(argc, argv);
       
   349 
       
   350     Mode mode = Test;
       
   351     QString file;
       
   352     bool showHelp = false;
       
   353 
       
   354     int newArgc = 1;
       
   355     char **newArgv = new char*[argc];
       
   356     newArgv[0] = argv[0];
       
   357 
       
   358     for (int ii = 1; ii < argc; ++ii) {
       
   359         QString arg(argv[ii]);
       
   360         if (arg == "-play" && (ii + 1) < argc) {
       
   361             mode = Play;
       
   362             file = argv[++ii];
       
   363         } else if (arg == "-record" && (ii + 1) < argc) {
       
   364             mode = Record;
       
   365             file = argv[++ii];
       
   366         } else if (arg == "-recordnovisuals" && (ii + 1) < argc) {
       
   367             mode = RecordNoVisuals;
       
   368             file = argv[++ii];
       
   369         }  else if (arg == "-recordsnapshot" && (ii + 1) < argc) {
       
   370             mode = RecordSnapshot;
       
   371             file = argv[++ii];
       
   372         } else if (arg == "-testvisuals" && (ii + 1) < argc) {
       
   373             mode = TestVisuals;
       
   374             file = argv[++ii];
       
   375         } else if (arg == "-removevisuals" && (ii + 1) < argc) {
       
   376             mode = RemoveVisuals;
       
   377             file = argv[++ii];
       
   378         } else if (arg == "-updatevisuals" && (ii + 1) < argc) {
       
   379             mode = UpdateVisuals;
       
   380             file = argv[++ii];
       
   381         } else if (arg == "-updateplatformvisuals" && (ii + 1) < argc) {
       
   382             mode = UpdatePlatformVisuals;
       
   383             file = argv[++ii];
       
   384         } else {
       
   385             newArgv[newArgc++] = argv[ii];
       
   386         }
       
   387         
       
   388         if (arg == "-help" || arg == "-?" || arg == "--help") {
       
   389             atexit(usage);
       
   390             showHelp = true;
       
   391         }
       
   392 
       
   393         if (arg == "-listtests") {
       
   394             QStringList list = tst_qmlvisual::findQmlFiles(QDir(QT_TEST_SOURCE_DIR));
       
   395             foreach (QString test, list) {
       
   396                 qWarning() << qPrintable(test);
       
   397             }
       
   398             return 0;
       
   399         }
       
   400     }
       
   401 
       
   402     if (mode == Test || showHelp) {
       
   403         tst_qmlvisual tc;
       
   404         return QTest::qExec(&tc, newArgc, newArgv);
       
   405     } else {
       
   406         if (!file.endsWith(QLatin1String(".qml"))) {
       
   407             qWarning() << "Error: Invalid file name (must end in .qml)";
       
   408             return -1;
       
   409         }
       
   410         QDir d = QDir::current();
       
   411         QString absFile = d.absoluteFilePath(file);
       
   412         if (!QFile::exists(absFile)) {
       
   413             qWarning() << "Error: File does not exist";
       
   414             return -1;
       
   415         }
       
   416 
       
   417         action(mode, absFile);
       
   418         return 0;
       
   419     }
       
   420 }
       
   421 
       
   422 #include "tst_qmlvisual.moc"