]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#t383] unit tests and major code cleanup
authorScottDowne <sdowne@learn.senecac.on.ca>
Mon, 16 May 2011 20:13:18 +0000 (16:13 -0400)
committerScottDowne <sdowne@learn.senecac.on.ca>
Mon, 16 May 2011 20:13:18 +0000 (16:13 -0400)
plugins/gml/gmlplayer.js [deleted file]
plugins/gml/popcorn.gml.html
plugins/gml/popcorn.gml.js
plugins/gml/popcorn.gml.unit.html [new file with mode: 0644]
plugins/gml/popcorn.gml.unit.js [new file with mode: 0644]

diff --git a/plugins/gml/gmlplayer.js b/plugins/gml/gmlplayer.js
deleted file mode 100644 (file)
index 451cf30..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-var _stroke = 0; // current stroke
-var onPt = 0; // current point on stroke
-var onStroke = 0; // current point on stroke
-var start = +(new Date());
-var x, y, rotation = false;
-var strokes=0;
-var data = {};
-var toggle = 0;
-var ready = false;
-var play = function(){};
-var dur = 0;
-var options = {};
-var video = document.getElementById("video");
-
-// empty on purpose
-void setup() {}
-void draw() {
-
-  play();
-}
-
-void construct( data, options ) {
-
-  size(640,640);
-  frameRate(60);
-  smooth();
-  reset();
-  noLoop();
-
-  dur = (options.end - options.start);
-  drawingDur = (options.endDrawing - options.start);
-
-  this.options = options;
-
-  var si = setInterval(function() {
-
-    if (!data) return;
-    clearInterval(si);
-
-    this.data = data;
-    strokes = this.data.gml.tag.drawing.stroke;
-    var tag = this.data.gml.tag;
-    var app_name = tag.header && tag.header.client && tag.header.client.name;
-    rotation = app_name == 'Graffiti Analysis 2.0: DustTag' ||
-               app_name == 'DustTag: Graffiti Analysis 2.0' ||
-               app_name == 'Fat Tag - Katsu Edition';
-
-
-    play = function() {
-
-      if ( video.currentTime < options.endDrawing ) {
-        seek( ( video.currentTime - options.start ) / dur * strokes.pt.length );
-      }
-
-    };
-  }, 50);
-}
-
-
-void reset() {
-  background( 0 );
-  onPt = onStroke = 0;
-  x = y = null;
-}
-
-function drawLine(x,y,x2,y2) {
-    _x = rotation ? y*height : x*width;
-    _y = rotation ? width-(x*width) : y*height;
-    _x2 = rotation ? y2 * height : x2*width;
-    _y2 = rotation ? width - (x2 * width) : y2*height;
-    stroke(0);
-    strokeWeight(13);
-    strokeCap(SQUARE);
-    line(_x,_y,_x2,_y2);
-    stroke(255);
-    strokeWeight(12);
-    strokeCap(ROUND);
-    line(_x,_y,_x2,_y2);
-    //ol = { x: _x, y: _y, x2: _x2, y2: _y2 };
-}
-
-function seek( point ) {
-    if ( point < onPt ) reset();
-
-    var start; var hyp;
-    while (onPt <= point) {
-        if (!strokes) return;
-        _stroke = strokes.length ? strokes[onStroke] : strokes;
-        pt = _stroke.pt[onPt];
-        var p = onPt;
-        if (x !== null ) drawLine(x,y,pt.x,pt.y);
-        /*if (onPt) {
-if (++onPt >= _stroke.pt.length) {
-//if (!strokes.length || ++onStroke >= strokes.length) return reset();
-//onPt = 0;
-}
-}*/
-        x = pt.x;
-        y = pt.y;
-        if (onPt==p) onPt++; //went back to 0
-    }
-}
-
index 8a19bc92ebda9dac8d6ad1acfcb4329f4ad9e414..ad607fde4b6d3c78e9ab962e6f34d2e8ab01704f 100644 (file)
@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
 <head>
