Skip to content

Commit db36f6a

Browse files
author
Vasyl Vavrychuk
committed
refactored view xml serialization
1 parent 58bfd9d commit db36f6a

10 files changed

+211
-214
lines changed

inc/extension_qt/qml_view_executor.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class QQmlViewCmdExecutor : public QViewCmdExecutor {
102102
virtual void GetMute(const ElementId& element, bool*, Error**error) NOT_SUPPORTED_IMPL;
103103
virtual void SetPlaybackSpeed(const ElementId& element, double, Error**error) NOT_SUPPORTED_IMPL;;
104104
virtual void GetPlaybackSpeed(const ElementId& element, double*, Error**error) NOT_SUPPORTED_IMPL;;
105-
virtual void VisualizerSource(std::string* source, Error** error) NOT_SUPPORTED_IMPL;
105+
virtual void VisualizerSource(std::string* source, Error** error);
106106
virtual void VisualizerShowPoint(Error** error) NOT_SUPPORTED_IMPL;
107107
virtual void TouchPinchZoom(const ElementId &element, const double &scale, Error** error) NOT_SUPPORTED_IMPL;
108108
virtual void TouchPinchRotate(const ElementId &element, const int &angle, Error** error) NOT_SUPPORTED_IMPL;
@@ -112,13 +112,10 @@ class QQmlViewCmdExecutor : public QViewCmdExecutor {
112112

113113
protected:
114114
QDeclarativeView* getView(const ViewId& viewId, Error** error);
115-
typedef QHash<QString, QDeclarativeItem*> XMLElementMap;
116115

117116
QDeclarativeItem* getElement(const ElementId &element, Error** error);
118117
bool FilterElement(const QDeclarativeItem* item, const std::string& locator, const std::string& query);
119118
void FindElementsByXpath(QDeclarativeItem* parent, const std::string &query, std::vector<ElementId>* elements, Error **error);
120-
void createUIXML(QDeclarativeItem *parent, QIODevice* buff, XMLElementMap& elementsMap, Error** error);
121-
void addItemToXML(QDeclarativeItem* parent, XMLElementMap& elementsMap, QXmlStreamWriter* writer);
122119

123120
private:
124121
DISALLOW_COPY_AND_ASSIGN(QQmlViewCmdExecutor);

inc/extension_qt/widget_view_executor.h

-54
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,8 @@
33

44
#include "extension_qt/q_view_executor.h"
55

6-
#include <QtCore/QDebug>
7-
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
8-
#include <QtCore/QXmlStreamWriter>
9-
#else
10-
#include <QtXml/QXmlStreamWriter>
11-
#endif
12-
136
namespace webdriver {
147

15-
class QWidgetXmlSerializer {
16-
public:
17-
typedef QHash<QString, QWidget*> XMLElementMap;
18-
19-
QWidgetXmlSerializer(QIODevice* buff);
20-
21-
void createXml(QWidget* widget);
22-
23-
const XMLElementMap& getElementsMap() {
24-
return elementsMap_;
25-
}
26-
27-
void setSession(Session* session) {
28-
session_ = session;
29-
}
30-
31-
void setViewId(ViewId viewId) {
32-
viewId_ = viewId;
33-
}
34-
35-
void setDumpAll(bool dumpAll) {
36-
dumpAll_ = dumpAll;
37-
}
38-
39-
void setSupportedClasses(const QStringList& classes) {
40-
supportedClasses_ = classes;
41-
}
42-
43-
void setStylesheet(const QString& stylesheet) {
44-
stylesheet_ = stylesheet;
45-
}
46-
47-
private:
48-
void addWidget(QWidget* widget);
49-
QString getElementName(const QObject* object) const;
50-
51-
QXmlStreamWriter writer_;
52-
XMLElementMap elementsMap_;
53-
Session* session_;
54-
ViewId viewId_;
55-
bool dumpAll_;
56-
QStringList supportedClasses_;
57-
QString stylesheet_;
58-
};
59-
608
class QWidgetViewCmdExecutorCreator : public ViewCmdExecutorCreator {
619
public:
6210
static const ViewType WIDGET_VIEW_TYPE;
@@ -158,8 +106,6 @@ class QWidgetViewCmdExecutor : public QViewCmdExecutor {
158106
virtual void IsOnline(bool*, Error** error) NOT_SUPPORTED_IMPL;
159107

160108
protected:
161-
typedef QHash<QString, QWidget*> XMLElementMap;
162-
163109
QWidget* getElement(const ElementId &element, Error** error);
164110
bool MatchNativeWidget(const QWidget* widget, const std::string& locator, const std::string& query);
165111
void FindNativeElementsByXpath(QWidget* parent, const std::string &query, std::vector<ElementId>* elements, Error **error);

src/webdriver/extension_qt/common_util.h

+55-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33

44
#include <string>
55

6+
#include <QtCore/QHash>
67
#include <QtCore/QtGlobal>
78
#include <QtCore/QRect>
89
#include <QtCore/QPoint>
910
//#include <QtGui/QMouseEvent>
11+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
12+
#include <QtCore/QXmlStreamWriter>
13+
#else
14+
#include <QtXml/QXmlStreamWriter>
15+
#endif
1016

1117
#include "webdriver_basic_types.h"
1218

1319

1420
namespace webdriver {
1521

16-
class Session;
22+
class Session;
1723
class ViewId;
1824

1925
class QCommonUtil {
@@ -24,10 +30,56 @@ class QCommonUtil {
2430
static Qt::MouseButton ConvertMouseButtonToQtMouseButton(MouseButton button);
2531

2632
private:
27-
QCommonUtil() {};
28-
~QCommonUtil(){}
33+
QCommonUtil() {}
34+
~QCommonUtil() {}
2935
};
3036

37+
template <class Widget>
38+
class QViewXmlSerializer {
39+
public:
40+
typedef QHash<QString, Widget*> XMLElementMap;
41+
42+
QViewXmlSerializer(QIODevice* buff)
43+
: session_(NULL), dumpAll_(false)
44+
{
45+
writer_.setDevice(buff);
46+
writer_.setAutoFormatting(true);
47+
}
48+
49+
void createXml(Widget* widget) {
50+
writer_.writeStartDocument();
51+
if (!stylesheet_.isEmpty()) {
52+
writer_.writeProcessingInstruction("xml-stylesheet", "href=\"" + stylesheet_ + "\"");
53+
}
54+
addWidget(widget);
55+
writer_.writeEndDocument();
56+
}
57+
58+
const XMLElementMap& getElementsMap() {
59+
return elementsMap_;
60+
}
61+
62+
void setDumpAll(bool dumpAll) {
63+
dumpAll_ = dumpAll;
64+
}
65+
66+
void setSession(Session* session) {
67+
session_ = session;
68+
}
69+
70+
void setStylesheet(const QString& stylesheet) {
71+
stylesheet_ = stylesheet;
72+
}
73+
74+
protected:
75+
virtual void addWidget(Widget* widget) = 0;
76+
77+
QXmlStreamWriter writer_;
78+
XMLElementMap elementsMap_;
79+
Session* session_;
80+
bool dumpAll_;
81+
QString stylesheet_;
82+
};
3183

3284
} // namespace webdriver
3385

src/webdriver/extension_qt/qml_view_executor.cc

+15-67
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "value_conversion_util.h"
88
#include "webdriver_session.h"
99
#include "webdriver_view_factory.h"
10-
#include "webdriver_util.h"
1110
#include "common_util.h"
1211
#include "q_key_converter.h"
1312
#include "extension_qt/widget_element_handle.h"
@@ -26,15 +25,6 @@
2625

2726
namespace webdriver {
2827

29-
#if 1
30-
#define REMOVE_INTERNAL_SUFIXES(qstr) \
31-
qstr.remove(QRegExp(QLatin1String("_QMLTYPE_\\d+"))); \
32-
qstr.remove(QRegExp(QLatin1String("_QML_\\d+"))); \
33-
if (qstr.startsWith(QLatin1String("QDeclarative"))) qstr = qstr.mid(12);
34-
#else
35-
#define REMOVE_INTERNAL_SUFIXES(qstr)
36-
#endif
37-
3828
const ViewType QQmlViewCmdExecutorCreator::QML_VIEW_TYPE = 0x13f6;
3929

4030
QQmlViewCmdExecutorCreator::QQmlViewCmdExecutorCreator()
@@ -118,16 +108,12 @@ void QQmlViewCmdExecutor::GetSource(std::string* source, Error** error) {
118108
return;
119109
}
120110

121-
XMLElementMap elementsMap;
122111
QByteArray byteArray;
123112
QBuffer buff(&byteArray);
124113
buff.open(QIODevice::ReadWrite);
125114

126-
createUIXML(parentItem, &buff, elementsMap, error);
127-
128-
if (*error)
129-
return;
130-
115+
QQmlXmlSerializer serializer(&buff);
116+
serializer.createXml(parentItem);
131117
*source = byteArray.data();
132118
}
133119

@@ -589,7 +575,7 @@ void QQmlViewCmdExecutor::GetElementTagName(const ElementId& element, std::strin
589575
return;
590576

591577
QString className(pItem->metaObject()->className());
592-
REMOVE_INTERNAL_SUFIXES(className);
578+
QQmlViewUtil::removeInternalSuffixes(className);
593579

594580
*tag_name = className.toStdString();
595581
}
@@ -787,9 +773,16 @@ void QQmlViewCmdExecutor::ExecuteScript(const std::string& script, const base::L
787773
*value = static_cast<Value*>(ret_value.release());
788774
}
789775

776+
void QQmlViewCmdExecutor::VisualizerSource(std::string* source, Error** error)
777+
{
778+
GetSource(source, error);
779+
session_->logger().Log(kInfoLogLevel, "VisualizerSource:");
780+
session_->logger().Log(kInfoLogLevel, *source);
781+
}
782+
790783
bool QQmlViewCmdExecutor::FilterElement(const QDeclarativeItem* item, const std::string& locator, const std::string& query) {
791784
QString className(item->metaObject()->className());
792-
REMOVE_INTERNAL_SUFIXES(className);
785+
QQmlViewUtil::removeInternalSuffixes(className);
793786

794787
if (locator == LocatorType::kClassName) {
795788
if (query == className.toStdString())
@@ -814,12 +807,10 @@ bool QQmlViewCmdExecutor::FilterElement(const QDeclarativeItem* item, const std:
814807
void QQmlViewCmdExecutor::FindElementsByXpath(QDeclarativeItem* parent, const std::string &query, std::vector<ElementId>* elements, Error **error) {
815808
QByteArray byteArray;
816809
QBuffer buff(&byteArray);
817-
818810
buff.open(QIODevice::ReadWrite);
819-
XMLElementMap elementsMap;
820-
createUIXML(parent, &buff, elementsMap, error);
821-
if (*error)
822-
return;
811+
812+
QQmlXmlSerializer serializer(&buff);
813+
serializer.createXml(parent);
823814

824815
buff.seek(0);
825816

@@ -846,6 +837,7 @@ void QQmlViewCmdExecutor::FindElementsByXpath(QDeclarativeItem* parent, const st
846837
QString elemId(node.node().attribute("elementId").value());
847838

848839
if (!elemId.isEmpty()) {
840+
const QQmlXmlSerializer::XMLElementMap& elementsMap = serializer.getElementsMap();
849841
if (elementsMap.contains(elemId)) {
850842
ElementId elm;
851843
session_->AddElement(view_id_, new QElementHandle(elementsMap[elemId]), &elm);
@@ -877,48 +869,4 @@ void QQmlViewCmdExecutor::FindElementsByXpath(QDeclarativeItem* parent, const st
877869
buff.close();
878870
}
879871

880-
void QQmlViewCmdExecutor::createUIXML(QDeclarativeItem *parent, QIODevice* buff, XMLElementMap& elementsMap, Error** error) {
881-
882-
QXmlStreamWriter* writer = new QXmlStreamWriter();
883-
884-
writer->setDevice(buff);
885-
writer->setAutoFormatting(true);
886-
writer->writeStartDocument();
887-
888-
addItemToXML(parent, elementsMap, writer);
889-
890-
writer->writeEndDocument();
891-
892-
delete writer;
893-
}
894-
895-
void QQmlViewCmdExecutor::addItemToXML(QDeclarativeItem* parent, XMLElementMap& elementsMap, QXmlStreamWriter* writer) {
896-
if (NULL == parent) {
897-
session_->logger().Log(kWarningLogLevel, "parent item is NULL.");
898-
return;
899-
}
900-
901-
QString className(parent->metaObject()->className());
902-
REMOVE_INTERNAL_SUFIXES(className);
903-
904-
writer->writeStartElement(className);
905-
906-
if (!parent->objectName().isEmpty())
907-
writer->writeAttribute("id", parent->objectName());
908-
909-
QString elementKey = GenerateRandomID().c_str();
910-
elementsMap.insert(elementKey, QPointer<QDeclarativeItem>(parent));
911-
writer->writeAttribute("elementId", elementKey);
912-
913-
QList<QObject*> childs = parent->children();
914-
foreach(QObject *child, childs) {
915-
QDeclarativeItem* childItem = qobject_cast<QDeclarativeItem*>(child);
916-
if (childItem)
917-
addItemToXML(childItem, elementsMap, writer);
918-
}
919-
920-
writer->writeEndElement();
921-
}
922-
923-
924872
} //namespace webdriver

src/webdriver/extension_qt/qml_view_util.cc

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "qml_view_util.h"
22
#include "webdriver_session.h"
33
#include "webdriver_error.h"
4+
#include "webdriver_util.h"
45
#include "q_content_type_resolver.h"
56

67
#include <QtNetwork/QNetworkAccessManager>
78
#include <QtCore/QFileInfo>
9+
#include <QtDeclarative/QDeclarativeItem>
810

911
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
1012
#include "extension_qt/qwindow_view_handle.h"
@@ -14,7 +16,6 @@
1416
#include <QtDeclarative/QDeclarativeView>
1517
#endif
1618

17-
1819
namespace webdriver {
1920

2021
bool QQmlViewUtil::isUrlSupported(const std::string& url) {
@@ -54,6 +55,12 @@ bool QQmlViewUtil::isContentTypeSupported(const std::string& mime) {
5455
return false;
5556
}
5657

58+
void QQmlViewUtil::removeInternalSuffixes(QString& str) {
59+
str.remove(QRegExp(QLatin1String("_QMLTYPE_\\d+")));
60+
str.remove(QRegExp(QLatin1String("_QML_\\d+")));
61+
if (str.startsWith(QLatin1String("QDeclarative"))) str = str.mid(12);
62+
}
63+
5764
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
5865
QWindow* QQmlViewUtil::getQWindowView(Session* session, const ViewId& viewId) {
5966
ViewHandle* viewHandle = session->GetViewHandle(viewId);
@@ -92,5 +99,34 @@ QDeclarativeView* QQmlViewUtil::getQMLView(Session* session, const ViewId& viewI
9299
}
93100
#endif
94101

102+
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
103+
void QQmlXmlSerializer::addWidget(QDeclarativeItem* item) {
104+
if (NULL == item) {
105+
session_->logger().Log(kWarningLogLevel, "parent item is NULL.");
106+
return;
107+
}
108+
109+
QString className(item->metaObject()->className());
110+
QQmlViewUtil::removeInternalSuffixes(className);
111+
112+
writer_.writeStartElement(className);
113+
114+
if (!item->objectName().isEmpty())
115+
writer_.writeAttribute("id", item->objectName());
116+
117+
QString elementKey = GenerateRandomID().c_str();
118+
elementsMap_.insert(elementKey, QPointer<QDeclarativeItem>(item));
119+
writer_.writeAttribute("elementId", elementKey);
120+
121+
QList<QObject*> childs = item->children();
122+
foreach(QObject *child, childs) {
123+
QDeclarativeItem* childItem = qobject_cast<QDeclarativeItem*>(child);
124+
if (childItem)
125+
addWidget(childItem);
126+
}
127+
128+
writer_.writeEndElement();
129+
}
130+
#endif
95131

96132
} // namespace webdriver

0 commit comments

Comments
 (0)