]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#221] initial parser plugin structure and tests
authorScottDowne <sdowne@learn.senecac.on.ca>
Tue, 21 Dec 2010 21:26:18 +0000 (16:26 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Tue, 21 Dec 2010 21:26:18 +0000 (16:26 -0500)
popcorn.js
test/data/parserData.json [new file with mode: 0644]
test/popcorn.unit.js

index 54434cd95e818060c21edbc098198c3dff410b3c..b3d6a1c50d79b6d341f45b42c83023eb89360091 100644 (file)
     return plugin;
   };
   
+  // stores parsers keyed on filetype
+  Popcorn.parsers = {};
+
+  // An interface for extending Popcorn
+  // with parser functionality
+  Popcorn.parser = function( name, type, definition ) {
+
+    if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
+      Popcorn.error("'" + name + "' is a protected function name");
+      return;
+    }
+
+    if ( typeof definition !== "function" ) {
+      return;
+    }
+
+    // Provides some sugar, but ultimately extends
+    // the definition into Popcorn.p
+    
+    var natives = Popcorn.events.all,
+        parseFn,
+        parser = {};
+    
+    parseFn = function ( filename ) {
+        
+      if ( !filename ) {
+        return this;
+      }
+
+      var that = this;
+
+      Popcorn.xhr({
+        url: filename,
+        success: function( data ) {
+
+          var tracksObject = definition( data );
+
+          // creating tracks out of parsed object
+          for ( var key in tracksObject ) {
+            if ( tracksObject.hasOwnProperty(key) ) {
+
+              // an array of tracks of all one type
+              if ( tracksObject[key].constructor === Array ) {
+
+                for (var i = 0, tol = tracksObject[key].length; i < tol; i++) {
+                  if ( typeof that[key] === "function") {
+                    that[key]( tracksObject[key][i] );
+                  }
+                }
+
+              // one single track
+              } else if ( typeof tracksObject[key] === "object" ) {
+                if ( typeof that[key] === "function") {
+                  that[key]( tracksObject[key] );
+                }
+              }
+            }
+          }
+        }
+      });
+
+      return this;
+    };
+
+    // Assign new named definition
+    parser[ name ] = parseFn;
+    
+    // Extend Popcorn.p with new named definition
+    Popcorn.extend( Popcorn.p, parser );
+    
+    // keys the function name by filetype extension
+    Popcorn.parsers[type] = name;
+
+    return parser;
+  };
+  
   
   var setup = {
     url: '',
diff --git a/test/data/parserData.json b/test/data/parserData.json
new file mode 100644 (file)
index 0000000..4974e8d
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "parserTest":
+    {
+      "start": 5,
+      "end": 6
+    }
+}
index a468e7f3cb75099ed4ab3f8b1b72133c8d067ca7..22f26afbce014cbb5a9d7357b82d98e8ce1375a4 100644 (file)
@@ -1063,6 +1063,85 @@ test("XML Response", function () {
 
 });
 
+module("Popcorn Parser");
+
+test("Parsing Functions", function () {
+
+  var expects = 3,
+      count = 0,
+      popperly = Popcorn("#video");
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+    }
+  }
+  
+  expect(expects);
+  
+  stop( 10000 );
+
+  ok(typeof Popcorn.parser === "function", "Popcorn.parser is a function");
+  plus();
+
+  Popcorn.parser( "parseJSON" , "json", function( data ){
+    return data.json;
+  });
+
+  ok(typeof popperly.parseJSON === "function", "Popcorn.parser created a parserJSON function");
+  plus();
+
+  ok(typeof popperly.parseJSON().parseJSON("data/test.js").parseJSON === "function" , "parseJSON function is chainable");
+  plus();
+
+});
+
+test("Parsing Integrity", function () {
+
+  var expects = 2,
+      count = 0,
+      timeOut = 0,
+      interval,
+      poppercore = Popcorn( "#video" );
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+      // clean up added events after tests
+      clearInterval( interval );
+      poppercore.removePlugin( "parserTest" );
+    }
+  }
+  
+  expect(expects);
+  
+  stop( 10000 );
+
+  Popcorn.parser( "parseJSON2" , "json", function( data ){
+    return data.json;
+  });
+
+  Popcorn.plugin("parserTest", {
+    
+    start: function () {
+      ok(true, "parserTest started");
+      plus();
+    },
+    end: function () {
+      ok(true, "parserTest ended");
+      plus();
+    }
+  });
+
+  poppercore.parseJSON2("data/parserData.json");
+
+  // interval used to wait for data to be parsed
+  interval = setInterval( function() {
+    poppercore.currentTime(5).play().currentTime(6);
+  }, 2000);
+
+});
+
 
 
 module("Popcorn Test Runner End");