]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#t479] unit tests and teardown
authorScottDowne <sdowne@learn.senecac.on.ca>
Mon, 16 May 2011 22:33:47 +0000 (18:33 -0400)
committerScottDowne <sdowne@learn.senecac.on.ca>
Mon, 16 May 2011 22:33:47 +0000 (18:33 -0400)
plugins/wordriver/popcorn.wordriver.html
plugins/wordriver/popcorn.wordriver.js
plugins/wordriver/popcorn.wordriver.unit.html [new file with mode: 0644]
plugins/wordriver/popcorn.wordriver.unit.js [new file with mode: 0644]

index a53fbcea4ff6b42ada9cd4a93762a2305054cc6f..d5b5f89fad3802e458a1818b18a28a0060344a4b 100644 (file)
@@ -10,7 +10,7 @@
     document.addEventListener('DOMContentLoaded', function () {
       var p = Popcorn('#video')
       .volume(0)
-      .play() 
+      .play()
       .wordriver({
                                start: 1, // seconds
                                end: 3, // seconds
                                target: "wordriverdiv",
                                color: "blue"
       })
+      .wordriver({
+                               start: 1, // seconds
+                               end: 4, // seconds
+                               text: "hello",
+                               target: "wordriverdiv2",
+                               color: "red"
+      })
+      .wordriver({
+                               start: 2, // seconds
+                               end: 4, // seconds
+                               text: "world",
+                               target: "wordriverdiv2",
+                               color: "blue"
+      });
 
     }, false);
 
@@ -52,6 +66,7 @@
     </video> 
   </div>
   <div id="wordriverdiv" style="height: 200px; width: 200px;"></div>
+  <div id="wordriverdiv2" style="height: 200px; width: 200px;"></div>
 
 </body>
 </html>
index dc601e64806c967409d685cce9f5e5b3fd352f7f..939cc81552727d5a96b544ec702e0f4bf5209fc5 100644 (file)
@@ -4,13 +4,10 @@
 
   var container = {},
       spanLocation = 0,
-      knownSpeaker = false,
-      knownIndex = 0,
-      unknownIndex = 0,
       setupContainer = function( target ) {
   
         container[ target ] = document.createElement( "div" );
-        document.getElementById( target ).appendChild( container[ target ] );
+        document.getElementById( target ) && document.getElementById( target ).appendChild( container[ target ] );
         
         container[ target ].style.height = "100%";
         container[ target ].style.position = "relative";
@@ -69,7 +66,8 @@
         options.word.style.MozTransform =
           options.word.style.webkitTransform =
           options.word.style.OTransform =
-          options.word.style.transform = "translateY(" + ( document.getElementById( options.target ).offsetHeight - options.word.offsetHeight ) + "px)";
+          options.word.style.transform = "translateY(" +
+            ( document.getElementById( options.target ).offsetHeight - options.word.offsetHeight ) + "px)";
         
         options.word.style.opacity = 1;
 
 
         // manually clears the word based on user interaction
         options.word.style.opacity = 0;
+      },
+      _teardown: function( options ) {
+
+        // removes word span from generated container
+        options._container.removeChild( options.word );
+
+        // if no more word spans exist in container, remove container
+        container[ options.target ] &&
+          !container[ options.target ].childElementCount &&
+          document.getElementById( options.target ).removeChild( container[ options.target ] ) &&
+          delete container[ options.target ];
       }
   });
 
diff --git a/plugins/wordriver/popcorn.wordriver.unit.html b/plugins/wordriver/popcorn.wordriver.unit.html
new file mode 100644 (file)
index 0000000..532f3de
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn Word River 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.wordriver.js"></script>
+
+  <script src="popcorn.wordriver.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn Word River 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="wordriverdiv" style="height: 200px; width: 200px;"></div>  
+</body>
+</html>
diff --git a/plugins/wordriver/popcorn.wordriver.unit.js b/plugins/wordriver/popcorn.wordriver.unit.js
new file mode 100644 (file)
index 0000000..6dcc4b2
--- /dev/null
@@ -0,0 +1,91 @@
+test( "Popcorn wordriver Plugin", function () {
+  
+  var popped = Popcorn( "#video" ),
+      expects = 16, 
+      count = 0,
+      firstTrack,
+      secondTrack,
+      wordriverdiv = document.getElementById( "wordriverdiv" );
+
+  expect(expects);
+  
+  function plus() {
+    if ( ++count === expects) {
+      start();
+    }
+  }
+  
+  stop();
+   
+  ok ( "wordriver" in popped, "wordriver is a method of the popped instance" );
+  plus();
+  
+  equals ( wordriverdiv.childElementCount, 0, "initially, there is nothing inside the wordriverdiv" );
+  plus();
+  
+  
+  popped.wordriver({
+               start: 0, // seconds
+               end: 2, // seconds
+               text: "hello",
+               target: "wordriverdiv",
+               color: "red"
+  });
+
+  firstTrack = popped.getLastTrackEventId();
+
+  popped.wordriver({
+               start: 2, // seconds
+               end: 4, // seconds
+               text: "world",
+               target: "wordriverdiv",
+               color: "blue"
+  })
+  .volume(0);
+
+  secondTrack = popped.getLastTrackEventId();
+
+  popped.exec( 0, function() {
+    equals( wordriverdiv.children[0].childElementCount, 1, "wordriverdiv now has one inner element" );
+    plus();
+    equals( wordriverdiv.children[0].children[0].style.opacity, 1, "first word is visible on the page" );
+    plus();
+    equals( wordriverdiv.children[0].children[0].innerHTML, "hello", "first word content is correct" );
+    plus();
+    ok( !wordriverdiv.children[0].children[1], "second word does not exist yet" );
+    plus();
+  });
+  
+  popped.exec( 2, function() {
+    equals( wordriverdiv.children[0].childElementCount, 2, "wordriverdiv now has two inner elements" );
+    plus();
+    equals( wordriverdiv.children[0].children[0].style.opacity, 0, "first word is not visible on the page" );
+    plus();
+    equals( wordriverdiv.children[0].children[0].innerHTML, "hello", "first word content is correct" );
+    plus();
+    equals( wordriverdiv.children[0].children[1].style.opacity, 1, "second word is visible on the page" );
+    plus();
+    equals( wordriverdiv.children[0].children[1].innerHTML, "world", "second word content is correct" );
+    plus();
+  });
+  
+  popped.exec( 4, function() {
+    equals( wordriverdiv.children[0].children[0].style.opacity, 0, "first word is not visible on the page" );
+    plus();
+    equals( wordriverdiv.children[0].children[1].style.opacity, 0, "second word is not visible on the page" );
+    plus();
+
+    popped.pause().removeTrackEvent( firstTrack );
+    equals( wordriverdiv.children[0].childElementCount, 1, "wordriverdiv now has one inner element" );
+    plus();
+    equals( wordriverdiv.children[0].children[0].innerHTML, "world", "first word content is changed" );
+    plus();
+
+    popped.pause().removeTrackEvent( secondTrack );
+    equals( wordriverdiv.childElementCount, 0, "wordriverdiv now has no inner element" );
+    plus();
+  });
+  
+  popped.play();
+
+});