summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2010-12-10 10:10:04 +1000
committerRhys Weatherley <rhys.weatherley@nokia.com>2010-12-10 10:10:04 +1000
commit0644fe7814930cb11f0fc2749b01beff86838bfd (patch)
treeb154e6bf8a88a9e836b6ac88d1f55cf933b785f4
parent200a8ac92afa0d8c5ff54c929a345dd3e05d3dd7 (diff)
Handle import paths in the test harness
The -import option can be supplied to the test harness to add extra import directories.
-rw-r--r--doc/testcases.txt6
-rw-r--r--features/qmltestcase.prf7
-rw-r--r--src/quicktestlib/quicktest.cpp37
3 files changed, 49 insertions, 1 deletions
diff --git a/doc/testcases.txt b/doc/testcases.txt
index 3ba44bc..5c6282a 100644
--- a/doc/testcases.txt
+++ b/doc/testcases.txt
@@ -73,6 +73,12 @@ See "tests/qmlexample" for an example of creating a test harness
that uses resources and "tests/qmlauto" for an example that uses
the QUICK_TEST_SOURCE_DIR macro.
+If your test case needs QML imports, then you can add them as
+"-import" options to the the test program command-line by adding
+the following line to your .pro file:
+
+ IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
+
Basic test cases
================
diff --git a/features/qmltestcase.prf b/features/qmltestcase.prf
index d5cbedc..de3c641 100644
--- a/features/qmltestcase.prf
+++ b/features/qmltestcase.prf
@@ -15,3 +15,10 @@ win32:CONFIG(debug, debug|release) {
} else {
LIBS += -lQtQuickTest$${QT_LIBINFIX}
}
+
+# If the .pro file specified an IMPORTPATH, then add that to
+# the command-line when the test is run.
+!isEmpty(IMPORTPATH) {
+ load(testcase)
+ for(import, IMPORTPATH): check.commands += -import \"$$import\"
+}
diff --git a/src/quicktestlib/quicktest.cpp b/src/quicktestlib/quicktest.cpp
index d4e9fe3..29eaa2d 100644
--- a/src/quicktestlib/quicktest.cpp
+++ b/src/quicktestlib/quicktest.cpp
@@ -96,13 +96,46 @@ private:
bool m_windowShown;
};
+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)
{
QApplication app(argc, argv);
+ // Look for QML-specific command-line options.
+ // -import dir Specify an import directory.
+ // -input dir Specify the input directory for test cases.
+ QStringList imports;
+ QString testPath;
+ 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 (outargc != index) {
+ argv[outargc++] = argv[index++];
+ } else {
+ ++outargc;
+ ++index;
+ }
+ }
+ argv[outargc] = 0;
+ argc = outargc;
+
// Determine where to look for the test data. If QUICK_TEST_SOURCE_DIR
// is set, then use that. Otherwise scan the application's resources.
- QString testPath = QString::fromLocal8Bit(qgetenv("QUICK_TEST_SOURCE_DIR"));
+ if (testPath.isEmpty())
+ testPath = QString::fromLocal8Bit(qgetenv("QUICK_TEST_SOURCE_DIR"));
if (testPath.isEmpty() && sourceDir)
testPath = QString::fromLocal8Bit(sourceDir);
if (testPath.isEmpty())
@@ -154,6 +187,8 @@ int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport
= engine->globalObject().property(QLatin1String("Qt"));
qtObject.setProperty
(QLatin1String("qtest_wrapper"), engine->newVariant(true));
+ foreach (QString path, imports)
+ view.engine()->addImportPath(path);
QString path = fi.absoluteFilePath();
if (path.startsWith(QLatin1String(":/")))
view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));