]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
added getTrackEvent method to popcorn, added unit tests for it.
authorChristopher De Cairos <chris.decairos@gmail.com>
Wed, 1 Jun 2011 22:13:56 +0000 (18:13 -0400)
committerChristopher De Cairos <chris.decairos@gmail.com>
Wed, 1 Jun 2011 22:13:56 +0000 (18:13 -0400)
popcorn.js
test/popcorn.unit.js

index e2b8de6b97bc3ac11c9c2ea4714220b100f86046..5945134dea274045d96cc0cc361b2fe2cdd2fdac 100644 (file)
     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 ];
     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 );
index a45f06f71729fd79c3e65f8930c9e3660025609d..2db787f458b2f89c1b51eba34dd42e7661080769 100644 (file)
@@ -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 () {