svgedit/test/math_test.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

/* eslint-env qunit */
(INCOMPLETE: ES6 Module conversion and linting) - Breaking change: Require `new` with `EmbeddedSVGEdit` (allows us to use `class` internally) - Breaking change: If `svgcanvas.setUiStrings` must now be called if not using editor in order to get strings (for sake of i18n) (and if using path.js alone, must also have its `setUiStrings` called) - Breaking change (ext-overview-window): Avoid global `overviewWindowGlobals` - Breaking change (ext-imagelib): Change to object-based encoding for namespacing of messages (though keep stringifying/parsing ourselves until we remove IE9 support) - Breaking change: Rename `jquery.js` to `jquery.min.js` - Breaking change: Remove `scoped` attribute from `style`; it is now deprecated and obsolete; also move to head (after other stylesheets) - Enhancement: Make SpinButton plugin independent of SVGEdit via generic state object for tool_scale - Enhancement: Remove now unused Python l10n scripts (#238) - Enhancement: ES6 Modules (including jQuery plugins but not jQuery) - Enhancement: Further JSDoc (incomplete) - Enhancement (Optimization): Compress images using imageoptim (and add npm script) (per #215) - Fix: i18nize path.js strings and canvas notifications - Fix: Attempt i18n for ext-markers - Refactoring (ext-storage): Move locale info to own file imported by the extension (toward modularity; still should be split into separate files by language and *dynamically* imported, but we'll wait for better `import` support to refactor this) - Refactoring: For imagelib, add local jQuery copy (using old 1.4.4 as had been using from server) - Refactoring: For MathJax, add local copy (using old 2.3 as had been using from server); server had not been working - Refactoring: Remove `use strict` (implicit in modules) - Refactoring: Remove trailing whitespace, fix some code within comments - Refactoring: Expect `jQuery` global rather than `$` for better modularity (also to adapt line later once available via `import`) - Refactoring: Prefer `const` (and then `let`) - Refactoring: Add block scope keywords closer to first block in which they appear - Refactoring: Use ES6 `class` - Refactoring `$.isArray` -> `Array.isArray` and avoid some other jQuery core methods with simple VanillaJS replacements - Refactoring: Use abbreviated object property syntax - Refactoring: Object destructuring - Refactoring: Remove `uiStrings` contents in svg-editor.js (obtains from locale) - Refactoring: Add favicon to embedded API file - Refactoring: Use arrow functions for brief functions (incomplete) - Refactoring: Use `Array.prototype.includes`/`String.prototype.includes`; `String.prototype.startsWith`, `String.prototype.trim` - Refactoring: Remove now unnecessary svgutils do/while resetting of variables - Refactoring: Use shorthand methods for object literals (avoid ": function") - Refactoring: Avoid quoting object property keys where unnecessary - Refactoring: Just do truthy/falsey check for lengths in place of comparison to 0 - Refactoring (Testing): Avoid jQuery usage within most test files (defer script, also in preparation for future switch to ES6 modules for tests) - Refactoring: Make jpicker variable declaration indent bearable - Refactoring (Linting): Finish svgcanvas.js - Docs: Mention in comment no longer an entry file as before - Docs: Migrate old config, extensions, and FAQ docs - Licensing: Indicate MIT is license type of rgbcolor; rename/add license file name for jgraduate and screencast to reflect type (Apache 2.0); rename file to reflect it contains license information (of type MIT) for Raphael icons
2018-05-18 03:25:45 +00:00
/* globals svgedit, equals */
// log function
QUnit.log = function (details) {
if (window.console && window.console.log) {
window.console.log(details.result + ' :: ' + details.message);
}
};
const svg = document.createElementNS(svgedit.NS.SVG, 'svg');
module('svgedit.math');
test('Test svgedit.math package', function () {
expect(7);
ok(svgedit.math);
ok(svgedit.math.transformPoint);
ok(svgedit.math.isIdentity);
ok(svgedit.math.matrixMultiply);
equals(typeof svgedit.math.transformPoint, typeof function () {});
equals(typeof svgedit.math.isIdentity, typeof function () {});
equals(typeof svgedit.math.matrixMultiply, typeof function () {});
});
test('Test svgedit.math.transformPoint() function', function () {
expect(6);
const {transformPoint} = svgedit.math;
const m = svg.createSVGMatrix();
m.a = 1; m.b = 0;
m.c = 0; m.d = 1;
m.e = 0; m.f = 0;
let pt = transformPoint(100, 200, m);
equals(pt.x, 100);
equals(pt.y, 200);
m.e = 300; m.f = 400;
pt = transformPoint(100, 200, m);
equals(pt.x, 400);
equals(pt.y, 600);
m.a = 0.5; m.b = 0.75;
m.c = 1.25; m.d = 2;
pt = transformPoint(100, 200, m);
equals(pt.x, 100 * m.a + 200 * m.c + m.e);
equals(pt.y, 100 * m.b + 200 * m.d + m.f);
});
test('Test svgedit.math.isIdentity() function', function () {
expect(2);
ok(svgedit.math.isIdentity(svg.createSVGMatrix()));
const m = svg.createSVGMatrix();
m.a = 1; m.b = 0;
m.c = 0; m.d = 1;
m.e = 0; m.f = 0;
ok(svgedit.math.isIdentity(m));
});
test('Test svgedit.math.matrixMultiply() function', function () {
expect(5);
const mult = svgedit.math.matrixMultiply;
const {isIdentity} = svgedit.math;
// translate there and back
const tr1 = svg.createSVGMatrix().translate(100, 50),
tr2 = svg.createSVGMatrix().translate(-90, 0),
tr3 = svg.createSVGMatrix().translate(-10, -50);
let I = mult(tr1, tr2, tr3);
ok(isIdentity(I), 'Expected identity matrix when translating there and back');
// rotate there and back
// TODO: currently Mozilla fails this when rotating back at -50 and then -40 degrees
// (b and c are *almost* zero, but not zero)
const rotThere = svg.createSVGMatrix().rotate(90),
rotBack = svg.createSVGMatrix().rotate(-90), // TODO: set this to -50
rotBackMore = svg.createSVGMatrix().rotate(0); // TODO: set this to -40
I = mult(rotThere, rotBack, rotBackMore);
ok(isIdentity(I), 'Expected identity matrix when rotating there and back');
// scale up and down
const scaleUp = svg.createSVGMatrix().scale(4),
scaleDown = svg.createSVGMatrix().scaleNonUniform(0.25, 1),
scaleDownMore = svg.createSVGMatrix().scaleNonUniform(1, 0.25);
I = mult(scaleUp, scaleDown, scaleDownMore);
ok(isIdentity(I), 'Expected identity matrix when scaling up and down');
// test multiplication with its inverse
I = mult(rotThere, rotThere.inverse());
ok(isIdentity(I), 'Expected identity matrix when multiplying a matrix by its inverse');
I = mult(rotThere.inverse(), rotThere);
ok(isIdentity(I), 'Expected identity matrix when multiplying a matrix by its inverse');
});
test('Test svgedit.math.transformBox() function', function () {
expect(12);
const {transformBox} = svgedit.math;
const m = svg.createSVGMatrix();
m.a = 1; m.b = 0;
m.c = 0; m.d = 1;
m.e = 0; m.f = 0;
const r = transformBox(10, 10, 200, 300, m);
equals(r.tl.x, 10);
equals(r.tl.y, 10);
equals(r.tr.x, 210);
equals(r.tr.y, 10);
equals(r.bl.x, 10);
equals(r.bl.y, 310);
equals(r.br.x, 210);
equals(r.br.y, 310);
equals(r.aabox.x, 10);
equals(r.aabox.y, 10);
equals(r.aabox.width, 200);
equals(r.aabox.height, 300);
});