Skip to content

Commit

Permalink
Default to testing only Bravura.
Browse files Browse the repository at this point in the history
Add a flag to generate_png_images.js to allow us to customize which
fonts to test. For example:
  node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=all
  node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=petaluma
  node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=bravura,gonville
  • Loading branch information
ronyeh committed Apr 28, 2021
1 parent 091c6bb commit 9b65051
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
27 changes: 15 additions & 12 deletions tests/vexflow_test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ if (!global.QUnit) {

global['VF'] = Vex.Flow;
VF.Test = (function () {
var DEFAULT_FONTS_TO_TEST = [VF.Fonts.Bravura, VF.Fonts.Gonville, VF.Fonts.Petaluma];

var Test = {
// Test Options.
RUN_CANVAS_TESTS: true,
Expand All @@ -67,8 +65,8 @@ VF.Test = (function () {
// Default font properties for tests.
Font: { size: 10 },

// Test all three fonts by default. Customize the FONTS_TO_TEST array to test fewer fonts.
FONTS_TO_TEST: DEFAULT_FONTS_TO_TEST,
// Customize this array to test more fonts (e.g., ['Bravura', 'Gonville', 'Petaluma']).
FONT_STACKS_TO_TEST: ['Bravura'],

FONT_STACKS: {
Bravura: [VF.Fonts.Bravura, VF.Fonts.Gonville, VF.Fonts.Custom],
Expand Down Expand Up @@ -242,7 +240,16 @@ VF.Test = (function () {
if (VF.Renderer.lastContext !== null) {
var moduleName = sanitizeName(QUnit.current_module);
var testName = sanitizeName(QUnit.current_test);
var fileName = `${VF.Test.NODE_IMAGEDIR}/${moduleName}.${testName}.${fontName}.png`;
var fileName;
if (fontName === 'Bravura' && VF.Test.FONT_STACKS_TO_TEST.length === 1) {
// If we are only testing Bravura, we do not add the font name
// to the output image file's name, which allows visual diffs against
// the previous release: version 3.0.9. In the future, if we decide
// to test all fonts by default, we can remove this check.
fileName = `${VF.Test.NODE_IMAGEDIR}/${moduleName}.${testName}.png`;
} else {
fileName = `${VF.Test.NODE_IMAGEDIR}/${moduleName}.${testName}.${fontName}.png`;
}

var imageData = canvas.toDataURL().split(';base64,').pop();
var image = Buffer.from(imageData, 'base64');
Expand All @@ -254,14 +261,10 @@ VF.Test = (function () {
VF.Test.runTestWithFonts(name, testFunc);
},

// Run QUnit.test() for each font that is included in VF.Test.FONTS_TO_TEST.
// Run QUnit.test() for each font that is included in VF.Test.FONT_STACKS_TO_TEST.
runTestWithFonts: function (name, func) {
if (!Array.isArray(VF.Test.FONTS_TO_TEST)) {
VF.Test.FONTS_TO_TEST = DEFAULT_FONTS_TO_TEST;
}

VF.Test.FONTS_TO_TEST.forEach((font) => {
QUnit.test(name, func(font.getName()));
VF.Test.FONT_STACKS_TO_TEST.forEach((fontName) => {
QUnit.test(name, func(fontName));
});
},

Expand Down
26 changes: 19 additions & 7 deletions tools/generate_png_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ document = dom.window.document;
const fs = require('fs');
const [scriptDir, imageDir] = process.argv.slice(2, 4);

// Optional: 3rd argument specifies which font stacks to test.
// For example:
// node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=all
// node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=petaluma
// node generate_png_images.js SCRIPT_DIR IMAGE_OUTPUT_DIR --fonts=bravura,gonville
let fontStacksToTest = ['Bravura'];
if (process.argv.length >= 5) {
const fontsOption = process.argv[4].toLowerCase();
if (fontsOption.startsWith('--fonts=')) {
const fontsList = fontsOption.split('=')[1].split(',');
if (fontsList.includes('all')) {
fontStacksToTest = ['Bravura', 'Petaluma', 'Gonville'];
} else {
fontStacksToTest = fontsList.map((fontName) => fontName.charAt(0).toUpperCase() + fontName.slice(1));
}
}
}

global['Vex'] = require(`${scriptDir}/vexflow-debug.js`);

require(`${scriptDir}/vexflow-tests.js`);
Expand All @@ -30,13 +48,7 @@ VF.Test.RUN_SVG_TESTS = false;
VF.Test.RUN_RAPHAEL_TESTS = false;
VF.Test.RUN_NODE_TESTS = true;
VF.Test.NODE_IMAGEDIR = imageDir;

// By default we test all fonts.
// Modify VF.Test.FONTS_TO_TEST to test fewer fonts at a time.
// In the future, we can allow customization via process.argv.
// VF.Test.FONTS_TO_TEST = [VF.Fonts.Petaluma];
// VF.Test.FONTS_TO_TEST = [VF.Fonts.Gonville];
// VF.Test.FONTS_TO_TEST = [VF.Fonts.Bravura];
VF.Test.FONT_STACKS_TO_TEST = fontStacksToTest;

// Create the image directory if it doesn't exist.
fs.mkdirSync(VF.Test.NODE_IMAGEDIR, { recursive: true });
Expand Down

0 comments on commit 9b65051

Please sign in to comment.