Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / examples / tst_examples.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QLibraryInfo>
44 #include <QDir>
45 #include <QProcess>
46 #include <QDebug>
47 #include "qmlruntime.h"
48 #include <QDeclarativeView>
49 #include <QDeclarativeError>
50
51 class tst_examples : public QObject
52 {
53     Q_OBJECT
54 public:
55     tst_examples();
56
57 private slots:
58     void examples_data();
59     void examples();
60
61     void namingConvention();
62 private:
63     QStringList excludedDirs;
64
65     void namingConvention(const QDir &);
66     QStringList findQmlFiles(const QDir &);
67 };
68
69 tst_examples::tst_examples()
70 {
71     // Add directories you want excluded here
72
73 #ifdef QT_NO_WEBKIT
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";
78 #endif
79
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";
85 #endif
86 }
87
88 /*
89 This tests that the examples follow the naming convention required
90 to have them tested by the examples() test.
91 */
92 void tst_examples::namingConvention(const QDir &d)
93 {
94     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
95         QString s = excludedDirs.at(ii);
96         if (d.absolutePath().endsWith(s))
97             return;
98     }
99
100     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
101                                     QDir::Files);
102
103     bool seenQml = !files.isEmpty();
104     bool seenLowercase = false;
105
106     foreach (const QString &file, files) {
107         if (file.at(0).isLower())
108             seenLowercase = true;
109     }
110
111     if (!seenQml) {
112         QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
113                 QDir::NoSymLinks);
114         foreach (const QString &dir, dirs) {
115             QDir sub = d;
116             sub.cd(dir);
117             namingConvention(sub);
118         }
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(","))));
124     }
125 }
126
127 void tst_examples::namingConvention()
128 {
129     QString examples = QLibraryInfo::location(QLibraryInfo::ExamplesPath);
130
131     namingConvention(QDir(examples));
132 }
133
134 QStringList tst_examples::findQmlFiles(const QDir &d)
135 {
136     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
137         QString s = excludedDirs.at(ii);
138         if (d.absolutePath().endsWith(s))
139             return QStringList();
140     }
141
142     QStringList rv;
143
144     QStringList cppfiles = d.entryList(QStringList() << QLatin1String("*.cpp"), QDir::Files);
145     if (cppfiles.isEmpty()) {
146         QStringList files = d.entryList(QStringList() << QLatin1String("*.qml"),
147                                         QDir::Files);
148         foreach (const QString &file, files) {
149             if (file.at(0).isLower()) {
150                 rv << d.absoluteFilePath(file);
151             }
152         }
153     }
154
155     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
156                                    QDir::NoSymLinks);
157     foreach (const QString &dir, dirs) {
158         QDir sub = d;
159         sub.cd(dir);
160         rv << findQmlFiles(sub);
161     }
162
163     return rv;
164 }
165
166 /*
167 This test runs all the examples in the declarative UI source tree and ensures
168 that they start and exit cleanly.
169
170 Examples are any .qml files under the examples/ directory that start
171 with a lower case letter.
172 */
173 static void silentErrorsMsgHandler(QtMsgType, const char *)
174 {
175 }
176
177
178 void tst_examples::examples_data()
179 {
180     QTest::addColumn<QString>("file");
181
182     QString examples = QLatin1String(SRCDIR) + "/../../../../examples/declarative/qtquick1";
183
184     QStringList files;
185     files << findQmlFiles(QDir(examples));
186
187     foreach (const QString &file, files)
188         QTest::newRow(qPrintable(file)) << file;
189 }
190
191 void tst_examples::examples()
192 {
193     QFETCH(QString, file);
194
195     QDeclarativeViewer viewer;
196
197     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
198     QVERIFY(viewer.open(file));
199     qInstallMsgHandler(old);
200
201     if (viewer.view()->status() == QDeclarativeView::Error)
202         qWarning() << viewer.view()->errors();
203
204     QCOMPARE(viewer.view()->status(), QDeclarativeView::Ready);
205     viewer.show();
206
207     QTest::qWaitForWindowShown(&viewer);
208 }
209
210 QTEST_MAIN(tst_examples)
211
212 #include "tst_examples.moc"