-  <title>Popcorn attribution Plug-in Demo</title>
+  <title>Popcorn Grafiti Markup Language Plug-in Demo</title>
   
   <script src="../../popcorn.js"></script>
   <script src="popcorn.gml.js"></script>
       .volume(0)
       .gml({
         start: 0, // seconds
-        end: 10, // seconds
+        end: 5, // seconds
         gmltag: '29582',
-        target: 'attribdiv'
+        target: 'gmldiv'
+      })
+      .gml({
+        start: 10, // seconds
+        end: 20, // seconds
+        gmltag: '155',
+        target: 'gmldiv'
+      })
+      .gml({
+        start: 20, // seconds
+        end: 40, // seconds
+        gmltag: '161',
+        target: 'gmldiv'
       });
-      /*.attribution({
-        start: 5, // seconds
-        end: 15, // seconds
-        nameofwork: "Internet",
-        nameofworkurl:"http://www.archive.org/details/CC1232_internet",
-        copyrightholder:"The Computer Chronicles",
-        license:"CC-BY-NC-ND",
-        licenseurl: "http://creativecommons.org/licenses/by-nc-nd/2.0/",
-        target: 'attribdiv'
-      } )*/
 
     }, false);
       
@@ -33,8 +35,9 @@
 </head>
 <body>
   <h1 id="qunit-header">Popcorn Attribution Plug-in Demo</h1>
-  <p> A attribution displaying 'A Shared Culture, Jesse Dylan, license: CC-BY-N6' will appear at 0 seconds and disappear at 10 seconds.</p>
-  <p> A attribution displaying 'Internet, The Computer Chronicles' will appear at 5 seconds and disappear at 15 seconds.</p>
+  <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/29582">29582</a> will appear at 0-5 seconds.</p>
+  <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/155">155</a> will appear at 10-20 seconds.</p>
+  <p> A Processing canvas displaying the GML tag <a href="http://000000book.com/data/161">161</a> will appear at 20-40 seconds.</p>
   <div>
     <video id='video' 
       controls preload='none' 
@@ -53,7 +56,6 @@
 
     </video> 
   </div>
-  <div id="attribdiv" width="50%" height="50%">
-  </div>
+  <div id="gmldiv"></div>
 </body>
 </html>
