From c762accb39054f31d98ec5a710ab13d98da4aee3 Mon Sep 17 00:00:00 2001 From: Steve Heffernan Date: Fri, 3 Feb 2012 17:53:59 -0800 Subject: [PATCH] Fixed issue with .one scope. Improved destroy method. Still building out tests. --- CHANGELOG.md | 1 + src/component.js | 2 +- src/player.js | 5 +- test/test.html | 2 + test/test.js | 269 ++++++++++++++++++++++++++++++++--------------- 5 files changed, 195 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 790cac6ba..b88a438bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +* Improved player.destory(). Now removes elements and references. * Refactored API to be more immediately available. -------------------------------------------------------------------------------- ^ ADD NEW CHANGES ABOVE ^ diff --git a/src/component.js b/src/component.js index 454dcb7e2..f97d552ec 100644 --- a/src/component.js +++ b/src/component.js @@ -154,7 +154,7 @@ _V_.Component = _V_.Class.extend({ return _V_.triggerEvent(this.el, type, e); }, one: function(type, fn) { - _V_.one.call(this, this.el, type, fn); + _V_.one(this.el, type, _V_.proxy(this, fn)); }, /* Ready - Trigger functions when component is ready diff --git a/src/player.js b/src/player.js index cc792bd89..0031ac8b0 100644 --- a/src/player.js +++ b/src/player.js @@ -110,7 +110,10 @@ _V_.Player = _V_.Component.extend({ // Ensure that tracking progress and time progress will stop and plater deleted this.stopTrackingProgress(); this.stopTrackingCurrentTime(); - delete _V_.players[this.id] + _V_.players[this.id] = null; + delete _V_.players[this.id]; + this.tech.destroy(); + this.el.parentNode.removeChild(this.el); }, createElement: function(type, options){}, diff --git a/test/test.html b/test/test.html index 948dfc891..ce705a0d8 100644 --- a/test/test.html +++ b/test/test.html @@ -42,5 +42,7 @@

    test markup
    + +
    diff --git a/test/test.js b/test/test.js index 151ab4ecf..2af291c93 100644 --- a/test/test.js +++ b/test/test.js @@ -4,26 +4,66 @@ // http://saucelabs.com/blog/index.php/2011/06/javascript-unit-testing-with-jellyfish-and-ondemand/ // https://github.com/admc/jellyfish/blob/master/test/fun/jfqunit.js -var tagCode = ''; +function createVideoTag(id){ + var tagCode, tag, attrs; + + tagCode = ''; + + tag = document.createElement("video"); + + tag.id = "vid1"; + tag.controls = true; + tag.className = "video-js vjs-default-skin"; + tag.preload = "auto"; + tag.width = "640"; + tag.height = "264"; + tag.poster = "http://video-js.zencoder.com/oceans-clip.png"; + + source1 = document.createElement("source"); + source1.src = "http://video-js.zencoder.com/oceans-clip.mp4"; + source1.type = "video/mp4"; + tag.appendChild(source1); + + source2 = document.createElement("source"); + source2.src = "http://video-js.zencoder.com/oceans-clip.webm"; + source2.type = "video/webm"; + tag.appendChild(source2); + + source3 = document.createElement("source"); + source3.src = "http://video-js.zencoder.com/oceans-clip.ogv"; + source3.type = "video/ogg"; + tag.appendChild(source3); + + return tag; +} function playerSetup(){ - document.body.innerHTML += tagCode; + + _V_.log(_V_.players) + + _V_.el("player_box").appendChild(createVideoTag()) + var vid = document.getElementById("vid1"); this.player = _V_(vid); stop(); + this.player.ready(_V_.proxy(this, function(){ start(); })); } function playerTeardown(){ + stop(); _V_("vid1").destroy(); - document.body.removeChild(document.getElementById("vid1")); + // document.body.removeChild(document.getElementById("vid1")); + setTimeout(function(){ + start(); + }, 500); } module("Video.js setup", { @@ -35,6 +75,145 @@ test("Player Set Up", function() { ok(this.player); }); +/* Methods +================================================================================ */ +module("API Methods", { + setup: playerSetup, + teardown: playerTeardown +}); + +function failOnEnded() { + this.player.one("ended", _V_.proxy(this, function(){ + start(); + })); +} + +// Play Method +test("play()", 1, function() { + stop(); + + this.player.one("playing", _V_.proxy(this, function(){ + ok(true); + start(); + })); + + this.player.play(); + + failOnEnded.call(this); +}); + +// Pause Method +test("pause()", 1, function() { + stop(); + + // Flash doesn't currently like calling pause immediately after 'playing'. + this.player.one("timeupdate", _V_.proxy(this, function(){ + + this.player.pause(); + + })); + + this.player.addEvent("pause", _V_.proxy(this, function(){ + ok(true); + start(); + })); + + this.player.play(); +}); + +// Paused Method +test("paused()", 2, function() { + stop(); + + this.player.one("timeupdate", _V_.proxy(this, function(){ + equal(this.player.paused(), false); + this.player.pause(); + })); + + this.player.addEvent("pause", _V_.proxy(this, function(){ + equal(this.player.paused(), true); + start(); + })); + + this.player.play(); +}); + +test("currentTime()", 1, function() { + stop(); + + // Try for 3 time updates, sometimes it updates at 0 seconds. + var tries = 0; + + this.player.one("loadeddata", _V_.proxy(this, function(){ + + this.player.addEvent("timeupdate", _V_.proxy(this, function(){ + + _V_.log(tries) + + if (this.player.currentTime() > 0) { + ok(true, "Time is greater than 0."); + start(); + } else { + tries++; + } + + if (tries >= 3) { + start(); + } + })); + + })); + + this.player.play(); +}); + + +test("currentTime(seconds)", 2, function() { + stop(); + + // var afterPlayback = _V_.proxy(this, function(){ + // this.player.currentTime(this.player.duration() / 2); + // + // this.player.addEvent("timeupdate", _V_.proxy(this, function(){ + // ok(this.player.currentTime() > 0, "Time is greater than 0."); + // + // this.player.pause(); + // + // this.player.addEvent("timeupdate", _V_.proxy(this, function(){ + // ok(this.player.currentTime() == 0, "Time is 0."); + // start(); + // })); + // + // this.player.currentTime(0); + // })); + // }); + + this.player.play(); + + this.player.one("timeupdate", _V_.proxy(this, function(){ + + this.player.currentTime(this.player.duration() / 2); + + this.player.one("timeupdate", _V_.proxy(this, function(){ + ok(this.player.currentTime() > 0, "Time is greater than 0."); + + this.player.pause(); + this.player.currentTime(0); + + this.player.one("timeupdate", _V_.proxy(this, function(){ + + ok(this.player.currentTime() == 0, "Time is 0."); + start(); + + })); + + })); + + + })); + +}); + /* Events ================================================================================ */ module("API Events", { @@ -96,78 +275,4 @@ test("Initial Events", 11, function() { } this.player.play(); -}); - -/* Methods -================================================================================ */ -module("API Methods", { - setup: playerSetup, - teardown: playerTeardown -}); - -// Play Method -test("play()", function() { - this.player.addEvent("playing", _V_.proxy(this, function(){ - start(); - ok(true); - })); - this.player.play(); -}); - -// Pause Method -test("pause()", function() { - this.player.addEvent("pause", _V_.proxy(this, function(){ - start(); - ok(true); - })); - this.player.addEvent("playing", _V_.proxy(this, function(){ - this.player.pause(); - })); - this.player.play(); -}); - -// test("currentTime()", function() { -// -// // Need video loaded before we can call current time -// this.player.addEvent("loadstart", _V_.proxy(this, function(){ -// start(); -// ok(true, "vid loading"); -// -// // Watch for timeudpate -// this.player.addEvent("timeupdate", _V_.proxy(this, function(){ -// start(); -// equal(this.player.currentTime(), 0, "time is 0"); -// this.player.removeEvent("timeupdate", arguments.callee); -// -// // Test again for later time -// this.player.addEvent("timeupdate", _V_.proxy(this, function(){ -// start(); -// notEqual(this.player.currentTime(), 0, "time is not 0"); -// this.player.removeEvent("timeupdate", arguments.callee); -// })); -// // Stop and trigger time -// stop(); -// this.player.currentTime(10); -// -// })); -// // Stop and trigger time -// stop(); -// this.player.currentTime(0); -// -// })); -// -// stop(); -// this.player.load(); -// -// -// -// // Watch for timeudpate -// this.player.addEvent("timeupdate", _V_.proxy(this, function(){ -// start(); -// notEqual(this.player.currentTime(), 0, "time is not 0"); -// this.player.removeEvent("timeupdate", arguments.callee); -// })); -// // Stop and trigger time -// stop(); -// this.player.load(); -// }); \ No newline at end of file +}); \ No newline at end of file