Browse Source

Async test support

v1.x
Tj Holowaychuk 14 years ago
parent
commit
a15b55e5a9
  1. 2
      test/public/app.js
  2. 18
      test/public/tests.js
  3. 15
      test/server.js

2
test/public/app.js

@ -49,7 +49,7 @@ function runTests() {
function runTest(name, canvas, dest) { function runTest(name, canvas, dest) {
var fn = tests[name] var fn = tests[name]
, start = new Date; , start = new Date;
fn(canvas.getContext('2d')); fn(canvas.getContext('2d'), function(){});
canvas.title += ' (rendered in ' + (new Date - start) + 'ms)'; canvas.title += ' (rendered in ' + (new Date - start) + 'ms)';
renderOnServer(name, canvas, function(res){ renderOnServer(name, canvas, function(res){
if (res.error) { if (res.error) {

18
test/public/tests.js

@ -1388,50 +1388,56 @@ tests['font state'] = function(ctx){
ctx.fillText('Boom again!', 50, 140); ctx.fillText('Boom again!', 50, 140);
}; };
tests['drawImage(img,0,0)'] = function(ctx){ tests['drawImage(img,0,0)'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 0, 0); ctx.drawImage(img, 0, 0);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };
tests['drawImage(img,x,y)'] = function(ctx){ tests['drawImage(img,x,y)'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 5, 25); ctx.drawImage(img, 5, 25);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };
tests['drawImage(img,x,y,w,h) scale down'] = function(ctx){ tests['drawImage(img,x,y,w,h) scale down'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 25, 25, 10, 10); ctx.drawImage(img, 25, 25, 10, 10);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };
tests['drawImage(img,x,y,w,h) scale up'] = function(ctx){ tests['drawImage(img,x,y,w,h) scale up'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 0, 0, 200, 200); ctx.drawImage(img, 0, 0, 200, 200);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };
tests['drawImage(img,x,y,w,h) scale vertical'] = function(ctx){ tests['drawImage(img,x,y,w,h) scale vertical'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 0, 0, img.width, 200); ctx.drawImage(img, 0, 0, img.width, 200);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };
tests['drawImage(img,sx,sy,sw,sh,x,y,w,h)'] = function(ctx){ tests['drawImage(img,sx,sy,sw,sh,x,y,w,h)'] = function(ctx, done){
var img = new Image; var img = new Image;
img.onload = function(){ img.onload = function(){
ctx.drawImage(img, 13, 13, 80, 80, 25, 25, img.width / 2, img.height / 2); ctx.drawImage(img, 13, 13, 80, 80, 25, 25, img.width / 2, img.height / 2);
done();
}; };
img.src = 'state.png'; img.src = 'state.png';
}; };

15
test/server.js

@ -30,6 +30,7 @@ app.get('/', function(req, res){
res.render('tests'); res.render('tests');
}); });
var n = 0;
app.post('/render', function(req, res, next){ app.post('/render', function(req, res, next){
// Normalize state.png as ./public/state.png // Normalize state.png as ./public/state.png
// no good way around this at the moment // no good way around this at the moment
@ -43,12 +44,16 @@ app.post('/render', function(req, res, next){
, ctx = canvas.getContext('2d') , ctx = canvas.getContext('2d')
, start = new Date; , start = new Date;
fn(ctx); function done(){
var duration = new Date - start; var duration = new Date - start;
canvas.toDataURL(function(err, str){
res.send({ data: str, duration: duration });
});
}
canvas.toDataURL(function(err, str){ 2 == fn.length
res.send({ data: str, duration: duration }); ? fn(ctx, done)
}); : fn(ctx), done();
}); });
app.listen(parseInt(process.argv[2] || '3000', 10)); app.listen(parseInt(process.argv[2] || '3000', 10));

Loading…
Cancel
Save