index aa12d19feb738550e20e4c150086245c330c8eb9..d9ea26b11864892116f4ca731758846686561320 100644 (file)
 
 (function (Popcorn) {
 
-  var processingLoaded = false;
-
-  var ajax = function ajax(url) {
-
-    var xhr = new XMLHttpRequest();
-    xhr.open("GET", url, false);
-    xhr.setRequestHeader("If-Modified-Since", "Fri, 1 Jan 1960 00:00:00 GMT");
-    xhr.send(null);
-    // failed request?
-    if (xhr.status !== 200 && xhr.status !== 0) { throw ("XMLHttpRequest failed, status code " + xhr.status); }
-    return xhr.responseText;
-  };
+  var processingLoaded = false,
+      gmlPlayer = function( $p ) {
+
+        var _stroke = 0,
+            onPt = 0, 
+            onStroke = 0,
+            x = null,
+            y = null,
+            rotation = false,
+            strokes = 0,
+            play =  function() {},
+            reset = function() {
+
+              $p.background( 0 );
+              onPt = onStroke = 0;
+              x = y = null;
+            },
+            drawLine = function( x, y, x2, y2 ) {
+
+              if ( rotation ) {
+
+                _x  = y * $p.height;
+                _y  = $p.width - ( x * $p.width );
+                _x2 = y2 * $p.height;
+                _y2 = $p.width - ( x2 * $p.width );
+              } else {
+
+                _x  = x * $p.width;
+                _y  = y * $p.height;
+                _x2 = x2 * $p.width;
+                _y2 = y2 * $p.height;
+              }
+              $p.stroke( 0 );
+              $p.strokeWeight( 13 );
+              $p.strokeCap( $p.SQUARE );
+              $p.line( _x, _y, _x2, _y2 );
+              $p.stroke( 255 );
+              $p.strokeWeight( 12 );
+              $p.strokeCap( $p.ROUND );
+              $p.line( _x, _y, _x2, _y2 );
+            },
+            seek = function( point ) {
+
+              if ( point < onPt ) reset();
+
+              while ( onPt <= point ) {
+
+                if ( !strokes ) return;
+                _stroke = strokes[ onStroke ] || strokes;
+                var pt = _stroke.pt[ onPt ];
+                var p = onPt;
+                if (x !== null ) drawLine( x, y, pt.x, pt.y );
+
+                x = pt.x;
+                y = pt.y;
+                if ( onPt === p ) onPt++;
+              }
+            };
+
+        $p.draw = function() {
+
+          play();
+        };
+        $p.setup = function() {};
+        $p.construct = function( media, data, options ) {
+
+          $p.size( 640,640 );
+          $p.frameRate( 60 );
+          $p.smooth();
+          reset();
+          $p.noLoop();
+
+          var si =  setInterval(function() {
+
+            if ( !data ) return;
+            clearInterval( si );
+
+            strokes = data.gml.tag.drawing.stroke;
+
+            var drawingDur = ( options.end - options.start ) / ( strokes.pt || (function( strokes ) {
+
+              var rStrokes = [];
+
+              for ( var i = 0, sl = strokes.length; i < sl; i++ ) {
+
+                rStrokes = rStrokes.concat( strokes[ i ].pt );
+              }
+
+              return rStrokes;
+            })( strokes ) ).length;
+
+            var tag = data.gml.tag;
+            var app_name =  tag.header && tag.header.client && tag.header.client.name;
+            rotation = app_name === 'Graffiti Analysis 2.0: DustTag' ||
+                       app_name === 'DustTag: Graffiti Analysis 2.0' ||
+                       app_name === 'Fat Tag - Katsu Edition';
+
+            play = function() {
+
+              if ( media.currentTime < options.endDrawing ) {
+
+                seek( ( media.currentTime - options.start ) / drawingDur );
+              }
+            };
+          }, 50);
+        };
+      };
 
   Popcorn.getScript( "http://processingjs.org/content/download/processing-js-1.1.0/processing-1.1.0.js", function() {
 
   });
   
   /**
-*/
+   * Grafiti markup Language (GML) popcorn plug-in 
+   * Renders a GML tag inside an HTML element
+   * Options parameter will need a mandatory start, end, target, gmltag.
+   * Optional parameters: none.
+   * Start is the time that you want this plug-in to execute
+   * End is the time that you want this plug-in to stop executing 
+   * Target is the id of the document element that you wish to render the grafiti in
+   * gmltag is the numerical reference to a gml tag via 000000book.com
+   * @param {Object} options
+   * 
+   * Example:
+     var p = Popcorn('#video')
+       .gml({
+         start: 0, // seconds
+         end: 5, // seconds
+         gmltag: '29582',
+         target: 'gmldiv'
+       });
+   *
+   */
   Popcorn.plugin( "gml" , {
-      
-    /**
-*/
+
     _setup : function( options ) {
 
       var self = this;
       
       options.endDrawing = options.endDrawing || options.end;
+
       // create a canvas to put in the target div
-      options.container = document.createElement( 'canvas' );
+      options.container = document.createElement( "canvas" );
 
       options.container.style.display = "none";
-      options.container.setAttribute( 'id', 'canvas' + options.gmltag );
+      options.container.setAttribute( "id", "canvas" + options.gmltag );
 
       if ( document.getElementById( options.target ) ) {
         document.getElementById( options.target ).appendChild( options.container );
       }
 
 
-      // makes sure both processing.js and the gml player are loaded
+      // makes sure both processing.js and the gml data are loaded
       var readyCheck = setInterval(function() {
 
         if ( !processingLoaded ) {
           return;
         }
 
-        clearInterval(readyCheck);
+        clearInterval( readyCheck );
         Popcorn.getJSONP( "http://000000book.com/data/" + options.gmltag + ".json?callback=", function( data ) {
 
-          new Processing( options.container, ajax( "gmlplayer.js" ) );
-          options.pjsInstance = Processing.getInstanceById( 'canvas' + data.id );
-          options.pjsInstance.construct( data, options );
+          new Processing( options.container, gmlPlayer );
+          options.pjsInstance = Processing.getInstanceById( "canvas" + data.id );
+          options.pjsInstance.construct( self.media, data, options );
           options._running && options.pjsInstance.loop();
         }, false );
       }, 5);
     },
     /**
-*/
+     * @member gml 
+     * The start function will be executed when the currentTime 
+     * of the video  reaches the start time provided by the 
+     * options variable
+     */
     start: function( event, options ) {
 
       options.pjsInstance && options.pjsInstance.loop();
       options.container.style.display = "block";
     },
     /**
-*/
+     * @member gml 
+     * The end function will be executed when the currentTime 
+     * of the video  reaches the end time provided by the 
+     * options variable
+     */
     end: function( event, options ) {
 
       options.pjsInstance && options.pjsInstance.noLoop();
       options.container.style.display = "none";
+    },
+    _teardown: function( options ) {
+
+      options.pjsInstance && options.pjsInstance.exit();
+
+      if ( document.getElementById( options.target ) ) {
+        document.getElementById( options.target ).removeChild( options.container );
+      }
     }
   });
 })( Popcorn );
