summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-11-13 04:47:54 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-11-13 04:47:54 (GMT)
commit2e1ad8b5d2b1927e80a3bd08209a75199e241b3c (patch)
treef4c3a97b17d1a2f19b1aacd9c5d1658b97f6c24c
parent3e16a19ba08b68aa92fd1eed5d5455fe02ef073f (diff)
downloadQt-2e1ad8b5d2b1927e80a3bd08209a75199e241b3c.zip
Qt-2e1ad8b5d2b1927e80a3bd08209a75199e241b3c.tar.gz
Qt-2e1ad8b5d2b1927e80a3bd08209a75199e241b3c.tar.bz2
XMLHttpRequest tests
-rw-r--r--src/declarative/qml/qmlxmlhttprequest.cpp153
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/cdata.qml135
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/cdata.xml2
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/document.qml6
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/document.xml2
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/element.qml28
-rw-r--r--tests/auto/declarative/xmlhttprequest/data/text.qml75
-rw-r--r--tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp22
8 files changed, 303 insertions, 120 deletions
diff --git a/src/declarative/qml/qmlxmlhttprequest.cpp b/src/declarative/qml/qmlxmlhttprequest.cpp
index 0a774e6..848533c 100644
--- a/src/declarative/qml/qmlxmlhttprequest.cpp
+++ b/src/declarative/qml/qmlxmlhttprequest.cpp
@@ -131,10 +131,10 @@ public:
QList<NodeImpl *> attributes;
};
-class DocumentImpl : public QmlRefCount
+class DocumentImpl : public NodeImpl
{
public:
- DocumentImpl() : root(0) { }
+ DocumentImpl() : root(0) { type = Document; }
virtual ~DocumentImpl() {
if (root) D(root);
}
@@ -158,12 +158,13 @@ public:
NamedNodeMap();
NamedNodeMap(const NamedNodeMap &);
- NamedNodeMap &operator=(const NamedNodeMap &);
~NamedNodeMap();
bool isNull();
NodeImpl *d;
QList<NodeImpl *> *list;
+private:
+ NamedNodeMap &operator=(const NamedNodeMap &);
};
class NamedNodeMapClass : public QScriptClass
@@ -180,7 +181,6 @@ class NodeList
public:
// JS API
static QScriptValue length(QScriptContext *context, QScriptEngine *engine);
- static QScriptValue item(QScriptContext *context, QScriptEngine *engine);
// C++ API
static QScriptValue prototype(QScriptEngine *);
@@ -188,11 +188,12 @@ public:
NodeList();
NodeList(const NodeList &);
- NodeList &operator=(const NodeList &);
~NodeList();
bool isNull();
NodeImpl *d;
+private:
+ NodeList &operator=(const NodeList &);
};
class NodeListClass : public QScriptClass
@@ -232,11 +233,13 @@ public:
Node();
Node(const Node &o);
- Node &operator=(const Node &);
~Node();
bool isNull() const;
NodeImpl *d;
+
+private:
+ Node &operator=(const Node &);
};
class Element : public Node
@@ -301,31 +304,29 @@ public:
// C++ API
static QScriptValue prototype(QScriptEngine *);
static QScriptValue load(QScriptEngine *engine, const QString &data);
-
- Document();
- Document(const Document &);
- Document &operator=(const Document &);
- ~Document();
- bool isNull() const;
-
- DocumentImpl *d;
-private:
- Document(DocumentImpl *);
};
-};
+}; // namespace
Q_DECLARE_METATYPE(Node);
Q_DECLARE_METATYPE(NodeList);
Q_DECLARE_METATYPE(NamedNodeMap);
-Q_DECLARE_METATYPE(Document);
QScriptValue Node::nodeName(QScriptContext *context, QScriptEngine *engine)
{
Node node = qscriptvalue_cast<Node>(context->thisObject());
if (node.isNull()) return engine->undefinedValue();
- return QScriptValue(node.d->name);
+ switch (node.d->type) {
+ case NodeImpl::Document:
+ return QScriptValue(QLatin1String("#document"));
+ case NodeImpl::CDATA:
+ return QScriptValue(QLatin1String("#cdata-section"));
+ case NodeImpl::Text:
+ return QScriptValue(QLatin1String("#text"));
+ default:
+ return QScriptValue(node.d->name);
+ }
}
QScriptValue Node::nodeValue(QScriptContext *context, QScriptEngine *engine)
@@ -643,6 +644,7 @@ QScriptValue Document::load(QScriptEngine *engine, const QString &data)
attr->name = a.name().toString();
attr->data = a.value().toString();
attr->parent = node;
+ A(attr->parent);
node->attributes.append(attr);
}
}
@@ -678,7 +680,9 @@ QScriptValue Document::load(QScriptEngine *engine, const QString &data)
QScriptValue instance = engine->newObject();
instance.setPrototype(Document::prototype(engine));
- return engine->newVariant(instance, qVariantFromValue(Document(document)));
+ Node documentNode;
+ documentNode.d = document;
+ return engine->newVariant(instance, qVariantFromValue(documentNode));
}
Node::Node()
@@ -692,14 +696,6 @@ Node::Node(const Node &o)
if (d) A(d);
}
-Node &Node::operator=(const Node &o)
-{
- if (o.d) A(o.d);
- if (d) D(d);
- d = o.d;
- return *this;
-}
-
Node::~Node()
{
if (d) D(d);
@@ -758,15 +754,6 @@ NamedNodeMap::NamedNodeMap(const NamedNodeMap &o)
if (d) A(d);
}
-NamedNodeMap &NamedNodeMap::operator=(const NamedNodeMap &o)
-{
- if (o.d) A(o.d);
- if (d) D(d);
- d = o.d;
- list = o.list;
- return *this;
-}
-
NamedNodeMap::~NamedNodeMap()
{
if (d) D(d);
@@ -777,20 +764,6 @@ bool NamedNodeMap::isNull()
return d == 0;
}
-QScriptValue NodeList::item(QScriptContext *context, QScriptEngine *engine)
-{
- NodeList list = qscriptvalue_cast<NodeList>(context->thisObject().data());
- if (list.isNull() || context->argumentCount() != 1)
- return engine->undefinedValue();
-
- qint32 index = context->argument(0).toInt32();
-
- if (index >= list.d->children.count())
- return engine->undefinedValue(); // ### Exception
- else
- return Node::create(engine, list.d->children.at(index));
-}
-
QScriptValue NodeList::length(QScriptContext *context, QScriptEngine *engine)
{
NodeList list = qscriptvalue_cast<NodeList>(context->thisObject().data());
@@ -804,7 +777,6 @@ QScriptValue NodeList::prototype(QScriptEngine *engine)
QScriptValue proto = engine->newObject();
proto.setProperty(QLatin1String("length"), engine->newFunction(length), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
- proto.setProperty(QLatin1String("item"), engine->newFunction(item, 1), QScriptValue::ReadOnly);
return proto;
}
@@ -839,14 +811,6 @@ NodeList::NodeList(const NodeList &o)
if (d) A(d);
}
-NodeList &NodeList::operator=(const NodeList &o)
-{
- if (o.d) A(o.d);
- if (d) D(d);
- d = o.d;
- return *this;
-}
-
NodeList::~NodeList()
{
if (d) D(d);
@@ -917,81 +881,36 @@ QScriptValue NodeListClass::property(const QScriptValue &object, const QScriptSt
return Node::create(engine(), list.d->children.at(id));
}
-Document::Document()
-: d(0)
-{
-}
-
-Document::Document(DocumentImpl *data)
-: d(data)
-{
-}
-
-Document::Document(const Document &o)
-: Node(o), d(o.d)
-{
- if (d) A(d);
-}
-
-Document &Document::operator=(const Document &o)
-{
- if (o.d) A(o.d);
- if (d) D(d);
- d = o.d;
- return *this;
-}
-
-Document::~Document()
-{
- if (d) D(d);
-}
-
-bool Document::isNull() const
-{
- return d == 0;
-}
-
QScriptValue Document::documentElement(QScriptContext *context, QScriptEngine *engine)
{
- Document document = qscriptvalue_cast<Document>(context->thisObject());
- if (document.isNull()) return engine->undefinedValue();
-
- if (!document.d->root) return engine->nullValue();
+ Node document = qscriptvalue_cast<Node>(context->thisObject());
+ if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
- return Node::create(engine, document.d->root);
+ return Node::create(engine, static_cast<DocumentImpl *>(document.d)->root);
}
QScriptValue Document::xmlStandalone(QScriptContext *context, QScriptEngine *engine)
{
- Document document = qscriptvalue_cast<Document>(context->thisObject());
- if (document.isNull()) return engine->undefinedValue();
-
- if (context->argumentCount())
- document.d->isStandalone = context->argument(0).toBool();
+ Node document = qscriptvalue_cast<Node>(context->thisObject());
+ if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
- return QScriptValue(document.d->isStandalone);
+ return QScriptValue(static_cast<DocumentImpl *>(document.d)->isStandalone);
}
QScriptValue Document::xmlVersion(QScriptContext *context, QScriptEngine *engine)
{
- Document document = qscriptvalue_cast<Document>(context->thisObject());
- if (document.isNull()) return engine->undefinedValue();
+ Node document = qscriptvalue_cast<Node>(context->thisObject());
+ if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
- if (context->argumentCount())
- document.d->version = context->argument(0).toString();
-
- return QScriptValue(document.d->version);
+ return QScriptValue(static_cast<DocumentImpl *>(document.d)->version);
}
QScriptValue Document::xmlEncoding(QScriptContext *context, QScriptEngine *engine)
{
- Document document = qscriptvalue_cast<Document>(context->thisObject());
- if (document.isNull()) return engine->undefinedValue();
-
- if (context->argumentCount())
- document.d->encoding = context->argument(0).toString();
+ Node document = qscriptvalue_cast<Node>(context->thisObject());
+ if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
- return QScriptValue(document.d->encoding);
+ return QScriptValue(static_cast<DocumentImpl *>(document.d)->encoding);
}
class QmlXMLHttpRequest : public QObject
diff --git a/tests/auto/declarative/xmlhttprequest/data/cdata.qml b/tests/auto/declarative/xmlhttprequest/data/cdata.qml
new file mode 100644
index 0000000..5faa359
--- /dev/null
+++ b/tests/auto/declarative/xmlhttprequest/data/cdata.qml
@@ -0,0 +1,135 @@
+import Qt 4.6
+
+Object {
+ property bool xmlTest: false
+ property bool dataOK: false
+
+ Script {
+ function checkCData(text, whitespacetext)
+ {
+ // This is essentially a copy of text.qml/checkText()
+
+ if (text == null)
+ return;
+
+ if (text.nodeName != "#cdata-section")
+ return;
+
+ if (text.nodeValue != "Hello world!")
+ return;
+
+ if (text.nodeType != 4)
+ return;
+
+ if (text.parentNode.nodeName != "item")
+ return;
+
+ if (text.childNodes.length != 0)
+ return;
+
+ if (text.firstChild != null)
+ return;
+
+ if (text.lastChild != null)
+ return;
+
+ if (text.previousSibling != null)
+ return;
+
+ if (text.nextSibling != null)
+ return;
+
+ if (text.attributes != null)
+ return;
+
+ if (text.wholeText != "Hello world!")
+ return;
+
+ if (text.data != "Hello world!")
+ return;
+
+ if (text.length != 12)
+ return;
+
+ if (text.isElementContentWhitespace != false)
+ return;
+
+ if (whitespacetext.nodeName != "#cdata-section")
+ return;
+
+ if (whitespacetext.nodeValue != " ")
+ return;
+
+ if (whitespacetext.nodeType != 4)
+ return;
+
+ if (whitespacetext.parentNode.nodeName != "item")
+ return;
+
+ if (whitespacetext.childNodes.length != 0)
+ return;
+
+ if (whitespacetext.firstChild != null)
+ return;
+
+ if (whitespacetext.lastChild != null)
+ return;
+
+ if (whitespacetext.previousSibling != null)
+ return;
+
+ if (whitespacetext.nextSibling != null)
+ return;
+
+ if (whitespacetext.attributes != null)
+ return;
+
+ if (whitespacetext.wholeText != " ")
+ return;
+
+ if (whitespacetext.data != " ")
+ return;
+
+ if (whitespacetext.length != 3)
+ return;
+
+ if (whitespacetext.isElementContentWhitespace != true)
+ return;
+
+
+ xmlTest = true;
+ }
+
+ function checkXML(document)
+ {
+ checkCData(document.documentElement.childNodes[0].childNodes[0],
+ document.documentElement.childNodes[1].childNodes[0]);
+
+ }
+ }
+
+ Component.onCompleted: {
+ var x = new XMLHttpRequest;
+
+ x.open("GET", "cdata.xml");
+
+ // Test to the end
+ x.onreadystatechange = function() {
+ if (x.readyState == XMLHttpRequest.DONE) {
+
+ dataOK = true;
+
+ if (x.responseXML != null)
+ checkXML(x.responseXML);
+
+ }
+ }
+
+ x.send()
+ }
+}
+
+
+
+
+
diff --git a/tests/auto/declarative/xmlhttprequest/data/cdata.xml b/tests/auto/declarative/xmlhttprequest/data/cdata.xml
new file mode 100644
index 0000000..061d37c
--- /dev/null
+++ b/tests/auto/declarative/xmlhttprequest/data/cdata.xml
@@ -0,0 +1,2 @@
+<root><item><![CDATA[Hello world!]]></item><item><![CDATA[ ]]></item></root>
+
diff --git a/tests/auto/declarative/xmlhttprequest/data/document.qml b/tests/auto/declarative/xmlhttprequest/data/document.qml
index fe78e31..7601a10 100644
--- a/tests/auto/declarative/xmlhttprequest/data/document.qml
+++ b/tests/auto/declarative/xmlhttprequest/data/document.qml
@@ -10,12 +10,18 @@ Object {
if (document.xmlVersion != "1.0")
return;
+ if (document.xmlEncoding != "UTF-8")
+ return;
+
if (document.xmlStandalone != true)
return;
if (document.documentElement == null)
return;
+ if (document.nodeName != "#document")
+ return;
+
if (document.nodeValue != null)
return;
diff --git a/tests/auto/declarative/xmlhttprequest/data/document.xml b/tests/auto/declarative/xmlhttprequest/data/document.xml
index b5fbe31..fb693ea 100644
--- a/tests/auto/declarative/xmlhttprequest/data/document.xml
+++ b/tests/auto/declarative/xmlhttprequest/data/document.xml
@@ -1,3 +1,3 @@
-<?xml version="1.0" standalone='yes' ?>
+<?xml version="1.0" encoding="UTF-8" standalone='yes'?>
<root>
</root>
diff --git a/tests/auto/declarative/xmlhttprequest/data/element.qml b/tests/auto/declarative/xmlhttprequest/data/element.qml
index a1ae2ab..79620bf 100644
--- a/tests/auto/declarative/xmlhttprequest/data/element.qml
+++ b/tests/auto/declarative/xmlhttprequest/data/element.qml
@@ -5,7 +5,7 @@ Object {
property bool dataOK: false
Script {
- function checkElement(e)
+ function checkElement(e, person, fruit)
{
if (e.tagName != "root")
return;
@@ -47,6 +47,9 @@ Object {
if (e.attributes == null)
return;
+ if (e.attributes.length != 2)
+ return;
+
var attr1 = e.attributes["attr"];
if (attr1.nodeValue != "value")
return;
@@ -67,12 +70,33 @@ Object {
if (attrIdx2 != null)
return;
+ // Check person and fruit sub elements
+ if (person.parentNode.nodeName != "root")
+ return;
+
+ if (person.previousSibling != null)
+ return;
+
+ if (person.nextSibling.nodeName != "fruit")
+ return;
+
+ if (fruit.parentNode.nodeName != "root")
+ return;
+
+ if (fruit.previousSibling.nodeName != "person")
+ return;
+
+ if (fruit.nextSibling != null)
+ return;
+
xmlTest = true;
}
function checkXML(document)
{
- checkElement(document.documentElement);
+ checkElement(document.documentElement,
+ document.documentElement.childNodes[0],
+ document.documentElement.childNodes[1]);
}
}
diff --git a/tests/auto/declarative/xmlhttprequest/data/text.qml b/tests/auto/declarative/xmlhttprequest/data/text.qml
index b64f80b..8c97504 100644
--- a/tests/auto/declarative/xmlhttprequest/data/text.qml
+++ b/tests/auto/declarative/xmlhttprequest/data/text.qml
@@ -7,15 +7,90 @@ Object {
Script {
function checkText(text, whitespacetext)
{
+ if (text == null)
+ return;
+
+ if (text.nodeName != "#text")
+ return;
+
+ if (text.nodeValue != "Hello world!")
+ return;
+
+ if (text.nodeType != 3)
+ return;
+
+ if (text.parentNode.nodeName != "item")
+ return;
+
+ if (text.childNodes.length != 0)
+ return;
+
+ if (text.firstChild != null)
+ return;
+
+ if (text.lastChild != null)
+ return;
+
+ if (text.previousSibling != null)
+ return;
+
+ if (text.nextSibling != null)
+ return;
+
+ if (text.attributes != null)
+ return;
+
if (text.wholeText != "Hello world!")
return;
+ if (text.data != "Hello world!")
+ return;
+
+ if (text.length != 12)
+ return;
+
if (text.isElementContentWhitespace != false)
return;
+ if (whitespacetext.nodeName != "#text")
+ return;
+
+ if (whitespacetext.nodeValue != " ")
+ return;
+
+ if (whitespacetext.nodeType != 3)
+ return;
+
+ if (whitespacetext.parentNode.nodeName != "item")
+ return;
+
+ if (whitespacetext.childNodes.length != 0)
+ return;
+
+ if (whitespacetext.firstChild != null)
+ return;
+
+ if (whitespacetext.lastChild != null)
+ return;
+
+ if (whitespacetext.previousSibling != null)
+ return;
+
+ if (whitespacetext.nextSibling != null)
+ return;
+
+ if (whitespacetext.attributes != null)
+ return;
+
if (whitespacetext.wholeText != " ")
return;
+ if (whitespacetext.data != " ")
+ return;
+
+ if (whitespacetext.length != 3)
+ return;
+
if (whitespacetext.isElementContentWhitespace != true)
return;
diff --git a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp
index cf1ffd3..100a11b 100644
--- a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp
+++ b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp
@@ -84,11 +84,14 @@ private slots:
void statusText();
void responseText();
void responseXML_invalid();
+ void invalidMethodUsage();
+ // Attributes
void document();
void element();
void attr();
void text();
+ void cdata();
// Crashes
// void outstanding_request_at_shutdown();
@@ -963,6 +966,10 @@ void tst_xmlhttprequest::responseText()
}
}
+void tst_xmlhttprequest::invalidMethodUsage()
+{
+}
+
void tst_xmlhttprequest::responseXML_invalid()
{
QmlComponent component(&engine, TEST_FILE("responseXML_invalid.qml"));
@@ -1018,6 +1025,7 @@ void tst_xmlhttprequest::attr()
delete object;
}
+// Test the Text DOM element
void tst_xmlhttprequest::text()
{
QmlComponent component(&engine, TEST_FILE("text.qml"));
@@ -1031,6 +1039,20 @@ void tst_xmlhttprequest::text()
delete object;
}
+// Test the CDataSection DOM element
+void tst_xmlhttprequest::cdata()
+{
+ QmlComponent component(&engine, TEST_FILE("cdata.qml"));
+ QObject *object = component.create();
+ QVERIFY(object != 0);
+
+ TRY_WAIT(object->property("dataOK").toBool() == true);
+
+ QCOMPARE(object->property("xmlTest").toBool(), true);
+
+ delete object;
+}
+
QTEST_MAIN(tst_xmlhttprequest)
#include "tst_xmlhttprequest.moc"