Skip to content

Commit e56cc31

Browse files
committed
Clean Up, Add Test for Loop and Remove some Embedly specific stuff
1 parent 6b38d08 commit e56cc31

File tree

5 files changed

+65
-42
lines changed

5 files changed

+65
-42
lines changed

src/core.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ playerjs.isObject = function(obj){
4848

4949
playerjs.isNone = function(obj){
5050
return (obj === null || obj === undefined);
51+
};
52+
53+
// Assert
54+
playerjs.assert = function(test, msg) {
55+
if (!test) {
56+
throw msg || "Player.js Assert Failed";
57+
}
5158
};

src/keeper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ playerjs.Keeper.prototype.add = function(id, event, cb, ctx, one){
5656
}
5757
};
5858

59-
playerjs.Keeper.prototype.execute = function(event, id, data){
59+
playerjs.Keeper.prototype.execute = function(event, id, data, ctx){
6060
if (!this.has(event, id)){
6161
return false;
6262
}
@@ -73,7 +73,7 @@ playerjs.Keeper.prototype.execute = function(event, id, data){
7373

7474
execute.push({
7575
cb: d.cb,
76-
ctx: d.ctx? d.ctx: window,
76+
ctx: d.ctx? d.ctx: ctx,
7777
data: data
7878
});
7979

src/player.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ playerjs.EVENTS = {
1515
ENDED: 'ended',
1616
SEEKED: 'seeked',
1717
TIMEUPDATE: 'timeupdate',
18-
PROGRESS: 'progress', // Not implemented yet.
19-
ERROR: 'error' // Not implemented yet.
18+
PROGRESS: 'progress',
19+
ERROR: 'error'
2020
};
2121

2222
playerjs.METHODS = [
@@ -128,7 +128,7 @@ playerjs.Player.prototype.receive = function(e){
128128
}
129129

130130
if (this.keeper.has(data.event, data.listener)){
131-
this.keeper.execute(data.event, data.listener, data.value);
131+
this.keeper.execute(data.event, data.listener, data.value, this);
132132
}
133133
};
134134

@@ -150,7 +150,7 @@ playerjs.Player.prototype.ready = function(){
150150
playerjs.log('Player.dequeue', data);
151151

152152
if (data.event === 'ready'){
153-
this.keeper.execute(data.event, data.listener, true);
153+
this.keeper.execute(data.event, data.listener, true, this);
154154
}
155155
this.send(data);
156156
}
@@ -195,38 +195,43 @@ playerjs.Player.prototype.off = function(event, callback){
195195
return false;
196196
};
197197

198-
for (var i = 0, l = playerjs.METHODS.length - 2; i < l; i++) {
199-
var methodName = playerjs.METHODS[i];
200-
playerjs.Player.prototype[methodName] = createPrototypeFunction(methodName)
201-
}
202-
203198
//create function to add to the Player prototype
204199
function createPrototypeFunction(name) {
200+
205201
return function() {
202+
206203
var data = {
207204
method: name
208205
};
209-
var args = Array.prototype.slice.call(arguments);
210-
var argsToCall;
211206

212-
//for setter add the first arg to the value field
213-
if (/^set/.test(name)) {
214-
data.value = args[0]
215-
}
207+
var args = Array.prototype.slice.call(arguments);
216208

217209
//for getters add the passed parameters to the arguments for the send call
218210
if (/^get/.test(name)) {
211+
playerjs.assert(args.length > 0, 'Get methods require a callback.');
219212
args.unshift(data);
220-
argsToCall = args;
221-
//otherwise only use the data object
222213
} else {
223-
argsToCall = [data];
214+
//for setter add the first arg to the value field
215+
if (/^set/.test(name)) {
216+
playerjs.assert(args.length !== 0, 'Set methods require a value.');
217+
data.value = args[0];
218+
}
219+
args = [data];
224220
}
225221

226-
this.send.apply(this, argsToCall)
227-
}
222+
this.send.apply(this, args);
223+
};
228224
}
229225

226+
// Loop through the methods to add them to the prototype.
227+
for (var i = 0, l = playerjs.METHODS.length; i < l; i++) {
228+
var methodName = playerjs.METHODS[i];
229+
230+
// We don't want to overwrite existing methods.
231+
if (!playerjs.Player.prototype.hasOwnProperty(methodName)){
232+
playerjs.Player.prototype[methodName] = createPrototypeFunction(methodName);
233+
}
234+
}
230235

231236
window.playerjs = playerjs;
232237

test/mock.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var video = {
1212
volume: 100,
1313
mute: false,
1414
playing: false,
15+
loop : false,
1516
play: function(){
1617
video.interval = setInterval(function(){
1718
video.currentTime += 0.25;
@@ -76,4 +77,12 @@ receiver.on('unmute', function(){
7677

7778
receiver.on('getMuted', function(callback){
7879
callback(video.mute);
80+
});
81+
82+
receiver.on('getLoop', function(callback){
83+
callback(video.loop);
84+
});
85+
86+
receiver.on('setLoop', function(value){
87+
video.loop = value;
7988
});

test/test.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/*globals asyncTest:true, ok:true, start:true, playerjs:true*/
22
var FRAMES = [
3-
'src=http%3A%2F%2Fdistilleryvesper8-8.ak.instagram.com%2Fdc7cdbb2418811e3a4fc22000a1fc7c7_101.mp4&src_secure=1&url=http%3A%2F%2Finstagram.com%2Fp%2FgGZegnh9Sg%2F&image=http%3A%2F%2Fdistilleryimage8.ak.instagram.com%2Fdc7cdbb2418811e3a4fc22000a1fc7c7_8.jpg&type=video%2Fmp4&schema=instagram',
4-
'src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FKYZIj0Nmdps&src_secure=1&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKYZIj0Nmdps&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FKYZIj0Nmdps%2Fhqdefault.jpg&key=internal&type=text%2Fhtml&schema=youtube',
5-
'src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F18150336&src_secure=1&url=http%3A%2F%2Fvimeo.com%2F18150336&image=http%3A%2F%2Fb.vimeocdn.com%2Fts%2F117%2F311%2F117311910_1280.jpg&type=text%2Fhtml&schema=vimeo',
6-
'src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttp%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F120478577%26auto_play%3Dfalse%26show_artwork%3Dtrue%26origin%3Dtwitter&src_secure=1&url=http%3A%2F%2Fsoundcloud.com%2Fkettelmusic%2Fhon&image=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000062945914-gt5axz-t500x500.jpg%3F3eddc42&type=text%2Fhtml&schema=soundcloud',
73
'http://localhost.com:8003/test/iframe.html'
8-
94
];
105

116
//playerjs.DEBUG = true;
@@ -14,7 +9,8 @@ var isNumber= function(obj){
149
return Object.prototype.toString.call(obj) === "[object Number]";
1510
};
1611

17-
function testCases(player){
12+
function testCases(){
13+
var player = this;
1814
asyncTest("Play", 3, function() {
1915
var count = 0;
2016
var done = function(){
@@ -29,15 +25,15 @@ function testCases(player){
2925

3026
player.on('play', function(){
3127
ok(true, "video has played");
32-
player.off('play');
28+
this.off('play');
3329
done();
3430
});
3531

3632
player.on('timeupdate', function(data){
3733
ok(isNumber(data.seconds));
3834
ok(isNumber(data.duration));
3935

40-
player.off('timeupdate');
36+
this.off('timeupdate');
4137
done();
4238
});
4339

@@ -47,9 +43,9 @@ function testCases(player){
4743
asyncTest("Pause", 2, function() {
4844
player.on('pause', function(){
4945
ok(true, "video has paused");
50-
player.off('pause');
46+
this.off('pause');
5147
// Test if paused works.
52-
player.getPaused(function(value){
48+
this.getPaused(function(value){
5349
ok( true === value, "video is paused" );
5450
start();
5551
});
@@ -102,6 +98,17 @@ function testCases(player){
10298
player.setCurrentTime(5);
10399
});
104100

101+
//Test Loop
102+
asyncTest("setLoop", 1, function() {
103+
player.setLoop(true);
104+
setTimeout(function(){
105+
player.getLoop(function(v){
106+
ok(v === true, 'Set Loop was not set');
107+
start();
108+
});
109+
}, 100);
110+
});
111+
105112
// Volumne tests
106113
asyncTest("volume", 3, function() {
107114

@@ -143,11 +150,12 @@ var loadPlayers = function() {
143150
count++;
144151
if (count === FRAMES.length){
145152
var iframes = document.getElementsByTagName('iframe');
153+
146154
for (var d=0; d<iframes.length; d++){
147155
var player = new playerjs.Player(iframes[d]);
148-
player.on('ready', function(){
149-
testCases(this);
150-
}, player);
156+
157+
player.on('ready', testCases, player);
158+
151159
players.push(player);
152160
}
153161
}
@@ -156,13 +164,7 @@ var loadPlayers = function() {
156164
for (var f in FRAMES){
157165
var iframe = document.createElement('iframe');
158166

159-
if (FRAMES[f].substr(0,4) === 'http'){
160-
iframe.src = FRAMES[f];
161-
} else {
162-
//iframe.src = 'http://cdn.embedly.com/widgets/media.html?' + FRAMES[f];
163-
iframe.src = 'http://localhost.com:8000/dist/html/media.html?' + FRAMES[f];
164-
}
165-
167+
iframe.src = FRAMES[f];
166168
iframe.id = 'iframe_'+f;
167169
iframe.width = 200;
168170
iframe.height = 200;

0 commit comments

Comments
 (0)