diff --git a/plugins/gml/popcorn.gml.unit.html b/plugins/gml/popcorn.gml.unit.html
new file mode 100644 (file)
index 0000000..c0761c5
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn GML Plugin</title>
+  <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
+  <script src="../../test/qunit/qunit.js"></script>
+  <!-- 
+    do not move - this must be called immediately prior to 
+    popcorn-api-draft.js
+  -->
+  
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.gml.js"></script>
+
+  <script src="popcorn.gml.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn GML Plugin</h1>
+  <h2 id="qunit-banner"></h2>
+  <div id="qunit-testrunner-toolbar"></div>
+  <h2 id="qunit-userAgent"></h2>
+  <ol id="qunit-tests"></ol>
+  <div id="qunit-fixture">  </div>
+  
+  <video id='video' 
+    controls preload='none' 
+    width= '250px'
+    poster="../../test/poster.png"> 
+
+    <source id='mp4'
+      src="../../test/trailer.mp4"
+      type='video/mp4; codecs="avc1, mp4a"'> 
+
+    <source id='ogv'
+      src="../../test/trailer.ogv"
+      type='video/ogg; codecs="theora, vorbis"'> 
+
+    <p>Your user agent does not support the HTML5 Video element.</p> 
+
+  </video> 
+  <div id="gmldiv"></div>  
+</body>
+</html>
diff --git a/plugins/gml/popcorn.gml.unit.js b/plugins/gml/popcorn.gml.unit.js
new file mode 100644 (file)
index 0000000..63676b9
--- /dev/null
@@ -0,0 +1,67 @@
+test("Popcorn GML Plugin", function () {
+  
+  var popped = Popcorn("#video"),
+      expects = 8, 
+      count = 0,
+      setupId,
+      gmldiv = document.getElementById('gmldiv');
+
+  expect(expects);
+  
+  function plus() {
+    if ( ++count===expects) {
+      start();
+    }
+  }
+  
+  stop( 10000 );
+   
+  ok ('gml' in popped, "gml is a method of the popped instance");
+  plus();
+  
+  equals ( gmldiv.childElementCount, 0, "initially, there is nothing inside the gmldiv" );
+  plus();
+  
+  popped.gml({
+    start: 0, // seconds
+    end: 1, // seconds
+    gmltag: '29582',
+    target: 'gmldiv'
+  })
+  .gml({
+    start: 1, // seconds
+    end: 2, // seconds
+    gmltag: '155',
+    target: 'gmldiv'
+  }).volume( 0 );
+
+  setupId = popped.getLastTrackEventId();
+
+  popped.exec( 0, function() {
+    equals( gmldiv.children[0].style.display , "block", "first GML is visible on the page" );
+    plus();
+  });
+  
+  popped.exec( 1, function() {
+    equals( gmldiv.children[1].style.display , "block", "second GML is visible on the page" );
+    plus();
+  });
+  
+  popped.exec( 2, function() {
+    equals( gmldiv.children[1].style.display , "none", "second GML is no longer visible on the page" );
+    plus();
+    equals( gmldiv.children[0].style.display , "none", "first GML is no longer visible on the page" );
+    plus();
+
+    popped.pause().removeTrackEvent( setupId );
+
+    ok( !Processing.getInstanceById( "canvas155" ), "Processing insteance was properly destroyed"  );
+    plus();
+
+    ok( !gmldiv.children[1], "canvas element was properly removed"  );
+    plus();
+  });
+  
+  popped.play();
+
+});