From 771b5f5c66579e7d0f6ae0a7a5eb55679ad5f614 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Fri, 1 Apr 2011 09:44:34 -0400 Subject: [PATCH] [#415] Adds Popcorn.prototype.position(); Adds unit tests --- popcorn.js | 55 ++++++++++++++++++------- test/popcorn.unit.js | 96 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 132 insertions(+), 19 deletions(-) diff --git a/popcorn.js b/popcorn.js index 8b064aa..2220f0a 100644 --- a/popcorn.js +++ b/popcorn.js @@ -397,6 +397,33 @@ return this; } + // Popcorn Object Element Utils + position: function() { + var media = this.video, + bounds = media.getBoundingClientRect(), + doc = media.ownerDocument, + docElem = document.documentElement, + body = document.body, + clientTop, clientLeft, scrollTop, scrollLeft, top, left; + + // Determine correct clientTop/Left + clientTop = docElem.clientTop || body.clientTop || 0; + clientLeft = docElem.clientLeft || body.clientLeft || 0; + + // Determine correct scrollTop/Left + scrollTop = (global.pageYOffset && docElem.scrollTop || body.scrollTop ); + scrollLeft = (global.pageXOffset && docElem.scrollLeft || body.scrollLeft); + + // Temp top/left + top = Math.ceil( bounds.top + scrollTop - clientTop ); + left = Math.ceil( bounds.left + scrollLeft - clientLeft ); + + for ( var p in bounds ) { + bounds[ p ] = Math.round( bounds[ p ] ); + } + + return Popcorn.extend({}, bounds, { top: top, left: left }); + } }); Popcorn.Events = { @@ -1061,25 +1088,25 @@ script.onload = script.onreadystatechange = function() { - if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) { + if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) { - // Handling remote script loading callbacks - if ( isScript ) { + // Handling remote script loading callbacks + if ( isScript ) { - // getScript - success && success(); - } + // getScript + success && success(); + } - // Executing for JSONP requests - if ( isFired ) { + // Executing for JSONP requests + if ( isFired ) { - // Garbage collect the callback - delete window[ callback ]; + // Garbage collect the callback + delete window[ callback ]; - // Garbage collect the script resource - head.removeChild( script ); - } - } + // Garbage collect the script resource + head.removeChild( script ); + } + } }; script.src = url; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 0f63263..4d4aa96 100755 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -307,6 +307,92 @@ test("exec", function () { }); +module("Popcorn Position"); +test("position", function () { + + var pops = [ Popcorn("#pos-1"), + Popcorn("#pos-2"), + Popcorn("#pos-3"), + Popcorn("#pos-4") + ], + expects = 22, + count = 0, + tests = [ + {"top":0,"height":150,"width":300,"bottom":150,"left":1123,"right":1423}, + {"top":100,"height":150,"width":300,"bottom":250,"left":1023,"right":1323}, + {"height":150,"width":300,"left":1366,"right":1666}, + {"top":0,"height":150,"width":300,"bottom":150,"left":1123,"right":1423} + ], + bounds, + against, + styles; + + expect( expects ); + + function plus(){ + if ( ++count == expects ) { + start(); + } + } + + stop( 10000 ); + + pops.forEach(function( pop, idx ) { + + bounds = pop.position(); + against = tests[ idx ]; + styles = document.getElementById( pop.video.id ).style; + + for ( var p in bounds ) { + if ( p in against ) { + equal( bounds[ p ], against[ p ], "video#" + pop.video.id + ": " + p ); + plus(); + } + } + }); +}); + +test("position called from plugin", function () { + + var $pop = Popcorn("#video"), + expects = 3, + count = 0; + + expect( expects ); + + function plus(){ + if ( ++count == expects ) { + start(); + Popcorn.removePlugin("positionPlugin"); + delete Popcorn.manifest.positionPlugin; + } + } + + stop( 10000 ); + + Popcorn.plugin( "positionPlugin" , function(){ + return { + _setup: function( options ) { + ok( "position" in this, "this.position() avaliable in _setup"); + plus(); + }, + start: function(event, options){ + + ok( "position" in this, "this.position() avaliable in start"); + plus(); + }, + end: function(event, options){ + ok( "position" in this, "this.position() avaliable in end"); + plus(); + } + }; + }); + + $pop.positionPlugin({ + start: 0, + end: 1 + }).currentTime(0).play(); +}); module("Popcorn Events"); @@ -1920,11 +2006,11 @@ test("Parser Support", function () { } }); - Popcorn.parser( "parseAudio", function( data ){ - ok( typeof data.json === "object", "data.json exists"); - plus(); - return data.json; - }); + Popcorn.parser( "parseAudio", function( data ){ + ok( typeof data.json === "object", "data.json exists"); + plus(); + return data.json; + }); audiocorn.parseAudio("data/parserAudio.json", function() { -- 2.43.0