tests/auto/declarative/examples/tst_examples.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     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 <QLibraryInfo>
       
    43 #include <QDir>
       
    44 #include <QProcess>
       
    45 #include <QDebug>
       
    46 #include "qmlruntime.h"
       
    47 #include <QDeclarativeView>
       
    48 #include <QDeclarativeError>
       
    49 
       
    50 class tst_examples : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 public:
       
    54     tst_examples();
       
    55 
       
    56 private slots:
       
    57     void examples_data();
       
    58     void examples();
       
    59 
       
    60     void namingConvention();
       
    61 private:
       
    62     QString qmlruntime;
       
    63     QStringList excludedDirs;
       
    64 
       
    65     void namingConvention(const QDir &);
       
    66     QStringList findQmlFiles(const QDir &);
       
    67 };
       
    68 
       
    69 tst_examples::tst_examples()
       
    70 {
       
    71     QString binaries = QLibraryInfo::location(QLibraryInfo::BinariesPath);
       
    72 
       
    73 #if defined(Q_WS_MAC)
       
    74     qmlruntime = QDir(binaries).absoluteFilePath("qml.app/Contents/MacOS/qml");
       
    75 #elif defined(Q_WS_WIN)
       
    76     qmlruntime = QDir(binaries).absoluteFilePath("qml.exe");
       
    77 #else
       
    78     qmlruntime = QDir(binaries).absoluteFilePath("qml");
       
    79 #endif
       
    80 
       
    81 
       
    82     // Add directories you want excluded here
       
    83 
       
    84 #ifdef QT_NO_WEBKIT
       
    85     excludedDirs << "examples/declarative/modelviews/webview";
       
    86     excludedDirs << "demos/declarative/webbrowser";
       
    87 #endif
       
    88 
       
    89 #ifdef QT_NO_XMLPATTERNS
       
    90     excludedDirs << "examples/declarative/xml/xmldata";
       
    91     excludedDirs << "demos/declarative/twitter";
       
    92     excludedDirs << "demos/declarative/flickr";
       
    93     excludedDirs << "demos/declarative/photoviewer";
       
    94 #endif
       
    95 }
       
    96 
       
    97 /*
       
    98 This tests that the demos and examples follow the naming convention required
       
    99 to have them tested by the examples() test.
       
   100 */
       
   101 void tst_examples::namingConvention(const QDir &d)
       
   102 {
       
   103     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
       
   104         QString s = excludedDirs.at(ii);
       
   105         if (d.absolutePath().endsWith(s))
       
   106             return;
       
   107     }
       
   108 
       
   109     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
       
   110                                     QDir::Files);
       
   111 
       
   112     bool seenQml = !files.isEmpty();
       
   113     bool seenLowercase = false;
       
   114 
       
   115     foreach (const QString &file, files) {
       
   116         if (file.at(0).isLower())
       
   117             seenLowercase = true;
       
   118     }
       
   119 
       
   120     if (!seenQml) {
       
   121         QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
       
   122                 QDir::NoSymLinks);
       
   123         foreach (const QString &dir, dirs) {
       
   124             QDir sub = d;
       
   125             sub.cd(dir);
       
   126             namingConvention(sub);
       
   127         }
       
   128     } else if(!seenLowercase) {
       
   129         QTest::qFail(QString("Directory " + d.absolutePath() + " violates naming convention").toLatin1().constData(), __FILE__, __LINE__);
       
   130     }
       
   131 }
       
   132 
       
   133 void tst_examples::namingConvention()
       
   134 {
       
   135     QString examples = QLibraryInfo::location(QLibraryInfo::ExamplesPath);
       
   136     QString demos = QLibraryInfo::location(QLibraryInfo::DemosPath);
       
   137 
       
   138     namingConvention(QDir(examples));
       
   139     namingConvention(QDir(demos));
       
   140 }
       
   141 
       
   142 QStringList tst_examples::findQmlFiles(const QDir &d)
       
   143 {
       
   144     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
       
   145         QString s = excludedDirs.at(ii);
       
   146         if (d.absolutePath().endsWith(s))
       
   147             return QStringList();
       
   148     }
       
   149 
       
   150     QStringList rv;
       
   151 
       
   152     QStringList cppfiles = d.entryList(QStringList() << QLatin1String("*.cpp"), QDir::Files);
       
   153     if (cppfiles.isEmpty()) {
       
   154         QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
       
   155                                         QDir::Files);
       
   156         foreach (const QString &file, files) {
       
   157             if (file.at(0).isLower()) {
       
   158                 rv << d.absoluteFilePath(file);
       
   159             }
       
   160         }
       
   161     }
       
   162 
       
   163     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
       
   164                                    QDir::NoSymLinks);
       
   165     foreach (const QString &dir, dirs) {
       
   166         QDir sub = d;
       
   167         sub.cd(dir);
       
   168         rv << findQmlFiles(sub);
       
   169     }
       
   170 
       
   171     return rv;
       
   172 }
       
   173 
       
   174 /*
       
   175 This test runs all the examples in the declarative UI source tree and ensures
       
   176 that they start and exit cleanly.
       
   177 
       
   178 Examples are any .qml files under the examples/ or demos/ directory that start
       
   179 with a lower case letter.
       
   180 */
       
   181 void tst_examples::examples_data()
       
   182 {
       
   183     QTest::addColumn<QString>("file");
       
   184 
       
   185     QString examples = QLatin1String(SRCDIR) + "/../../../../demos/declarative/";
       
   186     QString demos = QLatin1String(SRCDIR) + "/../../../../examples/declarative/";
       
   187     QString snippets = QLatin1String(SRCDIR) + "/../../../../doc/src/snippets/";
       
   188 
       
   189     QStringList files;
       
   190     files << findQmlFiles(QDir(examples));
       
   191     files << findQmlFiles(QDir(demos));
       
   192     files << findQmlFiles(QDir(snippets));
       
   193 
       
   194     foreach (const QString &file, files)
       
   195         QTest::newRow(qPrintable(file)) << file;
       
   196 }
       
   197 
       
   198 static void silentErrorsMsgHandler(QtMsgType, const char *)
       
   199 {
       
   200 }
       
   201 
       
   202 void tst_examples::examples()
       
   203 {
       
   204     QFETCH(QString, file);
       
   205 
       
   206     QDeclarativeViewer viewer;
       
   207 
       
   208     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
       
   209     QVERIFY(viewer.open(file));
       
   210     qInstallMsgHandler(old);
       
   211 
       
   212     if (viewer.view()->status() == QDeclarativeView::Error)
       
   213         qWarning() << viewer.view()->errors();
       
   214 
       
   215     QCOMPARE(viewer.view()->status(), QDeclarativeView::Ready);
       
   216     viewer.show();
       
   217 
       
   218     QTest::qWaitForWindowShown(&viewer);
       
   219 }
       
   220 
       
   221 QTEST_MAIN(tst_examples)
       
   222 
       
   223 #include "tst_examples.moc"