62cc4bb456d844f11fa3941a0cc76d23ea1ba9a7
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternsxqts / tst_suitetest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #ifdef QTEST_XMLPATTERNS
46
47 #include <QProcess>
48 #include "TestSuite.h"
49 #include "TestSuiteResult.h"
50 #include "XMLWriter.h"
51 #include "ExitCode.h"
52 #include "Worker.h"
53 #include "private/qautoptr_p.h"
54 #include "tst_suitetest.h"
55
56 using namespace QPatternistSDK;
57
58 tst_SuiteTest::tst_SuiteTest(const SuiteType suiteType,
59                              const bool alwaysRun) : m_existingBaseline(inputFile(QLatin1String("Baseline.xml")))
60                                                    , m_candidateBaseline(inputFile(QLatin1String("CandidateBaseline.xml")))
61                                                    , m_abortRun(!alwaysRun && !QFile::exists(QLatin1String("runTests")))
62                                                    , m_suiteType(suiteType)
63 {
64 }
65
66 /*!
67  Returns an absolute path to the XQTS catalog, or flags a failure using
68  QTestLib's mechanisms.
69
70  Finding the location of the catalog is done with `p4 where` such that we don't have
71  to care about where it is checked out.
72  */
73 void tst_SuiteTest::initTestCase()
74 {
75     catalogPath(m_catalogPath);
76 }
77
78 /*!
79   Just runs the test suite and writes the result to m_candidateBaseline.
80  */
81 void tst_SuiteTest::runTestSuite() const
82 {
83     if(m_abortRun)
84         QSKIP("The test suite is not available, no tests are run.", SkipAll);
85
86     QString errMsg;
87     const QFileInfo fi(m_catalogPath);
88     const QUrl catalogPath(QUrl::fromLocalFile(fi.absoluteFilePath()));
89
90     TestSuite::SuiteType suiteType;
91     switch (m_suiteType) {
92     case XQuerySuite:
93         suiteType = TestSuite::XQuerySuite;
94         break;
95     case XsltSuite:
96         suiteType = TestSuite::XsltSuite;
97         break;
98     case XsdSuite:
99         suiteType = TestSuite::XsdSuite;
100         break;
101     default:
102         break;
103     }
104
105     TestSuite *const ts = TestSuite::openCatalog(catalogPath, errMsg, true, suiteType);
106
107     QVERIFY2(ts, qPrintable(QString::fromLatin1("Failed to open the catalog, maybe it doesn't exist or is broken: %1").arg(errMsg)));
108
109     /* Run the tests, and serialize the result(as according to XQTSResult.xsd) to standard out. */
110     TestSuiteResult *const result = ts->runSuite();
111     Q_ASSERT(result);
112
113     QFile out(m_candidateBaseline);
114     QVERIFY(out.open(QIODevice::WriteOnly));
115
116     XMLWriter serializer(&out);
117     result->toXML(serializer);
118
119     delete result;
120     delete ts;
121 }
122
123 void tst_SuiteTest::checkTestSuiteResult() const
124 {
125     if(m_abortRun)
126         QSKIP("This test takes too long time to run on the majority of platforms.", SkipAll);
127
128     typedef QList<QFileInfo> QFileInfoList;
129
130     const QFileInfo baseline(m_existingBaseline);
131     const QFileInfo result(m_candidateBaseline);
132     QFileInfoList list;
133     list.append(baseline);
134     list.append(result);
135
136     const QFileInfoList::const_iterator end(list.constEnd());
137
138     QEventLoop eventLoop;
139     const QPatternist::AutoPtr<Worker> worker(new Worker(eventLoop, m_existingBaseline, result));
140
141     /* Passed to ResultThreader so it knows what kind of file it is handling. */
142     ResultThreader::Type type = ResultThreader::Baseline;
143
144     for(QFileInfoList::const_iterator it(list.constBegin()); it != end; ++it)
145     {
146         QFileInfo i(*it);
147         i.makeAbsolute();
148
149         QVERIFY2(i.exists(), qPrintable(QString::fromLatin1("File %1 does not exist.")
150                                                             .arg(i.fileName())));
151
152         QFile *const file = new QFile(i.absoluteFilePath(), worker.data());
153
154         QVERIFY2(file->open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Could not open file %1 for reading.")
155                                                              .arg(i.fileName())));
156
157         ResultThreader *handler = new ResultThreader(eventLoop, file, type, worker.data());
158
159         QObject::connect(handler, SIGNAL(finished()), worker.data(), SLOT(threadFinished()));
160
161         handler->start(); /* Start the thread. It now parses the file
162                              and emits threadFinished() when done. */
163         type = ResultThreader::Result;
164     }
165
166     const int exitCode = eventLoop.exec();
167
168     QCOMPARE(exitCode, 0);
169 }
170
171 bool tst_SuiteTest::dontRun() const
172 {
173     return m_abortRun;
174 }
175 #endif
176
177
178 // vim: et:ts=4:sw=4:sts=4