]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#324] added nop functions if needed, and error handling and simplified check
authorScottDowne <sdowne@learn.senecac.on.ca>
Fri, 11 Mar 2011 21:05:28 +0000 (16:05 -0500)
committerScottDowne <sdowne@learn.senecac.on.ca>
Fri, 11 Mar 2011 21:05:28 +0000 (16:05 -0500)
popcorn.js
test/popcorn.unit.js

index bd1c0e49162a298748d078b308fa7945f74b4605..f1225509d5bc7c92a0010438df1e3c746097f4fb 100644 (file)
           document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false);
         }
 
-
-
         return;
       }
 
       // check if entity is a valid string id
       matches = rIdExp.exec( entity );
 
-      // entity is a valid id
-      if ( matches ) {
+      // get video element by id or reference
+      this.video = matches && matches.length && matches[ 2 ] ? document.getElementById( matches[ 2 ] ) : entity;
 
-        // get video element by id
-        this.video = matches.length && matches[ 2 ] ? document.getElementById( matches[ 2 ] ) : null;
-      } else {
+      this.video instanceof Object || Popcorn.error( entity + " is not a valid reference to a video");
 
-        // get video like object by reference
-        this.video = entity;
-      }
+      // if a required function does not exist, simply do nothing when it is called
+      this.video.play = this.video.play || Popcorn.nop;
+      this.video.pause = this.video.pause || Popcorn.nop;
+      this.video.addEventListener = this.video.addEventListener || Popcorn.nop;
+      this.video.dispatchEvent = this.video.dispatchEvent || Popcorn.nop;
 
       Popcorn.addInstance( this );
 
index 32294c6fadc44643235b345328e61c83c63104ba..cf8f95bbc21a8f205e9500c35e41538fcaa5d165 100755 (executable)
@@ -183,7 +183,7 @@ test( "Object", function () {
       popObj = Popcorn( document.getElementById( "video" ) ),
       methods = "load play pause currentTime mute volume roundTime exec removePlugin",
       count = 0,
-      expects = 30;
+      expects = 34;
   
   expect( expects );
   
@@ -244,6 +244,46 @@ test( "Object", function () {
     ok( k in popObj, "instance by reference has method: " + k );
     plus();
   });
+
+  try {
+
+    Popcorn( document.getElementById( "videoNotHere" ) );
+    ok(false, "videoNotHere"); plus();
+  } catch ( e ) {
+
+    equal( e.toString() === "null is not a valid reference to a video", true, e );
+    plus();
+  }
+
+  try {
+
+    Popcorn( "videoNotHere" );
+    ok(false, "videoNotHere"); plus();
+  } catch ( e ) {
+
+    equal( e.toString() === "videoNotHere is not a valid reference to a video", true, e );
+    plus();
+  }
+
+  try {
+
+    Popcorn( 1 );
+    ok(false, 1); plus();
+  } catch ( e ) {
+
+    equal( e.toString() === "1 is not a valid reference to a video", true, e );
+    plus();
+  }
+
+  try {
+
+    Popcorn();
+    ok(false, "undefined"); plus();
+  } catch ( e ) {
+
+    equal( e.toString() === "undefined is not a valid reference to a video", true, e );
+    plus();
+  }
 });
 
 module("Popcorn Methods");