diff --git a/test/lib/index.js b/test/lib/index.js index f8bbbef..328a372 100644 --- a/test/lib/index.js +++ b/test/lib/index.js @@ -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}; \ No newline at end of file +export {assert, p5, testRender, testRendering}; \ No newline at end of file diff --git a/test/lib/test-render.js b/test/lib/test-render.js index 9975952..84c4ec2 100644 --- a/test/lib/test-render.js +++ b/test/lib/test-render.js @@ -13,6 +13,7 @@ p5svg = new p5(function(p) { svgGraphics = p.createCanvas(100, 100, p.SVG); p.noLoop(); p.isSVG = true; + p.__ready = true; }; }); @@ -21,6 +22,7 @@ p5canvas = new p5(function(p) { canvasGraphics = p.createCanvas(100, 100); p.noLoop(); p.isSVG = false; + p.__ready = true; }; }); @@ -303,4 +305,5 @@ testRender.unlock = function() { testRender.wait(0); }; +export {p5canvas, p5svg}; export default testRender; diff --git a/test/lib/test-rendering.js b/test/lib/test-rendering.js new file mode 100644 index 0000000..679ca7d --- /dev/null +++ b/test/lib/test-rendering.js @@ -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(); + } + }); + }); +} \ No newline at end of file diff --git a/test/unit/index.js b/test/unit/index.js index 3d49313..62e83e2 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -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'); diff --git a/test/unit/typography/fonts.js b/test/unit/typography/fonts.js new file mode 100644 index 0000000..d5478b8 --- /dev/null +++ b/test/unit/typography/fonts.js @@ -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/font-awesome@4.7.0/fonts/FontAwesome.otf', resolve, reject); + }) + }, + draw: function(p) { + p.fill('#ED225D'); + p.textFont(myFont); + p.textSize(36); + p.text("\uf092", 10, 50); + } + }) + }); + }); + +}); \ No newline at end of file