2011-05-20 01:57:29 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
** All rights reserved.
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
2011-07-07 12:52:03 +00:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this
|
|
|
|
** file. Please review the following information to ensure the GNU Lesser
|
|
|
|
** General Public License version 2.1 requirements will be met:
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-05-20 01:57:29 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-07-07 12:52:03 +00:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-05-20 01:57:29 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-07-07 12:52:03 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU General
|
|
|
|
** Public License version 3.0 as published by the Free Software Foundation
|
|
|
|
** and appearing in the file LICENSE.GPL included in the packaging of this
|
|
|
|
** file. Please review the following information to ensure the GNU General
|
|
|
|
** Public License version 3.0 requirements will be met:
|
|
|
|
** http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** Other Usage
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
2011-05-20 01:57:29 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "quicktest.h"
|
|
|
|
#include "quicktestresult_p.h"
|
|
|
|
#include <QtTest/qtestsystem.h>
|
|
|
|
#include "qtestoptions_p.h"
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QtDeclarative/qdeclarative.h>
|
2011-07-11 03:47:51 +00:00
|
|
|
#include <QtQuick1/qdeclarativeview.h>
|
2011-05-20 01:57:29 +00:00
|
|
|
#include <QtDeclarative/qdeclarativeengine.h>
|
|
|
|
#include <QtDeclarative/qdeclarativecontext.h>
|
|
|
|
#if defined(QML_VERSION) && QML_VERSION >= 0x020000
|
2011-10-14 08:51:42 +00:00
|
|
|
#include <QtDeclarative/qquickview.h>
|
2011-05-20 01:57:29 +00:00
|
|
|
#define QUICK_TEST_SCENEGRAPH 1
|
|
|
|
#endif
|
2011-07-29 08:25:44 +00:00
|
|
|
#include <QtDeclarative/qjsvalue.h>
|
|
|
|
#include <QtDeclarative/qjsengine.h>
|
2011-08-25 11:40:12 +00:00
|
|
|
#include <QtGui/qopengl.h>
|
2011-05-20 01:57:29 +00:00
|
|
|
#include <QtCore/qurl.h>
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qdir.h>
|
|
|
|
#include <QtCore/qdiriterator.h>
|
|
|
|
#include <QtCore/qfile.h>
|
|
|
|
#include <QtCore/qdebug.h>
|
|
|
|
#include <QtCore/qeventloop.h>
|
|
|
|
#include <QtGui/qtextdocument.h>
|
|
|
|
#include <stdio.h>
|
2011-09-09 12:43:33 +00:00
|
|
|
#include <QtGui/QGuiApplication>
|
2011-10-27 06:55:43 +00:00
|
|
|
#include <QtCore/QTranslator>
|
2011-05-20 01:57:29 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
class QTestRootObject : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
|
2011-10-24 00:44:16 +00:00
|
|
|
Q_PROPERTY(bool hasTestCase READ hasTestCase WRITE setHasTestCase NOTIFY hasTestCaseChanged)
|
2011-05-20 01:57:29 +00:00
|
|
|
public:
|
|
|
|
QTestRootObject(QObject *parent = 0)
|
2011-10-28 08:24:28 +00:00
|
|
|
: QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false) {}
|
2011-05-20 01:57:29 +00:00
|
|
|
|
2011-10-24 00:44:16 +00:00
|
|
|
bool hasQuit:1;
|
|
|
|
bool hasTestCase() const { return m_hasTestCase; }
|
|
|
|
void setHasTestCase(bool value) { m_hasTestCase = value; emit hasTestCaseChanged(); }
|
2011-05-20 01:57:29 +00:00
|
|
|
|
|
|
|
bool windowShown() const { return m_windowShown; }
|
|
|
|
void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void windowShownChanged();
|
2011-10-24 00:44:16 +00:00
|
|
|
void hasTestCaseChanged();
|
2011-05-20 01:57:29 +00:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void quit() { hasQuit = true; }
|
|
|
|
|
|
|
|
private:
|
2011-10-24 00:44:16 +00:00
|
|
|
bool m_windowShown : 1;
|
|
|
|
bool m_hasTestCase :1;
|
2011-05-20 01:57:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline QString stripQuotes(const QString &s)
|
|
|
|
{
|
|
|
|
if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
|
|
|
|
return s.mid(1, s.length() - 2);
|
|
|
|
else
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
|
|
|
|
{
|
2011-09-09 12:43:33 +00:00
|
|
|
QGuiApplication* app = 0;
|
2011-08-07 23:45:52 +00:00
|
|
|
if (!QCoreApplication::instance()) {
|
2011-09-09 12:43:33 +00:00
|
|
|
app = new QGuiApplication(argc, argv);
|
2011-08-07 23:45:52 +00:00
|
|
|
}
|
2011-05-20 01:57:29 +00:00
|
|
|
|
|
|
|
// Look for QML-specific command-line options.
|
|
|
|
// -import dir Specify an import directory.
|
|
|
|
// -input dir Specify the input directory for test cases.
|
|
|
|
// -qtquick1 Run with QtQuick 1 rather than QtQuick 2.
|
2011-10-27 06:55:43 +00:00
|
|
|
// -translation file Specify the translation file.
|
2011-05-20 01:57:29 +00:00
|
|
|
QStringList imports;
|
|
|
|
QString testPath;
|
2011-10-27 06:55:43 +00:00
|
|
|
QString translationFile;
|
2011-05-20 01:57:29 +00:00
|
|
|
bool qtQuick2 = true;
|
|
|
|
int outargc = 1;
|
|
|
|
int index = 1;
|
|
|
|
while (index < argc) {
|
|
|
|
if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
|
|
|
|
imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
|
|
|
|
index += 2;
|
|
|
|
} else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
|
|
|
|
testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
|
|
|
|
index += 2;
|
|
|
|
} else if (strcmp(argv[index], "-opengl") == 0) {
|
|
|
|
++index;
|
|
|
|
} else if (strcmp(argv[index], "-qtquick1") == 0) {
|
|
|
|
qtQuick2 = false;
|
|
|
|
++index;
|
2011-10-27 06:55:43 +00:00
|
|
|
} else if (strcmp(argv[index], "-translation") == 0 && (index + 1) < argc) {
|
|
|
|
translationFile = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
|
|
|
|
index += 2;
|
2011-05-20 01:57:29 +00:00
|
|
|
} else if (outargc != index) {
|
|
|
|
argv[outargc++] = argv[index++];
|
|
|
|
} else {
|
|
|
|
++outargc;
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv[outargc] = 0;
|
|
|
|
argc = outargc;
|
|
|
|
|
2011-08-08 01:35:48 +00:00
|
|
|
// Parse the command-line arguments.
|
|
|
|
QuickTestResult::parseArgs(argc, argv);
|
|
|
|
QuickTestResult::setProgramName(name);
|
|
|
|
|
2011-10-27 06:55:43 +00:00
|
|
|
QTranslator translator;
|
|
|
|
if (!translationFile.isEmpty()) {
|
|
|
|
if (translator.load(translationFile)) {
|
|
|
|
app->installTranslator(&translator);
|
|
|
|
} else {
|
|
|
|
qWarning() << "Could not load the translation file" << translationFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 01:57:29 +00:00
|
|
|
// Determine where to look for the test data.
|
|
|
|
if (testPath.isEmpty() && sourceDir)
|
|
|
|
testPath = QString::fromLocal8Bit(sourceDir);
|
|
|
|
if (testPath.isEmpty())
|
|
|
|
testPath = QLatin1String(".");
|
|
|
|
|
|
|
|
QStringList files;
|
2011-10-10 01:30:15 +00:00
|
|
|
|
|
|
|
if (testPath.endsWith(QLatin1String(".qml")) && QFileInfo(testPath).isFile()) {
|
|
|
|
files << testPath;
|
|
|
|
} else {
|
|
|
|
// Scan the test data directory recursively, looking for "tst_*.qml" files.
|
|
|
|
QStringList filters;
|
|
|
|
filters += QLatin1String("tst_*.qml");
|
|
|
|
QDirIterator iter(testPath, filters, QDir::Files,
|
|
|
|
QDirIterator::Subdirectories |
|
|
|
|
QDirIterator::FollowSymlinks);
|
|
|
|
while (iter.hasNext())
|
|
|
|
files += iter.next();
|
|
|
|
files.sort();
|
|
|
|
}
|
2011-05-20 01:57:29 +00:00
|
|
|
|
|
|
|
// Bail out if we didn't find any test cases.
|
|
|
|
if (files.isEmpty()) {
|
|
|
|
qWarning() << argv[0] << ": could not find any test cases under"
|
|
|
|
<< testPath;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Scan through all of the "tst_*.qml" files and run each of them
|
|
|
|
// in turn with a QDeclarativeView.
|
|
|
|
#ifdef QUICK_TEST_SCENEGRAPH
|
|
|
|
if (qtQuick2) {
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickView view;
|
2011-10-14 03:07:37 +00:00
|
|
|
QTestRootObject rootobj;
|
|
|
|
QEventLoop eventLoop;
|
|
|
|
QObject::connect(view.engine(), SIGNAL(quit()),
|
|
|
|
&rootobj, SLOT(quit()));
|
|
|
|
QObject::connect(view.engine(), SIGNAL(quit()),
|
|
|
|
&eventLoop, SLOT(quit()));
|
|
|
|
view.rootContext()->setContextProperty
|
|
|
|
(QLatin1String("qtest"), &rootobj);
|
|
|
|
foreach (QString path, imports)
|
|
|
|
view.engine()->addImportPath(path);
|
|
|
|
|
2011-05-20 01:57:29 +00:00
|
|
|
foreach (QString file, files) {
|
|
|
|
QFileInfo fi(file);
|
|
|
|
if (!fi.exists())
|
|
|
|
continue;
|
2011-10-14 03:07:37 +00:00
|
|
|
|
2011-10-24 00:44:16 +00:00
|
|
|
rootobj.setHasTestCase(false);
|
|
|
|
|
2011-05-20 01:57:29 +00:00
|
|
|
QString path = fi.absoluteFilePath();
|
|
|
|
if (path.startsWith(QLatin1String(":/")))
|
|
|
|
view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
|
|
|
|
else
|
|
|
|
view.setSource(QUrl::fromLocalFile(path));
|
2011-10-24 00:44:16 +00:00
|
|
|
|
2011-05-20 01:57:29 +00:00
|
|
|
if (QTest::printAvailableFunctions)
|
|
|
|
continue;
|
2011-10-14 08:51:42 +00:00
|
|
|
if (view.status() == QQuickView::Error) {
|
2011-05-20 01:57:29 +00:00
|
|
|
// Error compiling the test - flag failure in the log and continue.
|
|
|
|
QList<QDeclarativeError> errors = view.errors();
|
|
|
|
QuickTestResult results;
|
|
|
|
results.setTestCaseName(fi.baseName());
|
|
|
|
results.startLogging();
|
|
|
|
results.setFunctionName(QLatin1String("compile"));
|
|
|
|
results.setFunctionType(QuickTestResult::Func);
|
|
|
|
results.fail(errors.at(0).description(),
|
|
|
|
errors.at(0).url().toString(),
|
|
|
|
errors.at(0).line());
|
|
|
|
results.finishTestFunction();
|
|
|
|
results.setFunctionName(QString());
|
|
|
|
results.setFunctionType(QuickTestResult::NoWhere);
|
|
|
|
results.stopLogging();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!rootobj.hasQuit) {
|
|
|
|
// If the test already quit, then it was performed
|
|
|
|
// synchronously during setSource(). Otherwise it is
|
|
|
|
// an asynchronous test and we need to show the window
|
|
|
|
// and wait for the quit indication.
|
|
|
|
view.show();
|
2011-10-14 03:07:37 +00:00
|
|
|
QTest::qWaitForWindowShown(&view);
|
2011-05-20 01:57:29 +00:00
|
|
|
rootobj.setWindowShown(true);
|
2011-10-24 00:44:16 +00:00
|
|
|
if (!rootobj.hasQuit && rootobj.hasTestCase())
|
2011-05-20 01:57:29 +00:00
|
|
|
eventLoop.exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
foreach (QString file, files) {
|
|
|
|
QFileInfo fi(file);
|
|
|
|
if (!fi.exists())
|
|
|
|
continue;
|
|
|
|
QDeclarativeView view;
|
|
|
|
QTestRootObject rootobj;
|
|
|
|
QEventLoop eventLoop;
|
|
|
|
QObject::connect(view.engine(), SIGNAL(quit()),
|
|
|
|
&rootobj, SLOT(quit()));
|
|
|
|
QObject::connect(view.engine(), SIGNAL(quit()),
|
|
|
|
&eventLoop, SLOT(quit()));
|
|
|
|
if (createViewport)
|
|
|
|
view.setViewport((*createViewport)());
|
|
|
|
view.rootContext()->setContextProperty
|
|
|
|
(QLatin1String("qtest"), &rootobj);
|
|
|
|
foreach (QString path, imports)
|
|
|
|
view.engine()->addImportPath(path);
|
|
|
|
QString path = fi.absoluteFilePath();
|
|
|
|
if (path.startsWith(QLatin1String(":/")))
|
|
|
|
view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
|
|
|
|
else
|
|
|
|
view.setSource(QUrl::fromLocalFile(path));
|
|
|
|
if (QTest::printAvailableFunctions)
|
|
|
|
continue;
|
|
|
|
if (view.status() == QDeclarativeView::Error) {
|
|
|
|
// Error compiling the test - flag failure in the log and continue.
|
|
|
|
QList<QDeclarativeError> errors = view.errors();
|
|
|
|
QuickTestResult results;
|
|
|
|
results.setTestCaseName(fi.baseName());
|
|
|
|
results.startLogging();
|
|
|
|
results.setFunctionName(QLatin1String("compile"));
|
|
|
|
results.setFunctionType(QuickTestResult::Func);
|
|
|
|
results.fail(errors.at(0).description(),
|
|
|
|
errors.at(0).url().toString(),
|
|
|
|
errors.at(0).line());
|
|
|
|
results.finishTestFunction();
|
|
|
|
results.setFunctionName(QString());
|
|
|
|
results.setFunctionType(QuickTestResult::NoWhere);
|
|
|
|
results.stopLogging();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!rootobj.hasQuit) {
|
|
|
|
// If the test already quit, then it was performed
|
|
|
|
// synchronously during setSource(). Otherwise it is
|
|
|
|
// an asynchronous test and we need to show the window
|
|
|
|
// and wait for the quit indication.
|
|
|
|
view.show();
|
|
|
|
QTest::qWaitForWindowShown(&view);
|
|
|
|
rootobj.setWindowShown(true);
|
|
|
|
if (!rootobj.hasQuit)
|
|
|
|
eventLoop.exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the current logging stream.
|
|
|
|
QuickTestResult::setProgramName(0);
|
|
|
|
|
2011-08-07 23:45:52 +00:00
|
|
|
delete app;
|
2011-05-20 01:57:29 +00:00
|
|
|
// Return the number of failures as the exit code.
|
|
|
|
return QuickTestResult::exitCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#include "quicktest.moc"
|