umbrello  26.08.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 = nullptr);
38 
39 protected Q_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  QWidget *m_mainWidget = nullptr;
47 };
48 
58 {
59  Q_OBJECT
60 private Q_SLOTS:
61  virtual void initTestCase();
62 
63 protected:
64  QString m_tempPath;
65  QString temporaryPath();
66 };
67 
68 #include <QSignalBlocker>
69 typedef QSignalBlocker SignalBlocker;
70 
75 {
76 public:
77  SetLoading();
78  ~SetLoading();
79 protected:
80  bool _state;
81 };
82 
83 #include <QDomDocument>
84 #include <QXmlStreamWriter>
85 #include "umlapp.h"
86 #include "umldoc.h"
87 
91 template <class T, typename N>
92 class TestUML : public T
93 {
94 public:
95  TestUML() : T() {}
96  TestUML(N name) : T(name) {}
97  TestUML(N p1, UMLObject *p2, UMLObject *p3) : T(p1, p2, p3) {}
98  QString testSave1();
99  bool testLoad1(const QString& xml);
100  void testDump(const QString &title = QString());
101  UMLObject *secondary() const;
102 };
103 
104 template <class T, typename N>
106 {
107  QString xml;
108  QXmlStreamWriter stream(&xml);
109  stream.writeStartElement("unittest");
110  T::saveToXMI(stream);
111  stream.writeEndElement();
112  return xml;
113 }
114 
115 template <class T, typename N>
116 bool TestUML<T,N>::testLoad1(const QString& xml)
117 {
118  QDomDocument qDoc;
119  QString error;
120  int line;
121  if (!qDoc.setContent(xml, &error, &line))
122  return false;
123  QDomElement root = qDoc.childNodes().at(0).toElement();
124  QDomElement e = root.childNodes().at(0).toElement();
125  bool result = T::loadFromXMI(e);
126  if (result) {
127  const SignalBlocker sb(UMLApp::app()->document());
128  result = T::resolveRef();
129  }
130  return result;
131 }
132 
133 template <class T, typename N>
134 void TestUML<T,N>::testDump(const QString &title)
135 {
136  QString xml = testSave1();
137  qDebug() << title << xml;
138 }
139 
140 // used by resolveRef() tests
141 template <class T, typename N>
143 {
144  return T::m_pSecondary.data();
145 }
146 
150 template <class T, typename N>
151 class TestWidget : public T
152 {
153 public:
154  TestWidget(UMLScene *scene, N w) : T(scene, w) {}
155  QString testSave1();
156  bool testLoad1(const QString& xml);
157  void testDump(const QString &title = QString());
158 };
159 
160 template <class T, typename N>
162 {
163  QString xml;
164  QXmlStreamWriter stream(&xml);
165  stream.writeStartElement("unittest");
166  T::saveToXMI(stream);
167  stream.writeEndElement();
168  return xml;
169 }
170 
171 template <class T, typename N>
172 bool TestWidget<T,N>::testLoad1(const QString& xml)
173 {
174  QDomDocument qDoc;
175  QString error;
176  int line;
177  if (!qDoc.setContent(xml, &error, &line))
178  return false;
179  QDomElement root = qDoc.childNodes().at(0).toElement();
180  QDomElement e = root.childNodes().at(0).toElement();
181  bool result = T::loadFromXMI(e);
182  if (result) {
183  const SignalBlocker sb(UMLApp::app()->document());
184  result = T::activate(nullptr);
185  }
186  return result;
187 }
188 
189 template <class T, typename N>
190 void TestWidget<T,N>::testDump(const QString &title)
191 {
192  QString xml = testSave1();
193  qDebug() << title << xml;
194 }
195 
196 #endif // TESTBASE_H
QWidget * m_mainWidget
Definition: testbase.h:46
virtual void initTestCase()
Definition: testbase.cpp:40
TestUML(N p1, UMLObject *p2, UMLObject *p3)
Definition: testbase.h:97
QString testSave1()
Definition: testbase.h:161
UMLObject * secondary() const
Definition: testbase.h:142
TestBase(QObject *parent=nullptr)
Definition: testbase.cpp:35
TestWidget(UMLScene *scene, N w)
Definition: testbase.h:154
virtual void cleanupTestCase()
Definition: testbase.cpp:59
static UMLApp * app()
Definition: umlapp.cpp:284
Definition: testbase.h:57
virtual void initTestCase()
Definition: testbase.cpp:75
void testDump(const QString &title=QString())
Definition: testbase.h:134
QSignalBlocker SignalBlocker
Definition: testbase.h:69
Definition: testbase.h:92
The base class for UML objects.
Definition: umlobject.h:69
virtual void cleanupOnExit(QObject *p)
Definition: testbase.cpp:70
TestUML(N name)
Definition: testbase.h:96
Definition: testbase.h:74
Definition: testbase.h:33
QString temporaryPath()
Definition: testbase.cpp:88
bool testLoad1(const QString &xml)
Definition: testbase.h:116
QList< QPointer< QObject > > m_objectsToDelete
Definition: testbase.h:45
QString m_tempPath
holds path to temporary directory
Definition: testbase.h:64
QString testSave1()
Definition: testbase.h:105
bool testLoad1(const QString &xml)
Definition: testbase.h:172
void testDump(const QString &title=QString())
Definition: testbase.h:190
SetLoading()
Definition: testbase.cpp:93
~SetLoading()
Definition: testbase.cpp:99
TestUML()
Definition: testbase.h:95
Definition: umlscene.h:65
Definition: testbase.h:151
bool _state
Definition: testbase.h:80