Permalink
Browse files

recent_calls: added support for recent calls PubSub.

  • Loading branch information...
1 parent 585d4b5 commit 9cd8258b7d9615fb0c3656d117c66c2f6833a2d4 Tim Evans committed May 10, 2011
Showing with 190 additions and 45 deletions.
  1. +116 −1 src/services/recent_calls.js
  2. +74 −44 test/recent-calls.js
@@ -5,4 +5,119 @@
* @extends OX.Mixin.Subscribable
* @requires A 'connectionAdapter' property inherited from an {@link OX.Connection}.
*/
-OX.Service.RecentCalls = OX.Base.extend(OX.Mixin.Subscribable, /** @lends OX.Service.RecentCalls */{});
+OX.Service.RecentCalls = OX.Base.extend(OX.Mixin.Subscribable,
+ /** @lends OX.Service.RecentCalls */{
+
+ /**
+ * URI for this PubSub service.
+ */
+ pubSubURI: OX.Settings.URIs.pubSub.recentCalls,
+
+ /**
+ * @namespace
+ * Recent Call Item.
+ * @name OX.Service.RecentCalls.Item
+ * @extends OX.Item
+ */
+ Item: OX.Item.extend(OX.Mixin.CallDialog,
+ /** @lends OX.Service.RecentCalls.Item# */{
+
+ /** The URI of the call originator. */
+ fromURI: null,
+
+ /** The URI of the call terminator. */
+ toURI: null,
+
+ /** Caller ID information for the caller */
+ fromDisplay: null,
+
+ /** Caller ID information for the callee */
+ toDisplay: null,
+
+ /** The time at which the call started. */
+ startTime: null,
+
+ /** The duration of the call, in seconds */
+ duration: null,
+
+ /** Whether or not the call is outgoing */
+ isOutgoing: function () {
+ return this.toURI != null;
+ },
+
+ /** Whether or not the call is incoming */
+ isIncoming: function () {
+ return this.fromURI != null;
+ },
+
+ /** Whether or not the call was cancelled */
+ isCancelled: function () {
+ return this.isOutgoing() && this.duration === 0;
+ },
+
+ /** Whether or not the call was missed */
+ isMissed: function () {
+ return this.isIncoming() && this.duration === 0;
+ }
+ }),
+
+ /** @private
+ * Returns an {@link OX.Service.RecentCalls.Item} from an XML Document.
+ * This method should be called once for each item to be constructed.
+ * If a DOM Element contains more than one item node, only the first
+ * item node will be returned as an {@link OX.Service.RecentCalls.Item}
+ *
+ * @param {Element|Node} element The DOM Element or Node to parse into a {@link OX.Service.RecentCalls.Item}
+ * @returns {OX.Service.RecentCalls.Item} item
+ */
+ itemFromElement: function (element) {
+ if (!element) {
+ return undefined;
+ }
+
+ var recentCallNode = element.getElementsByTagName('recent-call'),
+ attrs = {connection: this.connection};
+
+ if (!recentCallNode || !recentCallNode[0]) {
+ return undefined;
+ }
+
+ var childNodes = recentCallNode[0].childNodes, node, d;
+ for (var i = 0, len = childNodes.length; i < len; i++) {
+ node = childNodes[i];
+
+ if (!node.nodeName) {
+ continue;
+ }
+
+ switch (node.nodeName.toLowerCase()) {
+ case 'from-uri':
+ attrs.fromURI = node.firstChild.nodeValue;
+ break;
+ case 'to-uri':
+ attrs.toURI = node.firstChild.nodeValue;
+ break;
+ case 'branch':
+ attrs.branch = node.firstChild && node.firstChild.nodeValue;
+ break;
+ case 'from-display':
+ attrs.fromDisplay = node.firstChild && node.firstChild.nodeValue;
+ break;
+ case 'to-display':
+ attrs.toDisplay = node.firstChild && node.firstChild.nodeValue;
+ break;
+ case 'time':
+ d = node.getAttribute('start').match(/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(([+-])(\d{2}):(\d{2})))$/i);
+ if (!d) throw new OX.Error("ISO Date was improperly formatted: " + node.getAttribute('start'));
+ attrs.startTime = new Date(
+ Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6] | 0, (d[6] * 1000 - ((d[6] | 0) * 1000)) | 0, d[7]) +
+ (d[7].toUpperCase() === "Z" ? 0 : (d[10] * 3600 + d[11] * 60) * (d[9] === "-" ? 1000 : -1000)));
+ attrs.duration = +node.getAttribute('duration');
+ break;
+ }
+ }
+
+ return this.Item.extend(attrs);
+ }
+
+});
View
@@ -1,18 +1,26 @@
OXTest.RecentCalls = new YAHOO.tool.TestCase({
name: 'RecentCalls Tests',
- _should: {
- /*
- * Don't target any recent-calls tests for now.
- */
- ignore: {
- testServiceMixin: true,
- testPubSubURI: true,
- testItemFromElement: true,
- testCallID: true,
- testLabel: true
- }
- },
+ cancelledCall: '<recent-call xmlns="onsip:recent-calls">\
+ <to-uri>sip:bob@incredibl.es</to-uri>\
+ <to-display>&quot;Bob Parr&quot;</to-display>\
+ <time start="2011-05-10T14:18:51Z"\
+ duration="0"/>\
+ </recent-call>',
+
+ missedCall: '<recent-call xmlns="onsip:recent-calls">\
+ <from-uri>sip:helen@incredibl.es</from-uri>\
+ <from-display>&quot;Helen Parr&quot;</from-display>\
+ <time start="2011-05-10T14:18:51Z"\
+ duration="0"/>\
+ </recent-call>',
+
+ recentCall: '<recent-call xmlns="onsip:recent-calls">\
+ <to-uri>sip:edna@mo.de</to-uri>\
+ <to-display>&quot;Edna Mode&quot;</to-display>\
+ <time start="2011-05-12T18:22:07Z"\
+ duration="600"/>\
+ </recent-call>',
setUp: function () {
this.conn = OXTest.ConnectionMock.extend();
@@ -27,61 +35,83 @@ OXTest.RecentCalls = new YAHOO.tool.TestCase({
},
testServiceMixin: function () {
- var Assert = YAHOO.util.Assert;
+ var assert = YAHOO.util.Assert;
- Assert.isObject(OX.Service.RecentCalls,
+ assert.isObject(OX.Service.RecentCalls,
'RecentCalls mixin is not available');
- Assert.isObject(this.ox.RecentCalls, 'RecentCalls is not initialized');
- Assert.areSame(this.ox, this.ox.RecentCalls.connection);
+ assert.isObject(this.ox.RecentCalls, 'RecentCalls is not initialized');
+ assert.areSame(this.ox, this.ox.RecentCalls.connection);
},
testPubSubURI: function () {
- var Assert = YAHOO.util.Assert;
+ var assert = YAHOO.util.Assert;
- Assert.areSame('xmpp:pubsub.recent-calls.xmpp.onsip.com',
- this.RecentCalls.pubSubURI,
+ assert.areSame('xmpp:pubsub.recent-calls.xmpp.onsip.com',
+ this.RecentCalls.pubSubURI.toString(),
'RecentCalls.pubSub URI is wrong.');
},
testItemFromElement: function () {
- var Assert = YAHOO.util.Assert;
+ var assert = YAHOO.util.Assert;
- Assert.isFunction(this.RecentCalls.itemFromElement,
+ assert.isFunction(this.RecentCalls.itemFromElement,
'Recent call service cannot turn packet into item.');
- var itemXML = '';
- var packet = OXTest.Message.extend({
- from: 'user-agents.xmpp.onsip.com',
- to: 'me@example.com',
- doc: OXTest.DOMParser.parse(itemXML)
- });
+ var packet = OXTest.DOMParser.parse(this.recentCall);
var item = this.RecentCalls.itemFromElement(packet.doc);
- Assert.isObject(item, 'RecentCalls.itemFromElement did not return an object.');
- Assert.areSame(this.conn, item.connection,
+ assert.isObject(item, 'RecentCalls.itemFromElement did not return an object.');
+ assert.areSame(this.ox, item.connection,
'Recent call item connection is wrong.');
},
- testCallID: function () {
- var Assert = YAHOO.util.Assert;
+ testRecentCall: function () {
+ var assert = YAHOO.util.Assert;
+
+ var packet = OXTest.DOMParser.parse(this.recentCall);
+ var item = this.RecentCalls.itemFromElement(packet.doc);
+
+ assert.areEqual(item.toURI, "sip:edna@mo.de", "the call's toURIs should match.");
+ assert.areEqual(item.toDisplay, "\"Edna Mode\"", "the call's toDisplays should match.");
+ assert.areEqual(item.startTime.getTime(), 1305224527000, "the call's startTime should be a date.");
+ assert.areEqual(item.duration, 600, "the call's duration should be 600 seconds");
+ assert.isFalse(item.isIncoming(), "the call should not be incoming");
+ assert.isTrue(item.isOutgoing(), "the call should be outgoing");
+ assert.isFalse(item.isCancelled(), "the call should not be cancelled");
+ assert.isFalse(item.isMissed(), "the call should not be missed");
+ },
+
+ testMissedCall: function () {
+ var assert = YAHOO.util.Assert;
+
+ var packet = OXTest.DOMParser.parse(this.missedCall);
+ var item = this.RecentCalls.itemFromElement(packet.doc);
- Assert.isNotUndefined(this.RecentCalls.Item.callID,
- 'RecentCalls.Item.callID is undefined');
+ assert.areEqual(item.fromURI, "sip:helen@incredibl.es", "the call's fromURIs should match.");
+ assert.areEqual(item.fromDisplay, "\"Helen Parr\"", "the call's fromDisplays should match.");
+ assert.areEqual(item.startTime.getTime(), 1305037131000, "the call's startTime should be a date.");
+ assert.areEqual(item.duration, 0, "the call's duration should be 0 seconds");
+ assert.isTrue(item.isIncoming(), "the call should be incoming");
+ assert.isFalse(item.isOutgoing(), "the call should not be outgoing");
+ assert.isFalse(item.isCancelled(), "the call should not be cancelled");
+ assert.isTrue(item.isMissed(), "the call should be missed");
},
- testLabel: function () {
- var Assert = YAHOO.util.Assert;
+ testCancelledCall: function () {
+ var assert = YAHOO.util.Assert;
- Assert.isFunction(this.RecentCalls.Item.label,
- 'RecentCalls.Item.label is not a function');
+ var packet = OXTest.DOMParser.parse(this.cancelledCall);
+ var item = this.RecentCalls.itemFromElement(packet.doc);
- var item = this.RecentCalls.Item.extend({callID: '123'});
- Assert.isFunction(item.label,
- 'recent call item\'s label is not a function');
- item.label('wauug');
- Assert.isCommand(this.conn._data, 'commands.recent-calls.xmpp.onsip.com',
- 'label', {'call-id': '123',
- 'label': 'wauug'});
+ assert.areEqual(item.toURI, "sip:bob@incredibl.es", "the call's toURIs should match.");
+ assert.areEqual(item.toDisplay, "\"Bob Parr\"", "the call's toDisplays should match.");
+ assert.areEqual(item.startTime.getTime(), 1305037131000, "the call's startTime should be a date.");
+ assert.areEqual(item.duration, 0, "the call's duration should be 0 seconds");
+ assert.isFalse(item.isIncoming(), "the call should be incoming");
+ assert.isTrue(item.isOutgoing(), "the call should not be outgoing");
+ assert.isTrue(item.isCancelled(), "the call should be cancelled");
+ assert.isFalse(item.isMissed(), "the call should not be missed");
}
+
});
YAHOO.tool.TestRunner.add(OXTest.RecentCalls);

0 comments on commit 9cd8258

Please sign in to comment.