From: Christopher De Cairos Date: Wed, 1 Jun 2011 22:13:56 +0000 (-0400) Subject: added getTrackEvent method to popcorn, added unit tests for it. X-Git-Tag: v0.7^2~53^2~9 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=2f3778484ed01b9a727ead5a6cb8cbbd04fd12c1;p=popcorn-js.git added getTrackEvent method to popcorn, added unit tests for it. --- diff --git a/popcorn.js b/popcorn.js index e2b8de6..5945134 100644 --- a/popcorn.js +++ b/popcorn.js @@ -776,6 +776,18 @@ return trackevents; }; + Popcorn.getTrackEvent = function( obj, trackId ) { + + var byStart = obj.data.trackEvents.byStart, + len = byStart.length, i; + + for (i = 0; i < len; i++) { + o = byStart[i]; + if ( o._id === trackId ) { + return o; + } + } + }; Popcorn.getLastTrackEventId = function( obj ) { return obj.data.history[ obj.data.history.length - 1 ]; @@ -787,6 +799,10 @@ getTrackEvents: function() { return Popcorn.getTrackEvents.call( null, this ); }, + + getTrackEvent: function( id ) { + return Popcorn.getTrackEvent.call( null, this, id ); + }, getLastTrackEventId: function() { return Popcorn.getLastTrackEventId.call( null, this ); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a45f06f..2db787f 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1607,6 +1607,69 @@ test("Functions", function () { +}); + +test("getTrackEvent", function () { + + // TODO: break this into sep. units per function + expect(5); + + var popped = Popcorn("#video"), + trackIds = [], + historyRef, trackEvents; + + + Popcorn.plugin("ff", function () { + return { + start: function () {}, + end: function () {} + }; + }); + + popped.ff({ + start: 3, + end: 4 + }); + + trackIds.push( popped.getLastTrackEventId() ); + + Popcorn.plugin("rw", function () { + return { + start: function () {}, + end: function () {} + }; + }); + + popped.rw({ + start: 1, + end: 2 + }); + + + trackIds.push( popped.getLastTrackEventId() ); + + popped.rw({ + start: 5, + end: 7 + }); + + trackIds.push( popped.getLastTrackEventId() ); + + var obj = popped.getTrackEvent( trackIds[0] ); + + equals( typeof obj === 'object', true, 'getTrackEvent() returned an object' ); + + trackIds.forEach (function( id ) { + trackEvent = popped.getTrackEvent( id ); + equals( id, trackEvent._id, "returned the correct TrackEvent"); + }); + + var oldId = trackIds[trackIds.length - 1]; + + popped.removeTrackEvent( oldId ); + + equals( popped.getTrackEvent( oldId ) !== true, true, "returned falsy value when id is not defined" ); + }); test("Index Integrity", function () {