umbrello  2.37.1
Umbrello UML Modeller is a Unified Modelling Language (UML) diagram program based on KDE Technology
testbase.h
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: 2015, 2019 Ralf Habacker <ralf.habacker@freenet.de>
3 
4  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 #ifndef TESTBASE_H
8 #define TESTBASE_H
9 
10 // qt includes
11 #include <QObject>
12 #include <QtTest>
13 
14 #ifdef RUN_ALL
15 #undef QCOMPARE
16 #define QCOMPARE(actual, expected) \
17  QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)
18 #undef QVERIFY
19 #define QVERIFY(statement) \
20  QTest::qVerify((statement), #statement, "", __FILE__, __LINE__)
21 #endif
22 
23 #define IS_NOT_IMPL() QSKIP("not implemented yet", SkipSingle)
24 
33 class TestBase : public QObject
34 {
35  Q_OBJECT
36 public:
37  explicit TestBase(QObject *parent = 0);
38 
39 protected slots:
40  virtual void initTestCase();
41  virtual void cleanupTestCase();
42  virtual void cleanupOnExit(QObject *p);
43 
44 protected:
45  QList<QPointer<QObject>> m_objectsToDelete;
46 };
47 
57 {
58  Q_OBJECT
59 private slots:
60  virtual void initTestCase();
61 
62 protected:
63  QString m_tempPath;
64  QString temporaryPath();
65 };
66 
67 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
71 class SignalBlocker
72 {
73 public:
74  explicit SignalBlocker(QObject *o)
75  : _o(o)
76  {
77  _state = _o->blockSignals(true);
78  }
79 
80  SignalBlocker(QObject &o)
81  : _o(&o)
82  {
83  _state = _o->blockSignals(true);
84  }
85 
86  ~SignalBlocker()
87  {
88  _o->blockSignals(_state);
89  }
90 
91 protected:
92  QObject *_o;
93  bool _state;
94 };
95 #else
96 #include <QSignalBlocker>
97 typedef QSignalBlocker SignalBlocker;
98 #endif
99 
104 {
105 public:
106  SetLoading();
107  ~SetLoading();
108 protected:
109  bool _state;
110 };
111 
112 #include <QDomDocument>
113 #include <QXmlStreamWriter>
114 #include "uml.h"
115 #include "umldoc.h"
116 
120 template <class T, typename N>
121 class TestUML : public T
122 {
123 public:
124  TestUML<T,N>() : T() {}
125  TestUML<T,N>(N name) : T(name) {}
126  TestUML<T,N>(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
127  QString testSave1();
128  bool testLoad1(const QString& xml);
129  void testDump(const QString &title = QString());
131 };
132 
133 template <class T, typename N>
135 {
136  QString xml;
137  QXmlStreamWriter stream(&xml);
138  stream.writeStartElement("unittest");
139  T::saveToXMI(stream);
140  stream.writeEndElement();
141  return xml;
142 }
143 
144 template <class T, typename N>
145 bool TestUML<T,N>::testLoad1(const QString& xml)
146 {
147  QDomDocument qDoc;
148  QString error;
149  int line;
150  if (!qDoc.setContent(xml, &error, &line))
151  return false;
152  QDomElement root = qDoc.childNodes().at(0).toElement();
153  QDomElement e = root.childNodes().at(0).toElement();
154  bool result = T::loadFromXMI(e);
155  if (result) {
156  const SignalBlocker sb(UMLApp::app()->document());
157  result = T::resolveRef();
158  }
159  return result;
160 }
161 
162 template <class T, typename N>
163 void TestUML<T,N>::testDump(const QString &title)
164 {
165  QString xml = testSave1();
166  qDebug() << title << xml;
167 }
168 
169 // used by resolveRef() tests
170 template <class T, typename N>
172 {
173  return T::m_pSecondary.data();
174 }
175 
179 template <class T, typename N>
180 class TestWidget : public T
181 {
182 public:
183  TestWidget<T,N>(UMLScene *scene, N w) : T(scene, w) {}
184  QString testSave1();
185  bool testLoad1(const QString& xml);
186  void testDump(const QString &title = QString());
187 };
188 
189 template <class T, typename N>
191 {
192  QString xml;
193  QXmlStreamWriter stream(&xml);
194  stream.writeStartElement("unittest");
195  T::saveToXMI(stream);
196  stream.writeEndElement();
197  return xml;
198 }
199 
200 template <class T, typename N>
201 bool TestWidget<T,N>::testLoad1(const QString& xml)
202 {
203  QDomDocument qDoc;
204  QString error;
205  int line;
206  if (!qDoc.setContent(xml, &error, &line))
207  return false;
208  QDomElement root = qDoc.childNodes().at(0).toElement();
209  QDomElement e = root.childNodes().at(0).toElement();
210  bool result = T::loadFromXMI(e);
211  if (result) {
212  const SignalBlocker sb(UMLApp::app()->document());
213  result = T::activate(nullptr);
214  }
215  return result;
216 }
217 
218 template <class T, typename N>
219 void TestWidget<T,N>::testDump(const QString &title)
220 {
221  QString xml = testSave1();
222  qDebug() << title << xml;
223 }
224 
225 #endif // TESTBASE_H
Definition: testbase.h:104
SetLoading()
Definition: testbase.cpp:78
~SetLoading()
Definition: testbase.cpp:84
bool _state
Definition: testbase.h:109
Definition: testbase.h:34
virtual void initTestCase()
Definition: testbase.cpp:34
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:50
TestBase(QObject *parent=0)
Definition: testbase.cpp:29
virtual void cleanupTestCase()
Definition: testbase.cpp:42
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:45
Definition: testbase.h:57
virtual void initTestCase()
Definition: testbase.cpp:55
QString temporaryPath()
Definition: testbase.cpp:73
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:63
Definition: testbase.h:122
QString testSave1()
Definition: testbase.h:134
UMLObject * secondary() const
Definition: testbase.h:171
void testDump(const QString &title=QString())
Definition: testbase.h:163
bool testLoad1(const QString &xml)
Definition: testbase.h:145
Definition: testbase.h:181
void testDump(const QString &title=QString())
Definition: testbase.h:219
bool testLoad1(const QString &xml)
Definition: testbase.h:201
QString testSave1()
Definition: testbase.h:190
static UMLApp * app()
Definition: uml.cpp:349
The base class for UML objects.
Definition: umlobject.h:70
Definition: umlscene.h:65
QSignalBlocker SignalBlocker
Definition: testbase.h:97