From 1e3fffdb1a4f790b078be1e857ef906102114d58 Mon Sep 17 00:00:00 2001 From: stevenaw Date: Tue, 18 Jan 2011 12:56:44 -0500 Subject: [PATCH] LastFM plugin, unit tests [#169 state:peer-review-requested] --- plugins/lastfm/popcorn.lastfm.html | 55 +++++++++++++++ plugins/lastfm/popcorn.lastfm.js | 91 +++++++++++++++++++++++++ plugins/lastfm/popcorn.lastfm.unit.html | 45 ++++++++++++ plugins/lastfm/popcorn.lastfm.unit.js | 53 ++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 plugins/lastfm/popcorn.lastfm.html create mode 100644 plugins/lastfm/popcorn.lastfm.js create mode 100644 plugins/lastfm/popcorn.lastfm.unit.html create mode 100644 plugins/lastfm/popcorn.lastfm.unit.js diff --git a/plugins/lastfm/popcorn.lastfm.html b/plugins/lastfm/popcorn.lastfm.html new file mode 100644 index 0000000..c98e184 --- /dev/null +++ b/plugins/lastfm/popcorn.lastfm.html @@ -0,0 +1,55 @@ + + + + Popcorn LastFM Plug-in Demo + + + + + + + +

Popcorn LastFM Plug-in Demo

+

Artist information for 'yacht' will appear at 5 seconds and disappear at 15 seconds.

+

Artist information for 'Coldplay' will appear at 20 seconds and disappear at 45 seconds.

+
+ +
+
+
+ + diff --git a/plugins/lastfm/popcorn.lastfm.js b/plugins/lastfm/popcorn.lastfm.js new file mode 100644 index 0000000..ad390ff --- /dev/null +++ b/plugins/lastfm/popcorn.lastfm.js @@ -0,0 +1,91 @@ +// PLUGIN: LASTFM + +(function (Popcorn) { + + /** + * LastFM popcorn plug-in + * Appends information about a LastFM artist to an element on the page. + * Options parameter will need a start, end, target and artist. + * 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 + * Artist is the name of who's LastFM information you wish to show + * Target is the id of the document element that the images are + * appended to, this target element must exist on the DOM + * + * @param {Object} options + * + * Example: + var p = Popcorn('#video') + .lastfm({ + start: 5, // seconds, mandatory + end: 15, // seconds, mandatory + artist: 'yacht', // mandatory + target: 'lastfmdiv' // mandatory + } ) + * + */ + Popcorn.plugin( "lastfm" , { + + manifest: { + about:{ + name: "Popcorn LastFM Plugin", + version: "0.1", + author: "Steven Weerdenburg", + website: "http://sweerdenburg.wordpress.com/" + }, + options:{ + start : {elem:'input', type:'text', label:'In'}, + end : {elem:'input', type:'text', label:'Out'}, + target : 'lastfm-container', + artist : {elem:'input', type:'text', label:'Artist'} + } + }, + + _setup: function( options ) { + options.container = document.createElement( 'div' ); + options.container.style.display = "none"; + if ( document.getElementById( options.target ) ) { + document.getElementById( options.target ).appendChild( options.container ); + } + + var htmlString = "";; + $.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+ options.artist +"&api_key=30ac38340e8be75f9268727cb4526b3d&format=json&callback=?", + function(data){ + htmlString += '

'+data.artist.name+'

'; + htmlString += ''; + htmlString += '

'+ data.artist.bio.summary +'

'; + htmlString += '

Tags

'; + htmlString += '

Similar

'; + options.container.innerHTML = htmlString; + } + ); + }, + /** + * @member LastFM + * 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.container.style.display = "inline"; + }, + /** + * @member LastFM + * 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.container.style.display = "none"; + } + }); + +})( Popcorn ); diff --git a/plugins/lastfm/popcorn.lastfm.unit.html b/plugins/lastfm/popcorn.lastfm.unit.html new file mode 100644 index 0000000..f7bf0f0 --- /dev/null +++ b/plugins/lastfm/popcorn.lastfm.unit.html @@ -0,0 +1,45 @@ + + + + Popcorn API + + + + + + + + + + + + +

Popcorn LastFM Plugin

+

+
+

+
    +
    + + +
    + + diff --git a/plugins/lastfm/popcorn.lastfm.unit.js b/plugins/lastfm/popcorn.lastfm.unit.js new file mode 100644 index 0000000..80c70b1 --- /dev/null +++ b/plugins/lastfm/popcorn.lastfm.unit.js @@ -0,0 +1,53 @@ +test("Popcorn LastFM Plugin", function () { + + var popped = Popcorn("#video"), + expects = 5, + count = 0, + interval, + interval2, + lastfmdiv = document.getElementById('lastfmdiv'); + + expect( expects ); + + function plus() { + if ( ++count === expects) { + start(); + } + } + + stop(); + + ok('lastfm' in popped, "lastfm is a method of the popped instance"); + plus(); + + equals ( lastfmdiv.innerHTML, "", "initially, there is nothing inside the lastfmdiv" ); + plus(); + + popped.lastfm({ + start: 1, // seconds + end: 3, // seconds + artist: 'yacht', + target: 'lastfmdiv' + } ); + + interval = setInterval( function() { + if( popped.currentTime() > 1 && popped.currentTime() < 3 ) { + ok( /display: inline;/.test( lastfmdiv.innerHTML ), "Div contents are displayed" ); + plus(); + ok( /img/.test( lastfmdiv.innerHTML ), "An image exists" ); + plus(); + clearInterval( interval ); + } + }, 500); + + interval2 = setInterval( function() { + if( popped.currentTime() > 3 ) { + ok( /display: none;/.test( lastfmdiv.innerHTML ), "Div contents are hidden again" ); + plus(); + clearInterval( interval2 ); + } + }, 500); + popped.volume(0); + popped.play(); + +}); -- 2.43.0