From f982ee4b3236cfeeb7aa567fd6d812316d7c2092 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Mon, 6 Dec 2010 12:40:27 +1000 Subject: Load test cases out of qrc resources --- doc/testcases.txt | 17 +++--- src/quicktestlib/qdeclarativetest.cpp | 110 ++++++++++++++++++---------------- src/quicktestlib/qdeclarativetest.h | 26 +------- tests/qmlexample/qmlexample.pro | 2 +- tests/qmlexample/qmlexample.qrc | 6 ++ 5 files changed, 77 insertions(+), 84 deletions(-) create mode 100644 tests/qmlexample/qmlexample.qrc diff --git a/doc/testcases.txt b/doc/testcases.txt index b012540..f515a9d 100644 --- a/doc/testcases.txt +++ b/doc/testcases.txt @@ -11,16 +11,19 @@ the following code: Where "qmlexample" is an identifier to use to uniquely identify this set of tests. -The test harness looks for "tst_*.qml" files in subdirectories under -the application's source directory, which is specified by the -QTEST_QUICK_SOURCE_DIR macro in the *.pro file: +The test harness scans recursively for "tst_*.qml" files in the qrc +resources that are bound into the test harness binary. The following +is an example .qrc file: - DEFINES += QTEST_QUICK_SOURCE_DIR=\"\\\"$$PWD\\\"\" + + + tst_basic.qml + tst_item.qml + + The QTEST_QUICK_SOURCE_DIR environment variable can also be set -at runtime to override the compiled-in default. This variable will -usually need to be set when the tests are run on a target device -where the directory names are different from the host system. +at runtime to run test cases from a non-resource directory. Other *.qml files may appear for auxillary QML components that are used by the test. diff --git a/src/quicktestlib/qdeclarativetest.cpp b/src/quicktestlib/qdeclarativetest.cpp index 67e8d70..3eecb38 100644 --- a/src/quicktestlib/qdeclarativetest.cpp +++ b/src/quicktestlib/qdeclarativetest.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -70,68 +71,71 @@ private Q_SLOTS: void quit() { hasQuit = true; } }; -int qtest_quick_main(int argc, char **argv, const char *name, const char *sourceDir, qtest_create_viewport createViewport) +int qtest_quick_main(int argc, char **argv, const char *name, qtest_create_viewport createViewport) { QApplication app(argc, argv); - // Parse the command-line arguments. - QDeclarativeTestResult::parseArgs(argc, argv); - QDeclarativeTestResult::setProgramName(name); - - // Determine where to look for the test data. On a device it will - // typically be necessary to set QTEST_QUICK_SOURCE_DIR. + // Determine where to look for the test data. If QTEST_QUICK_SOURCE_DIR + // is set, then use that. Otherwise scan the application's resources. QString testPath = QString::fromLocal8Bit(qgetenv("QTEST_QUICK_SOURCE_DIR")); - if (testPath.isEmpty() && sourceDir) - testPath = QString::fromLocal8Bit(sourceDir); - if (!testPath.isEmpty() && !QFile::exists(testPath)) - testPath = QString(); if (testPath.isEmpty()) - testPath = QLatin1String("."); + testPath = QLatin1String(":/"); - // Find the subdirectories that look like they may contain test cases. - // We also include "." in this list. - QDir dir(testPath); - QStringList entries = dir.entryList(QDir::Dirs); - entries.removeAll(QLatin1String("..")); - if (!entries.contains(QLatin1String("."))) - entries.append(QLatin1String(".")); - - // Scan through all of the "tst_*.qml" files in the subdirectories - // and run each of them in turn with a QDeclarativeView. + // Scan the test data directory recursively, looking for "tst_*.qml" files. QStringList filters; filters += QLatin1String("tst_*.qml"); + QStringList files; + QDirIterator iter(testPath, filters, QDir::Files, + QDirIterator::Subdirectories | + QDirIterator::FollowSymlinks); + while (iter.hasNext()) + files += iter.next(); + files.sort(); + + // Bail out if we didn't find any test cases. + if (files.isEmpty()) { + qWarning() << argv[1] << ": could not find any test cases under" + << testPath; + return 1; + } + + // Parse the command-line arguments. + QDeclarativeTestResult::parseArgs(argc, argv); + QDeclarativeTestResult::setProgramName(name); + + // Scan through all of the "tst_*.qml" files and run each of them + // in turn with a QDeclarativeView. bool compileFail = false; - foreach (QString name, entries) { - QDir subdir(testPath + QDir::separator() + name); - QStringList files = subdir.entryList(filters, QDir::Files); - foreach (QString file, files) { - QString source = subdir.path() + QDir::separator() + file; - QFileInfo fi(source); - if (fi.exists()) { - QDeclarativeView view; - QTestQuitObject quitobj; - QEventLoop eventLoop; - QObject::connect(view.engine(), SIGNAL(quit()), - &quitobj, SLOT(quit())); - QObject::connect(view.engine(), SIGNAL(quit()), - &eventLoop, SLOT(quit())); - if (createViewport) - view.setViewport((*createViewport)()); - view.setSource(QUrl::fromLocalFile(fi.absoluteFilePath())); - if (view.status() == QDeclarativeView::Error) { - // Error compiling the test - flag failure and continue. - compileFail = true; - continue; - } - if (!quitobj.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(); - eventLoop.exec(); - } - } + foreach (QString file, files) { + QFileInfo fi(file); + if (!fi.exists()) + continue; + QDeclarativeView view; + QTestQuitObject quitobj; + QEventLoop eventLoop; + QObject::connect(view.engine(), SIGNAL(quit()), + &quitobj, SLOT(quit())); + QObject::connect(view.engine(), SIGNAL(quit()), + &eventLoop, SLOT(quit())); + if (createViewport) + view.setViewport((*createViewport)()); + QString path = fi.absoluteFilePath(); + if (path.startsWith(QLatin1String(":/"))) + view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2))); + else + view.setSource(QUrl::fromLocalFile(path)); + if (view.status() == QDeclarativeView::Error) { + // Error compiling the test - flag failure and continue. + compileFail = true; + continue; + } + if (!quitobj.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(); + eventLoop.exec(); } } diff --git a/src/quicktestlib/qdeclarativetest.h b/src/quicktestlib/qdeclarativetest.h index 643653f..ef9bb81 100644 --- a/src/quicktestlib/qdeclarativetest.h +++ b/src/quicktestlib/qdeclarativetest.h @@ -52,14 +52,12 @@ QT_BEGIN_NAMESPACE typedef QWidget *(*qtest_create_viewport)(); -Q_TEST_QUICK_EXPORT int qtest_quick_main(int argc, char **argv, const char *name, const char *sourceDir, qtest_create_viewport createViewport); - -#ifndef QTEST_QUICK_SOURCE_DIR +Q_TEST_QUICK_EXPORT int qtest_quick_main(int argc, char **argv, const char *name, qtest_create_viewport createViewport); #define QTEST_QUICK_MAIN(name) \ int main(int argc, char **argv) \ { \ - return qtest_quick_main(argc, argv, #name, 0, 0); \ + return qtest_quick_main(argc, argv, #name, 0); \ } #define QTEST_QUICK_OPENGL_MAIN(name) \ @@ -69,29 +67,11 @@ Q_TEST_QUICK_EXPORT int qtest_quick_main(int argc, char **argv, const char *name } \ int main(int argc, char **argv) \ { \ - return qtest_quick_main(argc, argv, #name, 0, name##_create_viewport); \ + return qtest_quick_main(argc, argv, #name, name##_create_viewport); \ } #else -#define QTEST_QUICK_MAIN(name) \ - int main(int argc, char **argv) \ - { \ - return qtest_quick_main(argc, argv, #name, QTEST_QUICK_SOURCE_DIR, 0); \ - } - -#define QTEST_QUICK_OPENGL_MAIN(name) \ - static QWidget *name##_create_viewport() \ - { \ - return new QGLWidget(); \ - } \ - int main(int argc, char **argv) \ - { \ - return qtest_quick_main(argc, argv, #name, QTEST_QUICK_SOURCE_DIR, name##_create_viewport); \ - } - -#endif - QT_END_NAMESPACE #endif diff --git a/tests/qmlexample/qmlexample.pro b/tests/qmlexample/qmlexample.pro index de4f557..502d16a 100644 --- a/tests/qmlexample/qmlexample.pro +++ b/tests/qmlexample/qmlexample.pro @@ -2,5 +2,5 @@ TEMPLATE=app TARGET=tst_qmlexample CONFIG += warn_on testcase SOURCES += tst_qmlexample.cpp -DEFINES += QTEST_QUICK_SOURCE_DIR=\"\\\"$$PWD\\\"\" +RESOURCES += qmlexample.qrc include(../../src/quicktestlib/quicktestlib_dep.pri) diff --git a/tests/qmlexample/qmlexample.qrc b/tests/qmlexample/qmlexample.qrc new file mode 100644 index 0000000..ee7c380 --- /dev/null +++ b/tests/qmlexample/qmlexample.qrc @@ -0,0 +1,6 @@ + + + tst_basic.qml + tst_item.qml + + -- cgit v1.2.3