Skip to content

Commit

Permalink
test: add test for loadFont, fixes #147
Browse files Browse the repository at this point in the history
  • Loading branch information
zenozeng committed Jun 14, 2021
1 parent f7bdae1 commit f4ce71b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion test/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {assert} from 'chai';
import testRender from './test-render';
import testRendering from './test-rendering';

const p5 = window.p5;

export {assert, p5, testRender};
export {assert, p5, testRender, testRendering};
3 changes: 3 additions & 0 deletions test/lib/test-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ p5svg = new p5(function(p) {
svgGraphics = p.createCanvas(100, 100, p.SVG);
p.noLoop();
p.isSVG = true;
p.__ready = true;
};
});

Expand All @@ -21,6 +22,7 @@ p5canvas = new p5(function(p) {
canvasGraphics = p.createCanvas(100, 100);
p.noLoop();
p.isSVG = false;
p.__ready = true;
};
});

Expand Down Expand Up @@ -303,4 +305,5 @@ testRender.unlock = function() {
testRender.wait(0);
};

export {p5canvas, p5svg};
export default testRender;
30 changes: 30 additions & 0 deletions test/lib/test-rendering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import testRender from './test-render';
import {p5svg, p5canvas} from './test-render';

export default async function testRendering(options = {
draw: (p) => {},
before: async (p) => {}
}) {
// Waiting for p5svg & p5canvas setup done
while (true) {
if (p5svg.__ready && p5canvas.__ready) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
// Before
if (options.before) {
await options.before(p5svg);
await options.before(p5canvas);
}
// Draw
return new Promise((resolve, reject) => {
testRender(options.draw, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mocha.setup('bdd');
mocha.setup({timeout: 10000, slow: 2000});

var test = function() {
require('./typography/fonts');
require('./basic/basic');
require('./filter/filter');
require('./svg/element');
Expand Down
26 changes: 26 additions & 0 deletions test/unit/typography/fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {p5, testRendering} from '../../lib';

describe('Typography', function() {
this.timeout(0);

// https://p5js.org/reference/#/p5/loadFont
describe('loadFont', function() {
it('should load font', async function() {
let myFont;
return testRendering({
before: async function(p) {
myFont = await new Promise((resolve, reject) => {
p.loadFont('https://unpkg.com/[email protected]/fonts/FontAwesome.otf', resolve, reject);
})
},
draw: function(p) {
p.fill('#ED225D');
p.textFont(myFont);
p.textSize(36);
p.text("\uf092", 10, 50);
}
})
});
});

});

0 comments on commit f4ce71b

Please sign in to comment.