1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QLibraryInfo>
47 #include "qmlruntime.h"
48 #include <QDeclarativeView>
49 #include <QDeclarativeError>
51 class tst_examples : public QObject
61 void namingConvention();
63 QStringList excludedDirs;
65 void namingConvention(const QDir &);
66 QStringList findQmlFiles(const QDir &);
69 tst_examples::tst_examples()
71 // Add directories you want excluded here
74 excludedDirs << "examples/declarative/qtquick1/modelviews/webview";
75 excludedDirs << "examples/declarative/qtquick1/webbrowser";
76 excludedDirs << "doc/src/snippets/declarative/qtquick1/webview";
77 excludedDirs << "doc/src/snippets/qtquick1/qtquick1/webview";
80 #ifdef QT_NO_XMLPATTERNS
81 excludedDirs << "examples/declarative/qtquick1/xml/xmldata";
82 excludedDirs << "examples/declarative/qtquick1/twitter";
83 excludedDirs << "examples/declarative/qtquick1/flickr";
84 excludedDirs << "examples/declarative/qtquick1/photoviewer";
89 This tests that the examples follow the naming convention required
90 to have them tested by the examples() test.
92 void tst_examples::namingConvention(const QDir &d)
94 for (int ii = 0; ii < excludedDirs.count(); ++ii) {
95 QString s = excludedDirs.at(ii);
96 if (d.absolutePath().endsWith(s))
100 QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
103 bool seenQml = !files.isEmpty();
104 bool seenLowercase = false;
106 foreach (const QString &file, files) {
107 if (file.at(0).isLower())
108 seenLowercase = true;
112 QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
114 foreach (const QString &dir, dirs) {
117 namingConvention(sub);
119 } else if (!seenLowercase) {
120 QFAIL(qPrintable(QString(
121 "Directory %1 violates naming convention; expected at least one qml file "
122 "starting with lower case, got: %2"
123 ).arg(d.absolutePath()).arg(files.join(","))));
127 void tst_examples::namingConvention()
129 QString examples = QLibraryInfo::location(QLibraryInfo::ExamplesPath);
131 namingConvention(QDir(examples));
134 QStringList tst_examples::findQmlFiles(const QDir &d)
136 for (int ii = 0; ii < excludedDirs.count(); ++ii) {
137 QString s = excludedDirs.at(ii);
138 if (d.absolutePath().endsWith(s))
139 return QStringList();
144 QStringList cppfiles = d.entryList(QStringList() << QLatin1String("*.cpp"), QDir::Files);
145 if (cppfiles.isEmpty()) {
146 QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
148 foreach (const QString &file, files) {
149 if (file.at(0).isLower()) {
150 rv << d.absoluteFilePath(file);
155 QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
157 foreach (const QString &dir, dirs) {
160 rv << findQmlFiles(sub);
167 This test runs all the examples in the declarative UI source tree and ensures
168 that they start and exit cleanly.
170 Examples are any .qml files under the examples/ directory that start
171 with a lower case letter.
173 static void silentErrorsMsgHandler(QtMsgType, const char *)
178 void tst_examples::examples_data()
180 QTest::addColumn<QString>("file");
182 QString examples = QLatin1String(SRCDIR) + "/../../../../examples/declarative/qtquick1";
185 files << findQmlFiles(QDir(examples));
187 foreach (const QString &file, files)
188 QTest::newRow(qPrintable(file)) << file;
191 void tst_examples::examples()
193 QFETCH(QString, file);
195 QDeclarativeViewer viewer;
197 QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
198 QVERIFY(viewer.open(file));
199 qInstallMsgHandler(old);
201 if (viewer.view()->status() == QDeclarativeView::Error)
202 qWarning() << viewer.view()->errors();
204 QCOMPARE(viewer.view()->status(), QDeclarativeView::Ready);
207 QTest::qWaitForWindowShown(&viewer);
210 QTEST_MAIN(tst_examples)
212 #include "tst_examples.moc"