Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests pass in all browsers.

  • Loading branch information...
commit 53f548a6f39ce61ba5363ed0c702a12f5d09b291 1 parent b9bdb4e
Tim Evans authored
View
2  .jslint
@@ -6,5 +6,5 @@ options = {
white: true,
browser: true,
indent: 2,
- predef: ['Element', 'window', 'XC', 'Node', 'XC_DOMHelper', 'DOMParser', 'ActiveXObject']
+ predef: ['Element', 'window', 'XC', 'Node', 'XC_DOMHelper', 'DOMParser', 'ActiveXObject', 'XMLSerializer']
};
View
33 src/connection.js
@@ -42,6 +42,8 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
},
/**
+ * Initializes the Connection with services and templates
+ * hooked up with a connection and then stuck as top level objects.
* @private
*/
init: function ($super) {
@@ -114,13 +116,13 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
/**
* Register a handler for a stanza based on the following criteria:
- *
- * element - stanza element; 'iq', 'message', or 'presence'
- * xmlns - namespace of the stanza element OR first child
- * from - from JID
- * type - stanza type
- * id - stanza id
- *
+ * <ul>
+ * <li>element - The stanza element; 'iq', 'message', or 'presence'.</li>
+ * <li>xmlns - The namespace of the stanza element OR first child.</li>
+ * <li>from - The from JID.</li>
+ * <li>type - The stanza type.</li>
+ * <li>id - The stanza id.</li>
+ * </ul>
* This function is only to be called internally by the XC.Services.
* Client libraries should register their callbacks with each service,
* or directly with their bosh connection for services not provided
@@ -144,6 +146,9 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
},
/**
+ * Unregister the stanza handler, given the return id.
+ *
+ * @param {Mixed} id The id returned from registerStanzaHandler.
* @private
*/
unregisterStanzaHandler: function (id) {
@@ -152,11 +157,17 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
/**
* @private
+ * Set to true to debug packets, ensuring that they implement
+ * the interface as described in {@link XC.PacketAdapter}.
*/
DEBUG_PACKETS: false,
/**
* @private
+ * Validates incoming packets to ensure that they implement
+ * the packet interface as described in {@link XC.PacketAdapter}.
+ *
+ * @param {XC.PacketAdapter} packet Something that's supposed to implement the {@link XC.PacketAdapter} interface.
*/
_validatePacket: function (p) {
var pktInterface = /** @ignore */{
@@ -173,6 +184,7 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
return p.getTo && XC.isString(p.getTo());
}
};
+
for (var test in pktInterface) {
if (pktInterface.hasOwnProperty(test)) {
if (!pktInterface[test]()) {
@@ -183,10 +195,9 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
},
/**
+ * @private
* Find a set of registered callbacks whose set of criteria match the stanza
* and call the callbacks with the stanza.
- *
- * @private
*/
_dispatchStanza: function (stanza) {
var callbacks = this._stanzaHandlers.findCallbacks(stanza);
@@ -196,11 +207,11 @@ XC.Connection = XC.Base.extend(/** @lends XC.Connection# */{
},
/**
+ * @namespace
+ * @private
* Stanza Handlers are registered by the Services to register a callback
* for a specific stanza based on various criteria
*
- * @namespace
- * @private
* @see XC.Connection#registerStanzaHandler
* @see XC.Connection#unregisterStanzaHandler
*/
View
15 src/mixins/jid.js
@@ -8,8 +8,7 @@
XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
/**
- * Returns the bare JID of the entity.
- *
+ * @returns {String} The bare JID of the entity.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
@@ -25,8 +24,7 @@ XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
},
/**
- * Returns the JID node (commonly used as a username).
- *
+ * @returns {String} The JID node (commonly used as a username).
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
@@ -39,8 +37,7 @@ XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
},
/**
- * Returns the JID's domain.
- *
+ * @returns {String} The JID's domain.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
@@ -53,8 +50,7 @@ XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
},
/**
- * Returns the resource of the JID.
- *
+ * @returns {String} The resource of the JID.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
@@ -67,8 +63,7 @@ XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
},
/**
- * Returns the parts of the JID.
- *
+ * @returns {String} The parts of the JID.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
View
6 src/presence_stanza.js
@@ -48,6 +48,9 @@ XC.PresenceStanza = XC.Stanza.extend(/** @lends XC.PresenceStanza# */{
}
}.around(),
+ /**
+ * Accept a subscription request.
+ */
accept: function () {
var p = XC.PresenceStanza.extend();
@@ -72,6 +75,9 @@ XC.PresenceStanza = XC.Stanza.extend(/** @lends XC.PresenceStanza# */{
p.to.connection.send(p.toStanzaXML().convertToString());
},
+ /**
+ * Deny a subscription request.
+ */
deny: function () {
var p = XC.PresenceStanza.extend();
View
2  src/services/chat.js
@@ -34,7 +34,7 @@ XC.Service.Chat = XC.Base.extend(XC.Mixin.Discoverable,
* messages of type 'chat'.
* @name XC.Service.Chat#onMessage
* @event
- * @param {XC.Message} message A message sent to this resource.
+ * @param {XC.MessageStanza} message A message sent to this resource.
*/
/**
View
10 src/services/v_card.js
@@ -60,10 +60,14 @@ XC.Service.VCard = XC.Base.extend(XC.Mixin.Discoverable,
* @param {Function} callbacks.onSuccess Called on a successful vCard get.
*/
set: function (vCard, callbacks) {
- var iq = XC.XML.XMPP.IQ.extend();
-
+ var iq = XC.XML.XMPP.IQ.extend(),
+ self = this,
+ rawVCard = XC.Base.extend({ convertToString: function () {
+ return XC_DOMHelper.serializeToString(vCard);
+ }
+ });
iq.type('set');
- iq.addChild(vCard);
+ iq.addChild(rawVCard);
this.connection.send(iq.convertToString(), function (packet) {
if (packet.getType() === 'error' &&
View
2  src/util.js
@@ -72,6 +72,7 @@ XC.Base.mixin.call(Function.prototype, /** @lends Function.prototype */ {
* original function with the given arguments appended
* with the arguments supplied at runtime.
*
+ * @returns {Function} This function with pre-filled arguments.
* @example
* function aggregate () {
* var sum = 0, idx = arguments.length;
@@ -139,6 +140,7 @@ XC.Base.mixin.call(Array, /** @lends Array */ {
* Convert an iterable object into an Array.
*
* @param {Object} object An object that is iterable
+ * @returns {Array} The object converted into an Array.
* @example
* function commaSeparate () {
* return Array.from(arguments).join(', ');
View
30 src/xc_dom_helper.js
@@ -72,5 +72,35 @@ var XC_DOMHelper = {
*/
getTextContent: function (el) {
return el && (el.text || el.textContent);
+ },
+
+ /**
+ * Set the text of an element
+ *
+ * @param {Element|Node} el The document fragment to get the text of.
+ * @param {String} text The inner text of the fragment.
+ */
+ setTextContent: function (el, text) {
+ if (el) {
+ if (el.text) {
+ el.text = text;
+ } else {
+ el.textContent = text;
+ }
+ }
+ },
+
+ /**
+ * Serialize the Document / Element into a string.
+ *
+ * @param {Element|Node} node The document to serialize into a string.
+ * @returns {String} The document fragment as a string.
+ */
+ serializeToString: function (node) {
+ if ("XMLSerializer" in window) {
+ return (new XMLSerializer()).serializeToString(node);
+ } else {
+ return node.xml;
+ }
}
};
View
133 test/services/v_card.js
@@ -2,22 +2,7 @@
XC.Test.Service.VCard = new YAHOO.tool.TestCase({
name: 'XC vCard Service Tests',
- setUp: function () {
- this.conn = XC.Test.MockConnection.extend().init();
- this.xc = XC.Connection.extend({connectionAdapter: this.conn});
- },
-
- tearDown: function () {
- delete this.conn;
- delete this.xc;
- },
-
- testVCardGet: function () {
- var Assert = YAHOO.util.Assert;
-
- this.conn.addResponse(XC.Test.Packet.extendWithXML(
-"<iq id='v2' type='set'>\
- <vCard xmlns='vcard-temp'>\
+ vCard: "<vCard xmlns='vcard-temp'>\
<FN>Peter Saint-Andre</FN>\
<N>\
<FAMILY>Saint-Andre</FAMILY>\
@@ -62,8 +47,24 @@ XC.Test.Service.VCard = new YAHOO.tool.TestCase({
<DESC>\
Check out my blog at https://stpeter.im/\
</DESC>\
- </vCard>\
-</iq>"));
+ </vCard>",
+
+ setUp: function () {
+ this.conn = XC.Test.MockConnection.extend().init();
+ this.xc = XC.Connection.extend({connectionAdapter: this.conn});
+ },
+
+ tearDown: function () {
+ delete this.conn;
+ delete this.xc;
+ },
+
+ testVCardGet: function () {
+ var Assert = YAHOO.util.Assert;
+
+ this.conn.addResponse(XC.Test.Packet.extendWithXML(
+ "<iq id='v2' type='result'>" + this.vCard + "</iq>"));
+
var win = false, fail = false, that = this;
this.xc.VCard.get({
@@ -137,6 +138,102 @@ XC.Test.Service.VCard = new YAHOO.tool.TestCase({
Assert.isTrue(fail);
Assert.isFalse(win);
+ },
+
+ testSettingVCardSuccessfully: function () {
+ var Assert = YAHOO.util.Assert,
+ packet = XC.Test.Packet.extendWithXML(
+ "<iq id='v3'\
+ to='stpeter@jabber.org/roundabout'\
+ type='result'>\
+ <vCard xmlns='vcard-temp'/>\
+ </iq>"),
+ el = XC.Test.Packet.extendWithXML(this.vCard).getNode();
+
+ this.conn.addResponse(packet);
+
+ var win = false, fail = false;
+ this.xc.VCard.set(el, {
+ onSuccess: function () {
+ win = true;
+ },
+ onError: function (pkt) {
+ fail = true;
+ }
+ });
+
+ Assert.isTrue(win);
+ Assert.isFalse(fail);
+ },
+
+ testSettingVCardWithErrorResponse: function () {
+ var Assert = YAHOO.util.Assert,
+ packet = XC.Test.Packet.extendWithXML(
+ "<iq id='v3'\
+ to='stpeter@jabber.org/roundabout'\
+ type='error'>\
+ <vCard xmlns='vcard-temp'/>\
+ <error type='cancel'>\
+ <service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>\
+ </error>\
+ </iq>"),
+ el = XC.Test.Packet.extendWithXML(this.vCard).getNode();
+
+ this.conn.addResponse(packet);
+
+ var win = false, fail = false;
+ this.xc.VCard.set(el, {
+ onSuccess: function () {
+ win = true;
+ },
+ onError: function (pkt) {
+ fail = true;
+ Assert.areSame(packet, pkt);
+ }
+ });
+
+ Assert.isTrue(fail);
+ Assert.isFalse(win);
+ },
+
+ testVCardHasSaneOutgoingXML: function () {
+ var Assert = YAHOO.util.Assert,
+ packet = XC.Test.Packet.extendWithXML(
+ "<iq id='v3'\
+ to='stpeter@jabber.org/roundabout'\
+ type='result'>\
+ <vCard xmlns='vcard-temp'/>\
+ </iq>"),
+ el = XC.Test.Packet.extendWithXML(this.vCard).getNode();
+
+ this.conn.addResponse(packet);
+
+ var win = false, fail = false;
+ this.xc.VCard.set(el);
+
+ // Test a few element to ensure that they're there.
+ Assert.XPathTests(this.conn.getLastStanzaXML(), {
+ type: {
+ xpath: '/iq/@type',
+ value: 'set'
+ },
+ FN: {
+ xpath: '/iq/vcard:vCard/vcard:FN/text()',
+ value: 'Peter Saint-Andre'
+ },
+ NICKNAME: {
+ xpath: '/iq/vcard:vCard/vcard:NICKNAME/text()',
+ value: 'stpeter'
+ },
+ JABBERID: {
+ xpath: '/iq/vcard:vCard/vcard:FN/text()',
+ value: 'Peter Saint-Andre'
+ },
+ TELNUMBER: {
+ xpath: '/iq/vcard:vCard/vcard:TEL/vcard:NUMBER/text()',
+ value: '303-308-3282'
+ }
+ });
}
});
View
10 test/strophe_adapter.js
@@ -16,14 +16,14 @@ XC.Test.StropheAdapter = new YAHOO.tool.TestCase({
testQueueSize: function () {
var Assert = YAHOO.util.Assert,
empty = function () {}, id;
- this.adapter.send("<test>", empty);
+ this.adapter.send("<test/>", empty);
Assert.areSame(1, this.adapter._callbackQueue.length);
id = this.conn.recentlyRegistered;
this.conn.triggerEvent(id, XC.Test.Packet.extendWithXML("<iq id=\"" + id + "\"/>").getNode());
Assert.areSame(0, this.adapter._callbackQueue.length);
- this.adapter.send("<test>", empty);
- this.adapter.send("<test>", empty);
+ this.adapter.send("<test/>", empty);
+ this.adapter.send("<test/>", empty);
id = this.conn.recentlyRegistered;
this.conn.triggerEvent(id, XC.Test.Packet.extendWithXML("<iq id=\"" + id + "\"/>").getNode());
Assert.areSame(1, this.adapter._callbackQueue.length);
@@ -32,11 +32,11 @@ XC.Test.StropheAdapter = new YAHOO.tool.TestCase({
testQueueLimit: function () {
var Assert = YAHOO.util.Assert,
empty = function () {};
- this.adapter.send("<test>", empty);
+ this.adapter.send("<test/>", empty);
for (var i = 1; i < 125; i++) {
Assert.areSame(i > 100 ? 100 : i, this.adapter._callbackQueue.length);
- this.adapter.send("<test>", empty);
+ this.adapter.send("<test/>", empty);
}
Assert.areSame(100, this.adapter._callbackQueue.length);
}
Please sign in to comment.
Something went wrong with that request. Please try again.