Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / tests / auto / xmlpatternssdk / XSDTSTestCase.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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 autotests of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QColor>
43 #include <QFile>
44 #include <QFileInfo>
45 #include <QVariant>
46 #include <QtDebug>
47
48 #include "XSDTSTestCase.h"
49
50 #include "qxmlschema.h"
51 #include "qxmlschemavalidator.h"
52
53 using namespace QPatternistSDK;
54 using namespace QPatternist;
55
56 XSDTSTestCase::XSDTSTestCase(const Scenario scen, TreeItem *p, TestType testType)
57                             : m_scenario(scen)
58                             , m_parent(p)
59                             , m_testType(testType)
60 {
61 }
62
63 XSDTSTestCase::~XSDTSTestCase()
64 {
65     qDeleteAll(m_baseLines);
66 }
67
68 TestResult::List XSDTSTestCase::execute(const ExecutionStage, TestSuite*)
69 {
70     ErrorHandler errHandler;
71     ErrorHandler::installQtMessageHandler(&errHandler);
72
73     TestResult::List retval;
74     TestResult::Status resultStatus = TestResult::Unknown;
75     QString serialized;
76
77     if (m_testType == SchemaTest) {
78         executeSchemaTest(resultStatus, serialized, &errHandler);
79     } else {
80         executeInstanceTest(resultStatus, serialized, &errHandler);
81     }
82
83     resultStatus = TestBaseLine::scan(serialized, baseLines());
84     Q_ASSERT(resultStatus != TestResult::Unknown);
85
86     m_result = new TestResult(name(), resultStatus, 0, errHandler.messages(),
87                               QPatternist::Item::List(), serialized);
88     retval.append(m_result);
89     ErrorHandler::installQtMessageHandler(0);
90     changed(this);
91     return retval;
92 }
93
94 void XSDTSTestCase::executeSchemaTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler)
95 {
96     QFile file(m_schemaUri.path());
97     if (!file.open(QIODevice::ReadOnly)) {
98         resultStatus = TestResult::Fail;
99         serialized = QString();
100         return;
101     }
102
103     QXmlSchema schema;
104     schema.setMessageHandler(handler);
105     schema.load(&file, m_schemaUri);
106
107     if (schema.isValid()) {
108         resultStatus = TestResult::Pass;
109         serialized = QString::fromLatin1("true");
110     } else {
111         resultStatus = TestResult::Pass;
112         serialized = QString::fromLatin1("false");
113     }
114 }
115
116 void XSDTSTestCase::executeInstanceTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler)
117 {
118     QFile instanceFile(m_instanceUri.path());
119     if (!instanceFile.open(QIODevice::ReadOnly)) {
120         resultStatus = TestResult::Fail;
121         serialized = QString();
122         return;
123     }
124
125     QXmlSchema schema;
126     if (m_schemaUri.isValid()) {
127         QFile file(m_schemaUri.path());
128         if (!file.open(QIODevice::ReadOnly)) {
129             resultStatus = TestResult::Fail;
130             serialized = QString();
131             return;
132         }
133
134         schema.setMessageHandler(handler);
135         schema.load(&file, m_schemaUri);
136
137         if (!schema.isValid()) {
138             resultStatus = TestResult::Pass;
139             serialized = QString::fromLatin1("false");
140             return;
141         }
142     }
143
144     QXmlSchemaValidator validator(schema);
145     validator.setMessageHandler(handler);
146
147     qDebug("check %s", qPrintable(m_instanceUri.path()));
148     if (validator.validate(&instanceFile, m_instanceUri)) {
149         resultStatus = TestResult::Pass;
150         serialized = QString::fromLatin1("true");
151     } else {
152         resultStatus = TestResult::Pass;
153         serialized = QString::fromLatin1("false");
154     }
155 }
156
157 QVariant XSDTSTestCase::data(const Qt::ItemDataRole role, int column) const
158 {
159     if(role == Qt::DisplayRole)
160     {
161         if(column == 0)
162             return title();
163
164         const TestResult *const tr = testResult();
165         if(!tr)
166         {
167             if(column == 1)
168                 return TestResult::displayName(TestResult::NotTested);
169             else
170                 return QString();
171         }
172         const TestResult::Status status = tr->status();
173
174         switch(column)
175         {
176             case 1:
177                 return status == TestResult::Pass ? QString(QChar::fromLatin1('1'))
178                                                   : QString(QChar::fromLatin1('0'));
179             case 2:
180                 return status == TestResult::Fail ? QString(QChar::fromLatin1('1'))
181                                                   : QString(QChar::fromLatin1('0'));
182             default:
183                 return QString();
184         }
185     }
186
187     if(role != Qt::BackgroundRole)
188         return QVariant();
189
190     const TestResult *const tr = testResult();
191
192     if(!tr)
193     {
194         if(column == 0)
195             return Qt::yellow;
196         else
197             return QVariant();
198     }
199
200     const TestResult::Status status = tr->status();
201
202     if(status == TestResult::NotTested || status == TestResult::Unknown)
203         return Qt::yellow;
204
205     switch(column)
206     {
207         case 1:
208             return status == TestResult::Pass ? Qt::green : QVariant();
209         case 2:
210             return status == TestResult::Fail ? Qt::red : QVariant();
211         default:
212             return QVariant();
213     }
214 }
215
216 QString XSDTSTestCase::sourceCode(bool &ok) const
217 {
218     QFile file((m_testType == SchemaTest ? m_schemaUri : m_instanceUri).toLocalFile());
219
220     QString err;
221
222     if(!file.exists())
223         err = QString::fromLatin1("Error: %1 does not exist.").arg(file.fileName());
224     else if(!QFileInfo(file.fileName()).isFile())
225         err = QString::fromLatin1("Error: %1 is not a file, cannot display it.").arg(file.fileName());
226     else if(!file.open(QIODevice::ReadOnly))
227         err = QString::fromLatin1("Error: Could not open %1. Likely a permission error.")
228                                   .arg(file.fileName());
229
230     if(err.isNull()) /* No errors. */
231     {
232         ok = true;
233         /* Scary, we assume the query is stored in UTF-8. */
234         return QString::fromUtf8(file.readAll());
235     }
236     else
237     {
238         ok = false;
239         return err;
240     }
241 }
242
243 int XSDTSTestCase::columnCount() const
244 {
245     return 2;
246 }
247
248 void XSDTSTestCase::addBaseLine(TestBaseLine *line)
249 {
250     m_baseLines.append(line);
251 }
252
253 QString XSDTSTestCase::name() const
254 {
255     return m_name;
256 }
257
258 QString XSDTSTestCase::creator() const
259 {
260     return m_creator;
261 }
262
263 QString XSDTSTestCase::description() const
264 {
265     return m_description;
266 }
267
268 QDate XSDTSTestCase::lastModified() const
269 {
270     return m_lastModified;
271 }
272
273 bool XSDTSTestCase::isXPath() const
274 {
275     return false;
276 }
277
278 TestCase::Scenario XSDTSTestCase::scenario() const
279 {
280     return m_scenario;
281 }
282
283 void XSDTSTestCase::setName(const QString &n)
284 {
285     m_name = n;
286 }
287
288 void XSDTSTestCase::setCreator(const QString &ctor)
289 {
290     m_creator = ctor;
291 }
292
293 void XSDTSTestCase::setDescription(const QString &descriptionP)
294 {
295     m_description = descriptionP;
296 }
297
298 void XSDTSTestCase::setLastModified(const QDate &date)
299 {
300     m_lastModified = date;
301 }
302
303 void XSDTSTestCase::setSchemaUri(const QUrl &uri)
304 {
305     m_schemaUri = uri;
306 }
307
308 void XSDTSTestCase::setInstanceUri(const QUrl &uri)
309 {
310     m_instanceUri = uri;
311 }
312
313 TreeItem *XSDTSTestCase::parent() const
314 {
315     return m_parent;
316 }
317
318 QString XSDTSTestCase::title() const
319 {
320     return m_name;
321 }
322
323 TestBaseLine::List XSDTSTestCase::baseLines() const
324 {
325     Q_ASSERT_X(!m_baseLines.isEmpty(), Q_FUNC_INFO,
326                qPrintable(QString::fromLatin1("The test %1 has no base lines, it should have at least one.").arg(name())));
327     return m_baseLines;
328 }
329
330 QUrl XSDTSTestCase::schemaUri() const
331 {
332     return m_schemaUri;
333 }
334
335 QUrl XSDTSTestCase::instanceUri() const
336 {
337     return m_instanceUri;
338 }
339
340 void XSDTSTestCase::setContextItemSource(const QUrl &uri)
341 {
342     m_contextItemSource = uri;
343 }
344
345 QUrl XSDTSTestCase::contextItemSource() const
346 {
347     return m_contextItemSource;
348 }
349
350 void XSDTSTestCase::setParent(TreeItem *const p)
351 {
352     m_parent = p;
353 }
354
355 QPatternist::ExternalVariableLoader::Ptr XSDTSTestCase::externalVariableLoader() const
356 {
357     return QPatternist::ExternalVariableLoader::Ptr();
358 }
359
360 TestResult *XSDTSTestCase::testResult() const
361 {
362     return m_result;
363 }
364
365 TestItem::ResultSummary XSDTSTestCase::resultSummary() const
366 {
367     if(m_result)
368         return ResultSummary(m_result->status() == TestResult::Pass ? 1 : 0,
369                              1);
370
371     return ResultSummary(0, 1);
372 }
373
374 // vim: et:ts=4:sw=4:sts=4
375