From 2ecf32919fd3747f537df14c938d2520d240a993 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Fri, 13 Nov 2009 17:07:15 +1000 Subject: XMLHttpRequest tests --- src/declarative/qml/qmlxmlhttprequest.cpp | 47 ++++-- .../declarative/xmlhttprequest/data/document.qml | 3 + .../declarative/xmlhttprequest/data/element.qml | 22 +++ .../data/getAllResponseHeaders_args.qml | 23 +++ .../data/getAllResponseHeaders_sent.qml | 20 +++ .../data/getAllResponseHeaders_unsent.qml | 16 +++ .../xmlhttprequest/data/getResponseHeader_args.qml | 23 +++ .../xmlhttprequest/data/getResponseHeader_sent.qml | 20 +++ .../data/getResponseHeader_unsent.qml | 16 +++ .../xmlhttprequest/data/invalidMethodUsage.qml | 160 +++++++++++++++++++++ .../declarative/xmlhttprequest/data/open_user.qml | 53 +++++++ .../xmlhttprequest/data/send_data.4.qml | 1 - .../xmlhttprequest/data/send_data.7.qml | 23 +++ .../xmlhttprequest/data/setRequestHeader_args.qml | 18 +++ .../data/setRequestHeader_illegalName.qml | 2 + .../xmlhttprequest/tst_xmlhttprequest.cpp | 159 ++++++++++++++++++++ 16 files changed, 590 insertions(+), 16 deletions(-) create mode 100644 tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/open_user.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/send_data.7.qml create mode 100644 tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml diff --git a/src/declarative/qml/qmlxmlhttprequest.cpp b/src/declarative/qml/qmlxmlhttprequest.cpp index 848533c..72af43d 100644 --- a/src/declarative/qml/qmlxmlhttprequest.cpp +++ b/src/declarative/qml/qmlxmlhttprequest.cpp @@ -91,16 +91,16 @@ namespace { -class NodeImpl : public QmlRefCount +class DocumentImpl; +class NodeImpl { public: - NodeImpl() : type(Element), parent(0) {} + NodeImpl() : type(Element), document(0), parent(0) {} virtual ~NodeImpl() { - if (parent) D(parent); for (int ii = 0; ii < children.count(); ++ii) - D(children.at(ii)); + delete children.at(ii); for (int ii = 0; ii < attributes.count(); ++ii) - D(attributes.at(ii)); + delete attributes.at(ii); } // These numbers are copied from the Node IDL definition @@ -125,18 +125,22 @@ public: QString data; + void addref(); + void release(); + + DocumentImpl *document; NodeImpl *parent; QList children; QList attributes; }; -class DocumentImpl : public NodeImpl +class DocumentImpl : public QmlRefCount, public NodeImpl { public: DocumentImpl() : root(0) { type = Document; } virtual ~DocumentImpl() { - if (root) D(root); + if (root) delete root; } QString version; @@ -144,6 +148,9 @@ public: bool isStandalone; NodeImpl *root; + + void addref() { QmlRefCount::addref(); } + void release() { QmlRefCount::release(); } }; class NamedNodeMap @@ -312,6 +319,16 @@ Q_DECLARE_METATYPE(Node); Q_DECLARE_METATYPE(NodeList); Q_DECLARE_METATYPE(NamedNodeMap); +void NodeImpl::addref() +{ + A(document); +} + +void NodeImpl::release() +{ + D(document); +} + QScriptValue Node::nodeName(QScriptContext *context, QScriptEngine *engine) { Node node = qscriptvalue_cast(context->thisObject()); @@ -616,6 +633,7 @@ QScriptValue Document::load(QScriptEngine *engine, const QString &data) case QXmlStreamReader::StartDocument: Q_ASSERT(!document); document = new DocumentImpl; + document->document = document; document->version = reader.documentVersion().toString(); document->encoding = reader.documentEncoding().toString(); document->isStandalone = reader.isStandaloneDocument(); @@ -626,25 +644,25 @@ QScriptValue Document::load(QScriptEngine *engine, const QString &data) { Q_ASSERT(document); NodeImpl *node = new NodeImpl; + node->document = document; node->namespaceUri = reader.namespaceUri().toString(); node->name = reader.name().toString(); if (nodeStack.isEmpty()) { document->root = node; } else { node->parent = nodeStack.top(); - A(node->parent); node->parent->children.append(node); } nodeStack.append(node); foreach (const QXmlStreamAttribute &a, reader.attributes()) { NodeImpl *attr = new NodeImpl; + attr->document = document; attr->type = NodeImpl::Attr; attr->namespaceUri = a.namespaceUri().toString(); attr->name = a.name().toString(); attr->data = a.value().toString(); attr->parent = node; - A(attr->parent); node->attributes.append(attr); } } @@ -655,9 +673,9 @@ QScriptValue Document::load(QScriptEngine *engine, const QString &data) case QXmlStreamReader::Characters: { NodeImpl *node = new NodeImpl; + node->document = document; node->type = reader.isCDATA()?NodeImpl::CDATA:NodeImpl::Text; node->parent = nodeStack.top(); - A(node->parent); node->parent->children.append(node); node->data = reader.text().toString(); } @@ -827,8 +845,7 @@ NamedNodeMapClass::QueryFlags NamedNodeMapClass::queryProperty(const QScriptValu return 0; NamedNodeMap map = qscriptvalue_cast(object.data()); - if (map.isNull()) - return 0; + Q_ASSERT(!map.isNull()); bool ok = false; QString nameString = name.toString(); @@ -1307,7 +1324,7 @@ static QScriptValue qmlxmlhttprequest_setRequestHeader(QScriptContext *context, THROW_REFERENCE("Not an XMLHttpRequest object"); if (context->argumentCount() != 2) - THROW_SYNTAX("Incorrect argument count"); + THROW_DOM(SYNTAX_ERR, "Incorrect argument count"); if (request->readyState() != QmlXMLHttpRequest::Opened || @@ -1388,7 +1405,7 @@ static QScriptValue qmlxmlhttprequest_getResponseHeader(QScriptContext *context, THROW_REFERENCE("Not an XMLHttpRequest object"); if (context->argumentCount() != 1) - THROW_SYNTAX("Incorrect argument count"); + THROW_DOM(SYNTAX_ERR, "Incorrect argument count"); if (request->readyState() != QmlXMLHttpRequest::Loading && request->readyState() != QmlXMLHttpRequest::Done && @@ -1408,7 +1425,7 @@ static QScriptValue qmlxmlhttprequest_getAllResponseHeaders(QScriptContext *cont THROW_REFERENCE("Not an XMLHttpRequest object"); if (context->argumentCount() != 0) - THROW_SYNTAX("Incorrect argument count"); + THROW_DOM(SYNTAX_ERR, "Incorrect argument count"); if (request->readyState() != QmlXMLHttpRequest::Loading && request->readyState() != QmlXMLHttpRequest::Done && diff --git a/tests/auto/declarative/xmlhttprequest/data/document.qml b/tests/auto/declarative/xmlhttprequest/data/document.qml index 7601a10..ce9e35f 100644 --- a/tests/auto/declarative/xmlhttprequest/data/document.qml +++ b/tests/auto/declarative/xmlhttprequest/data/document.qml @@ -25,6 +25,9 @@ Object { if (document.nodeValue != null) return; + if (document.parentNode != null) + return; + // ### Test other node properties // ### test encoding (what is a valid qt encoding?) xmlTest = true; diff --git a/tests/auto/declarative/xmlhttprequest/data/element.qml b/tests/auto/declarative/xmlhttprequest/data/element.qml index 79620bf..228db18 100644 --- a/tests/auto/declarative/xmlhttprequest/data/element.qml +++ b/tests/auto/declarative/xmlhttprequest/data/element.qml @@ -32,6 +32,15 @@ Object { if (e.childNodes[childTagNames.length + 1] != null) return; + // Check writing fails + e.childNodes[0] = null; + if (e.childNodes[0] == null) + return; + + e.childNodes[10] = 10; + if (e.childNodes[10] != null) + return; + if (e.firstChild.tagName != e.childNodes[0].tagName) return; @@ -70,6 +79,19 @@ Object { if (attrIdx2 != null) return; + // Check writing fails + e.attributes[0] = null; + if (e.attributes[0] == null) + return; + + e.attributes["attr"] = null; + if (e.attributes["attr"] == null) + return; + + e.attributes["attr3"] = 10; + if (e.attributes["attr3"] != null) + return; + // Check person and fruit sub elements if (person.parentNode.nodeName != "root") return; diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml new file mode 100644 index 0000000..c2cf898 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + try { + x.getAllResponseHeaders("Test-header"); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml new file mode 100644 index 0000000..9583f9d --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml @@ -0,0 +1,20 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + try { + x.getAllResponseHeaders(); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml new file mode 100644 index 0000000..fac5259 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml @@ -0,0 +1,16 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + try { + x.getAllResponseHeaders(); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml new file mode 100644 index 0000000..ca7aed8 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + try { + x.getResponseHeader(); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml new file mode 100644 index 0000000..148a19c --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml @@ -0,0 +1,20 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + try { + x.getResponseHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml new file mode 100644 index 0000000..5abdf0a --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml @@ -0,0 +1,16 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + try { + x.getResponseHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml b/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml new file mode 100644 index 0000000..893eb8b --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml @@ -0,0 +1,160 @@ +import Qt 4.6 + +Object { + property bool onreadystatechange: false + property bool readyState: false + property bool status: false + property bool statusText: false + property bool responseText: false + property bool responseXML: false + + property bool open: false + property bool setRequestHeader: false + property bool send: false + property bool abort: false + property bool getResponseHeader: false + property bool getAllResponseHeaders: false + + Component.onCompleted: { + var o = 10; + + try { + XMLHttpRequest.prototype.onreadystatechange + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + onreadystatechange = true; + } + try { + XMLHttpRequest.prototype.readyState + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + readyState = true; + } + try { + XMLHttpRequest.prototype.status + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + status = true; + } + try { + XMLHttpRequest.prototype.statusText + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + statusText = true; + } + try { + XMLHttpRequest.prototype.responseText + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + responseText = true; + } + try { + XMLHttpRequest.prototype.responseXML + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + responseXML = true; + } + + try { + XMLHttpRequest.prototype.open.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + open = true; + } + + try { + XMLHttpRequest.prototype.setRequestHeader.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + setRequestHeader = true; + } + + try { + XMLHttpRequest.prototype.send.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + send = true; + } + + try { + XMLHttpRequest.prototype.abort.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + abort = true; + } + + try { + XMLHttpRequest.prototype.getResponseHeader.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + getResponseHeader = true; + } + + try { + XMLHttpRequest.prototype.getAllResponseHeaders.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + getAllResponseHeaders = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/open_user.qml b/tests/auto/declarative/xmlhttprequest/data/open_user.qml new file mode 100644 index 0000000..cc90433 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/open_user.qml @@ -0,0 +1,53 @@ +import Qt 4.6 + +Object { + property string url + + property bool readyState: false + property bool openedState: false + + property bool status: false + property bool statusText: false + property bool responseText: false + property bool responseXML: false + + property bool dataOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + if (x.readyState == XMLHttpRequest.UNSENT) + readyState = true; + + x.open("GET", url, true, "username", "password"); + + if (x.readyState == XMLHttpRequest.OPENED) + openedState = true; + + try { + var a = x.status; + } catch (error) { + if (error.code == DOMException.INVALID_STATE_ERR) + status = true; + } + try { + var a = x.statusText; + } catch (error) { + if (error.code == DOMException.INVALID_STATE_ERR) + statusText = true; + } + responseText = (x.responseText == ""); + responseXML = (x.responseXML == null); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == "QML Rocks!\n"); + } + } + + + x.send() + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml b/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml index 375f2fa..4705007 100644 --- a/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml +++ b/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml @@ -21,4 +21,3 @@ Object { } } - diff --git a/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml b/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml new file mode 100644 index 0000000..3a2ba56 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property string url + + property bool dataOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + x.open("POST", url); + x.setRequestHeader("Content-Type", "text/plain"); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == "QML Rocks!\n"); + } + } + + x.send("My Sent Data"); + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml new file mode 100644 index 0000000..6824af2 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + + try { + x.setRequestHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml index cf5ebcc..8029bc4 100644 --- a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml +++ b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml @@ -22,6 +22,8 @@ Object { x.open("GET", url); + x.setRequestHeader(header, "Value"); + if (x.readyState == XMLHttpRequest.OPENED) openedState = true; diff --git a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp index 100a11b..e74d1c5 100644 --- a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp +++ b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp @@ -71,6 +71,7 @@ private slots: void setRequestHeader_illegalName_data(); void setRequestHeader_illegalName(); void setRequestHeader_sent(); + void setRequestHeader_args(); void send_unsent(); void send_alreadySent(); void send_ignoreData(); @@ -79,7 +80,13 @@ private slots: void abort_unsent(); void abort_opened(); void getResponseHeader(); + void getResponseHeader_unsent(); + void getResponseHeader_sent(); + void getResponseHeader_args(); void getAllResponseHeaders(); + void getAllResponseHeaders_unsent(); + void getAllResponseHeaders_sent(); + void getAllResponseHeaders_args(); void status(); void statusText(); void responseText(); @@ -283,6 +290,34 @@ void tst_xmlhttprequest::open() delete object; } + + // User/pass + { + TestHTTPServer server(SERVER_PORT); + QVERIFY(server.isValid()); + QVERIFY(server.wait(TEST_FILE("open_network.expect"), + TEST_FILE("open_network.reply"), + TEST_FILE("testdocument.html"))); + + QmlComponent component(&engine, TEST_FILE("open_user.qml")); + QObject *object = component.beginCreate(engine.rootContext()); + QVERIFY(object != 0); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); + + QCOMPARE(object->property("readyState").toBool(), true); + QCOMPARE(object->property("openedState").toBool(), true); + QCOMPARE(object->property("status").toBool(), true); + QCOMPARE(object->property("statusText").toBool(), true); + QCOMPARE(object->property("responseText").toBool(), true); + QCOMPARE(object->property("responseXML").toBool(), true); + + TRY_WAIT(object->property("dataOK").toBool() == true); + + // ### Check that the username/password were sent to the server + + delete object; + } } // Test that calling XMLHttpRequest.open() with an invalid method raises an exception @@ -445,6 +480,18 @@ void tst_xmlhttprequest::setRequestHeader_sent() delete object; } +// Invalid arg count throws exception +void tst_xmlhttprequest::setRequestHeader_args() +{ + QmlComponent component(&engine, TEST_FILE("setRequestHeader_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("exceptionThrown").toBool(), true); + + delete object; +} + // Test that calling send() in UNSENT state throws an exception void tst_xmlhttprequest::send_unsent() { @@ -628,6 +675,25 @@ void tst_xmlhttprequest::send_withdata() delete object; } + + // Correct content-type - no charset + { + TestHTTPServer server(SERVER_PORT); + QVERIFY(server.isValid()); + QVERIFY(server.wait(TEST_FILE("send_data.1.expect"), + TEST_FILE("send_data.reply"), + TEST_FILE("testdocument.html"))); + + QmlComponent component(&engine, TEST_FILE("send_data.7.qml")); + QObject *object = component.beginCreate(engine.rootContext()); + QVERIFY(object != 0); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); + + TRY_WAIT(object->property("dataOK").toBool() == true); + + delete object; + } } // Test abort() has no effect in unsent state @@ -736,6 +802,42 @@ void tst_xmlhttprequest::getResponseHeader() delete object; } +// Test getResponseHeader throws an exception in an invalid state +void tst_xmlhttprequest::getResponseHeader_unsent() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_unsent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Test getResponseHeader throws an exception in an invalid state +void tst_xmlhttprequest::getResponseHeader_sent() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_sent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Invalid arg count throws exception +void tst_xmlhttprequest::getResponseHeader_args() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + TRY_WAIT(object->property("exceptionThrown").toBool() == true); + + delete object; +} + void tst_xmlhttprequest::getAllResponseHeaders() { QmlEngine engine; // Avoid cookie contamination @@ -768,6 +870,42 @@ void tst_xmlhttprequest::getAllResponseHeaders() delete object; } +// Test getAllResponseHeaders throws an exception in an invalid state +void tst_xmlhttprequest::getAllResponseHeaders_unsent() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_unsent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Test getAllResponseHeaders throws an exception in an invalid state +void tst_xmlhttprequest::getAllResponseHeaders_sent() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_sent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Invalid arg count throws exception +void tst_xmlhttprequest::getAllResponseHeaders_args() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + TRY_WAIT(object->property("exceptionThrown").toBool() == true); + + delete object; +} + void tst_xmlhttprequest::status() { { @@ -966,8 +1104,29 @@ void tst_xmlhttprequest::responseText() } } +// Test that calling hte XMLHttpRequest methods on a non-XMLHttpRequest object +// throws an exception void tst_xmlhttprequest::invalidMethodUsage() { + QmlComponent component(&engine, TEST_FILE("invalidMethodUsage.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("onreadystatechange").toBool(), true); + QCOMPARE(object->property("readyState").toBool(), true); + QCOMPARE(object->property("status").toBool(), true); + QCOMPARE(object->property("statusText").toBool(), true); + QCOMPARE(object->property("responseText").toBool(), true); + QCOMPARE(object->property("responseXML").toBool(), true); + + QCOMPARE(object->property("open").toBool(), true); + QCOMPARE(object->property("setRequestHeader").toBool(), true); + QCOMPARE(object->property("send").toBool(), true); + QCOMPARE(object->property("abort").toBool(), true); + QCOMPARE(object->property("getResponseHeader").toBool(), true); + QCOMPARE(object->property("getAllResponseHeaders").toBool(), true); + + delete object; } void tst_xmlhttprequest::responseXML_invalid() -- cgit v0.12