1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 "XQTSTestCase.h"
49 
50 using namespace QPatternistSDK;
51 using namespace QPatternist;
52 
XQTSTestCase(const Scenario scen,TreeItem * p,const QXmlQuery::QueryLanguage lang)53 XQTSTestCase::XQTSTestCase(const Scenario scen,
54                            TreeItem *p,
55                            const QXmlQuery::QueryLanguage lang) : m_isXPath(false)
56                                                                 , m_scenario(scen)
57                                                                 , m_parent(p)
58                                                                 , m_lang(lang)
59 {
60 }
61 
~XQTSTestCase()62 XQTSTestCase::~XQTSTestCase()
63 {
64     qDeleteAll(m_baseLines);
65 }
66 
data(const Qt::ItemDataRole role,int column) const67 QVariant XQTSTestCase::data(const Qt::ItemDataRole role, int column) const
68 {
69     if(role == Qt::DisplayRole)
70     {
71         if(column == 0)
72             return title();
73 
74         const TestResult *const tr = testResult();
75         if(!tr)
76         {
77             if(column == 1)
78                 return TestResult::displayName(TestResult::NotTested);
79             else
80                 return QString();
81         }
82         const TestResult::Status status = tr->status();
83 
84         switch(column)
85         {
86             case 1:
87                 return status == TestResult::Pass ? QString(QChar::fromLatin1('1'))
88                                                   : QString(QChar::fromLatin1('0'));
89             case 2:
90                 return status == TestResult::Fail ? QString(QChar::fromLatin1('1'))
91                                                   : QString(QChar::fromLatin1('0'));
92             default:
93                 return QString();
94         }
95     }
96 
97     if(role != Qt::BackgroundRole)
98         return QVariant();
99 
100     const TestResult *const tr = testResult();
101 
102     if(!tr)
103     {
104         if(column == 0)
105             return Qt::yellow;
106         else
107             return QVariant();
108     }
109 
110     const TestResult::Status status = tr->status();
111 
112     if(status == TestResult::NotTested || status == TestResult::Unknown)
113         return Qt::yellow;
114 
115     switch(column)
116     {
117         case 1:
118             return status == TestResult::Pass ? Qt::green : QVariant();
119         case 2:
120             return status == TestResult::Fail ? Qt::red : QVariant();
121         default:
122             return QVariant();
123     }
124 }
125 
sourceCode(bool & ok) const126 QString XQTSTestCase::sourceCode(bool &ok) const
127 {
128     QFile file(m_queryPath.toLocalFile());
129 
130     QString err;
131 
132     if(!file.exists())
133         err = QString::fromLatin1("Error: %1 does not exist.").arg(file.fileName());
134     else if(!QFileInfo(file.fileName()).isFile())
135         err = QString::fromLatin1("Error: %1 is not a file, cannot display it.").arg(file.fileName());
136     else if(!file.open(QIODevice::ReadOnly))
137         err = QString::fromLatin1("Error: Could not open %1. Likely a permission error.")
138                                   .arg(file.fileName());
139 
140     if(err.isNull()) /* No errors. */
141     {
142         ok = true;
143         /* Scary, we assume the query is stored in UTF-8. */
144         return QString::fromUtf8(file.readAll());
145     }
146     else
147     {
148         ok = false;
149         return err;
150     }
151 }
152 
columnCount() const153 int XQTSTestCase::columnCount() const
154 {
155     return 2;
156 }
157 
addBaseLine(TestBaseLine * line)158 void XQTSTestCase::addBaseLine(TestBaseLine *line)
159 {
160     m_baseLines.append(line);
161 }
162 
name() const163 QString XQTSTestCase::name() const
164 {
165     return m_name;
166 }
167 
creator() const168 QString XQTSTestCase::creator() const
169 {
170     return m_creator;
171 }
172 
description() const173 QString XQTSTestCase::description() const
174 {
175     return m_description;
176 }
177 
lastModified() const178 QDate XQTSTestCase::lastModified() const
179 {
180     return m_lastModified;
181 }
182 
isXPath() const183 bool XQTSTestCase::isXPath() const
184 {
185     return m_isXPath;
186 }
187 
scenario() const188 TestCase::Scenario XQTSTestCase::scenario() const
189 {
190     return m_scenario;
191 }
192 
setName(const QString & n)193 void XQTSTestCase::setName(const QString &n)
194 {
195     m_name = n;
196 }
197 
setCreator(const QString & ctor)198 void XQTSTestCase::setCreator(const QString &ctor)
199 {
200     m_creator = ctor;
201 }
202 
setDescription(const QString & descriptionP)203 void XQTSTestCase::setDescription(const QString &descriptionP)
204 {
205     m_description = descriptionP;
206 }
207 
setLastModified(const QDate & date)208 void XQTSTestCase::setLastModified(const QDate &date)
209 {
210     m_lastModified = date;
211 }
212 
setIsXPath(const bool isXPathP)213 void XQTSTestCase::setIsXPath(const bool isXPathP)
214 {
215     m_isXPath = isXPathP;
216 }
217 
setQueryPath(const QUrl & uri)218 void XQTSTestCase::setQueryPath(const QUrl &uri)
219 {
220     m_queryPath = uri;
221 }
222 
parent() const223 TreeItem *XQTSTestCase::parent() const
224 {
225     return m_parent;
226 }
227 
title() const228 QString XQTSTestCase::title() const
229 {
230     return m_name;
231 }
232 
baseLines() const233 TestBaseLine::List XQTSTestCase::baseLines() const
234 {
235     Q_ASSERT_X(!m_baseLines.isEmpty(), Q_FUNC_INFO,
236                qPrintable(QString::fromLatin1("The test %1 has no base lines, it should have at least one.").arg(name())));
237     return m_baseLines;
238 }
239 
testCasePath() const240 QUrl XQTSTestCase::testCasePath() const
241 {
242     return m_queryPath;
243 }
244 
setExternalVariableLoader(const QPatternist::ExternalVariableLoader::Ptr & loader)245 void XQTSTestCase::setExternalVariableLoader(const QPatternist::ExternalVariableLoader::Ptr &loader)
246 {
247     m_externalVariableLoader = loader;
248 }
249 
externalVariableLoader() const250 QPatternist::ExternalVariableLoader::Ptr XQTSTestCase::externalVariableLoader() const
251 {
252     return m_externalVariableLoader;
253 }
254 
setContextItemSource(const QUrl & uri)255 void XQTSTestCase::setContextItemSource(const QUrl &uri)
256 {
257     m_contextItemSource = uri;
258 }
259 
contextItemSource() const260 QUrl XQTSTestCase::contextItemSource() const
261 {
262     return m_contextItemSource;
263 }
264 
language() const265 QXmlQuery::QueryLanguage XQTSTestCase::language() const
266 {
267     return m_lang;
268 }
269 
setParent(TreeItem * const p)270 void XQTSTestCase::setParent(TreeItem *const p)
271 {
272     m_parent = p;
273 }
274 
setInitialTemplateName(const QXmlName & name)275 void XQTSTestCase::setInitialTemplateName(const QXmlName &name)
276 {
277     m_initialTemplateName = name;
278 }
279 
initialTemplateName() const280 QXmlName XQTSTestCase::initialTemplateName() const
281 {
282     return m_initialTemplateName;
283 }
284 
285 // vim: et:ts=4:sw=4:sts=4
286 
287